Skip to content

Commit

Permalink
feat: getStorageAt block param
Browse files Browse the repository at this point in the history
  • Loading branch information
badurinantun committed Jul 18, 2022
1 parent 4f0c00b commit d89bf30
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 11 deletions.
18 changes: 15 additions & 3 deletions __tests__/defaultProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,31 @@ describe('defaultProvider', () => {
describe('getStorageAt', () => {
test('with "key" type of number', () => {
return expect(
testProvider.getStorageAt(exampleContractAddress, 0, 36663)
testProvider.getStorageAt(
exampleContractAddress,
0,
'0x1b77403cfce4a31f6919cf6c64c5bca7f9bba841ec6492d2cda8cf4486a58e1'
)
).resolves.not.toThrow();
});

test('"key" type of string', () => {
return expect(
testProvider.getStorageAt(exampleContractAddress, '0x0', 36663)
testProvider.getStorageAt(
exampleContractAddress,
'0x0',
'0x1b77403cfce4a31f6919cf6c64c5bca7f9bba841ec6492d2cda8cf4486a58e1'
)
).resolves.not.toThrow();
});

test('with "key" type of BN', () => {
return expect(
testProvider.getStorageAt(exampleContractAddress, toBN('0x0'), 36663)
testProvider.getStorageAt(
exampleContractAddress,
toBN('0x0'),
'0x1b77403cfce4a31f6919cf6c64c5bca7f9bba841ec6492d2cda8cf4486a58e1'
)
).resolves.not.toThrow();
});

Expand Down
3 changes: 2 additions & 1 deletion src/provider/default.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { StarknetChainId } from '../constants';
import {
BlockTag,
Call,
CallContractResponse,
DeclareContractPayload,
Expand Down Expand Up @@ -66,7 +67,7 @@ export class Provider implements ProviderInterface {
public async getStorageAt(
contractAddress: string,
key: BigNumberish,
blockIdentifier: BlockIdentifier = 'pending'
blockIdentifier: BlockTag | BigNumberish = 'latest'
): Promise<BigNumberish> {
return this.provider.getStorageAt(contractAddress, key, blockIdentifier);
}
Expand Down
5 changes: 3 additions & 2 deletions src/provider/interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { StarknetChainId } from '../constants';
import type {
BlockTag,
Call,
CallContractResponse,
ContractClass,
Expand Down Expand Up @@ -58,13 +59,13 @@ export abstract class ProviderInterface {
*
* @param contractAddress
* @param key - from getStorageVarAddress('<STORAGE_VARIABLE_NAME>') (WIP)
* @param blockIdentifier - block identifier
* @param blockHashOrTag - block hash or tag (pending, latest)
* @returns the value of the storage variable
*/
public abstract getStorageAt(
contractAddress: string,
key: BigNumberish,
blockIdentifier?: BlockIdentifier
blockHashOrTag?: BlockTag | BigNumberish
): Promise<BigNumberish>;

/**
Expand Down
9 changes: 7 additions & 2 deletions src/provider/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fetch from 'cross-fetch';

import { StarknetChainId } from '../constants';
import {
BlockTag,
Call,
CallContractResponse,
DeclareContractPayload,
Expand Down Expand Up @@ -103,10 +104,14 @@ export class RpcProvider implements ProviderInterface {
public async getStorageAt(
contractAddress: string,
key: BigNumberish,
blockHash: BlockIdentifier = 'pending'
blockHashOrTag: BlockTag | BigNumberish = 'pending'
): Promise<BigNumberish> {
const parsedKey = toHex(toBN(key));
return this.fetchEndpoint('starknet_getStorageAt', [contractAddress, parsedKey, blockHash]);
return this.fetchEndpoint('starknet_getStorageAt', [
contractAddress,
parsedKey,
blockHashOrTag,
]);
}

public async getTransaction(txHash: BigNumberish): Promise<GetTransactionResponse> {
Expand Down
5 changes: 3 additions & 2 deletions src/provider/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import urljoin from 'url-join';

import { ONE, StarknetChainId, ZERO } from '../constants';
import {
BlockTag,
Call,
CallContractResponse,
ContractClass,
Expand Down Expand Up @@ -226,11 +227,11 @@ export class SequencerProvider implements ProviderInterface {
public async getStorageAt(
contractAddress: string,
key: BigNumberish,
blockIdentifier: BlockIdentifier = 'pending'
blockHashOrTag: BlockTag | BigNumberish = 'latest'
): Promise<BigNumberish> {
const parsedKey = toBN(key).toString(10);
return this.fetchEndpoint('get_storage_at', {
blockIdentifier,
blockIdentifier: blockHashOrTag,
contractAddress,
key: parsedKey,
});
Expand Down
3 changes: 2 additions & 1 deletion src/types/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ export type Abi = Array<FunctionAbi | StructAbi>;

export type EntryPointsByType = object;
export type Program = Record<any, any>;
export type BlockNumber = 'pending' | 'latest' | null | number;
export type BlockTag = 'pending' | 'latest';
export type BlockNumber = BlockTag | null | number;

export type CompiledContract = {
abi: Abi;
Expand Down

0 comments on commit d89bf30

Please sign in to comment.