diff --git a/CHANGELOG.md b/CHANGELOG.md index e9cc3fb8a..c46ed3813 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/sdk/src/driftClient.ts b/sdk/src/driftClient.ts index 4c71a49ab..96c04b88d 100644 --- a/sdk/src/driftClient.ts +++ b/sdk/src/driftClient.ts @@ -2843,23 +2843,27 @@ export class DriftClient { marketIndex: number, associatedTokenAccount: PublicKey, subAccountId?: number, - reduceOnly = false + reduceOnly = false, + overrides?: { + authority?: PublicKey; + } ): Promise { 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; @@ -2873,7 +2877,8 @@ export class DriftClient { associatedTokenAccount, subAccountId, reduceOnly, - true + true, + overrides ); instructions.push(depositCollateralIx); @@ -2883,8 +2888,8 @@ export class DriftClient { instructions.push( createCloseAccountInstruction( associatedTokenAccount, - signerAuthority, - signerAuthority, + signer, + signer, [] ) ); @@ -2953,14 +2958,18 @@ export class DriftClient { subAccountId?: number, reduceOnly = false, txParams?: TxParams, - initSwiftAccount = false + initSwiftAccount = false, + overrides?: { + authority?: PublicKey; + } ): Promise { const instructions = await this.getDepositTxnIx( amount, marketIndex, associatedTokenAccount, subAccountId, - reduceOnly + reduceOnly, + overrides ); if (initSwiftAccount) { @@ -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, @@ -3003,7 +3015,10 @@ export class DriftClient { subAccountId?: number, reduceOnly = false, txParams?: TxParams, - initSwiftAccount = false + initSwiftAccount = false, + overrides?: { + authority?: PublicKey; + } ): Promise { const tx = await this.createDepositTxn( amount, @@ -3012,7 +3027,8 @@ export class DriftClient { subAccountId, reduceOnly, txParams, - initSwiftAccount + initSwiftAccount, + overrides ); const { txSig, slot } = await this.sendTransaction(tx, [], this.opts); @@ -3026,7 +3042,10 @@ export class DriftClient { userTokenAccount: PublicKey, subAccountId?: number, reduceOnly = false, - userInitialized = true + userInitialized = true, + overrides?: { + authority?: PublicKey; + } ): Promise { const userAccountPublicKey = await getUserAccountPublicKey( this.program.programId, @@ -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, @@ -3071,7 +3091,7 @@ export class DriftClient { user: userAccountPublicKey, userStats: this.getUserStatsAccountPublicKey(), userTokenAccount: userTokenAccount, - authority: this.wallet.publicKey, + authority, tokenProgram, }, remainingAccounts, @@ -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);