Skip to content

Commit

Permalink
fix: add blockIdentifier for getNonce
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvkelawala committed Sep 7, 2022
1 parent a8bd811 commit 7f2edab
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 11 deletions.
5 changes: 3 additions & 2 deletions src/account/default.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ZERO } from '../constants';
import { ProviderInterface, ProviderOptions } from '../provider';
import { Provider } from '../provider/default';
import { BlockIdentifier } from '../provider/utils';
import { Signer, SignerInterface } from '../signer';
import {
Abi,
Expand Down Expand Up @@ -32,8 +33,8 @@ export class Account extends Provider implements AccountInterface {
'getPubKey' in keyPairOrSigner ? keyPairOrSigner : new Signer(keyPairOrSigner as KeyPair);
}

public async getNonce(): Promise<BigNumberish> {
return super.getNonce(this.address);
public async getNonce(blockIdentifier?: BlockIdentifier): Promise<BigNumberish> {
return super.getNonce(this.address, blockIdentifier);
}

public async estimateFee(
Expand Down
3 changes: 2 additions & 1 deletion src/account/interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ProviderInterface } from '../provider';
import { BlockIdentifier } from '../provider/utils';
import { SignerInterface } from '../signer';
import {
Abi,
Expand Down Expand Up @@ -92,5 +93,5 @@ export abstract class AccountInterface extends ProviderInterface {
*/
public abstract verifyMessageHash(hash: BigNumberish, signature: Signature): Promise<boolean>;

public abstract getNonce(): Promise<BigNumberish>;
public abstract getNonce(blockIdentifier?: BlockIdentifier): Promise<BigNumberish>;
}
7 changes: 5 additions & 2 deletions src/provider/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,11 @@ export class Provider implements ProviderInterface {
return this.provider.getEstimateFee(invocation, invocationDetails, blockIdentifier);
}

public async getNonce(contractAddress: string): Promise<BigNumberish> {
return this.provider.getNonce(contractAddress);
public async getNonce(
contractAddress: string,
blockIdentifier?: BlockIdentifier
): Promise<BigNumberish> {
return this.provider.getNonce(contractAddress, blockIdentifier);
}

public async getStorageAt(
Expand Down
5 changes: 4 additions & 1 deletion src/provider/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ export abstract class ProviderInterface {
* @param contractAddress - contract address
* @returns the hex nonce
*/
public abstract getNonce(contractAddress: string): Promise<BigNumberish>;
public abstract getNonce(
contractAddress: string,
blockIdentifier?: BlockIdentifier
): Promise<BigNumberish>;

/**
* Gets the contract's storage variable at a specific key.
Expand Down
11 changes: 9 additions & 2 deletions src/provider/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,15 @@ export class RpcProvider implements ProviderInterface {
return this.fetchEndpoint('starknet_getBlockWithTxs', [blockIdentifierGetter.getIdentifier()]);
}

public async getNonce(contractAddress: string): Promise<BigNumberish> {
return this.fetchEndpoint('starknet_getNonce', [contractAddress]);
public async getNonce(
contractAddress: string,
blockIdentifier: BlockIdentifier = 'pending'
): Promise<BigNumberish> {
const blockIdentifierGetter = new BlockIdentifierClass(blockIdentifier);
return this.fetchEndpoint('starknet_getNonce', [
contractAddress,
blockIdentifierGetter.getIdentifier(),
]);
}

public async getStorageAt(
Expand Down
7 changes: 5 additions & 2 deletions src/provider/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,11 @@ export class SequencerProvider implements ProviderInterface {
);
}

public async getNonce(contractAddress: string): Promise<BigNumberish> {
return this.fetchEndpoint('get_nonce', { contractAddress });
public async getNonce(
contractAddress: string,
blockIdentifier: BlockIdentifier = 'pending'
): Promise<BigNumberish> {
return this.fetchEndpoint('get_nonce', { contractAddress, blockIdentifier });
}

public async getStorageAt(
Expand Down
1 change: 1 addition & 0 deletions src/types/api/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ export namespace Sequencer {
get_nonce: {
QUERY: {
contractAddress: string;
blockIdentifier: BlockIdentifier;
};
REQUEST: never;
RESPONSE: BigNumberish;
Expand Down
2 changes: 1 addition & 1 deletion www/docs/API/provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ Gets the latest block number.

<hr/>

provider.**getNonce**(contractAddress) => _Promise< BigNumberish >_
provider.**getNonce**(contractAddress, blockIdentifier) => _Promise< BigNumberish >_

Gets the nonce of the provided contractAddress

Expand Down

0 comments on commit 7f2edab

Please sign in to comment.