diff --git a/.github/workflows/_test.yml b/.github/workflows/_test.yml index 56ee1885b..35ee381b3 100644 --- a/.github/workflows/_test.yml +++ b/.github/workflows/_test.yml @@ -2,15 +2,10 @@ name: _ Run Tests on: workflow_call: inputs: - use-rpc-devnet: - type: boolean - default: false - use-seq-devnet: + use-devnet: type: boolean default: false secrets: - TEST_PROVIDER_BASE_URL: - required: false TEST_RPC_URL: required: false TEST_ACCOUNT_PRIVATE_KEY: @@ -26,20 +21,16 @@ jobs: # TODO - periodically check if conditional services are supported; https://github.com/actions/runner/issues/822 services: devnet: - image: ${{ inputs.use-rpc-devnet && 'shardlabs/starknet-devnet-rs:0c82d023f35bb77c4b5f4afd885ab14e44b73130-amd-seed0' || inputs.use-seq-devnet && 'shardlabs/starknet-devnet:0.6.3-seed0' || '' }} - # image: ${{ (inputs.use-rpc-devnet || inputs.use-seq-devnet) && 'shardlabs/starknet-devnet:0.6.3-seed0' || '' }} - # image: shardlabs/starknet-devnet-rs:0c82d023f35bb77c4b5f4afd885ab14e44b73130-amd-seed0 + image: ${{ (inputs.use-devnet) && 'shardlabs/starknet-devnet-rs:85495efb71a37ad3921c8986474b7e78a9a9f5fc-amd-seed0' || '' }} ports: - 5050:5050 env: - TEST_PROVIDER_BASE_URL: ${{ secrets.TEST_PROVIDER_BASE_URL }} TEST_RPC_URL: ${{ secrets.TEST_RPC_URL }} TEST_ACCOUNT_PRIVATE_KEY: ${{ secrets.TEST_ACCOUNT_PRIVATE_KEY }} TEST_ACCOUNT_ADDRESS: ${{ secrets.TEST_ACCOUNT_ADDRESS }} steps: - - run: echo ${{ secrets.TEST_PROVIDER_BASE_URL }} - run: echo ${{ secrets.TEST_RPC_URL }} - uses: actions/checkout@v3 - uses: actions/setup-node@v3 diff --git a/.github/workflows/manual-tests-devnet.yml b/.github/workflows/manual-tests-devnet.yml index 793ab5636..201c6b6fe 100644 --- a/.github/workflows/manual-tests-devnet.yml +++ b/.github/workflows/manual-tests-devnet.yml @@ -7,17 +7,12 @@ jobs: strategy: fail-fast: false matrix: - name: [sequencer-devnet, rpc-devnet] include: - - name: sequencer-devnet - TEST_PROVIDER_BASE_URL: http://127.0.0.1:5050/ - name: rpc-devnet TEST_RPC_URL: http://127.0.0.1:5050/rpc uses: ./.github/workflows/_test.yml with: - use-rpc-devnet: ${{ matrix.TEST_RPC_URL != '' }} - use-seq-devnet: ${{ matrix.TEST_PROVIDER_BASE_URL != '' }} + use-devnet: ${{ matrix.TEST_RPC_URL != '' }} secrets: - TEST_PROVIDER_BASE_URL: ${{ matrix.TEST_PROVIDER_BASE_URL }} TEST_RPC_URL: ${{ matrix.TEST_RPC_URL }} diff --git a/.github/workflows/manual-tests-testnet.yml b/.github/workflows/manual-tests-testnet.yml index 2416b76af..65d0c9d2f 100644 --- a/.github/workflows/manual-tests-testnet.yml +++ b/.github/workflows/manual-tests-testnet.yml @@ -5,20 +5,12 @@ jobs: tests: name: Run test on ${{ matrix.name }} strategy: - max-parallel: 1 # needed until we get a seperate account for the rpc provider, until then running them in parallel would result in a nonce issue + max-parallel: 1 matrix: - name: [sequencer-goerli, rpc-goerli] - include: - - name: sequencer-goerli - ENABLE_SEQUENCER: true - ENABLE_RPC: false - - name: rpc-goerli - ENABLE_SEQUENCER: false - ENABLE_RPC: true + name: [rpc-goerli] uses: ./.github/workflows/_test.yml secrets: - TEST_PROVIDER_BASE_URL: ${{ matrix.ENABLE_SEQUENCER && secrets.TEST_PROVIDER_BASE_URL || ''}} - TEST_RPC_URL: ${{ matrix.ENABLE_RPC && secrets.TEST_RPC_URL || ''}} + TEST_RPC_URL: ${{ secrets.TEST_RPC_URL }} TEST_ACCOUNT_PRIVATE_KEY: ${{ secrets.TEST_ACCOUNT_PRIVATE_KEY }} TEST_ACCOUNT_ADDRESS: ${{ secrets.TEST_ACCOUNT_ADDRESS }} diff --git a/.github/workflows/pr-push-dev.yml b/.github/workflows/pr-push-dev.yml index 2bffd8913..8a83e0944 100644 --- a/.github/workflows/pr-push-dev.yml +++ b/.github/workflows/pr-push-dev.yml @@ -44,19 +44,14 @@ jobs: strategy: fail-fast: false matrix: - name: [sequencer-devnet, rpc-devnet] include: - - name: sequencer-devnet - TEST_PROVIDER_BASE_URL: http://127.0.0.1:5050/ - name: rpc-devnet TEST_RPC_URL: http://127.0.0.1:5050/rpc uses: ./.github/workflows/_test.yml with: - use-rpc-devnet: ${{ matrix.TEST_RPC_URL != '' }} - use-seq-devnet: ${{ matrix.TEST_PROVIDER_BASE_URL != '' }} + use-devnet: ${{ matrix.TEST_RPC_URL != '' }} secrets: - TEST_PROVIDER_BASE_URL: ${{ matrix.TEST_PROVIDER_BASE_URL }} TEST_RPC_URL: ${{ matrix.TEST_RPC_URL }} release: diff --git a/.github/workflows/pr-push-main.yml b/.github/workflows/pr-push-main.yml index c0d4aa3cc..ba64e23a6 100644 --- a/.github/workflows/pr-push-main.yml +++ b/.github/workflows/pr-push-main.yml @@ -38,21 +38,13 @@ jobs: needs: [skip_check] if: needs.skip_check.outputs.should_skip != 'true' strategy: - max-parallel: 1 # needed until we get a seperate account for the rpc provider, until then running them in parallel would result in a nonce issue + max-parallel: 1 matrix: - name: [sequencer-goerli, rpc-goerli] - include: - - name: sequencer-goerli - ENABLE_SEQUENCER: true - ENABLE_RPC: false - - name: rpc-goerli - ENABLE_SEQUENCER: false - ENABLE_RPC: true + name: [rpc-goerli] uses: ./.github/workflows/_test.yml secrets: - TEST_PROVIDER_BASE_URL: ${{ matrix.ENABLE_SEQUENCER && secrets.TEST_PROVIDER_BASE_URL || ''}} - TEST_RPC_URL: ${{ matrix.ENABLE_RPC && secrets.TEST_RPC_URL || ''}} + TEST_RPC_URL: ${{ secrets.TEST_RPC_URL }} TEST_ACCOUNT_PRIVATE_KEY: ${{ secrets.TEST_ACCOUNT_PRIVATE_KEY }} TEST_ACCOUNT_ADDRESS: ${{ secrets.TEST_ACCOUNT_ADDRESS }} diff --git a/CHANGELOG.md b/CHANGELOG.md index 9718054be..9eea941fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,39 @@ +# [6.0.0-beta.11](https://github.com/starknet-io/starknet.js/compare/v6.0.0-beta.10...v6.0.0-beta.11) (2024-01-04) + +### Bug Fixes + +- utilize provided resourceBounds value ([65bea53](https://github.com/starknet-io/starknet.js/commit/65bea53ab03a04fdd4c122ba2fecbb1010100e18)) + +# [6.0.0-beta.10](https://github.com/starknet-io/starknet.js/compare/v6.0.0-beta.9...v6.0.0-beta.10) (2023-12-20) + +### Features + +- req id counter ([cedd1ea](https://github.com/starknet-io/starknet.js/commit/cedd1ea32cd58107c504d357a3dd5e9a40010a80)) + +# [6.0.0-beta.9](https://github.com/starknet-io/starknet.js/compare/v6.0.0-beta.8...v6.0.0-beta.9) (2023-12-19) + +### Bug Fixes + +- acc deploy_acc ([732cd94](https://github.com/starknet-io/starknet.js/commit/732cd946f26c2b0fe893067f25c1fe712d72d49b)) + +# [6.0.0-beta.8](https://github.com/starknet-io/starknet.js/compare/v6.0.0-beta.7...v6.0.0-beta.8) (2023-12-19) + +### Bug Fixes + +- estimateFeeBulk provided version ([da717be](https://github.com/starknet-io/starknet.js/commit/da717be8e656087eaba21c865a6dd0b648664491)) + +# [6.0.0-beta.7](https://github.com/starknet-io/starknet.js/compare/v6.0.0-beta.6...v6.0.0-beta.7) (2023-12-14) + +### Bug Fixes + +- provider rpc 0.5-0.6 getTransactionReceipt response standardization ([76b6ab4](https://github.com/starknet-io/starknet.js/commit/76b6ab49f6721d1f76c3f30d3d88d6dbc8b80bda)) + +### Features + +- curves ([405de46](https://github.com/starknet-io/starknet.js/commit/405de462032579ef0e8e434c62976dcb84ee1af8)) +- **provider:** fix and clean provider response and response parser, removed seqeuncer api ([7ecb069](https://github.com/starknet-io/starknet.js/commit/7ecb069e3d2c37653cedc44dfbb635c5bc8eec7f)) +- **provider:** pending response discrimination, pending type guards ([013a22b](https://github.com/starknet-io/starknet.js/commit/013a22bf4dd8cb6df817ccf6ff2d82837fe91693)) + # [6.0.0-beta.6](https://github.com/starknet-io/starknet.js/compare/v6.0.0-beta.5...v6.0.0-beta.6) (2023-12-11) ### Features diff --git a/__tests__/account.test.ts b/__tests__/account.test.ts index 19e40c90b..3c8ab4cb3 100644 --- a/__tests__/account.test.ts +++ b/__tests__/account.test.ts @@ -97,7 +97,9 @@ describe('deploy and test Wallet', () => { }); expect(result).toMatchSchemaRef('EstimateFee'); - expect(innerInvokeEstFeeSpy.mock.calls[0][1].version).toBe(constants.TRANSACTION_VERSION.F1); + expect([constants.TRANSACTION_VERSION.F1, constants.TRANSACTION_VERSION.F3]).toContain( + innerInvokeEstFeeSpy.mock.calls[0][1].version + ); innerInvokeEstFeeSpy.mockClear(); }); diff --git a/__tests__/config/jest.setup.ts b/__tests__/config/jest.setup.ts index aa784e2bd..90bbc1b5b 100644 --- a/__tests__/config/jest.setup.ts +++ b/__tests__/config/jest.setup.ts @@ -38,8 +38,9 @@ if (process.env.DEBUG === 'true') { response(response) { const cloned = response.clone(); - cloned.json().then(({ result }) => { - const match: any = combiner.find((it: any) => typeof it.result === 'undefined'); + cloned.json().then((res) => { + const { result } = res; + const match: any = combiner.find((it: any) => it.request.body.id === res.id); if (match && 'request' in match) { match.result = result; console.log(util.inspect(match, false, null, true /* enable colors */)); diff --git a/__tests__/contract.test.ts b/__tests__/contract.test.ts index 357da7052..773978313 100644 --- a/__tests__/contract.test.ts +++ b/__tests__/contract.test.ts @@ -2,9 +2,9 @@ import { BigNumberish, Contract, ContractFactory, + GetTransactionReceiptResponse, ParsedEvents, RawArgs, - SuccessfulTransactionReceiptResponse, json, stark, } from '../src'; @@ -720,7 +720,7 @@ describe('Complex interaction', () => { test('invoke compiled data', async () => { const result = await erc20Echo20Contract.iecho(CallData.compile(request)); const transaction = await provider.waitForTransaction(result.transaction_hash); - expect((transaction as SuccessfulTransactionReceiptResponse).execution_status).toBeDefined(); + expect((transaction as GetTransactionReceiptResponse).execution_status).toBeDefined(); }); // skip on live for performance @@ -730,19 +730,19 @@ describe('Complex interaction', () => { const result = await erc20Echo20Contract.iecho(calldata); const transaction = await provider.waitForTransaction(result.transaction_hash); - expect((transaction as SuccessfulTransactionReceiptResponse).execution_status).toBeDefined(); + expect((transaction as GetTransactionReceiptResponse).execution_status).toBeDefined(); const result1 = await erc20Echo20Contract.iecho(...args); const transaction1 = await provider.waitForTransaction(result1.transaction_hash); - expect((transaction1 as SuccessfulTransactionReceiptResponse).execution_status).toBeDefined(); + expect((transaction1 as GetTransactionReceiptResponse).execution_status).toBeDefined(); const result2 = await erc20Echo20Contract.invoke('iecho', calldata); const transaction2 = await provider.waitForTransaction(result2.transaction_hash); - expect((transaction2 as SuccessfulTransactionReceiptResponse).execution_status).toBeDefined(); + expect((transaction2 as GetTransactionReceiptResponse).execution_status).toBeDefined(); const result3 = await erc20Echo20Contract.invoke('iecho', args); const transaction3 = await provider.waitForTransaction(result3.transaction_hash); - expect((transaction3 as SuccessfulTransactionReceiptResponse).execution_status).toBeDefined(); + expect((transaction3 as GetTransactionReceiptResponse).execution_status).toBeDefined(); }); describe('speedup live tests', () => { @@ -795,9 +795,7 @@ describe('Complex interaction', () => { { formatResponse } ); const transaction = await provider.waitForTransaction(result.transaction_hash); - expect( - (transaction as SuccessfulTransactionReceiptResponse).execution_status - ).toBeDefined(); + expect((transaction as GetTransactionReceiptResponse).execution_status).toBeDefined(); }); }); diff --git a/__tests__/defaultProvider.test.ts b/__tests__/defaultProvider.test.ts index b081ec4a7..232ab7c27 100644 --- a/__tests__/defaultProvider.test.ts +++ b/__tests__/defaultProvider.test.ts @@ -18,6 +18,8 @@ import { } from './config/fixtures'; import { initializeMatcher } from './config/schema'; +const { isPendingStateUpdate } = provider; + const testProvider = new Provider(getTestProvider()); describe('defaultProvider', () => { @@ -81,32 +83,20 @@ describe('defaultProvider', () => { test(`getStateUpdate(blockHash=${exampleBlockHash}, blockNumber=undefined)`, async () => { const stateUpdate = await testProvider.getStateUpdate(exampleBlockHash); - provider.defStateUpdate( - stateUpdate, - (state) => { - expect(state.block_hash).toBe(exampleBlockHash); - expect(state).toMatchSchemaRef('StateUpdateResponse'); - }, - (pending) => { - fail('exampleBlockHash is latest block, should not be pending'); - expect(pending).toMatchSchemaRef('PendingStateUpdateResponse'); - } - ); + if (isPendingStateUpdate(stateUpdate)) { + fail('exampleBlockHash is latest block, should not be pending'); + } + expect(stateUpdate.block_hash).toBe(exampleBlockHash); + expect(stateUpdate).toMatchSchemaRef('StateUpdateResponse'); }); test(`getStateUpdate(blockHash=undefined, blockNumber=${exampleBlockNumber})`, async () => { const stateUpdate = await testProvider.getStateUpdate(exampleBlockNumber); - provider.defStateUpdate( - stateUpdate, - (state) => { - expect(state.block_hash).toBe(exampleBlockHash); - expect(state).toMatchSchemaRef('StateUpdateResponse'); - }, - (pending) => { - fail('exampleBlockHash is latest block, should not be pending'); - expect(pending).toMatchSchemaRef('PendingStateUpdateResponse'); - } - ); + if (isPendingStateUpdate(stateUpdate)) { + fail('exampleBlockHash is latest block, should not be pending'); + } + expect(stateUpdate.block_hash).toBe(exampleBlockHash); + expect(stateUpdate).toMatchSchemaRef('StateUpdateResponse'); }); }); @@ -181,7 +171,7 @@ describe('defaultProvider', () => { }), }) .then((res) => { - expect(Array.isArray(res.result)).toBe(true); + expect(Array.isArray(res)).toBe(true); }) ).resolves.not.toThrow(); }); diff --git a/__tests__/rpcProvider.test.ts b/__tests__/rpcProvider.test.ts index cba76a7d4..3da84184c 100644 --- a/__tests__/rpcProvider.test.ts +++ b/__tests__/rpcProvider.test.ts @@ -2,9 +2,9 @@ import { getStarkKey, utils } from '@scure/starknet'; import { Account, + Block, CallData, Contract, - GetBlockResponse, RPC, TransactionExecutionStatus, stark, @@ -77,12 +77,6 @@ describeIfRpc('RPCProvider', () => { expect(typeof spec).toBe('string'); }); - test('getCode - not implemented', async () => { - expect( - rpcProvider.getCode('0x058d97f7d76e78f44905cc30cb65b91ea49a4b908a76703c54197bca90f81773') - ).rejects.toThrow(); - }); - describe('Test Estimate message fee', () => { const L1_ADDRESS = '0x8359E4B0152ed5A731162D3c7B0D8D56edB165A0'; let l1l2ContractAddress: string; @@ -113,8 +107,8 @@ describeIfRpc('RPCProvider', () => { describe('waitForTransaction', () => { const receipt = {}; - const transactionStatusSpy = jest.spyOn(rpcProvider as any, 'getTransactionStatus'); - const transactionReceiptSpy = jest.spyOn(rpcProvider as any, 'getTransactionReceipt'); + const transactionStatusSpy = jest.spyOn(rpcProvider.channel as any, 'getTransactionStatus'); + const transactionReceiptSpy = jest.spyOn(rpcProvider.channel as any, 'getTransactionReceipt'); const generateOptions = (o: waitForTransactionOptions) => ({ retryInterval: 10, ...o }); const generateTransactionStatus = ( @@ -172,7 +166,7 @@ describeIfRpc('RPCProvider', () => { }); describe('RPC methods', () => { - let latestBlock: GetBlockResponse; + let latestBlock: Block; beforeAll(async () => { latestBlock = await provider.getBlock('latest'); diff --git a/__tests__/schemas/provider.json b/__tests__/schemas/provider.json index 486836dab..cff283f84 100644 --- a/__tests__/schemas/provider.json +++ b/__tests__/schemas/provider.json @@ -203,7 +203,16 @@ ] }, "actual_fee": { - "type": "string" + "type": "object", + "properties": { + "amount": { + "type": "string" + }, + "unit": { + "type": "string", + "enum": ["WEI", "FRI"] + } + } }, "status_data": { "type": "string" diff --git a/__tests__/utils/stark.test.ts b/__tests__/utils/stark.test.ts index 63dddda97..f2247e551 100644 --- a/__tests__/utils/stark.test.ts +++ b/__tests__/utils/stark.test.ts @@ -1,4 +1,5 @@ -import { CallData, EstimateFeeResponse, RawArgs, json, stark } from '../../src'; +import { CallData, RawArgs, UniversalDetails, json, stark } from '../../src'; +import { EDataAvailabilityMode, FeeEstimate } from '../../src/types/api'; import { toBigInt, toHex } from '../../src/utils/num'; import { compiledOpenZeppelinAccount } from '../config/fixtures'; @@ -71,14 +72,36 @@ describe('stark', () => { }); test('estimateFeeToBounds', () => { - const estimateFeeResponse: EstimateFeeResponse = { - gas_consumed: 100n, - gas_price: 10n, - overall_fee: 1000n, + const estimateFeeResponse: FeeEstimate = { + gas_consumed: '100', + gas_price: '10', + overall_fee: '1000', + unit: 'FRI', }; expect(stark.estimateFeeToBounds(estimateFeeResponse)).toStrictEqual({ l2_gas: { max_amount: '0x0', max_price_per_unit: '0x0' }, l1_gas: { max_amount: '0x6e', max_price_per_unit: '0xf' }, }); }); + + test('v3Details', () => { + const setValues = (o: {}, v: any) => Object.fromEntries(Object.keys(o).map((k) => [k, v])); + + const details: UniversalDetails = { + tip: 99n, + paymasterData: [99n, 99n], + accountDeploymentData: [99n, 99n], + nonceDataAvailabilityMode: EDataAvailabilityMode.L2, + feeDataAvailabilityMode: EDataAvailabilityMode.L2, + resourceBounds: { + l1_gas: { max_amount: '0x99', max_price_per_unit: '0x99' }, + l2_gas: { max_amount: '0x99', max_price_per_unit: '0x99' }, + }, + }; + const detailsUndefined = setValues(details, undefined); + const detailsAnything = setValues(details, expect.anything()); + + expect(stark.v3Details(details)).toMatchObject(details); + expect(stark.v3Details(detailsUndefined)).toEqual(expect.objectContaining(detailsAnything)); + }); }); diff --git a/package-lock.json b/package-lock.json index 053d46e9c..4a2d9ba10 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,17 +1,17 @@ { "name": "starknet", - "version": "6.0.0-beta.6", + "version": "6.0.0-beta.11", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "starknet", - "version": "6.0.0-beta.6", + "version": "6.0.0-beta.11", "license": "MIT", "dependencies": { - "@noble/curves": "~1.2.0", - "@scure/base": "^1.1.3", - "@scure/starknet": "~0.3.0", + "@noble/curves": "~1.3.0", + "@scure/base": "~1.1.3", + "@scure/starknet": "~1.0.0", "isomorphic-fetch": "^3.0.0", "lossless-json": "^2.0.8", "pako": "^2.0.4", @@ -3059,18 +3059,20 @@ } }, "node_modules/@noble/curves": { - "version": "1.2.0", - "license": "MIT", + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/@noble/curves/-/curves-1.3.0.tgz", + "integrity": "sha512-t01iSXPuN+Eqzb4eBX0S5oubSqXbK/xXa1Ne18Hj8f9pStxztHCE2gfboSp/dZRLSqfuLpRK2nDXDK+W9puocA==", "dependencies": { - "@noble/hashes": "1.3.2" + "@noble/hashes": "1.3.3" }, "funding": { "url": "https://paulmillr.com/funding/" } }, "node_modules/@noble/hashes": { - "version": "1.3.2", - "license": "MIT", + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.3.3.tgz", + "integrity": "sha512-V7/fPHgl+jsVPXqqeOzT8egNj2iBIVt+ECeMMG8TdcnTikP3oaBtUVqpT/gYCR68aEBJSF+XbYUxStjbFMqIIA==", "engines": { "node": ">= 16" }, @@ -3315,17 +3317,15 @@ } }, "node_modules/@scure/starknet": { - "version": "0.3.0", - "funding": [ - { - "type": "individual", - "url": "https://paulmillr.com/funding/" - } - ], - "license": "MIT", + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@scure/starknet/-/starknet-1.0.0.tgz", + "integrity": "sha512-o5J57zY0f+2IL/mq8+AYJJ4Xpc1fOtDhr+mFQKbHnYFmm3WQrC+8zj2HEgxak1a+x86mhmBC1Kq305KUpVf0wg==", "dependencies": { - "@noble/curves": "~1.2.0", - "@noble/hashes": "~1.3.2" + "@noble/curves": "~1.3.0", + "@noble/hashes": "~1.3.3" + }, + "funding": { + "url": "https://paulmillr.com/funding/" } }, "node_modules/@semantic-release/changelog": { diff --git a/package.json b/package.json index 0d9d06eba..f3f215bbc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "starknet", - "version": "6.0.0-beta.6", + "version": "6.0.0-beta.11", "description": "JavaScript library for Starknet", "main": "dist/index.js", "module": "dist/index.mjs", @@ -90,9 +90,9 @@ "typescript": "^5.0.4" }, "dependencies": { - "@noble/curves": "~1.2.0", - "@scure/base": "^1.1.3", - "@scure/starknet": "~0.3.0", + "@noble/curves": "~1.3.0", + "@scure/base": "~1.1.3", + "@scure/starknet": "~1.0.0", "isomorphic-fetch": "^3.0.0", "lossless-json": "^2.0.8", "pako": "^2.0.4", diff --git a/src/account/default.ts b/src/account/default.ts index 27aebc276..96b3da3e0 100644 --- a/src/account/default.ts +++ b/src/account/default.ts @@ -23,7 +23,6 @@ import { EstimateFee, EstimateFeeAction, EstimateFeeBulk, - EstimateFeeResponse, Invocation, Invocations, InvocationsSignerDetails, @@ -50,7 +49,6 @@ import { toBigInt, toCairoBool } from '../utils/num'; import { parseContract } from '../utils/provider'; import { estimateFeeToBounds, - estimatedFeeToMaxFee, formatSignature, randomAddress, reduceV2, @@ -158,18 +156,12 @@ export class Account extends Provider implements AccountInterface { }; const invocation = await this.buildInvocation(transactions, signerDetails); - const estimateFeeResponse = await super.getInvokeEstimateFee( + return super.getInvokeEstimateFee( { ...invocation }, { ...v3Details(details), version, nonce }, blockIdentifier, details.skipValidate ); - - return { - ...estimateFeeResponse, - suggestedMaxFee: estimatedFeeToMaxFee(estimateFeeResponse.overall_fee), - resourceBounds: estimateFeeToBounds(estimateFeeResponse), - }; } public async estimateDeclareFee( @@ -196,18 +188,12 @@ export class Account extends Provider implements AccountInterface { cairoVersion: undefined, // unused parameter }); - const estimateFeeResponse = await super.getDeclareEstimateFee( + return super.getDeclareEstimateFee( declareContractTransaction, { ...v3Details(details), version, nonce }, blockIdentifier, details.skipValidate ); - - return { - ...estimateFeeResponse, - suggestedMaxFee: estimatedFeeToMaxFee(estimateFeeResponse.overall_fee), - resourceBounds: estimateFeeToBounds(estimateFeeResponse), - }; } public async estimateAccountDeployFee( @@ -240,18 +226,12 @@ export class Account extends Provider implements AccountInterface { } ); - const estimateFeeResponse = await super.getDeployAccountEstimateFee( + return super.getDeployAccountEstimateFee( { ...payload }, { ...v3Details(details), version, nonce }, blockIdentifier, details.skipValidate ); - - return { - ...estimateFeeResponse, - suggestedMaxFee: estimatedFeeToMaxFee(estimateFeeResponse.overall_fee), - resourceBounds: estimateFeeToBounds(estimateFeeResponse), - }; } public async estimateDeployFee( @@ -266,29 +246,24 @@ export class Account extends Provider implements AccountInterface { invocations: Invocations, details: UniversalDetails = {} ): Promise { - const { nonce, blockIdentifier } = details; + const { nonce, blockIdentifier, version } = details; const accountInvocations = await this.accountInvocationsFactory(invocations, { ...v3Details(details), versions: [ ETransactionVersion.F1, // non-sierra - this.getPreferredVersion(ETransactionVersion.F2, ETransactionVersion.F3), // sierra + toTransactionVersion( + this.getPreferredVersion(ETransactionVersion.F2, ETransactionVersion.F3), + version + ), // sierra ], nonce, blockIdentifier, }); - const EstimateFeeResponseBulk = await super.getEstimateFeeBulk(accountInvocations, { + return super.getEstimateFeeBulk(accountInvocations, { blockIdentifier, skipValidate: details.skipValidate, }); - - return [].concat(EstimateFeeResponseBulk as []).map((elem: EstimateFeeResponse) => { - return { - ...elem, - suggestedMaxFee: estimatedFeeToMaxFee(elem.overall_fee), - resourceBounds: estimateFeeToBounds(elem), - }; - }); } public async simulateTransaction( @@ -680,8 +655,11 @@ export class Account extends Provider implements AccountInterface { default: feeEstimate = { - suggestedMaxFee: ZERO, + gas_consumed: 0n, + gas_price: 0n, overall_fee: ZERO, + unit: 'FRI', + suggestedMaxFee: ZERO, resourceBounds: estimateFeeToBounds(ZERO), }; break; @@ -759,6 +737,7 @@ export class Account extends Provider implements AccountInterface { }); return { + ...v3Details(details), classHash, addressSalt, constructorCalldata: compiledCalldata, diff --git a/src/channel/rpc_0_6.ts b/src/channel/rpc_0_6.ts index c533c9ef8..05952e2bf 100644 --- a/src/channel/rpc_0_6.ts +++ b/src/channel/rpc_0_6.ts @@ -42,6 +42,8 @@ export class RpcChannel { readonly retries: number; + public requestId: number; + readonly blockIdentifier: BlockIdentifier; private chainId?: StarknetChainId; @@ -65,6 +67,7 @@ export class RpcChannel { this.blockIdentifier = blockIdentifier || defaultOptions.blockIdentifier; this.chainId = chainId; this.waitMode = waitMode || false; + this.requestId = 0; } public fetch(method: string, params?: object, id: string | number = 0) { @@ -102,7 +105,7 @@ export class RpcChannel { params?: RPC.Methods[T]['params'] ): Promise { try { - const rawResult = await this.fetch(method, params); + const rawResult = await this.fetch(method, params, (this.requestId += 1)); const { error, result } = await rawResult.json(); this.errorHandler(method, params, error); return result as RPC.Methods[T]['result']; diff --git a/src/contract/default.ts b/src/contract/default.ts index d47227a35..df421d514 100644 --- a/src/contract/default.ts +++ b/src/contract/default.ts @@ -248,14 +248,14 @@ export class Contract implements ContractInterface { }, blockIdentifier ) - .then((x) => { + .then((it) => { if (!parseResponse) { - return x.result; + return it; } if (formatResponse) { - return this.callData.format(method, x.result, formatResponse); + return this.callData.format(method, it, formatResponse); } - return this.callData.parse(method, x.result); + return this.callData.parse(method, it); }); } diff --git a/src/provider/extensions/starknetId.ts b/src/provider/extensions/starknetId.ts index b75c53b00..0728a5cdc 100644 --- a/src/provider/extensions/starknetId.ts +++ b/src/provider/extensions/starknetId.ts @@ -38,7 +38,7 @@ export class StarknetId { address, }), }); - const decimalDomain = hexDomain.result.map((element) => BigInt(element)).slice(1); + const decimalDomain = hexDomain.map((element) => BigInt(element)).slice(1); const stringDomain = useDecoded(decimalDomain); @@ -72,7 +72,7 @@ export class StarknetId { }), }); - return addressData.result[0]; + return addressData[0]; } catch { throw Error('Could not get address from stark name'); } diff --git a/src/provider/interface.ts b/src/provider/interface.ts index d29876aaf..2ed6c81b4 100644 --- a/src/provider/interface.ts +++ b/src/provider/interface.ts @@ -3,6 +3,7 @@ import { StarknetChainId } from '../constants'; import type { AccountInvocations, BigNumberish, + Block, BlockIdentifier, Call, CallContractResponse, @@ -16,13 +17,13 @@ import type { EstimateFeeResponse, EstimateFeeResponseBulk, GetBlockResponse, - GetCodeResponse, GetTransactionReceiptResponse, GetTransactionResponse, Invocation, InvocationsDetailsWithNonce, InvokeFunctionResponse, Nonce, + PendingBlock, SimulateTransactionResponse, StateUpdateResponse, Storage, @@ -60,16 +61,11 @@ export abstract class ProviderInterface { * @param blockIdentifier block identifier * @returns the block object */ + public abstract getBlock(): Promise; + public abstract getBlock(blockIdentifier: 'pending'): Promise; + public abstract getBlock(blockIdentifier: 'latest'): Promise; public abstract getBlock(blockIdentifier: BlockIdentifier): Promise; - /** - * @deprecated The method should not be used - */ - public abstract getCode( - contractAddress: string, - blockIdentifier?: BlockIdentifier - ): Promise; - /** * Gets the contract class of the deployed contract. * diff --git a/src/provider/rpc.ts b/src/provider/rpc.ts index c8195557a..f71ee8f48 100644 --- a/src/provider/rpc.ts +++ b/src/provider/rpc.ts @@ -2,17 +2,22 @@ import { RpcChannel } from '../channel/rpc_0_6'; import { AccountInvocations, BigNumberish, + Block, BlockIdentifier, BlockTag, Call, ContractVersion, DeclareContractTransaction, DeployAccountContractTransaction, - GetCodeResponse, + GetBlockResponse, Invocation, InvocationsDetailsWithNonce, + PendingBlock, + PendingStateUpdate, RPC, RpcProviderOptions, + StateUpdate, + StateUpdateResponse, TransactionType, getContractVersionOptions, getEstimateFeeBulkOptions, @@ -56,6 +61,10 @@ export class RpcProvider implements ProviderInterface { return this.channel.getNonceForAddress(contractAddress, blockIdentifier); } + public async getBlock(): Promise; + public async getBlock(blockIdentifier: 'pending'): Promise; + public async getBlock(blockIdentifier: 'latest'): Promise; + public async getBlock(blockIdentifier?: BlockIdentifier): Promise; public async getBlock(blockIdentifier?: BlockIdentifier) { return this.channel .getBlockWithTxHashes(blockIdentifier) @@ -88,6 +97,10 @@ export class RpcProvider implements ProviderInterface { public getStateUpdate = this.getBlockStateUpdate; + public async getBlockStateUpdate(): Promise; + public async getBlockStateUpdate(blockIdentifier: 'pending'): Promise; + public async getBlockStateUpdate(blockIdentifier: 'latest'): Promise; + public async getBlockStateUpdate(blockIdentifier?: BlockIdentifier): Promise; public async getBlockStateUpdate(blockIdentifier?: BlockIdentifier) { return this.channel.getBlockStateUpdate(blockIdentifier); } @@ -113,9 +126,7 @@ export class RpcProvider implements ProviderInterface { } public async getTransaction(txHash: BigNumberish) { - return this.channel - .getTransactionByHash(txHash) - .then(this.responseParser.parseGetTransactionResponse); + return this.channel.getTransactionByHash(txHash); } public async getTransactionByHash(txHash: BigNumberish) { @@ -127,7 +138,9 @@ export class RpcProvider implements ProviderInterface { } public async getTransactionReceipt(txHash: BigNumberish) { - return this.channel.getTransactionReceipt(txHash); + return this.channel + .getTransactionReceipt(txHash) + .then(this.responseParser.parseTransactionReceipt); } public async getTransactionTrace(txHash: BigNumberish) { @@ -153,7 +166,9 @@ export class RpcProvider implements ProviderInterface { options?: getSimulateTransactionOptions ) { // can't be named simulateTransaction because of argument conflict with account - return this.channel.simulateTransaction(invocations, options); + return this.channel + .simulateTransaction(invocations, options) + .then(this.responseParser.parseSimulateTransactionResponse); } public async waitForTransaction(txHash: BigNumberish, options?: waitForTransactionOptions) { @@ -188,13 +203,6 @@ export class RpcProvider implements ProviderInterface { .then(this.responseParser.parseContractClassResponse); } - public async getCode( - _contractAddress: string, - _blockIdentifier?: BlockIdentifier - ): Promise { - throw new Error('RPC does not implement getCode function'); - } - public async getContractVersion( contractAddress: BigNumberish, classHash?: undefined, @@ -339,9 +347,7 @@ export class RpcProvider implements ProviderInterface { } public async callContract(call: Call, blockIdentifier?: BlockIdentifier) { - return this.channel - .callContract(call, blockIdentifier) - .then(this.responseParser.parseCallContractResponse); + return this.channel.callContract(call, blockIdentifier); } /** diff --git a/src/types/account.ts b/src/types/account.ts index 2c9950a14..a54a56247 100644 --- a/src/types/account.ts +++ b/src/types/account.ts @@ -1,11 +1,18 @@ import { EDataAvailabilityMode, ETransactionVersion, ResourceBounds } from './api'; -import { BigNumberish, BlockIdentifier, V3TransactionDetails } from './lib'; +import { + AllowArray, + BigNumberish, + BlockIdentifier, + Call, + DeclareContractPayload, + DeployAccountContractPayload, + TransactionType, + UniversalDeployerContractPayload, + V3TransactionDetails, +} from './lib'; import { DeclareTransactionReceiptResponse, EstimateFeeResponse } from './provider'; -export interface EstimateFee extends EstimateFeeResponse { - suggestedMaxFee: bigint; - resourceBounds: ResourceBounds; -} +export interface EstimateFee extends EstimateFeeResponse {} export type EstimateFeeBulk = Array; @@ -72,3 +79,21 @@ export enum SIMULATION_FLAG { SKIP_VALIDATE = 'SKIP_VALIDATE', SKIP_EXECUTE = 'SKIP_EXECUTE', } + +export type EstimateFeeAction = + | { + type: TransactionType.INVOKE; + payload: AllowArray; + } + | { + type: TransactionType.DECLARE; + payload: DeclareContractPayload; + } + | { + type: TransactionType.DEPLOY_ACCOUNT; + payload: DeployAccountContractPayload; + } + | { + type: TransactionType.DEPLOY; + payload: UniversalDeployerContractPayload; + }; diff --git a/src/types/api/rpcspec_0_6/components.ts b/src/types/api/rpcspec_0_6/components.ts index c98f361b0..039370dd1 100644 --- a/src/types/api/rpcspec_0_6/components.ts +++ b/src/types/api/rpcspec_0_6/components.ts @@ -134,6 +134,7 @@ export type STATE_DIFF = { export type PENDING_STATE_UPDATE = { old_root: FELT; state_diff: STATE_DIFF; + block_hash: never; // diverge: this makes it distinct }; export type STATE_UPDATE = { diff --git a/src/types/api/rpcspec_0_6/methods.ts b/src/types/api/rpcspec_0_6/methods.ts index 471b681f8..0350b0360 100644 --- a/src/types/api/rpcspec_0_6/methods.ts +++ b/src/types/api/rpcspec_0_6/methods.ts @@ -11,11 +11,9 @@ import { FELT, FUNCTION_CALL, MSG_FROM_L1, - PENDING_STATE_UPDATE, RESULT_PAGE_REQUEST, SIMULATION_FLAG, SIMULATION_FLAG_FOR_ESTIMATE_FEE, - STATE_UPDATE, STORAGE_KEY, TXN_HASH, } from './components'; @@ -33,6 +31,7 @@ import { InvokedTransaction, Nonce, SimulateTransactionResponse, + StateUpdate, Syncing, TransactionReceipt, TransactionStatus, @@ -72,7 +71,7 @@ type ReadMethods = { params: { block_id: BLOCK_ID; }; - result: STATE_UPDATE | PENDING_STATE_UPDATE; + result: StateUpdate; errors: Errors.BLOCK_NOT_FOUND; }; diff --git a/src/types/api/rpcspec_0_6/nonspec.ts b/src/types/api/rpcspec_0_6/nonspec.ts index 55a9d732b..f3453b43b 100644 --- a/src/types/api/rpcspec_0_6/nonspec.ts +++ b/src/types/api/rpcspec_0_6/nonspec.ts @@ -12,9 +12,12 @@ import { CONTRACT_CLASS, CONTRACT_STORAGE_DIFF_ITEM, DEPRECATED_CONTRACT_CLASS, + EMITTED_EVENT, + EVENT, EVENTS_CHUNK, EVENT_FILTER, FEE_ESTIMATE, + FEE_PAYMENT, FELT, MSG_FROM_L1, NONCE_UPDATE, @@ -22,6 +25,7 @@ import { PENDING_BLOCK_WITH_TX_HASHES, PENDING_STATE_UPDATE, PENDING_TXN_RECEIPT, + PRICE_UNIT, REPLACED_CLASS, RESOURCE_BOUNDS_MAPPING, RESULT_PAGE_REQUEST, @@ -40,10 +44,11 @@ import { // response starknet_getClass export type ContractClass = CONTRACT_CLASS | DEPRECATED_CONTRACT_CLASS; // response starknet_simulateTransactions -export type SimulateTransactionResponse = { +export type SimulateTransaction = { transaction_trace: TRANSACTION_TRACE; fee_estimation: FEE_ESTIMATE; -}[]; +}; +export type SimulateTransactionResponse = SimulateTransaction[]; // response starknet_estimateFee export type FeeEstimate = FEE_ESTIMATE; // response starknet_getTransactionByHash, starknet_getTransactionByBlockIdAndIndex @@ -62,6 +67,8 @@ export type BlockTransactionsTraces = { transaction_hash: FELT; trace_root: TRAN export type Syncing = false | SYNC_STATUS; // response starknet_getEvents export type Events = EVENTS_CHUNK; +export type EmittedEvent = EMITTED_EVENT; +export type Event = EVENT; // response starknet_addInvokeTransaction export type InvokedTransaction = { transaction_hash: TXN_HASH }; // response starknet_addDeclareTransaction @@ -77,6 +84,8 @@ export type TransactionHash = TXN_HASH; export type TransactionTrace = TRANSACTION_TRACE; export type BlockHash = BLOCK_HASH; export type TransactionReceipt = TXN_RECEIPT | PENDING_TXN_RECEIPT; +export type Receipt = TXN_RECEIPT; +export type PendingReceipt = PENDING_TXN_RECEIPT; export type EventFilter = EVENT_FILTER & RESULT_PAGE_REQUEST; export type SimulationFlags = Array; export type L1Message = MSG_FROM_L1; @@ -88,6 +97,8 @@ export type TransactionStatus = { execution_status?: TXN_EXECUTION_STATUS; }; export type ResourceBounds = RESOURCE_BOUNDS_MAPPING; +export type FeePayment = FEE_PAYMENT; +export type PriceUnit = PRICE_UNIT; // Diff Than Seq export type StorageDiffs = Array; diff --git a/src/types/api/sequencer.ts b/src/types/api/sequencer.ts deleted file mode 100644 index 1b1f9e786..000000000 --- a/src/types/api/sequencer.ts +++ /dev/null @@ -1,517 +0,0 @@ -import { - Abi, - AllowArray, - BigNumberish, - BlockIdentifier, - BlockNumber, - BlockStatus, - ByteCode, - CairoAssembly, - CompiledContract, - ContractClass, - EntryPointType, - RawCalldata, - TransactionExecutionStatus, - TransactionFinalityStatus, - TransactionStatus, - TransactionType, -} from '../lib'; - -// #region | originally not included in the namespace -export type GetTransactionStatusResponse = { - tx_status: TransactionStatus; - execution_status: TransactionExecutionStatus; - finality_status: TransactionFinalityStatus; - block_hash?: string; - tx_failure_reason?: { - code: string; - error_message: string; - }; - tx_revert_reason?: string; -}; - -export type GetContractAddressesResponse = { - Starknet: string; - GpsStatementVerifier: string; -}; - -export type FunctionInvocation = { - caller_address: string; - contract_address: string; - calldata: RawCalldata; - call_type?: string; - class_hash?: string; - selector?: string; - entry_point_type?: EntryPointType.EXTERNAL; // TODO: check this - result: Array; - execution_resources: ExecutionResources; - internal_calls: Array; - events: Array; - messages: Array; -}; - -export type ExecutionResources = { - n_steps: number; - builtin_instance_counter: { - pedersen_builtin: number; - range_check_builtin: number; - bitwise_builtin: number; - output_builtin: number; - ecdsa_builtin: number; - ec_op_builtin?: number; - }; - n_memory_holes: number; -}; - -export type CallL1Handler = { - from_address: string; - to_address: string; - entry_point_selector: string; - payload: Array; -}; - -export type DeployedContractItem = { - address: string; - class_hash: string; -}; - -export type SequencerIdentifier = { blockHash: string } | { blockNumber: BlockNumber }; -// #endregion - -export type TransactionTraceResponse = { - validate_invocation?: FunctionInvocation; - function_invocation?: FunctionInvocation; - fee_transfer_invocation?: FunctionInvocation; - constructor_invocation?: FunctionInvocation; - signature: string[]; -}; - -export type DeclareTransaction = { - type: TransactionType.DECLARE; - sender_address: string; - contract_class: ContractClass; - signature?: string[]; - nonce: BigNumberish; - max_fee?: BigNumberish; - version?: BigNumberish; - compiled_class_hash?: string; // v2 declare -}; - -export type DeployTransaction = { - type: TransactionType.DEPLOY; - contract_definition: ContractClass; - contract_address_salt: BigNumberish; - constructor_calldata: string[]; - nonce?: BigNumberish; -}; - -export type DeployAccountTransaction = { - type: TransactionType.DEPLOY_ACCOUNT; - class_hash: string; - contract_address_salt: BigNumberish; - constructor_calldata: string[]; - signature?: string[]; - max_fee?: BigNumberish; - version?: BigNumberish; - nonce?: BigNumberish; -}; - -export type InvokeFunctionTransaction = { - type: TransactionType.INVOKE; - sender_address: string; - signature?: string[]; - entry_point_type?: EntryPointType.EXTERNAL; // TODO: check this - calldata?: RawCalldata; - nonce: BigNumberish; - max_fee?: BigNumberish; - version?: BigNumberish; -}; - -export type Transaction = - | DeclareTransaction - | DeployTransaction - | InvokeFunctionTransaction - | DeployAccountTransaction; - -export type AddTransactionResponse = { - transaction_hash: string; - code?: 'TRANSACTION_RECEIVED'; - address?: string; - class_hash?: string; -}; - -export type GetCodeResponse = { - bytecode: ByteCode; - abi: Abi; -}; - -export interface InvokeFunctionTransactionResponse extends InvokeFunctionTransaction { - transaction_hash: string; - entry_point_selector: string; -} - -export type TransactionResponse = - | DeclareTransaction - | DeployTransaction - | InvokeFunctionTransactionResponse; - -export type SuccessfulTransactionResponse = { - execution_status: TransactionExecutionStatus.SUCCEEDED; - finality_status: TransactionFinalityStatus; - status: TransactionStatus; - block_hash: string; - block_number: BlockNumber; - transaction_index: number; - transaction: TransactionResponse; -}; - -export type RevertedTransactionResponse = { - execution_status: TransactionExecutionStatus.REVERTED; - finality_status: TransactionFinalityStatus; - status: TransactionStatus; - block_hash: string; - block_number: BlockNumber; - transaction_index: number; - transaction: TransactionResponse; - revert_error: string; -}; - -export type FailedTransactionResponse = { - status: TransactionStatus.REJECTED; - transaction_failure_reason: { - code: string; - error_message: string; - }; - transaction: TransactionResponse; -}; - -export type GetTransactionResponse = - | SuccessfulTransactionResponse - | RevertedTransactionResponse - | FailedTransactionResponse; - -export type TransactionReceiptResponse = - | SuccessfulTransactionReceiptResponse - | RevertedTransactionReceiptResponse - | RejectedTransactionReceiptResponse; - -export type SuccessfulTransactionReceiptResponse = { - execution_status: TransactionExecutionStatus.SUCCEEDED; - finality_status: TransactionFinalityStatus; - status: TransactionStatus; - actual_fee: string; - block_hash: string; - block_number: BlockNumber; - transaction_hash: string; - transaction_index: number; - l2_to_l1_messages: string[]; - events: string[]; - execution_resources?: ExecutionResources; // INVOKE ONLY -}; - -export type RevertedTransactionReceiptResponse = { - execution_status: TransactionExecutionStatus.REVERTED; - finality_status: TransactionFinalityStatus; - status: TransactionStatus.REVERTED; - actual_fee: string; - block_hash: string; - block_number: BlockNumber; - transaction_hash: string; - transaction_index: number; - l2_to_l1_messages: string[]; - events: string[]; - revert_error: string; -}; - -export type RejectedTransactionReceiptResponse = { - execution_status: TransactionExecutionStatus.REJECTED; - finality_status: TransactionFinalityStatus; - status: TransactionStatus.REJECTED; - transaction_hash: string; - l2_to_l1_messages: string[]; - events: string[]; - transaction_failure_reason: { - code: string; - error_message: string; - }; -}; - -export type GetBlockResponse = { - block_number: number; - state_root: string; - block_hash: string; - transactions: { - [txHash: string]: TransactionResponse; - }; - timestamp: number; - transaction_receipts: { - [txHash: string]: { - block_hash: string; - transaction_hash: string; - l2_to_l1_messages: { - to_address: string; - payload: string[]; - from_address: string; - }[]; - block_number: BlockNumber; - status: TransactionStatus; - transaction_index: number; - }; - }; - parent_block_hash: string; - status: BlockStatus; - gas_price: string; - sequencer_address: string; - starknet_version: string; -}; - -export type CallContractTransaction = { - calldata?: RawCalldata; - max_fee?: BigNumberish; - version?: BigNumberish; - entry_point_selector: string; -} & ( - | { - sender_address: string; - signature: string[]; - } - | { - contract_address: string; - signature?: never; - } -); - -export type CallContractResponse = { - result: string[]; -}; - -export type InvokeEstimateFee = Omit; -export type DeclareEstimateFee = Omit; -export type DeployAccountEstimateFee = Omit; -export type DeployEstimateFee = DeployTransaction; - -export type SimulateTransactionResponse = { - trace: TransactionTraceResponse; // diff with OPENRPC "transaction_trace" - fee_estimation: EstimateFeeResponse; -}; - -export type AccountTransactionItem = - | InvokeEstimateFee - | DeclareEstimateFee - | DeployEstimateFee - | DeployAccountEstimateFee; - -/** - * Transaction filled with account data - */ -export type AccountTransaction = AllowArray; - -// Support 0.9.1 changes in a backward-compatible way -export type EstimateFeeResponse = - | { - overall_fee: number; - gas_price: number; - gas_usage: number; - uint: string; - } - | { - amount: bigint; - unit: string; - }; - -export type EstimateFeeResponseBulk = AllowArray; - -export type BlockTransactionTracesResponse = { - traces: Array; -}; - -export type Storage = string; - -export type StateUpdateResponse = { - block_hash: string; - new_root: string; - old_root: string; - state_diff: { - storage_diffs: StorageDiffs; - nonces: Nonces; - deployed_contracts: Array; - old_declared_contracts: OldDeclaredContracts; - declared_classes: DeclaredClasses; - replaced_classes: ReplacedClasses; // no definition is it array of string - }; -}; - -export type StorageDiffs = { [address: string]: Array }; - -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; - REQUEST: never; - RESPONSE: GetContractAddressesResponse; - }; - add_transaction: { - QUERY: never; - REQUEST: Transaction; - RESPONSE: AddTransactionResponse; - }; - get_transaction: { - QUERY: { - transactionHash: string; - }; - REQUEST: never; - RESPONSE: GetTransactionResponse; - }; - get_transaction_status: { - QUERY: { - transactionHash: string; - }; - REQUEST: never; - RESPONSE: GetTransactionStatusResponse; - }; - get_transaction_trace: { - QUERY: { - transactionHash: string; - }; - REQUEST: never; - RESPONSE: TransactionTraceResponse; - }; - get_transaction_receipt: { - QUERY: { - transactionHash: string; - }; - REQUEST: never; - RESPONSE: TransactionReceiptResponse; - }; - get_nonce: { - QUERY: { - contractAddress: string; - blockIdentifier: BlockIdentifier; - }; - REQUEST: never; - RESPONSE: Nonce; - }; - get_storage_at: { - QUERY: { - contractAddress: string; - key: BigNumberish; - blockIdentifier: BlockIdentifier; - }; - REQUEST: never; - RESPONSE: Storage; - }; - get_code: { - QUERY: { - contractAddress: string; - blockIdentifier: BlockIdentifier; - }; - REQUEST: never; - RESPONSE: GetCodeResponse; - }; - get_block: { - QUERY: { - blockIdentifier: BlockIdentifier; - }; - REQUEST: never; - RESPONSE: GetBlockResponse; - }; - call_contract: { - QUERY: { - blockIdentifier: BlockIdentifier; - }; - REQUEST: CallContractTransaction; - RESPONSE: CallContractResponse; - }; - estimate_fee: { - QUERY: { - blockIdentifier: BlockIdentifier; - skipValidate: boolean; - }; - REQUEST: AccountTransactionItem; - RESPONSE: EstimateFeeResponse; - }; - get_class_by_hash: { - QUERY: { - classHash: string; - blockIdentifier?: BlockIdentifier; - }; - REQUEST: never; - RESPONSE: CompiledContract; - }; - get_class_hash_at: { - QUERY: { - contractAddress: string; - blockIdentifier?: BlockIdentifier; - }; - REQUEST: never; - RESPONSE: string; - }; - get_state_update: { - QUERY: { - blockHash?: string; - blockNumber?: BlockNumber; - }; - REQUEST: never; - RESPONSE: StateUpdateResponse; - }; - get_full_contract: { - QUERY: { - contractAddress: string; - blockIdentifier?: BlockIdentifier; - }; - REQUEST: never; - RESPONSE: CompiledContract; - }; - estimate_message_fee: { - QUERY: any; - REQUEST: any; - RESPONSE: EstimateFeeResponse; - }; - simulate_transaction: { - QUERY: { - blockIdentifier: BlockIdentifier; - skipValidate: boolean; - }; - REQUEST: AccountTransaction; - RESPONSE: SimulateTransactionResponse; - }; - estimate_fee_bulk: { - QUERY: { - blockIdentifier: BlockIdentifier; - skipValidate: boolean; - }; - REQUEST: AccountTransaction; - RESPONSE: EstimateFeeResponseBulk; - }; - get_block_traces: { - QUERY: { - blockHash?: string; - blockNumber?: BlockNumber; - }; - REQUEST: never; - RESPONSE: BlockTransactionTracesResponse; - }; - get_compiled_class_by_class_hash: { - QUERY: { - classHash: string; - blockIdentifier?: BlockIdentifier; - }; - REQUEST: any; - RESPONSE: CairoAssembly; - }; -}; diff --git a/src/types/index.ts b/src/types/index.ts index 02f824e4c..a096ffccc 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -8,13 +8,3 @@ export * from './typedData'; export * from './cairoEnum'; export * as RPC from './api'; -export { - CallL1Handler, - DeployedContractItem, - ExecutionResources, - FunctionInvocation, - GetContractAddressesResponse, - GetTransactionStatusResponse, - SequencerIdentifier, -} from './api/sequencer'; -export * as Sequencer from './api/sequencer'; diff --git a/src/types/provider/configuration.ts b/src/types/provider/configuration.ts index a4e067b07..b4d614f6d 100644 --- a/src/types/provider/configuration.ts +++ b/src/types/provider/configuration.ts @@ -12,20 +12,3 @@ export type RpcProviderOptions = { default?: boolean; waitMode?: boolean; }; - -export type SequencerHttpMethod = 'POST' | 'GET'; - -export type SequencerProviderOptions = { - headers?: Record; - blockIdentifier?: BlockIdentifier; - chainId?: StarknetChainId; -} & ( - | { - network: NetworkName | StarknetChainId; - } - | { - baseUrl: string; - feederGatewayUrl?: string; - gatewayUrl?: string; - } -); diff --git a/src/types/provider/response.ts b/src/types/provider/response.ts index 848ce2745..2ff3eded7 100644 --- a/src/types/provider/response.ts +++ b/src/types/provider/response.ts @@ -4,238 +4,84 @@ */ import * as RPC from '../api'; -import * as Sequencer from '../api/sequencer'; -import { - AllowArray, - BlockNumber, - BlockStatus, - ByteCode, - Call, - CompiledSierra, - DeclareContractPayload, - DeployAccountContractPayload, - LegacyContractClass, - RawCalldata, - Signature, - TransactionExecutionStatus, - TransactionFinalityStatus, - TransactionStatus, - TransactionType, - UniversalDeployerContractPayload, -} from '../lib'; - -export interface GetBlockResponse { - timestamp: number; - block_hash: string; - block_number: number; - new_root: string; - parent_hash: string; - status: BlockStatus; - transactions: Array; - gas_price?: string; - sequencer_address?: string; - starknet_version?: string; - transaction_receipts?: any; -} - -export interface GetCodeResponse { - bytecode: ByteCode; - // abi: string; // is not consistent between rpc and sequencer (is it?), therefore not included in the provider interface -} - -export interface ContractEntryPoint { - offset: string; - selector: string; -} - -export type GetTransactionResponse = - | InvokeTransactionResponse - | DeclareTransactionResponse - | RejectedTransactionResponse; - -export interface CommonTransactionResponse { - transaction_hash?: string; - version?: string; - signature?: Signature; - max_fee?: string; - nonce?: string; -} - -export interface InvokeTransactionResponse extends CommonTransactionResponse { - contract_address?: string; // TODO: Added for RPC comp, remove when rpc update to sender_address - sender_address?: string; - entry_point_selector?: string; - calldata: RawCalldata; -} +import { CompiledSierra, LegacyContractClass } from '../lib'; -export interface DeclareTransactionResponse extends CommonTransactionResponse { - contract_class?: any; - sender_address?: string; -} +export type GetBlockResponse = PendingBlock | Block; -export interface MessageToL1 { - to_address: string; - payload: Array; -} - -export interface Event { - from_address: string; - keys: Array; - data: Array; -} - -export interface MessageToL2 { - from_address: string; - payload: Array; -} - -export type RejectedTransactionResponse = { - status: `${TransactionStatus.REJECTED}`; - transaction_failure_reason: { - code: string; - error_message: string; - }; +export type PendingBlock = { + status: 'PENDING'; + parent_hash: RPC.SPEC.BLOCK_HASH; + timestamp: number; + sequencer_address: RPC.Felt; + l1_gas_price: RPC.SPEC.RESOURCE_PRICE; + starknet_version: string; + transactions: RPC.SPEC.TXN_HASH[]; }; -export type GetTransactionReceiptResponse = - | SuccessfulTransactionReceiptResponse - | RevertedTransactionReceiptResponse - | RejectedTransactionReceiptResponse; - -export type SuccessfulTransactionReceiptResponse = - | InvokeTransactionReceiptResponse - | DeployTransactionReceiptResponse - | DeclareTransactionReceiptResponse; - -export interface InvokeTransactionReceiptResponse { - type?: TransactionType; // RPC only - execution_status: TransactionExecutionStatus; - finality_status: TransactionFinalityStatus; - status?: `${TransactionStatus}`; // SEQ only - actual_fee: string | RPC.SPEC.FEE_ESTIMATE; - block_hash: RPC.BlockHash; - block_number: BlockNumber; - transaction_hash: string; - transaction_index?: number; // SEQ only - messages_sent: Array; // Casted SEQ l2_to_l1_messages - events: any[]; - execution_resources?: any; // SEQ Only -} - -export type DeclareTransactionReceiptResponse = { - type?: TransactionType; // RPC only - execution_status: TransactionExecutionStatus; - finality_status: TransactionFinalityStatus; - status?: `${TransactionStatus}`; // SEQ only - actual_fee: string; - block_hash: RPC.BlockHash; - block_number: BlockNumber; - transaction_hash: string; - transaction_index?: number; // SEQ only - messages_sent: Array; // Casted SEQ l2_to_l1_messages - events: any[]; +export type Block = { + status: 'ACCEPTED_ON_L2' | 'ACCEPTED_ON_L1' | 'REJECTED'; + block_hash: RPC.SPEC.BLOCK_HASH; + parent_hash: RPC.SPEC.BLOCK_HASH; + block_number: RPC.SPEC.BLOCK_NUMBER; + new_root: RPC.SPEC.FELT; + timestamp: number; + sequencer_address: RPC.SPEC.FELT; + l1_gas_price: RPC.SPEC.RESOURCE_PRICE; + starknet_version: string; + transactions: RPC.SPEC.TXN_HASH[]; }; -export type DeployTransactionReceiptResponse = InvokeTransactionReceiptResponse; - -// TODO: Missing RPC DEPLOY_ACCOUNT_TXN_RECEIPT - -// TODO: Missing RPC PENDING_TXN_RECEIPT - -// TODO: Missing RPC L1_HANDLER_TXN_RECEIPT - -export type RejectedTransactionReceiptResponse = { - status: `${TransactionStatus.REJECTED}`; - transaction_failure_reason: { - code: string; - error_message: string; - }; -}; +export type GetTransactionResponse = RPC.TransactionWithHash; -export type RevertedTransactionReceiptResponse = { - type?: TransactionType | any; // RPC only // any due to RPC Spec issue - execution_status: TransactionExecutionStatus.REVERTED | any; // any due to RPC Spec issue - finality_status: TransactionFinalityStatus | any; - status?: TransactionStatus; // SEQ only - actual_fee: string | RPC.SPEC.FEE_PAYMENT; - block_hash?: string; // ?~ optional due to RPC spec issue - block_number?: BlockNumber; // ?~ optional due to RCP spec issue - transaction_hash: string; - transaction_index?: number; // SEQ only - messages_sent: Array; // SEQ Casted l2_to_l1_messages - events: any[]; - revert_reason?: string; // SEQ Casted revert_error // ?~ optional due to RCP spec issue -}; +export type GetTransactionReceiptResponse = RPC.Receipt | RPC.PendingReceipt; +// Spread individual types for usage convenience +export type InvokeTransactionReceiptResponse = + | RPC.SPEC.INVOKE_TXN_RECEIPT + | RPC.SPEC.PENDING_INVOKE_TXN_RECEIPT; +export type DeclareTransactionReceiptResponse = + | RPC.SPEC.DECLARE_TXN_RECEIPT + | RPC.SPEC.PENDING_DECLARE_TXN_RECEIPT; +export type DeployTransactionReceiptResponse = InvokeTransactionReceiptResponse; +export type DeployAccountTransactionReceiptResponse = + | RPC.SPEC.DEPLOY_ACCOUNT_TXN_RECEIPT + | RPC.SPEC.PENDING_DEPLOY_ACCOUNT_TXN_RECEIPT; +export type L1HandlerTransactionReceiptResponse = + | RPC.SPEC.L1_HANDLER_TXN_RECEIPT + | RPC.SPEC.PENDING_L1_HANDLER_TXN_RECEIPT; export interface EstimateFeeResponse { + gas_consumed: bigint; overall_fee: bigint; - gas_consumed?: bigint; - gas_price?: bigint; - suggestedMaxFee?: bigint; + gas_price: bigint; + unit: RPC.PriceUnit; + suggestedMaxFee: bigint; + resourceBounds: RPC.ResourceBounds; } -export interface InvokeFunctionResponse { - transaction_hash: string; -} - -export interface DeclareContractResponse { - transaction_hash: string; - class_hash: string; -} +export type EstimateFeeResponseBulk = Array; -export type CallContractResponse = { - result: Array; -}; +export type InvokeFunctionResponse = RPC.InvokedTransaction; -export type EstimateFeeAction = - | { - type: TransactionType.INVOKE; - payload: AllowArray; - } - | { - type: TransactionType.DECLARE; - payload: DeclareContractPayload; - } - | { - type: TransactionType.DEPLOY_ACCOUNT; - payload: DeployAccountContractPayload; - } - | { - type: TransactionType.DEPLOY; - payload: UniversalDeployerContractPayload; - }; +export type DeclareContractResponse = RPC.DeclaredTransaction; -export type EstimateFeeResponseBulk = Array; +export type CallContractResponse = string[]; -export type Storage = Sequencer.Storage; +export type Storage = RPC.Felt; -export type Nonce = Sequencer.Nonce; +export type Nonce = string; export type SimulationFlags = RPC.SimulationFlags; -export type SimulatedTransaction = { - transaction_trace: RPC.TransactionTrace | Sequencer.TransactionTraceResponse; - fee_estimation: RPC.FeeEstimate | Sequencer.EstimateFeeResponse; - suggestedMaxFee?: string | bigint; +export type SimulatedTransaction = RPC.SimulateTransaction & { + suggestedMaxFee: bigint; + resourceBounds: RPC.ResourceBounds; }; export type SimulateTransactionResponse = SimulatedTransaction[]; -// 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: RPC.StorageDiffs; // API DIFF - deployed_contracts: Sequencer.DeployedContracts; - nonces: RPC.NonceUpdates; // API DIFF - old_declared_contracts?: Sequencer.OldDeclaredContracts; // Sequencer Only - declared_classes?: Sequencer.DeclaredClasses; - replaced_classes?: Sequencer.ReplacedClasses | RPC.ReplacedClasses; - deprecated_declared_classes?: RPC.DeprecatedDeclaredClasses; // RPC Only - }; -} +export type StateUpdateResponse = StateUpdate | PendingStateUpdate; +export type StateUpdate = RPC.SPEC.STATE_UPDATE; +export type PendingStateUpdate = RPC.SPEC.PENDING_STATE_UPDATE; /** * Standardized type diff --git a/src/utils/events/index.ts b/src/utils/events/index.ts index c0d96025d..d6d623575 100644 --- a/src/utils/events/index.ts +++ b/src/utils/events/index.ts @@ -8,8 +8,8 @@ import { LegacyEvent, ParsedEvent, ParsedEvents, + RPC, } from '../../types'; -import { Event as ProviderEvent } from '../../types/provider/response'; import responseParser from '../calldata/responseParser'; import { starkCurve } from '../ec'; import { addHexPrefix, utf8ToArray } from '../encode'; @@ -36,7 +36,7 @@ export function getAbiEvents(abi: Abi): AbiEvents { * @return ParsedEvents - parsed events corresponding to the abi */ export function parseEvents( - providerReceivedEvents: Array, + providerReceivedEvents: RPC.Event[], abiEvents: AbiEvents, abiStructs: AbiStructs, abiEnums: AbiEnums diff --git a/src/utils/provider.ts b/src/utils/provider.ts index e94d87c2e..7e5f52ec3 100644 --- a/src/utils/provider.ts +++ b/src/utils/provider.ts @@ -2,16 +2,19 @@ import { NetworkName, RPC_NODES } from '../constants'; import { BigNumberish, BlockIdentifier, - BlockNumber, BlockTag, CompiledContract, CompiledSierra, ContractClass, + GetBlockResponse, + GetTransactionReceiptResponse, InvocationsDetailsWithNonce, LegacyContractClass, + PendingBlock, + PendingStateUpdate, RPC, - SequencerIdentifier, SierraContractClass, + StateUpdateResponse, V3TransactionDetails, } from '../types'; import { ETransactionVersion } from '../types/api'; @@ -163,22 +166,11 @@ export class Block { toString = () => this.hash; - get sequencerIdentifier(): SequencerIdentifier { + /* get sequencerIdentifier(): SequencerIdentifier { return this.hash !== null ? { blockHash: this.hash as string } : { blockNumber: (this.number ?? this.tag) as BlockNumber }; - } -} - -export function defStateUpdate( - state: RPC.SPEC.STATE_UPDATE | RPC.SPEC.PENDING_STATE_UPDATE, - accepted: (state: RPC.SPEC.STATE_UPDATE) => unknown, - pending: (state: RPC.SPEC.PENDING_STATE_UPDATE) => unknown -) { - if ('block_hash' in state) { - return accepted(state); - } - return pending(state); + } */ } export function isV3Tx(details: InvocationsDetailsWithNonce): details is V3TransactionDetails { @@ -192,3 +184,29 @@ export function isVersion(version: '0.5' | '0.6', response: string) { return majorS === majorR && minorS === minorR; } + +/** + * Guard Pending Block + */ +export function isPendingBlock(response: GetBlockResponse): response is PendingBlock { + return response.status === 'PENDING'; +} + +/** + * Guard Pending Transaction + */ +export function isPendingTransaction( + response: GetTransactionReceiptResponse +): response is RPC.PendingReceipt { + return !('block_hash' in response); +} + +/** + * Guard Pending State Update + * ex. if(isPendingStateUpdate(stateUpdate)) throw Error('Update must be final') + */ +export function isPendingStateUpdate( + response: StateUpdateResponse +): response is PendingStateUpdate { + return !('block_hash' in response); +} diff --git a/src/utils/responseParser/rpc.ts b/src/utils/responseParser/rpc.ts index 292ad4824..7d5ccce70 100644 --- a/src/utils/responseParser/rpc.ts +++ b/src/utils/responseParser/rpc.ts @@ -3,24 +3,16 @@ * Intersection (sequencer response ∩ (∪ rpc responses)) */ import { - BlockStatus, - CallContractResponse, ContractClassResponse, EstimateFeeResponse, EstimateFeeResponseBulk, GetBlockResponse, - GetTransactionResponse, + GetTransactionReceiptResponse, + RPC, SimulateTransactionResponse, } from '../../types'; -import { - BlockWithTxHashes, - ContractClass, - FeeEstimate, - SimulateTransactionResponse as RPCSimulateTransactionResponse, - TransactionWithHash, -} from '../../types/api/rpcspec_0_6'; import { toBigInt } from '../num'; -import { estimatedFeeToMaxFee } from '../stark'; +import { estimateFeeToBounds, estimatedFeeToMaxFee } from '../stark'; import { ResponseParser } from '.'; export class RPCResponseParser @@ -31,67 +23,66 @@ export class RPCResponseParser | 'parseDeployContractResponse' | 'parseInvokeFunctionResponse' | 'parseGetTransactionReceiptResponse' + | 'parseGetTransactionResponse' + | 'parseCallContractResponse' > { - public parseGetBlockResponse(res: BlockWithTxHashes): GetBlockResponse { - return { - timestamp: res.timestamp, - block_hash: 'block_hash' in res ? res.block_hash : '', - block_number: 'block_number' in res ? res.block_number : -1, - new_root: 'new_root' in res ? res.new_root : '', - parent_hash: res.parent_hash, - status: 'status' in res ? (res.status as BlockStatus) : BlockStatus.PENDING, - transactions: res.transactions, - }; + public parseGetBlockResponse(res: RPC.BlockWithTxHashes): GetBlockResponse { + return { status: 'PENDING', ...res }; } - public parseGetTransactionResponse(res: TransactionWithHash): GetTransactionResponse { - return { - calldata: 'calldata' in res ? res.calldata : [], - contract_address: 'contract_address' in res ? res.contract_address : '', - sender_address: 'sender_address' in res ? res.sender_address : '', - max_fee: 'max_fee' in res ? res.max_fee : '', - nonce: 'nonce' in res ? res.nonce : '', - signature: 'signature' in res ? res.signature : [], - transaction_hash: res.transaction_hash, - version: res.version, - }; + public parseTransactionReceipt(res: RPC.TransactionReceipt): GetTransactionReceiptResponse { + // HOTFIX RPC 0.5 to align with RPC 0.6 + // This case is RPC 0.5. It can be only v2 thx with FRI units + if ('actual_fee' in res && typeof res.actual_fee === 'string') { + return { + ...res, + actual_fee: { + amount: res.actual_fee, + unit: 'FRI' as RPC.PriceUnit, + }, + }; + } + + return res; } - public parseFeeEstimateResponse(res: FeeEstimate[]): EstimateFeeResponse { + public parseFeeEstimateResponse(res: RPC.FeeEstimate[]): EstimateFeeResponse { + const val = res[0]; return { - overall_fee: toBigInt(res[0].overall_fee), - gas_consumed: toBigInt(res[0].gas_consumed), - gas_price: toBigInt(res[0].gas_price), + overall_fee: toBigInt(val.overall_fee), + gas_consumed: toBigInt(val.gas_consumed), + gas_price: toBigInt(val.gas_price), + unit: val.unit, + suggestedMaxFee: estimatedFeeToMaxFee(val.overall_fee), + resourceBounds: estimateFeeToBounds(val), }; } - public parseFeeEstimateBulkResponse(res: FeeEstimate[]): EstimateFeeResponseBulk { + public parseFeeEstimateBulkResponse(res: RPC.FeeEstimate[]): EstimateFeeResponseBulk { return res.map((val) => ({ overall_fee: toBigInt(val.overall_fee), gas_consumed: toBigInt(val.gas_consumed), gas_price: toBigInt(val.gas_price), + unit: val.unit, + suggestedMaxFee: estimatedFeeToMaxFee(val.overall_fee), + resourceBounds: estimateFeeToBounds(val), })); } - public parseCallContractResponse(res: string[]): CallContractResponse { - return { - result: res, - }; - } - public parseSimulateTransactionResponse( - res: RPCSimulateTransactionResponse + res: RPC.SimulateTransactionResponse ): SimulateTransactionResponse { return res.map((it) => { return { ...it, suggestedMaxFee: estimatedFeeToMaxFee(BigInt(it.fee_estimation.overall_fee)), + resourceBounds: estimateFeeToBounds(it.fee_estimation), }; }); } - public parseContractClassResponse(res: ContractClass): ContractClassResponse { + public parseContractClassResponse(res: RPC.ContractClass): ContractClassResponse { return { ...res, abi: typeof res.abi === 'string' ? JSON.parse(res.abi) : res.abi, diff --git a/src/utils/responseParser/sequencer.ts b/src/utils/responseParser/sequencer.ts deleted file mode 100644 index 360cd1a22..000000000 --- a/src/utils/responseParser/sequencer.ts +++ /dev/null @@ -1,210 +0,0 @@ -/** - * Map Sequencer Response to common interface response - * Intersection (sequencer response ∩ (∪ rpc responses)) - */ - -import { LibraryError } from '../../provider/errors'; -import { - CallContractResponse, - CompiledContract, - ContractClassResponse, - DeclareContractResponse, - DeployContractResponse, - EstimateFeeResponse, - EstimateFeeResponseBulk, - GetBlockResponse, - GetTransactionReceiptResponse, - GetTransactionResponse, - HexCalldata, - InvokeFunctionResponse, - Sequencer, - SimulateTransactionResponse, - StateUpdateResponse, - TransactionFinalityStatus, - TransactionStatus, -} from '../../types'; -import { isSierra } from '../contract'; -import { toBigInt } from '../num'; -import { parseContract } from '../provider'; -import { estimatedFeeToMaxFee } from '../stark'; -import { ResponseParser } from '.'; - -export class SequencerAPIResponseParser extends ResponseParser { - public parseGetBlockResponse(res: Sequencer.GetBlockResponse): GetBlockResponse { - return { - ...res, - new_root: res.state_root, - parent_hash: res.parent_block_hash, - transactions: Object.values(res.transactions) - .map((value) => 'transaction_hash' in value && value.transaction_hash) - .filter(Boolean) as Array, - }; - } - - public parseGetTransactionResponse( - res: Sequencer.GetTransactionResponse - ): GetTransactionResponse { - if ( - res.status === TransactionStatus.NOT_RECEIVED && - res.finality_status === TransactionFinalityStatus.NOT_RECEIVED - ) { - throw new LibraryError(); - } - - return { - ...res, - calldata: 'calldata' in res.transaction ? (res.transaction.calldata as HexCalldata) : [], - contract_class: - 'contract_class' in res.transaction ? (res.transaction.contract_class as any) : undefined, - entry_point_selector: - 'entry_point_selector' in res.transaction - ? res.transaction.entry_point_selector - : undefined, - max_fee: 'max_fee' in res.transaction ? (res.transaction.max_fee as string) : undefined, - nonce: res.transaction.nonce as string, - sender_address: - 'sender_address' in res.transaction - ? (res.transaction.sender_address as string) - : undefined, - signature: 'signature' in res.transaction ? res.transaction.signature : undefined, - transaction_hash: - 'transaction_hash' in res.transaction ? res.transaction.transaction_hash : undefined, - version: 'version' in res.transaction ? (res.transaction.version as string) : undefined, - }; - } - - public parseGetTransactionReceiptResponse( - res: Sequencer.TransactionReceiptResponse - ): GetTransactionReceiptResponse { - return { - ...res, - messages_sent: res.l2_to_l1_messages as any, - ...('revert_error' in res && { revert_reason: res.revert_error }), - }; - } - - public parseFeeEstimateResponse(res: Sequencer.EstimateFeeResponse): EstimateFeeResponse { - if ('overall_fee' in res) { - let gasInfo = {}; - - try { - gasInfo = { - gas_consumed: toBigInt(res.gas_usage), - gas_price: toBigInt(res.gas_price), - }; - } catch { - // do nothing - } - - return { - overall_fee: toBigInt(res.overall_fee), - ...gasInfo, - }; - } - return { - overall_fee: toBigInt(res.amount), - }; - } - - public parseFeeEstimateBulkResponse( - res: Sequencer.EstimateFeeResponseBulk - ): EstimateFeeResponseBulk { - return [].concat(res as []).map((item: Sequencer.EstimateFeeResponse) => { - if ('overall_fee' in item) { - let gasInfo = {}; - - try { - gasInfo = { - gas_consumed: toBigInt(item.gas_usage), - gas_price: toBigInt(item.gas_price), - }; - } catch { - // do nothing - } - - return { - overall_fee: toBigInt(item.overall_fee), - ...gasInfo, - }; - } - return { - overall_fee: toBigInt(item.amount), - }; - }); - } - - public parseSimulateTransactionResponse( - res: Sequencer.SimulateTransactionResponse - ): SimulateTransactionResponse { - const suggestedMaxFee = - 'overall_fee' in res.fee_estimation - ? res.fee_estimation.overall_fee - : res.fee_estimation.amount; - return [ - { - transaction_trace: res.trace, - fee_estimation: res.fee_estimation, - suggestedMaxFee: estimatedFeeToMaxFee(BigInt(suggestedMaxFee)), - }, - ]; - } - - public parseCallContractResponse(res: Sequencer.CallContractResponse): CallContractResponse { - return { - result: res.result, - }; - } - - public parseInvokeFunctionResponse( - res: Sequencer.AddTransactionResponse - ): InvokeFunctionResponse { - return { - transaction_hash: res.transaction_hash, - }; - } - - public parseDeployContractResponse( - res: Sequencer.AddTransactionResponse - ): DeployContractResponse { - return { - transaction_hash: res.transaction_hash, - contract_address: res.address as string, - }; - } - - public parseDeclareContractResponse( - res: Sequencer.AddTransactionResponse - ): DeclareContractResponse { - return { - transaction_hash: res.transaction_hash, - class_hash: res.class_hash as string, - }; - } - - public parseGetStateUpdateResponse(res: Sequencer.StateUpdateResponse): StateUpdateResponse { - 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, - nonces, - }, - }; - } - - public parseContractClassResponse(res: CompiledContract): ContractClassResponse { - const response = isSierra(res) ? res : parseContract(res); - return { - ...response, - abi: typeof response.abi === 'string' ? JSON.parse(response.abi) : response.abi, - }; - } -} diff --git a/src/utils/stark.ts b/src/utils/stark.ts index 52b722d38..50877560d 100644 --- a/src/utils/stark.ts +++ b/src/utils/stark.ts @@ -6,12 +6,17 @@ import { ArraySignatureType, BigNumberish, CompressedProgram, - EstimateFeeResponse, Program, Signature, UniversalDetails, } from '../types'; -import { EDAMode, EDataAvailabilityMode, ETransactionVersion, ResourceBounds } from '../types/api'; +import { + EDAMode, + EDataAvailabilityMode, + ETransactionVersion, + FeeEstimate, + ResourceBounds, +} from '../types/api'; import { addHexPrefix, arrayBufferToString, atobUniversal, btoaUniversal } from './encode'; import { parse, stringify } from './json'; import { @@ -100,7 +105,7 @@ export function estimatedFeeToMaxFee(estimatedFee: BigNumberish, overhead: numbe } export function estimateFeeToBounds( - estimate: EstimateFeeResponse | 0n, + estimate: FeeEstimate | 0n, amountOverhead: number = 10, priceOverhead = 50 ): ResourceBounds { @@ -166,8 +171,7 @@ export function toFeeVersion(providedVersion?: BigNumberish) { } /** - * Rerturn provided or default v3 tx details - * @param details EstimateFeeDetails + * Return provided or default v3 tx details */ export function v3Details(details: UniversalDetails) { return { @@ -176,7 +180,7 @@ export function v3Details(details: UniversalDetails) { accountDeploymentData: details.accountDeploymentData || [], nonceDataAvailabilityMode: details.nonceDataAvailabilityMode || EDataAvailabilityMode.L1, feeDataAvailabilityMode: details.feeDataAvailabilityMode || EDataAvailabilityMode.L1, - resourceBounds: estimateFeeToBounds(ZERO), + resourceBounds: details.resourceBounds ?? estimateFeeToBounds(ZERO), }; } diff --git a/www/docs/guides/events.md b/www/docs/guides/events.md index 64523470b..274efdcd7 100644 --- a/www/docs/guides/events.md +++ b/www/docs/guides/events.md @@ -167,7 +167,7 @@ while (continuationToken) { address: myContractAddress, keys: [keyFilter], chunk_size: 5, - continuation_token: continuationToken + continuation_token: continuationToken === "0" ? undefined : continuationToken }); const nbEvents = eventsRes.events.length; continuationToken=eventsRes.continuation_token;