Skip to content

Commit

Permalink
feat: getNonce endpoint for sequencer
Browse files Browse the repository at this point in the history
  • Loading branch information
ivpavici authored and dhruvkelawala committed Sep 2, 2022
1 parent 5c56584 commit add4900
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 9 deletions.
5 changes: 5 additions & 0 deletions __tests__/defaultProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ describe('defaultProvider', () => {
return expect(block).toHaveProperty('block_number');
});

test('getNonce()', async () => {
const nonce = await testProvider.getNonce(exampleContractAddress);
return expect(nonce).toEqual('0x0');
});

describe('getStorageAt', () => {
test('with "key" type of number', () => {
return expect(testProvider.getStorageAt(exampleContractAddress, 0)).resolves.not.toThrow();
Expand Down
18 changes: 10 additions & 8 deletions __tests__/sequencerProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import {

// Run only if Devnet Sequencer
describeIfSequencer('SequencerProvider', () => {
let provider: SequencerProvider;
let sequencerProvider: SequencerProvider;
let customSequencerProvider: Provider;
let exampleContractAddress: string;

beforeAll(async () => {
provider = getTestProvider() as SequencerProvider;
sequencerProvider = getTestProvider() as SequencerProvider;
customSequencerProvider = new Provider({
sequencer: {
baseUrl: 'https://alpha4.starknet.io',
Expand All @@ -28,32 +28,34 @@ describeIfSequencer('SequencerProvider', () => {
let exampleTransactionHash: string;

beforeAll(async () => {
const { transaction_hash, contract_address } = await provider.deployContract({
const { transaction_hash, contract_address } = await sequencerProvider.deployContract({
contract: compiledErc20,
});
await provider.waitForTransaction(transaction_hash);
await sequencerProvider.waitForTransaction(transaction_hash);
exampleTransactionHash = transaction_hash;
exampleContractAddress = contract_address;
});

test('getTransactionStatus()', async () => {
return expect(provider.getTransactionStatus(exampleTransactionHash)).resolves.not.toThrow();
return expect(
sequencerProvider.getTransactionStatus(exampleTransactionHash)
).resolves.not.toThrow();
});

test('transaction trace', async () => {
const transactionTrace = await provider.getTransactionTrace(exampleTransactionHash);
const transactionTrace = await sequencerProvider.getTransactionTrace(exampleTransactionHash);
expect(transactionTrace).toHaveProperty('function_invocation');
expect(transactionTrace).toHaveProperty('signature');
});

test('getCode() -> { bytecode }', async () => {
const code = await provider.getCode(exampleContractAddress);
const code = await sequencerProvider.getCode(exampleContractAddress);
return expect(Array.isArray(code.bytecode)).toBe(true);
});

describeIfNotDevnet('which are not available on devnet', () => {
test('getContractAddresses()', async () => {
const { GpsStatementVerifier, Starknet } = await provider.getContractAddresses();
const { GpsStatementVerifier, Starknet } = await sequencerProvider.getContractAddresses();
expect(typeof GpsStatementVerifier).toBe('string');
expect(typeof Starknet).toBe('string');
});
Expand Down
4 changes: 4 additions & 0 deletions src/provider/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ export class Provider implements ProviderInterface {
return this.provider.getEstimateFee(invocation, blockIdentifier, invocationDetails);
}

public async getNonce(contractAddress: string): Promise<any> {
return this.provider.getNonce(contractAddress);
}

public async getStorageAt(
contractAddress: string,
key: BigNumberish,
Expand Down
8 changes: 8 additions & 0 deletions src/provider/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ export abstract class ProviderInterface {
blockIdentifier?: BlockIdentifier
): Promise<ContractClass>;

/**
* Gets the nonce of a contract with respect to a specific block
*
* @param contractAddress - contract address
* @returns the hex nonce
*/
public abstract getNonce(contractAddress: string): Promise<any>;

/**
* Gets the contract's storage variable at a specific key.
*
Expand Down
4 changes: 4 additions & 0 deletions src/provider/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ export class SequencerProvider implements ProviderInterface {
);
}

public async getNonce(contractAddress: string): Promise<any> {
return this.fetchEndpoint('get_nonce', { contractAddress });
}

public async getStorageAt(
contractAddress: string,
key: BigNumberish,
Expand Down
2 changes: 1 addition & 1 deletion src/types/api/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export namespace RPC {
starknet_getNonce: {
QUERY: never;
REQUEST: any[];
RESPONSE: string;
RESPONSE: any;
};
starknet_getStorageAt: {
QUERY: never;
Expand Down
7 changes: 7 additions & 0 deletions src/types/api/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,13 @@ export namespace Sequencer {
REQUEST: never;
RESPONSE: TransactionReceiptResponse;
};
get_nonce: {
QUERY: {
contractAddress: string;
};
REQUEST: never;
RESPONSE: any;
};
get_storage_at: {
QUERY: {
contractAddress: string;
Expand Down

0 comments on commit add4900

Please sign in to comment.