Skip to content

Commit

Permalink
sdk: make it possible to update multiple accounts margin settings in … (
Browse files Browse the repository at this point in the history
#659)

* sdk: make it possible to update multiple accounts margin settings in single tx

* update tests and changelog
  • Loading branch information
lowkeynicc authored Oct 20, 2023
1 parent 19565f5 commit c221991
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Breaking

- sdk: remove getMakerLimitBids/Asks from DLOB
- sdk: updateUserMarginEnabled and updateUserCustomMarginRatio now take in an array of params to allow multiple subaccounts to be update in a single tx

## [2.41.0] - 2023-10-05

Expand Down
68 changes: 41 additions & 27 deletions sdk/src/driftClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -875,21 +875,48 @@ export class DriftClient {
}

public async updateUserCustomMarginRatio(
updates: { marginRatio: number; subAccountId: number }[]
): Promise<TransactionSignature> {
const ixs = await Promise.all(
updates.map(async ({ marginRatio, subAccountId }) => {
const ix = await this.getUpdateUserCustomMarginRatioIx(
marginRatio,
subAccountId
);
return ix;
})
);

const tx = await this.buildTransaction(ixs, this.txParams);

const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
}

public async getUpdateUserCustomMarginRatioIx(
marginRatio: number,
subAccountId = 0
): Promise<TransactionSignature> {
const tx = await this.program.transaction.updateUserCustomMarginRatio(
): Promise<TransactionInstruction> {
const userAccountPublicKey = getUserAccountPublicKeySync(
this.program.programId,
this.wallet.publicKey,
subAccountId
);

await this.addUser(subAccountId, this.wallet.publicKey);

const ix = this.program.instruction.updateUserCustomMarginRatio(
subAccountId,
marginRatio,
{
accounts: {
user: await this.getUserAccountPublicKey(),
user: userAccountPublicKey,
authority: this.wallet.publicKey,
},
}
);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;

return ix;
}

public async getUpdateUserMarginTradingEnabledIx(
Expand Down Expand Up @@ -930,31 +957,18 @@ export class DriftClient {
}

public async updateUserMarginTradingEnabled(
marginTradingEnabled: boolean,
subAccountId = 0
updates: { marginTradingEnabled: boolean; subAccountId: number }[]
): Promise<TransactionSignature> {
const userAccountPublicKey = getUserAccountPublicKeySync(
this.program.programId,
this.wallet.publicKey,
subAccountId
const ixs = await Promise.all(
updates.map(async ({ marginTradingEnabled, subAccountId }) => {
return await this.getUpdateUserMarginTradingEnabledIx(
marginTradingEnabled,
subAccountId
);
})
);

await this.addUser(subAccountId, this.wallet.publicKey);
const remainingAccounts = this.getRemainingAccounts({
userAccounts: [this.getUserAccount(subAccountId)],
});

const tx = await this.program.transaction.updateUserMarginTradingEnabled(
subAccountId,
marginTradingEnabled,
{
accounts: {
user: userAccountPublicKey,
authority: this.wallet.publicKey,
},
remainingAccounts,
}
);
const tx = await this.buildTransaction(ixs, this.txParams);

const { txSig } = await this.sendTransaction(tx, [], this.opts);
return txSig;
Expand Down
4 changes: 3 additions & 1 deletion tests/spotWithdrawUtil100.ts
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,9 @@ describe('test function when spot market at >= 100% util', () => {

const marketIndex = 1;

await firstUserDriftClient.updateUserMarginTradingEnabled(true, 0);
const updates = [{ marginTradingEnabled: true, subAccountId: 0 }];

await firstUserDriftClient.updateUserMarginTradingEnabled(updates);

const takerDriftClientUser = new User({
driftClient: firstUserDriftClient,
Expand Down
7 changes: 3 additions & 4 deletions tests/subaccounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,9 @@ describe('subaccounts', () => {
it('Update custom margin ratio', async () => {
const subAccountId = 0;
const customMarginRatio = MARGIN_PRECISION.toNumber() * 2;
await driftClient.updateUserCustomMarginRatio(
customMarginRatio,
subAccountId
);

const updates = [{ marginRatio: customMarginRatio, subAccountId }];
await driftClient.updateUserCustomMarginRatio(updates);

await driftClient.fetchAccounts();
assert(driftClient.getUserAccount().maxMarginRatio === customMarginRatio);
Expand Down

0 comments on commit c221991

Please sign in to comment.