Skip to content

Commit

Permalink
feat: get_state_update, fix types, fix responseParser, fix BigNumberi…
Browse files Browse the repository at this point in the history
…sh api response, fix OPENRPC
  • Loading branch information
tabaktoni committed Feb 27, 2023
1 parent fc33d19 commit 50a2c29
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 60 deletions.
6 changes: 4 additions & 2 deletions src/provider/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ import {
InvocationBulk,
InvocationsDetailsWithNonce,
InvokeFunctionResponse,
Nonce,
RPC,
StateUpdateResponse,
Storage,
TransactionSimulationResponse,
waitForTransactionOptions,
} from '../types';
Expand Down Expand Up @@ -115,15 +117,15 @@ export class Provider implements ProviderInterface {
public async getNonceForAddress(
contractAddress: string,
blockIdentifier?: BlockIdentifier
): Promise<BigNumberish> {
): Promise<Nonce> {
return this.provider.getNonceForAddress(contractAddress, blockIdentifier);
}

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

Expand Down
6 changes: 4 additions & 2 deletions src/provider/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import type {
InvocationBulk,
InvocationsDetailsWithNonce,
InvokeFunctionResponse,
Nonce,
RPC,
StateUpdateResponse,
Storage,
TransactionSimulationResponse,
waitForTransactionOptions,
} from '../types';
Expand Down Expand Up @@ -103,7 +105,7 @@ export abstract class ProviderInterface {
public abstract getNonceForAddress(
contractAddress: string,
blockIdentifier?: BlockIdentifier
): Promise<BigNumberish>;
): Promise<Nonce>;

/**
* Gets the contract's storage variable at a specific key.
Expand All @@ -117,7 +119,7 @@ export abstract class ProviderInterface {
contractAddress: string,
key: BigNumberish,
blockIdentifier?: BlockIdentifier
): Promise<BigNumberish>;
): Promise<Storage>;

/**
* Gets the transaction information from a tx id.
Expand Down
2 changes: 1 addition & 1 deletion src/provider/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export class RpcProvider implements ProviderInterface {
contractAddress: string,
key: BigNumberish,
blockIdentifier: BlockIdentifier = this.blockIdentifier
): Promise<BigNumberish> {
): Promise<RPC.Storage> {
const parsedKey = toHex(key);
const block_id = new Block(blockIdentifier).identifier;
return this.fetchEndpoint('starknet_getStorageAt', {
Expand Down
4 changes: 2 additions & 2 deletions src/provider/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,15 +288,15 @@ export class SequencerProvider implements ProviderInterface {
public async getNonceForAddress(
contractAddress: string,
blockIdentifier: BlockIdentifier = this.blockIdentifier
): Promise<BigNumberish> {
): Promise<Sequencer.Nonce> {
return this.fetchEndpoint('get_nonce', { contractAddress, blockIdentifier });
}

public async getStorageAt(
contractAddress: string,
key: BigNumberish,
blockIdentifier: BlockIdentifier = this.blockIdentifier
): Promise<BigNumberish> {
): Promise<Sequencer.Storage> {
const parsedKey = toBigInt(key).toString(10);
return this.fetchEndpoint('get_storage_at', {
blockIdentifier,
Expand Down
6 changes: 3 additions & 3 deletions src/types/api/openrpc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Starknet RPC version 0.2.0
* Starknet RPC version 0.2.1
*
* Starknet Node API 0.45.0 - rpc 0.2.1
* Starknet Node Write API 0.3.0 - rpc 0.2.1
Expand Down Expand Up @@ -252,9 +252,9 @@ type CONTRACT_ENTRY_POINT = {
offset: NUM_AS_HEX;
selector: FELT;
};
type CONTRACT_STORAGE_DIFF_ITEM = {
export type CONTRACT_STORAGE_DIFF_ITEM = {
address: FELT;
storage_entries: [key: FELT, value: FELT];
storage_entries: { key: FELT; value: FELT }[];
};

type DEPLOYED_CONTRACT_ITEM = {
Expand Down
12 changes: 10 additions & 2 deletions src/types/api/rpc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ADDRESS, FELT, OPENRPC } from './openrpc';
import { ADDRESS, CONTRACT_STORAGE_DIFF_ITEM, FELT, OPENRPC } from './openrpc';

export namespace RPC {
export type Response = {
Expand Down Expand Up @@ -42,13 +42,21 @@ export namespace RPC {
export type DeclaredTransaction = OPENRPC.DeclaredTransaction;
export type DeployedTransaction = OPENRPC.DeployedTransaction;
export type Methods = OPENRPC.Methods;
export type Storage = OPENRPC.Storage;

// 'DECLARE' | 'DEPLOY' | 'DEPLOY_ACCOUNT' | 'INVOKE' | 'L1_HANDLER'
export enum TransactionType {
DECLARE = 'DECLARE',
DEPLOY = 'DEPLOY',
DEPLOY_ACCOUNT = 'DEPLOY_ACCOUNT',
INVOKE = 'INVOKE',
L1_HANDLER = 'L1_HANDLER',
}

// Exported Diff on Sequencer (can be removed when diff resolved by new RPC v)
export type StorageDiffs = Array<CONTRACT_STORAGE_DIFF_ITEM>;
export type DeclaredContractHashes = Array<FELT>;
export type Nonces = Array<{
contract_address: ADDRESS;
nonce: FELT;
}>;
}
49 changes: 27 additions & 22 deletions src/types/api/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,26 +70,11 @@ export type CallL1Handler = {
payload: Array<string>;
};

export type StateDiffItem = {
key: string;
value: string;
};

export type StorageDiffItem = {
address: string;
storage_entries: [key: string, value: string];
};

export type DeployedContractItem = {
address: string;
class_hash: string;
};

export type Nonces = {
contract_address: string;
nonce: string;
};

export type SequencerIdentifier = { blockHash: string } | { blockNumber: BlockNumber };

export namespace Sequencer {
Expand Down Expand Up @@ -286,20 +271,40 @@ export namespace Sequencer {
traces: Array<TransactionTraceResponse & { transaction_hash: string }>;
};

export type Storage = string;

export type StateUpdateResponse = {
block_hash: string;
new_root: string;
old_root: string;
state_diff: {
storage_diffs: Array<{
[address: string]: Array<StateDiffItem>;
}>;
declared_contract_hashes: Array<string>;
storage_diffs: StorageDiffs;
nonces: Nonces;
deployed_contracts: Array<DeployedContractItem>;
nonces: Array<Nonces>;
old_declared_contracts: OldDeclaredContracts;
declared_classes: DeclaredClasses;
replaced_classes: ReplacedClasses; // no definition is it array of string
};
};

export type StorageDiffs = { [address: string]: Array<StateDiffItem> };

export type StateDiffItem = { key: string; value: string };

export type Nonces = { [address: string]: Nonce };

export type Nonce = string;

export type DeployedContracts = DeployedContractItem[];

export type OldDeclaredContracts = string[];

export type DeclaredClasses = DeclaredClass[];

export type DeclaredClass = { class_hash: string; compiled_class_hash: string };

export type ReplacedClasses = string[]; // no definition is it array of string ?

export type Endpoints = {
get_contract_addresses: {
QUERY: never;
Expand Down Expand Up @@ -345,7 +350,7 @@ export namespace Sequencer {
blockIdentifier: BlockIdentifier;
};
REQUEST: never;
RESPONSE: BigNumberish;
RESPONSE: Nonce;
};
get_storage_at: {
QUERY: {
Expand All @@ -354,7 +359,7 @@ export namespace Sequencer {
blockIdentifier: BlockIdentifier;
};
REQUEST: never;
RESPONSE: string;
RESPONSE: Storage;
};
get_code: {
QUERY: {
Expand Down
24 changes: 14 additions & 10 deletions src/types/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@
* Intersection (sequencer response ∩ (∪ rpc responses))
*/

import {
DeployedContractItem,
Nonces,
StorageDiffItem,
TransactionTraceResponse,
} from './api/sequencer';
import { RPC } from './api/rpc';
import { Sequencer, TransactionTraceResponse } from './api/sequencer';
import {
AllowArray,
ByteCode,
Expand Down Expand Up @@ -142,19 +138,27 @@ export type EstimateFeeAction =

export type EstimateFeeResponseBulk = Array<EstimateFeeResponse>;

export type Storage = Sequencer.Storage;

export type Nonce = Sequencer.Nonce;

export interface TransactionSimulationResponse {
trace: TransactionTraceResponse;
fee_estimation: EstimateFeeResponse;
}

// As RPC and Sequencer response diverge, use RPC as common response
export interface StateUpdateResponse {
block_hash: string;
new_root: string;
old_root: string;
state_diff: {
storage_diffs: Array<StorageDiffItem>;
declared_contract_hashes: Array<string>;
deployed_contracts: Array<DeployedContractItem>;
nonces: Array<Nonces>;
storage_diffs: RPC.StorageDiffs; // API DIFF
declared_contract_hashes?: RPC.DeclaredContractHashes; // RPC only
deployed_contracts: Sequencer.DeployedContracts;
nonces: RPC.Nonces; // API DIFF
old_declared_contracts?: Sequencer.OldDeclaredContracts; // Sequencer Only
declared_classes?: Sequencer.DeclaredClasses; // Sequencer Only
replaced_classes?: Sequencer.ReplacedClasses; // Sequencer Only
};
}
25 changes: 9 additions & 16 deletions src/utils/responseParser/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,26 +198,19 @@ export class SequencerAPIResponseParser extends ResponseParser {
}

public parseGetStateUpdateResponse(res: Sequencer.StateUpdateResponse): StateUpdateResponse {
const nonces = [].concat(res.state_diff.nonces as []).map(({ contract_address, nonce }) => {
return {
contract_address,
nonce: nonce as string,
};
});
const storage_diffs = []
.concat(res.state_diff.storage_diffs as [])
.map(({ address, storage_entries }) => {
return {
address,
storage_entries,
};
});
const nonces = Object.entries(res.state_diff.nonces).map(([contract_address, nonce]) => ({
contract_address,
nonce,
}));
const storage_diffs = Object.entries(res.state_diff.storage_diffs).map(
([address, storage_entries]) => ({ address, storage_entries })
);

return {
...res,
state_diff: {
...res.state_diff,
storage_diffs,
declared_contract_hashes: res.state_diff.declared_contract_hashes,
deployed_contracts: res.state_diff.deployed_contracts,
nonces,
},
};
Expand Down

0 comments on commit 50a2c29

Please sign in to comment.