Skip to content

Commit

Permalink
sdk: explicitly specify state admin for certain functions (#1389)
Browse files Browse the repository at this point in the history
* sdk: explicitly specify state admin for certain functions

* add useHotWalletAdmin to config
  • Loading branch information
wphan authored Dec 18, 2024
1 parent 93f3a8d commit 40eb187
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
47 changes: 27 additions & 20 deletions sdk/src/adminClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,9 @@ export class AdminClient extends DriftClient {
const tokenProgram = this.getTokenProgramForSpotMarket(spotMarket);
return await this.program.instruction.depositIntoSpotMarketVault(amount, {
accounts: {
admin: this.wallet.publicKey,
admin: this.useHotWalletAdmin
? this.wallet.publicKey
: this.getStateAccount().admin,
state: await this.getStatePublicKey(),
sourceVault,
spotMarket: spotMarket.pubkey,
Expand Down Expand Up @@ -1192,7 +1194,9 @@ export class AdminClient extends DriftClient {
},
{
accounts: {
admin: this.wallet.publicKey,
admin: this.useHotWalletAdmin
? this.wallet.publicKey
: this.getStateAccount().admin,
state: await this.getStatePublicKey(),
perpMarket: await getPerpMarketPublicKey(
this.program.programId,
Expand Down Expand Up @@ -3885,7 +3889,9 @@ export class AdminClient extends DriftClient {
fuelBonusInsurance || null,
{
accounts: {
admin: this.wallet.publicKey,
admin: this.useHotWalletAdmin
? this.wallet.publicKey
: this.getStateAccount().admin,
state: await this.getStatePublicKey(),
user,
userStats,
Expand All @@ -3895,12 +3901,10 @@ export class AdminClient extends DriftClient {
}

public async initializePythPullOracle(
feedId: string,
isAdmin = false
feedId: string
): Promise<TransactionSignature> {
const initializePythPullOracleIx = await this.getInitializePythPullOracleIx(
feedId,
isAdmin
feedId
);
const tx = await this.buildTransaction(initializePythPullOracleIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);
Expand All @@ -3909,15 +3913,16 @@ export class AdminClient extends DriftClient {
}

public async getInitializePythPullOracleIx(
feedId: string,
isAdmin = false
feedId: string
): Promise<TransactionInstruction> {
const feedIdBuffer = getFeedIdUint8Array(feedId);
return await this.program.instruction.initializePythPullOracle(
feedIdBuffer,
{
accounts: {
admin: isAdmin ? this.getStateAccount().admin : this.wallet.publicKey,
admin: this.useHotWalletAdmin
? this.wallet.publicKey
: this.getStateAccount().admin,
state: await this.getStatePublicKey(),
systemProgram: SystemProgram.programId,
priceFeed: getPythPullOraclePublicKey(
Expand All @@ -3931,24 +3936,24 @@ export class AdminClient extends DriftClient {
}

public async initializePythLazerOracle(
feedId: number,
isAdmin = false
feedId: number
): Promise<TransactionSignature> {
const initializePythPullOracleIx =
await this.getInitializePythLazerOracleIx(feedId, isAdmin);
await this.getInitializePythLazerOracleIx(feedId);
const tx = await this.buildTransaction(initializePythPullOracleIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);

return txSig;
}

public async getInitializePythLazerOracleIx(
feedId: number,
isAdmin = false
feedId: number
): Promise<TransactionInstruction> {
return await this.program.instruction.initializePythLazerOracle(feedId, {
accounts: {
admin: isAdmin ? this.getStateAccount().admin : this.wallet.publicKey,
admin: this.useHotWalletAdmin
? this.wallet.publicKey
: this.getStateAccount().admin,
state: await this.getStatePublicKey(),
systemProgram: SystemProgram.programId,
lazerOracle: getPythLazerOraclePublicKey(
Expand Down Expand Up @@ -4030,10 +4035,11 @@ export class AdminClient extends DriftClient {
}

public async initializeProtectedMakerModeConfig(
maxUsers: number
maxUsers: number,
stateAdmin?: boolean
): Promise<TransactionSignature> {
const initializeProtectedMakerModeConfigIx =
await this.getInitializeProtectedMakerModeConfigIx(maxUsers);
await this.getInitializeProtectedMakerModeConfigIx(maxUsers, stateAdmin);

const tx = await this.buildTransaction(
initializeProtectedMakerModeConfigIx
Expand All @@ -4045,13 +4051,14 @@ export class AdminClient extends DriftClient {
}

public async getInitializeProtectedMakerModeConfigIx(
maxUsers: number
maxUsers: number,
stateAdmin?: boolean
): Promise<TransactionInstruction> {
return await this.program.instruction.initializeProtectedMakerModeConfig(
maxUsers,
{
accounts: {
admin: this.isSubscribed
admin: stateAdmin
? this.getStateAccount().admin
: this.wallet.publicKey,
state: await this.getStatePublicKey(),
Expand Down
2 changes: 2 additions & 0 deletions sdk/src/driftClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ export class DriftClient {
public program: Program;
provider: AnchorProvider;
opts?: ConfirmOptions;
useHotWalletAdmin?: boolean;
users = new Map<string, User>();
userStats?: UserStats;
activeSubAccountId: number;
Expand Down Expand Up @@ -251,6 +252,7 @@ export class DriftClient {
this.opts = config.opts || {
...DEFAULT_CONFIRMATION_OPTS,
};
this.useHotWalletAdmin = config.useHotWalletAdmin ?? false;
if (config?.connection?.commitment) {
// At the moment this ensures that our transaction simulations (which use Connection object) will use the same commitment level as our Transaction blockhashes (which use these opts)
this.opts.commitment = config.connection.commitment;
Expand Down
1 change: 1 addition & 0 deletions sdk/src/driftClientConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export type DriftClientConfig = {
enableMetricsEvents?: boolean;
txHandlerConfig?: TxHandlerConfig;
delistedMarketSetting?: DelistedMarketSetting;
useHotWalletAdmin?: boolean;
};

export type DriftClientSubscriptionConfig =
Expand Down

0 comments on commit 40eb187

Please sign in to comment.