Skip to content

Commit

Permalink
feat: add support for get_state_update in provider
Browse files Browse the repository at this point in the history
  • Loading branch information
irisdv committed Jan 13, 2023
1 parent be7b792 commit 76035a1
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 5 deletions.
8 changes: 8 additions & 0 deletions __tests__/defaultProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ describe('defaultProvider', () => {
const block = await testProvider.getBlock('latest');
return expect(block).toHaveProperty('block_number');
});

test('getStateUpdate()', async () => {
const stateUpdate = await testProvider.getStateUpdate('latest');
expect(stateUpdate).toHaveProperty('block_hash');
expect(stateUpdate).toHaveProperty('new_root');
expect(stateUpdate).toHaveProperty('old_root');
expect(stateUpdate).toHaveProperty('state_diff');
});
});

test('getNonceForAddress()', async () => {
Expand Down
5 changes: 5 additions & 0 deletions src/provider/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
InvocationBulk,
InvocationsDetailsWithNonce,
InvokeFunctionResponse,
StateUpdateResponse,
Status,
TransactionSimulationResponse,
} from '../types';
Expand Down Expand Up @@ -202,4 +203,8 @@ export class Provider implements ProviderInterface {
): Promise<TransactionSimulationResponse> {
return this.provider.getSimulateTransaction(invocation, invocationDetails, blockIdentifier);
}

public async getStateUpdate(blockIdentifier?: BlockIdentifier): Promise<StateUpdateResponse> {
return this.provider.getStateUpdate(blockIdentifier);
}
}
9 changes: 9 additions & 0 deletions src/provider/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
InvocationBulk,
InvocationsDetailsWithNonce,
InvokeFunctionResponse,
StateUpdateResponse,
Status,
TransactionSimulationResponse,
} from '../types';
Expand Down Expand Up @@ -318,4 +319,12 @@ export abstract class ProviderInterface {
invocationDetails: InvocationsDetailsWithNonce,
blockIdentifier?: BlockIdentifier
): Promise<TransactionSimulationResponse>;

/**
* Gets the state changes in a specific block
*
* @param blockIdentifier - block identifier
* @returns StateUpdateResponse
*/
public abstract getStateUpdate(blockIdentifier?: BlockIdentifier): Promise<StateUpdateResponse>;
}
7 changes: 5 additions & 2 deletions src/provider/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
InvocationsDetailsWithNonce,
InvokeFunctionResponse,
RPC,
StateUpdateResponse,
TransactionSimulationResponse,
} from '../types';
import fetch from '../utils/fetchPonyfill';
Expand Down Expand Up @@ -167,9 +168,11 @@ export class RpcProvider implements ProviderInterface {

public async getStateUpdate(
blockIdentifier: BlockIdentifier = this.blockIdentifier
): Promise<RPC.StateUpdate> {
): Promise<StateUpdateResponse> {
const block_id = new Block(blockIdentifier).identifier;
return this.fetchEndpoint('starknet_getStateUpdate', { block_id });
return this.fetchEndpoint('starknet_getStateUpdate', { block_id }).then(
this.responseParser.parseGetStateUpdateResponse
);
}

public async getStorageAt(
Expand Down
9 changes: 9 additions & 0 deletions src/provider/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
InvocationsDetailsWithNonce,
InvokeFunctionResponse,
Sequencer,
StateUpdateResponse,
TransactionSimulationResponse,
TransactionTraceResponse,
} from '../types';
Expand Down Expand Up @@ -587,4 +588,12 @@ export class SequencerProvider implements ProviderInterface {
}
).then(this.responseParser.parseFeeSimulateTransactionResponse);
}

public async getStateUpdate(
blockIdentifier: BlockIdentifier = this.blockIdentifier
): Promise<StateUpdateResponse> {
return this.fetchEndpoint('get_state_update', { blockIdentifier }).then(
this.responseParser.parseGetStateUpdateResponse
);
}
}
33 changes: 31 additions & 2 deletions src/types/api/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@ export type CallL1Handler = {
payload: Array<string>;
};

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

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

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

export namespace Sequencer {
export type DeclareTransaction = {
type: 'DECLARE';
Expand Down Expand Up @@ -260,6 +275,20 @@ export namespace Sequencer {

export type EstimateFeeResponseBulk = AllowArray<EstimateFeeResponse>;

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>;
deployed_contracts: Array<DeployedContractItem>;
nonces: Array<Nonces>;
};
};

export type Endpoints = {
get_contract_addresses: {
QUERY: never;
Expand Down Expand Up @@ -362,10 +391,10 @@ export namespace Sequencer {
};
get_state_update: {
QUERY: {
blockHash: string;
blockIdentifier: BlockIdentifier;
};
REQUEST: never;
RESPONSE: any;
RESPONSE: StateUpdateResponse;
};
get_full_contract: {
QUERY: {
Expand Down
21 changes: 20 additions & 1 deletion src/types/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
*/
import BN from 'bn.js';

import { TransactionTraceResponse } from './api/sequencer';
import {
DeployedContractItem,
Nonces,
StateDiffItem,
TransactionTraceResponse,
} from './api/sequencer';
import {
AllowArray,
Call,
Expand Down Expand Up @@ -140,3 +145,17 @@ export interface TransactionSimulationResponse {
trace: TransactionTraceResponse;
fee_estimation: EstimateFeeResponse;
}

export interface StateUpdateResponse {
block_hash: string;
new_root: string;
old_root: string;
state_diff: {
storage_diffs: Array<{
[address: string]: Array<StateDiffItem>;
}>;
declared_contract_hashes: Array<string>;
deployed_contracts: Array<DeployedContractItem>;
nonces: Array<Nonces>;
};
}
22 changes: 22 additions & 0 deletions src/utils/responseParser/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
GetBlockResponse,
GetTransactionResponse,
RPC,
StateUpdateResponse,
} from '../../types';
import { toBN } from '../number';
import { ResponseParser } from '.';
Expand Down Expand Up @@ -67,4 +68,25 @@ export class RPCResponseParser
result: res,
};
}

public parseGetStateUpdateResponse(res: RPC.StateUpdate): StateUpdateResponse {
const storageDiff = []
.concat(res.state_diff.storage_diffs as [])
.map(({ address, storage_entries }) => {
return {
[address]: storage_entries,
};
});
return {
block_hash: res.block_hash,
new_root: res.new_root,
old_root: res.old_root,
state_diff: {
storage_diffs: storageDiff,
declared_contract_hashes: res.state_diff.declared_contract_hashes,
deployed_contracts: res.state_diff.deployed_contracts,
nonces: res.state_diff.nonces,
},
};
}
}
19 changes: 19 additions & 0 deletions src/utils/responseParser/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
GetTransactionResponse,
InvokeFunctionResponse,
Sequencer,
StateUpdateResponse,
TransactionSimulationResponse,
} from '../../types';
import { toBN } from '../number';
Expand Down Expand Up @@ -193,4 +194,22 @@ export class SequencerAPIResponseParser extends ResponseParser {
class_hash: res.class_hash as string,
};
}

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,
};
});
return {
...res,
state_diff: {
storage_diffs: res.state_diff.storage_diffs,
declared_contract_hashes: res.state_diff.declared_contract_hashes,
deployed_contracts: res.state_diff.deployed_contracts,
nonces,
},
};
}
}
26 changes: 26 additions & 0 deletions www/docs/API/Provider/provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,29 @@ Estimate fee for declare transaction.
provider.**waitForTransaction**(txHash [ , retryInterval]) => _Promise < GetTransactionReceiptResponse >_

Wait for the transaction to be accepted on L2 or L1.

---

### getStateUpdate()

provider.**getStateUpdate**(transaction, details) => _Promise < StateUpdateResponse >_

Gets the state changes in a specific block

###### StateUpdateResponse

```typescript
{
block_hash: string;
new_root: string;
old_root: string;
state_diff: {
storage_diffs: Array<{
[address: string]: Array<StateDiffItem>;
}>;
declared_contract_hashes: Array<string>;
deployed_contracts: Array<DeployedContractItem>;
nonces: Array<Nonces>;
};
};
```

0 comments on commit 76035a1

Please sign in to comment.