Skip to content

Commit 7488419

Browse files
committed
sdk: allow other authority signer for deposits
1 parent 151cd42 commit 7488419

File tree

2 files changed

+54
-44
lines changed

2 files changed

+54
-44
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## Unreleased
99

1010
### Features
11+
- sdk: allow deposit from external authority directly to drift account
1112

1213
### Fixes
1314

sdk/src/driftClient.ts

Lines changed: 53 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,8 @@ export class DriftClient {
349349
this.authoritySubAccountMap = config.authoritySubAccountMap
350350
? config.authoritySubAccountMap
351351
: config.subAccountIds
352-
? new Map([[this.authority.toString(), config.subAccountIds]])
353-
: new Map<string, number[]>();
352+
? new Map([[this.authority.toString(), config.subAccountIds]])
353+
: new Map<string, number[]>();
354354

355355
this.includeDelegates = config.includeDelegates ?? false;
356356
if (config.accountSubscription?.type === 'polling') {
@@ -840,8 +840,8 @@ export class DriftClient {
840840
this.authoritySubAccountMap = authoritySubaccountMap
841841
? authoritySubaccountMap
842842
: subAccountIds
843-
? new Map([[this.authority.toString(), subAccountIds]])
844-
: new Map<string, number[]>();
843+
? new Map([[this.authority.toString(), subAccountIds]])
844+
: new Map<string, number[]>();
845845

846846
/* Reset user stats account */
847847
if (this.userStats?.isSubscribed) {
@@ -2843,23 +2843,25 @@ export class DriftClient {
28432843
marketIndex: number,
28442844
associatedTokenAccount: PublicKey,
28452845
subAccountId?: number,
2846-
reduceOnly = false
2846+
reduceOnly = false,
2847+
signerAuthority?: PublicKey
28472848
): Promise<TransactionInstruction[]> {
28482849
const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
28492850

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

2852-
const signerAuthority = this.wallet.publicKey;
2853+
const signer = signerAuthority ?? this.wallet.publicKey;
28532854

28542855
const createWSOLTokenAccount =
2855-
isSolMarket && associatedTokenAccount.equals(signerAuthority);
2856+
isSolMarket && associatedTokenAccount.equals(signer);
28562857

28572858
const instructions = [];
28582859

28592860
if (createWSOLTokenAccount) {
28602861
const { ixs, pubkey } = await this.getWrappedSolAccountCreationIxs(
28612862
amount,
2862-
true
2863+
true,
2864+
signerAuthority
28632865
);
28642866

28652867
associatedTokenAccount = pubkey;
@@ -2873,7 +2875,8 @@ export class DriftClient {
28732875
associatedTokenAccount,
28742876
subAccountId,
28752877
reduceOnly,
2876-
true
2878+
true,
2879+
signerAuthority
28772880
);
28782881

28792882
instructions.push(depositCollateralIx);
@@ -2883,8 +2886,8 @@ export class DriftClient {
28832886
instructions.push(
28842887
createCloseAccountInstruction(
28852888
associatedTokenAccount,
2886-
signerAuthority,
2887-
signerAuthority,
2889+
signer,
2890+
signer,
28882891
[]
28892892
)
28902893
);
@@ -2953,14 +2956,16 @@ export class DriftClient {
29532956
subAccountId?: number,
29542957
reduceOnly = false,
29552958
txParams?: TxParams,
2956-
initSwiftAccount = false
2959+
initSwiftAccount = false,
2960+
signerAuthority?: PublicKey
29572961
): Promise<VersionedTransaction | Transaction> {
29582962
const instructions = await this.getDepositTxnIx(
29592963
amount,
29602964
marketIndex,
29612965
associatedTokenAccount,
29622966
subAccountId,
2963-
reduceOnly
2967+
reduceOnly,
2968+
signerAuthority
29642969
);
29652970

29662971
if (initSwiftAccount) {
@@ -2995,6 +3000,9 @@ export class DriftClient {
29953000
* @param associatedTokenAccount can be the wallet public key if using native sol
29963001
* @param subAccountId subaccountId to deposit
29973002
* @param reduceOnly if true, deposit must not increase account risk
3003+
* @param txParams transaction parameters
3004+
* @param initSwiftAccount if true, initialize a swift account for the user
3005+
* @param signerAuthority the authority to sign the transaction, allowing for any authority to deposit directly to a Drift account
29983006
*/
29993007
public async deposit(
30003008
amount: BN,
@@ -3003,7 +3011,8 @@ export class DriftClient {
30033011
subAccountId?: number,
30043012
reduceOnly = false,
30053013
txParams?: TxParams,
3006-
initSwiftAccount = false
3014+
initSwiftAccount = false,
3015+
signerAuthority?: PublicKey
30073016
): Promise<TransactionSignature> {
30083017
const tx = await this.createDepositTxn(
30093018
amount,
@@ -3012,7 +3021,8 @@ export class DriftClient {
30123021
subAccountId,
30133022
reduceOnly,
30143023
txParams,
3015-
initSwiftAccount
3024+
initSwiftAccount,
3025+
signerAuthority
30163026
);
30173027

30183028
const { txSig, slot } = await this.sendTransaction(tx, [], this.opts);
@@ -3026,7 +3036,8 @@ export class DriftClient {
30263036
userTokenAccount: PublicKey,
30273037
subAccountId?: number,
30283038
reduceOnly = false,
3029-
userInitialized = true
3039+
userInitialized = true,
3040+
signerAuthority?: PublicKey
30303041
): Promise<TransactionInstruction> {
30313042
const userAccountPublicKey = await getUserAccountPublicKey(
30323043
this.program.programId,
@@ -3071,7 +3082,7 @@ export class DriftClient {
30713082
user: userAccountPublicKey,
30723083
userStats: this.getUserStatsAccountPublicKey(),
30733084
userTokenAccount: userTokenAccount,
3074-
authority: this.wallet.publicKey,
3085+
authority: signerAuthority ?? this.wallet.publicKey,
30753086
tokenProgram,
30763087
},
30773088
remainingAccounts,
@@ -3091,14 +3102,15 @@ export class DriftClient {
30913102

30923103
public async getWrappedSolAccountCreationIxs(
30933104
amount: BN,
3094-
includeRent?: boolean
3105+
includeRent?: boolean,
3106+
signerAuthority?: PublicKey
30953107
): Promise<{
30963108
ixs: anchor.web3.TransactionInstruction[];
30973109
/** @deprecated - this array is always going to be empty, in the current implementation */
30983110
signers: Signer[];
30993111
pubkey: PublicKey;
31003112
}> {
3101-
const authority = this.wallet.publicKey;
3113+
const authority = signerAuthority ?? this.wallet.publicKey;
31023114

31033115
// Generate a random seed for wrappedSolAccount.
31043116
const seed = Keypair.generate().publicKey.toBase58().slice(0, 32);
@@ -3316,15 +3328,15 @@ export class DriftClient {
33163328
marketIndex,
33173329
fromSubAccountId,
33183330
subAccountId
3319-
)
3331+
)
33203332
: await this.getDepositInstruction(
33213333
amount,
33223334
marketIndex,
33233335
userTokenAccount,
33243336
subAccountId,
33253337
false,
33263338
false
3327-
);
3339+
);
33283340

33293341
if (subAccountId === 0) {
33303342
if (
@@ -4367,11 +4379,11 @@ export class DriftClient {
43674379
[orderParams, ...bracketOrdersParams],
43684380
positionMaxLev,
43694381
userAccount.subAccountId
4370-
)
4382+
)
43714383
: this.getPlaceOrdersIx(
43724384
[orderParams, ...bracketOrdersParams],
43734385
userAccount.subAccountId
4374-
);
4386+
);
43754387

43764388
ixPromisesForTxs.marketOrderTx = marketOrderTxIxs;
43774389

@@ -4522,7 +4534,7 @@ export class DriftClient {
45224534
this.program.programId,
45234535
this.authority,
45244536
subAccountId
4525-
)
4537+
)
45264538
: await this.getUserAccountPublicKey(subAccountId);
45274539

45284540
const remainingAccounts = this.getRemainingAccounts({
@@ -5160,13 +5172,13 @@ export class DriftClient {
51605172
? order.marketIndex
51615173
: userAccount.orders.find(
51625174
(order) => order.orderId === userAccount.nextOrderId - 1
5163-
).marketIndex;
5175+
).marketIndex;
51645176

51655177
makerInfo = Array.isArray(makerInfo)
51665178
? makerInfo
51675179
: makerInfo
5168-
? [makerInfo]
5169-
: [];
5180+
? [makerInfo]
5181+
: [];
51705182

51715183
const userAccounts = [userAccount];
51725184
for (const maker of makerInfo) {
@@ -5305,9 +5317,8 @@ export class DriftClient {
53055317
subAccountId?: number
53065318
): Promise<TransactionInstruction> {
53075319
orderParams = getOrderParams(orderParams, { marketType: MarketType.SPOT });
5308-
const userAccountPublicKey = await this.getUserAccountPublicKey(
5309-
subAccountId
5310-
);
5320+
const userAccountPublicKey =
5321+
await this.getUserAccountPublicKey(subAccountId);
53115322

53125323
const remainingAccounts = this.getRemainingAccounts({
53135324
userAccounts: [this.getUserAccount(subAccountId)],
@@ -5383,13 +5394,13 @@ export class DriftClient {
53835394
? order.marketIndex
53845395
: userAccount.orders.find(
53855396
(order) => order.orderId === userAccount.nextOrderId - 1
5386-
).marketIndex;
5397+
).marketIndex;
53875398

53885399
makerInfo = Array.isArray(makerInfo)
53895400
? makerInfo
53905401
: makerInfo
5391-
? [makerInfo]
5392-
: [];
5402+
? [makerInfo]
5403+
: [];
53935404

53945405
const userAccounts = [userAccount];
53955406
for (const maker of makerInfo) {
@@ -6617,9 +6628,8 @@ export class DriftClient {
66176628

66186629
const prepSettlePnlTx = async () => {
66196630
if (settlePnl && isVariant(orderParams.marketType, 'perp')) {
6620-
const userAccountPublicKey = await this.getUserAccountPublicKey(
6621-
subAccountId
6622-
);
6631+
const userAccountPublicKey =
6632+
await this.getUserAccountPublicKey(subAccountId);
66236633

66246634
const settlePnlIx = await this.settlePNLIx(
66256635
userAccountPublicKey,
@@ -6728,8 +6738,8 @@ export class DriftClient {
67286738
makerInfo = Array.isArray(makerInfo)
67296739
? makerInfo
67306740
: makerInfo
6731-
? [makerInfo]
6732-
: [];
6741+
? [makerInfo]
6742+
: [];
67336743

67346744
const userAccounts = [this.getUserAccount(subAccountId)];
67356745
for (const maker of makerInfo) {
@@ -6977,11 +6987,11 @@ export class DriftClient {
69776987
? this.program.coder.types.encode(
69786988
'SignedMsgOrderParamsDelegateMessage',
69796989
withBuilderDefaults as SignedMsgOrderParamsDelegateMessage
6980-
)
6990+
)
69816991
: this.program.coder.types.encode(
69826992
'SignedMsgOrderParamsMessage',
69836993
withBuilderDefaults as SignedMsgOrderParamsMessage
6984-
),
6994+
),
69856995
]);
69866996
return buf;
69876997
}
@@ -9651,9 +9661,8 @@ export class DriftClient {
96519661
}
96529662

96539663
if (initializeStakeAccount) {
9654-
const initializeIx = await this.getInitializeInsuranceFundStakeIx(
9655-
marketIndex
9656-
);
9664+
const initializeIx =
9665+
await this.getInitializeInsuranceFundStakeIx(marketIndex);
96579666
addIfStakeIxs.push(initializeIx);
96589667
}
96599668

@@ -10701,7 +10710,7 @@ export class DriftClient {
1070110710
const remainingAccounts = userAccount
1070210711
? this.getRemainingAccounts({
1070310712
userAccounts: [userAccount],
10704-
})
10713+
})
1070510714
: undefined;
1070610715

1070710716
const ix = await this.program.instruction.disableUserHighLeverageMode(

0 commit comments

Comments
 (0)