Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased

### Features
- sdk: allow deposit from external authority directly to drift account

### Fixes

Expand Down
53 changes: 38 additions & 15 deletions sdk/src/driftClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2843,23 +2843,27 @@ export class DriftClient {
marketIndex: number,
associatedTokenAccount: PublicKey,
subAccountId?: number,
reduceOnly = false
reduceOnly = false,
overrides?: {
authority?: PublicKey;
}
): Promise<TransactionInstruction[]> {
const spotMarketAccount = this.getSpotMarketAccount(marketIndex);

const isSolMarket = spotMarketAccount.mint.equals(WRAPPED_SOL_MINT);

const signerAuthority = this.wallet.publicKey;
const signer = overrides?.authority ?? this.wallet.publicKey;

const createWSOLTokenAccount =
isSolMarket && associatedTokenAccount.equals(signerAuthority);
isSolMarket && associatedTokenAccount.equals(signer);

const instructions = [];

if (createWSOLTokenAccount) {
const { ixs, pubkey } = await this.getWrappedSolAccountCreationIxs(
amount,
true
true,
overrides
);

associatedTokenAccount = pubkey;
Expand All @@ -2873,7 +2877,8 @@ export class DriftClient {
associatedTokenAccount,
subAccountId,
reduceOnly,
true
true,
overrides
);

instructions.push(depositCollateralIx);
Expand All @@ -2883,8 +2888,8 @@ export class DriftClient {
instructions.push(
createCloseAccountInstruction(
associatedTokenAccount,
signerAuthority,
signerAuthority,
signer,
signer,
[]
)
);
Expand Down Expand Up @@ -2953,14 +2958,18 @@ export class DriftClient {
subAccountId?: number,
reduceOnly = false,
txParams?: TxParams,
initSwiftAccount = false
initSwiftAccount = false,
overrides?: {
authority?: PublicKey;
}
): Promise<VersionedTransaction | Transaction> {
const instructions = await this.getDepositTxnIx(
amount,
marketIndex,
associatedTokenAccount,
subAccountId,
reduceOnly
reduceOnly,
overrides
);

if (initSwiftAccount) {
Expand Down Expand Up @@ -2995,6 +3004,9 @@ export class DriftClient {
* @param associatedTokenAccount can be the wallet public key if using native sol
* @param subAccountId subaccountId to deposit
* @param reduceOnly if true, deposit must not increase account risk
* @param txParams transaction parameters
* @param initSwiftAccount if true, initialize a swift account for the user
* @param overrides allows overriding authority for the deposit transaction
*/
public async deposit(
amount: BN,
Expand All @@ -3003,7 +3015,10 @@ export class DriftClient {
subAccountId?: number,
reduceOnly = false,
txParams?: TxParams,
initSwiftAccount = false
initSwiftAccount = false,
overrides?: {
authority?: PublicKey;
}
): Promise<TransactionSignature> {
const tx = await this.createDepositTxn(
amount,
Expand All @@ -3012,7 +3027,8 @@ export class DriftClient {
subAccountId,
reduceOnly,
txParams,
initSwiftAccount
initSwiftAccount,
overrides
);

const { txSig, slot } = await this.sendTransaction(tx, [], this.opts);
Expand All @@ -3026,7 +3042,10 @@ export class DriftClient {
userTokenAccount: PublicKey,
subAccountId?: number,
reduceOnly = false,
userInitialized = true
userInitialized = true,
overrides?: {
authority?: PublicKey;
}
): Promise<TransactionInstruction> {
const userAccountPublicKey = await getUserAccountPublicKey(
this.program.programId,
Expand Down Expand Up @@ -3058,6 +3077,7 @@ export class DriftClient {
);
}

const authority = overrides?.authority ?? this.wallet.publicKey;
const tokenProgram = this.getTokenProgramForSpotMarket(spotMarketAccount);
return await this.program.instruction.deposit(
marketIndex,
Expand All @@ -3071,7 +3091,7 @@ export class DriftClient {
user: userAccountPublicKey,
userStats: this.getUserStatsAccountPublicKey(),
userTokenAccount: userTokenAccount,
authority: this.wallet.publicKey,
authority,
tokenProgram,
},
remainingAccounts,
Expand All @@ -3091,14 +3111,17 @@ export class DriftClient {

public async getWrappedSolAccountCreationIxs(
amount: BN,
includeRent?: boolean
includeRent?: boolean,
overrides?: {
authority?: PublicKey;
}
): Promise<{
ixs: anchor.web3.TransactionInstruction[];
/** @deprecated - this array is always going to be empty, in the current implementation */
signers: Signer[];
pubkey: PublicKey;
}> {
const authority = this.wallet.publicKey;
const authority = overrides?.authority ?? this.wallet.publicKey;

// Generate a random seed for wrappedSolAccount.
const seed = Keypair.generate().publicKey.toBase58().slice(0, 32);
Expand Down
Loading