Skip to content

Commit

Permalink
refactor(sdk): add oracle feeds to crank
Browse files Browse the repository at this point in the history
  • Loading branch information
ChesterSim committed Nov 20, 2024
1 parent 76c7d74 commit 85e50da
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion ts/sdk/src/vaultClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
unstakeSharesToAmount as depositSharesToVaultAmount,
ZERO,
getInsuranceFundVaultPublicKey,
OracleSource,
} from '@drift-labs/sdk';
import { BorshAccountsCoder, Program, ProgramAccount } from '@coral-xyz/anchor';
import { DriftVaults } from './types/drift_vaults';
Expand Down Expand Up @@ -58,6 +59,7 @@ export type TxParams = {
cuPriceMicroLamports?: number;
simulateTransaction?: boolean;
lookupTables?: AddressLookupTableAccount[];
oracleFeedsToCrank?: { feed: PublicKey; oracleSource: OracleSource }[];
};

export class VaultClient {
Expand Down Expand Up @@ -1661,7 +1663,12 @@ export class VaultClient {
.rpc();
}
} else {
const oracleFeedsToCrankIxs = await this.getOracleFeedsToCrank(
txParams?.oracleFeedsToCrank
);

const ixs = [
...oracleFeedsToCrankIxs,
await this.program.methods
.withdraw()
.accounts({
Expand Down Expand Up @@ -1823,6 +1830,10 @@ export class VaultClient {
.remainingAccounts(remainingAccounts)
.rpc();
} else {
const oracleFeedsToCrankIxs = await this.getOracleFeedsToCrank(
txParams?.oracleFeedsToCrank
);

const cancelRequestWithdrawIx =
this.program.instruction.cancelRequestWithdraw({
accounts: {
Expand All @@ -1832,7 +1843,10 @@ export class VaultClient {
remainingAccounts,
});

return await this.createAndSendTxn([cancelRequestWithdrawIx], txParams);
return await this.createAndSendTxn(
[...oracleFeedsToCrankIxs, cancelRequestWithdrawIx],
txParams
);
}
}

Expand Down Expand Up @@ -2398,4 +2412,29 @@ export class VaultClient {
cuLimit: 1_000_000,
});
}

private async getOracleFeedsToCrank(
oracleFeedsToCrank: TxParams['oracleFeedsToCrank']
) {
const oracleFeedsToCrankIxs: TransactionInstruction[] = oracleFeedsToCrank
? ((await Promise.all(
oracleFeedsToCrank.map(async (feedConfig) => {
if (
JSON.stringify(feedConfig.oracleSource) !==
JSON.stringify(OracleSource.SWITCHBOARD_ON_DEMAND)
) {
throw new Error(
'Only SWITCHBOARD_ON_DEMAND oracle feeds are supported for cranking'
);
}

return this.driftClient.getPostSwitchboardOnDemandUpdateAtomicIx(
feedConfig.feed
);
})
)) as TransactionInstruction[])
: [];

return oracleFeedsToCrankIxs;
}
}

0 comments on commit 85e50da

Please sign in to comment.