Skip to content

Commit

Permalink
refactor(sdk): abstract createDepositTxn
Browse files Browse the repository at this point in the history
  • Loading branch information
ChesterSim committed Jul 2, 2024
1 parent ad28ed3 commit b5a4ccd
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 37 deletions.
Binary file modified ts/sdk/bun.lockb
Binary file not shown.
137 changes: 100 additions & 37 deletions ts/sdk/src/vaultClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ export class VaultClient {
.rpc();
} else {
const requestWithdrawIx = this.program.instruction.managerRequestWithdraw(
// @ts-ignore
amount,
withdrawUnit,
{
Expand Down Expand Up @@ -681,22 +682,14 @@ export class VaultClient {
}
}

/**
* Depositor funds into the specified vault.
* @param vaultDepositor
* @param amount
* @param initVaultDepositor If true, will initialize the vault depositor account
* @returns
*/
public async deposit(
public async prepDepositTx(
vaultDepositor: PublicKey,
amount: BN,
initVaultDepositor?: {
authority: PublicKey;
vault: PublicKey;
},
txParams?: TxParams
): Promise<TransactionSignature> {
}
) {
let vaultPubKey: PublicKey;
if (initVaultDepositor) {
vaultPubKey = initVaultDepositor.vault;
Expand Down Expand Up @@ -747,7 +740,68 @@ export class VaultClient {
tokenProgram: TOKEN_PROGRAM_ID,
};

return {
vaultAccount, accounts, remainingAccounts
};
}

public async createDepositTx(
vaultDepositor: PublicKey,
amount: BN,
initVaultDepositor?: {
authority: PublicKey;
vault: PublicKey;
},
txParams?: TxParams
): Promise<VersionedTransaction> {
const { vaultAccount, accounts, remainingAccounts } = await this.prepDepositTx(
vaultDepositor,
amount,
initVaultDepositor
);

const depositIx = this.program.instruction.deposit(amount, {
accounts: {
authority: this.driftClient.wallet.publicKey,
...accounts,
},
remainingAccounts,
});

if (initVaultDepositor) {
const initIx = this.createInitVaultDepositorIx(
vaultAccount.pubkey,
initVaultDepositor.authority
);
return await this.createTxn([initIx, depositIx], txParams);
} else {
return await this.createTxn([depositIx], txParams);
}
}

/**
* Depositor funds into the specified vault.
* @param vaultDepositor
* @param amount
* @param initVaultDepositor If true, will initialize the vault depositor account
* @returns
*/
public async deposit(
vaultDepositor: PublicKey,
amount: BN,
initVaultDepositor?: {
authority: PublicKey;
vault: PublicKey;
},
txParams?: TxParams
): Promise<TransactionSignature> {
if (this.cliMode) {
const { vaultAccount, accounts, remainingAccounts } = await this.prepDepositTx(
vaultDepositor,
amount,
initVaultDepositor
);

if (initVaultDepositor) {
await this.initializeVaultDepositor(
vaultAccount.pubkey,
Expand All @@ -760,23 +814,14 @@ export class VaultClient {
.remainingAccounts(remainingAccounts)
.rpc();
} else {
const depositIx = this.program.instruction.deposit(amount, {
accounts: {
authority: this.driftClient.wallet.publicKey,
...accounts,
},
remainingAccounts,
});
const depositTxn = await this.createDepositTx(
vaultDepositor,
amount,
initVaultDepositor,
txParams
);

if (initVaultDepositor) {
const initIx = this.createInitVaultDepositorIx(
vaultAccount.pubkey,
initVaultDepositor.authority
);
return await this.createAndSendTxn([initIx, depositIx], txParams);
} else {
return await this.createAndSendTxn([depositIx], txParams);
}
return this.sendTxn(depositTxn, txParams?.simulateTransaction);
}
}

Expand Down Expand Up @@ -821,6 +866,7 @@ export class VaultClient {
.rpc();
} else {
const requestWithdrawIx = this.program.instruction.requestWithdraw(
// @ts-ignore
amount,
withdrawUnit,
{
Expand Down Expand Up @@ -1111,13 +1157,10 @@ export class VaultClient {
}
}

/**
* Used for UI wallet adapters compatibility
*/
public async createAndSendTxn(
public async createTxn(
vaultIxs: TransactionInstruction[],
txParams?: TxParams
): Promise<TransactionSignature> {
): Promise<VersionedTransaction> {
const ixs = [
ComputeBudgetProgram.setComputeUnitLimit({
units: txParams?.cuLimit ?? 400_000,
Expand All @@ -1128,7 +1171,7 @@ export class VaultClient {
...vaultIxs,
];

const tx = (await this.driftClient.txHandler.buildTransaction({
return (await this.driftClient.txHandler.buildTransaction({
connection: this.driftClient.connection,
instructions: ixs,
lookupTables: txParams?.lookupTables ?? [],
Expand All @@ -1138,10 +1181,16 @@ export class VaultClient {
fetchMarketLookupTableAccount:
this.driftClient.fetchMarketLookupTableAccount.bind(this.driftClient),
})) as VersionedTransaction;
let txSig = bs58.encode(tx.signatures[0]);
if (txParams?.simulateTransaction) {
}

public async sendTxn(
transaction: VersionedTransaction,
simulateTransaction?: boolean,
): Promise<TransactionSignature> {
let txSig = bs58.encode(transaction.signatures[0]);
if (simulateTransaction) {
try {
const resp = await this.driftClient.connection.simulateTransaction(tx, {
const resp = await this.driftClient.connection.simulateTransaction(transaction, {
sigVerify: false,
commitment: this.driftClient.connection.commitment,
});
Expand All @@ -1154,7 +1203,7 @@ export class VaultClient {
}
} else {
const resp = await this.driftClient.sendTransaction(
tx,
transaction,
[],
this.driftClient.opts
);
Expand All @@ -1167,6 +1216,20 @@ export class VaultClient {
}

return txSig!;

}

/**
* Used for UI wallet adapters compatibility
*/
public async createAndSendTxn(
vaultIxs: TransactionInstruction[],
txParams?: TxParams
): Promise<TransactionSignature> {
const tx = await this.createTxn(vaultIxs, txParams);
const txSig = await this.sendTxn(tx, txParams?.simulateTransaction);

return txSig;
}

/**
Expand Down

0 comments on commit b5a4ccd

Please sign in to comment.