Skip to content

Commit

Permalink
fix: rpc test update, comepleted rpc to openrpc mapping, rpc provider…
Browse files Browse the repository at this point in the history
… methods
  • Loading branch information
tabaktoni committed Sep 13, 2022
1 parent 121ce22 commit 8b41e0f
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 73 deletions.
35 changes: 22 additions & 13 deletions __tests__/rpcProvider.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Account, RpcProvider, ec } from '../src';
import { Account, GetBlockResponse, RpcProvider, ec } from '../src';
import {
compiledOpenZeppelinAccount,
describeIfRpc,
Expand All @@ -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();
Expand All @@ -38,30 +42,35 @@ 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();
expect(transaction_hash).toBeTruthy();
});

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');
});
});

Expand Down
15 changes: 12 additions & 3 deletions src/provider/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -79,6 +80,7 @@ export class RpcProvider implements ProviderInterface {
}
}

// Interface
public async getChainId(): Promise<any> {
return this.fetchEndpoint('starknet_chainId');
}
Expand Down Expand Up @@ -112,7 +114,8 @@ export class RpcProvider implements ProviderInterface {
blockIdentifier: BlockIdentifier,
contractAddress: RPC.ContractAddress
): Promise<RPC.Felt> {
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<any> {
Expand All @@ -128,7 +131,8 @@ export class RpcProvider implements ProviderInterface {
}

public async getStateUpdate(blockIdentifier: BlockIdentifier): Promise<RPC.StateUpdate> {
return this.fetchEndpoint('starknet_getStateUpdate', [blockIdentifier]);
const block = new Block(blockIdentifier);
return this.fetchEndpoint('starknet_getStateUpdate', [block.identifier]);
}

public async getStorageAt(
Expand Down Expand Up @@ -160,7 +164,11 @@ export class RpcProvider implements ProviderInterface {
blockIdentifier: BlockIdentifier,
index: number
): Promise<RPC.GetTransactionByBlockIdAndIndex> {
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<GetTransactionReceiptResponse> {
Expand Down Expand Up @@ -253,6 +261,7 @@ export class RpcProvider implements ProviderInterface {
]).then(this.responseParser.parseInvokeFunctionResponse);
}

// Interface
public async callContract(
call: Call,
blockIdentifier: BlockIdentifier = 'pending'
Expand Down
70 changes: 35 additions & 35 deletions src/types/api/openrpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ export namespace OPENRPC {
export type TransactionReceipt = TXN_RECEIPT;
export type ContractClass = CONTRACT_CLASS;
export type CallResponse = Array<FELT>;
export type EstimateFee = FEE_ESTIMATE;
export type EstimatedFee = FEE_ESTIMATE;
export type BlockNumber = BLOCK_NUMBER;
export type BlockHashAndNumber = {
block_hash: BLOCK_HASH;
Expand Down Expand Up @@ -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<FELT>;
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;
Expand All @@ -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
Expand All @@ -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: {
Expand All @@ -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';
Expand Down
34 changes: 13 additions & 21 deletions src/types/api/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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: {
Expand Down Expand Up @@ -131,7 +113,7 @@ export namespace RPC {
starknet_estimateFee: {
QUERY: never;
REQUEST: any[];
RESPONSE: EstimateFeeResponse;
RESPONSE: OPENRPC.EstimatedFee;
};
starknet_blockNumber: {
QUERY: never;
Expand Down Expand Up @@ -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[];
Expand Down
2 changes: 1 addition & 1 deletion src/utils/responseParser/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 8b41e0f

Please sign in to comment.