Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sdk: add emulateAccount fn #1157

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 48 additions & 3 deletions sdk/src/driftClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,51 @@ export class DriftClient {
return success;
}

/**
* Update the subscribed accounts to a given authority, while leaving the
* connected wallet intact. This allows a user to emulate another user's
* account on the UI and sign permissionless transactions with their own wallet.
* @param emulateAuthority
*/
public async emulateAccount(emulateAuthority: PublicKey): Promise<boolean> {
this.skipLoadUsers = false;
// Update provider for txSender with new wallet details
this.authority = emulateAuthority;
this.userStatsAccountPublicKey = undefined;
this.includeDelegates = true;
const walletSupportsVersionedTxns =
//@ts-ignore
this.wallet.supportedTransactionVersions?.size ?? 0 > 1;
this.txVersion = walletSupportsVersionedTxns ? 0 : 'legacy';

this.authoritySubAccountMap = new Map<string, number[]>();

/* Reset user stats account */
if (this.userStats?.isSubscribed) {
await this.userStats.unsubscribe();
}

this.userStats = undefined;

this.userStats = new UserStats({
driftClient: this,
userStatsAccountPublicKey: this.getUserStatsAccountPublicKey(),
accountSubscription: this.userStatsAccountSubscriptionConfig,
});

await this.userStats.subscribe();

let success = true;

if (this.isSubscribed) {
await Promise.all(this.unsubscribeUsers());
this.users.clear();
success = await this.addAndSubscribeToUsers(emulateAuthority);
}

return success;
}

public async switchActiveUser(subAccountId: number, authority?: PublicKey) {
const authorityChanged = authority && !this.authority?.equals(authority);

Expand Down Expand Up @@ -740,7 +785,7 @@ export class DriftClient {
/**
* Adds and subscribes to users based on params set by the constructor or by updateWallet.
*/
public async addAndSubscribeToUsers(): Promise<boolean> {
public async addAndSubscribeToUsers(authority?: PublicKey): Promise<boolean> {
// save the rpc calls if driftclient is initialized without a real wallet
if (this.skipLoadUsers) return true;

Expand Down Expand Up @@ -768,12 +813,12 @@ export class DriftClient {
let delegatedAccounts = [];

const userAccountsPromise = this.getUserAccountsForAuthority(
this.wallet.publicKey
authority ?? this.wallet.publicKey
);

if (this.includeDelegates) {
const delegatedAccountsPromise = this.getUserAccountsForDelegate(
this.wallet.publicKey
authority ?? this.wallet.publicKey
);
[userAccounts, delegatedAccounts] = await Promise.all([
userAccountsPromise,
Expand Down
Loading