diff --git a/__tests__/rpcProvider.test.ts b/__tests__/rpcProvider.test.ts index cbd4854c4..60f60894e 100644 --- a/__tests__/rpcProvider.test.ts +++ b/__tests__/rpcProvider.test.ts @@ -1,4 +1,4 @@ -import { Account, RpcProvider, ec } from '../src'; +import { Account, GetBlockResponse, RpcProvider, ec } from '../src'; import { compiledOpenZeppelinAccount, describeIfRpc, @@ -20,8 +20,12 @@ describeIfRpc('RPCProvider', () => { accountPublicKey = ec.getStarkKey(accountKeyPair); }); - describe('RPC methods', async () => { - const latestBlock = await rpcProvider.getBlock('latest'); + describe('RPC methods', () => { + let latestBlock: GetBlockResponse; + + beforeAll(async () => { + latestBlock = await rpcProvider.getBlock('latest'); + }); test('getChainId', async () => { const chainId = await rpcProvider.getChainId(); @@ -38,13 +42,18 @@ describeIfRpc('RPCProvider', () => { expect(blockResponse).toHaveProperty('transactions'); }); - describe('deployContract', async () => { - const { contract_address, transaction_hash } = await rpcProvider.deployContract({ - contract: compiledOpenZeppelinAccount, - constructorCalldata: [accountPublicKey], - addressSalt: accountPublicKey, + describe('deployContract', () => { + let contract_address; + let transaction_hash; + + beforeAll(async () => { + ({ contract_address, transaction_hash } = await rpcProvider.deployContract({ + contract: compiledOpenZeppelinAccount, + constructorCalldata: [accountPublicKey], + addressSalt: accountPublicKey, + })); + await rpcProvider.waitForTransaction(transaction_hash); }); - await rpcProvider.waitForTransaction(transaction_hash); test('deployContract result', () => { expect(contract_address).toBeTruthy(); @@ -52,16 +61,16 @@ describeIfRpc('RPCProvider', () => { }); test('getTransactionByHash', async () => { - const blockResponse = await rpcProvider.getTransactionByHash(transaction_hash); - expect(blockResponse).toHaveProperty('transactions'); + const transaction = await rpcProvider.getTransactionByHash(transaction_hash); + expect(transaction).toHaveProperty('transaction_hash'); }); test('getTransactionByBlockIdAndIndex', async () => { - const blockResponse = await rpcProvider.getTransactionByBlockIdAndIndex( + const transaction = await rpcProvider.getTransactionByBlockIdAndIndex( latestBlock.block_number, 0 ); - expect(blockResponse).toHaveProperty('transactions'); + expect(transaction).toHaveProperty('transaction_hash'); }); }); diff --git a/src/provider/rpc.ts b/src/provider/rpc.ts index bd5ac6207..e4170b405 100644 --- a/src/provider/rpc.ts +++ b/src/provider/rpc.ts @@ -36,6 +36,7 @@ export type RpcProviderOptions = { nodeUrl: string }; export class RpcProvider implements ProviderInterface { public nodeUrl: string; + // interface public chainId!: StarknetChainId; private responseParser = new RPCResponseParser(); @@ -79,6 +80,7 @@ export class RpcProvider implements ProviderInterface { } } + // Interface public async getChainId(): Promise { return this.fetchEndpoint('starknet_chainId'); } @@ -112,7 +114,8 @@ export class RpcProvider implements ProviderInterface { blockIdentifier: BlockIdentifier, contractAddress: RPC.ContractAddress ): Promise { - return this.fetchEndpoint('starknet_getClassHashAt', [blockIdentifier, contractAddress]); + const block = new Block(blockIdentifier); + return this.fetchEndpoint('starknet_getClassHashAt', [block.identifier, contractAddress]); } public async getNonce(contractAddress: string): Promise { @@ -128,7 +131,8 @@ export class RpcProvider implements ProviderInterface { } public async getStateUpdate(blockIdentifier: BlockIdentifier): Promise { - return this.fetchEndpoint('starknet_getStateUpdate', [blockIdentifier]); + const block = new Block(blockIdentifier); + return this.fetchEndpoint('starknet_getStateUpdate', [block.identifier]); } public async getStorageAt( @@ -160,7 +164,11 @@ export class RpcProvider implements ProviderInterface { blockIdentifier: BlockIdentifier, index: number ): Promise { - return this.fetchEndpoint('starknet_getTransactionByHash', [blockIdentifier, index]); + const block = new Block(blockIdentifier); + return this.fetchEndpoint('starknet_getTransactionByBlockIdAndIndex', [ + block.identifier, + index, + ]); } public async getTransactionReceipt(txHash: BigNumberish): Promise { @@ -253,6 +261,7 @@ export class RpcProvider implements ProviderInterface { ]).then(this.responseParser.parseInvokeFunctionResponse); } + // Interface public async callContract( call: Call, blockIdentifier: BlockIdentifier = 'pending' diff --git a/src/types/api/openrpc.ts b/src/types/api/openrpc.ts index af42f224e..76305380d 100644 --- a/src/types/api/openrpc.ts +++ b/src/types/api/openrpc.ts @@ -242,7 +242,7 @@ export namespace OPENRPC { export type TransactionReceipt = TXN_RECEIPT; export type ContractClass = CONTRACT_CLASS; export type CallResponse = Array; - export type EstimateFee = FEE_ESTIMATE; + export type EstimatedFee = FEE_ESTIMATE; export type BlockNumber = BLOCK_NUMBER; export type BlockHashAndNumber = { block_hash: BLOCK_HASH; @@ -276,85 +276,85 @@ export namespace OPENRPC { starknet_getBlockWithTxHashes: { params: { block_id: BLOCK_ID }; result: BlockWithTxHashes; - errors: errors.INVALID_BLOCK_ID; + errors: Errors.INVALID_BLOCK_ID; }; starknet_getBlockWithTxs: { params: { block_id: BLOCK_ID }; result: BlockWithTxs; - errors: errors.INVALID_BLOCK_ID; + errors: Errors.INVALID_BLOCK_ID; }; starknet_getStateUpdate: { params: { block_id: BLOCK_ID }; result: StateUpdate; - errors: errors.INVALID_BLOCK_ID; + errors: Errors.INVALID_BLOCK_ID; }; starknet_getStorageAt: { params: { contract_address: ADDRESS; key: STORAGE_KEY; block_id: BLOCK_ID }; result: Storage; - errors: errors.CONTRACT_NOT_FOUND | errors.INVALID_BLOCK_ID; + errors: Errors.CONTRACT_NOT_FOUND | Errors.INVALID_BLOCK_ID; }; starknet_getTransactionByHash: { params: { transaction_hash: TXN_HASH }; result: Transaction; - errors: errors.INVALID_TXN_HASH; + errors: Errors.INVALID_TXN_HASH; }; starknet_getTransactionByBlockIdAndIndex: { params: { block_id: BLOCK_ID; index: number }; result: Transaction; - errors: errors.INVALID_BLOCK_ID | errors.INVALID_TXN_INDEX; + errors: Errors.INVALID_BLOCK_ID | Errors.INVALID_TXN_INDEX; }; starknet_getTransactionReceipt: { params: { transaction_hash: TXN_HASH }; result: TransactionReceipt; - errors: errors.INVALID_TXN_HASH; + errors: Errors.INVALID_TXN_HASH; }; starknet_getClass: { params: { class_hash: FELT }; result: ContractClass; - errors: errors.INVALID_CONTRACT_CLASS_HASH; + errors: Errors.INVALID_CONTRACT_CLASS_HASH; }; starknet_getClassHashAt: { params: { block_id: BLOCK_ID; contract_address: ADDRESS }; result: FELT; - errors: errors.INVALID_BLOCK_ID | errors.CONTRACT_NOT_FOUND; + errors: Errors.INVALID_BLOCK_ID | Errors.CONTRACT_NOT_FOUND; }; starknet_getClassAt: { params: { block_id: BLOCK_ID; contract_address: ADDRESS }; result: ContractClass; - errors: errors.INVALID_BLOCK_ID | errors.CONTRACT_NOT_FOUND; + errors: Errors.INVALID_BLOCK_ID | Errors.CONTRACT_NOT_FOUND; }; starknet_getBlockTransactionCount: { params: { block_id: BLOCK_ID }; result: number; - errors: errors.INVALID_BLOCK_ID; + errors: Errors.INVALID_BLOCK_ID; }; starknet_call: { params: { request: FUNCTION_CALL; block_id: BLOCK_ID }; result: Array; errors: - | errors.CONTRACT_NOT_FOUND - | errors.INVALID_MESSAGE_SELECTOR - | errors.INVALID_CALL_DATA - | errors.CONTRACT_ERROR - | errors.INVALID_BLOCK_ID; + | Errors.CONTRACT_NOT_FOUND + | Errors.INVALID_MESSAGE_SELECTOR + | Errors.INVALID_CALL_DATA + | Errors.CONTRACT_ERROR + | Errors.INVALID_BLOCK_ID; }; starknet_estimateFee: { params: { request: INVOKE_TXN; block_id: BLOCK_ID }; result: FEE_ESTIMATE; errors: - | errors.CONTRACT_NOT_FOUND - | errors.INVALID_MESSAGE_SELECTOR - | errors.INVALID_CALL_DATA - | errors.CONTRACT_ERROR - | errors.INVALID_BLOCK_ID; + | Errors.CONTRACT_NOT_FOUND + | Errors.INVALID_MESSAGE_SELECTOR + | Errors.INVALID_CALL_DATA + | Errors.CONTRACT_ERROR + | Errors.INVALID_BLOCK_ID; }; starknet_blockNumber: { result: BLOCK_NUMBER; - errors: errors.NO_BLOCKS; + errors: Errors.NO_BLOCKS; }; starknet_blockHashAndNumber: { result: BLOCK_HASH & BLOCK_NUMBER; - errors: errors.NO_BLOCKS; + errors: Errors.NO_BLOCKS; }; starknet_chainId: { result: CHAIN_ID; @@ -368,12 +368,12 @@ export namespace OPENRPC { starknet_getEvents: { params: { filter: EVENT_FILTER & RESULT_PAGE_REQUEST }; result: { events: EMITTED_EVENT; page_number: number; is_last_page: boolean }; - errors: errors.PAGE_SIZE_TOO_BIG; + errors: Errors.PAGE_SIZE_TOO_BIG; }; starknet_getNonce: { params: { contract_address: ADDRESS }; result: FELT; - errors: errors.CONTRACT_NOT_FOUND; + errors: Errors.CONTRACT_NOT_FOUND; }; // Write API @@ -392,7 +392,7 @@ export namespace OPENRPC { version: NUM_AS_HEX; }; result: DeclaredTransaction; - errors: errors.INVALID_CONTRACT_CLASS; + errors: Errors.INVALID_CONTRACT_CLASS; }; starknet_addDeployTransaction: { params: { @@ -401,28 +401,28 @@ export namespace OPENRPC { contract_definition: CONTRACT_CLASS; }; result: DeployedTransaction; - errors: errors.INVALID_CONTRACT_CLASS; + errors: Errors.INVALID_CONTRACT_CLASS; }; // Trace API starknet_traceTransaction: { params: { transaction_hash: TXN_HASH }; - result: { trace: Trace }; + result: Trace; errors: - | errors.INVALID_TXN_HASH - | errors.NO_TRACE_AVAILABLE - | errors.INVALID_BLOCK_HASH - | errors.INVALID_TXN_HASH; + | Errors.INVALID_TXN_HASH + | Errors.NO_TRACE_AVAILABLE + | Errors.INVALID_BLOCK_HASH + | Errors.INVALID_TXN_HASH; }; starknet_traceBlockTransactions: { params: { block_hash: BLOCK_HASH }; result: Traces; - errors: errors.INVALID_BLOCK_HASH; + errors: Errors.INVALID_BLOCK_HASH; }; }; } -namespace errors { +export namespace Errors { export interface FAILED_TO_RECEIVE_TXN { code: 1; message: 'Failed to write transaction'; diff --git a/src/types/api/rpc.ts b/src/types/api/rpc.ts index f294fbedf..4aff7d1bc 100644 --- a/src/types/api/rpc.ts +++ b/src/types/api/rpc.ts @@ -23,7 +23,7 @@ export namespace RPC { export type BlockHash = OPENRPC.BlockHash; export type BlockHashAndNumber = OPENRPC.BlockHashAndNumber; export type GetClassResponse = OPENRPC.ContractClass; - export type EstimateFeeResponse = OPENRPC.EstimateFee; + export type EstimateFeeResponse = OPENRPC.EstimatedFee; export type GetBlockWithTxHashesResponse = OPENRPC.BlockWithTxHashes; export type GetBlockWithTxs = OPENRPC.BlockWithTxs; export type GetStorageAtResponse = OPENRPC.Storage; @@ -35,27 +35,9 @@ export namespace RPC { export type GetSyncingStatsResponse = OPENRPC.SyncingStatus; export type EventFilter = OPENRPC.EventFilter; export type GetEventsResponse = OPENRPC.Events; - - // export type InvokedTransaction = OPENRPC.InvokedTransaction; export type DeclaredTransaction = OPENRPC.DeclaredTransaction; - export type DeployContractResponse = OPENRPC.DeployedTransaction; - - export type StarknetEvent = { - from_address: string; - keys: string[]; - data: string[]; - }; - - export type MessageToL1 = { - to_address: string; - payload: string[]; - }; - - export type MessageToL2 = { - from_address: string; - payload: string[]; - }; + export type DeployedTransaction = OPENRPC.DeployedTransaction; export type Methods = { starknet_pendingTransactions: { @@ -131,7 +113,7 @@ export namespace RPC { starknet_estimateFee: { QUERY: never; REQUEST: any[]; - RESPONSE: EstimateFeeResponse; + RESPONSE: OPENRPC.EstimatedFee; }; starknet_blockNumber: { QUERY: never; @@ -163,6 +145,16 @@ export namespace RPC { REQUEST: any[]; RESPONSE: OPENRPC.DeployedTransaction; }; + /* + starknet_addDeployTransaction: { + params: { + contract_address_salt: FELT; + constructor_calldata: FELT; + contract_definition: OPENRPC.ContractClass; + }; + result: DeployedTransaction; + errors: Errors.INVALID_CONTRACT_CLASS; + }; */ starknet_addDeclareTransaction: { QUERY: never; REQUEST: any[]; diff --git a/src/utils/responseParser/rpc.ts b/src/utils/responseParser/rpc.ts index 0b6ce12d0..13d655006 100644 --- a/src/utils/responseParser/rpc.ts +++ b/src/utils/responseParser/rpc.ts @@ -88,7 +88,7 @@ export class RPCResponseParser extends ResponseParser { }; } - public parseDeployContractResponse(res: RPC.DeployContractResponse): DeployContractResponse { + public parseDeployContractResponse(res: RPC.DeployedTransaction): DeployContractResponse { return { transaction_hash: res.transaction_hash, contract_address: res.contract_address,