Skip to content

Commit

Permalink
Sync sender address state before txns (MystenLabs#3621)
Browse files Browse the repository at this point in the history
  • Loading branch information
gegaowp authored Aug 2, 2022
1 parent c689474 commit ebb1399
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 0 deletions.
15 changes: 15 additions & 0 deletions sdk/typescript/src/providers/json-rpc-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
} from '../types';

const isNumber = (val: any): val is number => typeof val === 'number';
const isAny = (_val: any): _val is any => true;

export class JsonRpcProvider extends Provider {
private client: JsonRpcClient;
Expand Down Expand Up @@ -245,4 +246,18 @@ export class JsonRpcProvider extends Provider {
);
}
}

async syncAccountState(address: string): Promise<any> {
try {
return await this.client.requestWithType(
'sui_syncAccountState',
[address],
isAny,
);
} catch (err) {
throw new Error(
`Error sync account address for address: ${address} with error: ${err}`,
);
}
}
}
1 change: 1 addition & 0 deletions sdk/typescript/src/providers/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@ export abstract class Provider {
pubkey: string
): Promise<TransactionResponse>;

abstract syncAccountState(address: string): Promise<any>
// TODO: add more interface methods
}
4 changes: 4 additions & 0 deletions sdk/typescript/src/providers/void-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ export class VoidProvider extends Provider {
throw this.newError('getRecentTransactions');
}

async syncAccountState(_address: string): Promise<any> {
throw this.newError('syncAccountState');
}

private newError(operation: string): Error {
return new Error(`Please use a valid provider for ${operation}`);
}
Expand Down
9 changes: 9 additions & 0 deletions sdk/typescript/src/signers/signer-with-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ export abstract class SignerWithProvider implements Signer {
);
}

/**
* Trigger gateway to sync account state related to the address,
* based on the account state on validators.
*/
async syncAccountState(): Promise<any> {
const address = await this.getAddress();
return await this.provider.syncAccountState(address);
}

/**
* Serialize and Sign a `TransferObject` transaction and submit to the Gateway for execution
*/
Expand Down
3 changes: 3 additions & 0 deletions wallet/src/ui/app/redux/slices/sui-objects/Coin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export class Coin {
amount: bigint,
recipient: SuiAddress
): Promise<TransactionResponse> {
await signer.syncAccountState();
const coin = await Coin.selectCoin(signer, coins, amount);
return await signer.transferObject({
objectId: coin,
Expand All @@ -98,6 +99,7 @@ export class Coin {
amount: bigint,
recipient: SuiAddress
): Promise<TransactionResponse> {
await signer.syncAccountState();
const coin = await Coin.prepareCoinWithEnoughBalance(
signer,
coins,
Expand Down Expand Up @@ -127,6 +129,7 @@ export class Coin {
validator: SuiAddress
): Promise<TransactionResponse> {
const coin = await Coin.selectCoin(signer, coins, amount);
await signer.syncAccountState();
return await signer.executeMoveCall({
packageObjectId: '0x2',
module: 'sui_system',
Expand Down
2 changes: 2 additions & 0 deletions wallet/src/ui/app/redux/slices/sui-objects/NFT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class ExampleNFT {
description?: string,
imageUrl?: string
): Promise<TransactionResponse> {
await signer.syncAccountState();
return await signer.executeMoveCall({
packageObjectId: '0x2',
module: 'devnet_nft',
Expand All @@ -39,6 +40,7 @@ export class ExampleNFT {
recipientID: string,
transferCost: number
): Promise<TransactionResponse> {
await signer.syncAccountState();
return await signer.transferObject({
objectId: nftId,
gasBudget: transferCost,
Expand Down

0 comments on commit ebb1399

Please sign in to comment.