Skip to content

Commit

Permalink
feat: rpc 0.2.0 types spec
Browse files Browse the repository at this point in the history
  • Loading branch information
tabaktoni committed Oct 23, 2022
1 parent 2b1b71e commit 25b2d29
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 18 deletions.
29 changes: 24 additions & 5 deletions src/provider/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
EstimateFeeResponse,
GetBlockResponse,
GetCodeResponse,
GetTransactionReceiptResponse,
GetTransactionResponse,
Invocation,
InvocationsDetailsWithNonce,
Expand Down Expand Up @@ -186,17 +185,21 @@ export class RpcProvider implements ProviderInterface {
return this.fetchEndpoint('starknet_getTransactionByBlockIdAndIndex', { block_id, index });
}

public async getTransactionReceipt(txHash: string): Promise<GetTransactionReceiptResponse> {
public async getTransactionReceipt(txHash: string): Promise<RPC.TransactionReceipt> {
return this.fetchEndpoint('starknet_getTransactionReceipt', { transaction_hash: txHash });
}

public async getClass(classHash: RPC.Felt): Promise<RPC.ContractClass> {
return this.fetchEndpoint('starknet_getClass', { class_hash: classHash });
public async getClass(
classHash: RPC.Felt,
blockIdentifier: BlockIdentifier
): Promise<RPC.ContractClass> {
const block_id = new Block(blockIdentifier).identifier;
return this.fetchEndpoint('starknet_getClass', { class_hash: classHash, block_id });
}

public async getClassAt(
contractAddress: string,
blockIdentifier: BlockIdentifier
blockIdentifier: BlockIdentifier = 'pending'
): Promise<RPC.ContractClass> {
const block_id = new Block(blockIdentifier).identifier;
return this.fetchEndpoint('starknet_getClassAt', {
Expand Down Expand Up @@ -351,6 +354,17 @@ export class RpcProvider implements ProviderInterface {
});
}

/*
sender_address: ADDRESS;
calldata: Array<FELT>;
type: TXN_TYPE;
max_fee: FELT;
version: NUM_AS_HEX;
signature: SIGNATURE;
nonce: FELT;
*/

// Methods from Interface
public async callContract(
call: Call,
Expand Down Expand Up @@ -391,6 +405,11 @@ export class RpcProvider implements ProviderInterface {
// eslint-disable-next-line no-await-in-loop
const res = await this.getTransactionReceipt(txHash);

if (!('status' in res)) {
const error = new Error('pending transaction');
throw error;
}

if (res.status && successStates.includes(res.status)) {
onchain = true;
} else if (res.status && errorStates.includes(res.status)) {
Expand Down
18 changes: 9 additions & 9 deletions src/types/api/openrpcv2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,22 @@ type EVENT = {
};

type COMMON_RECEIPT_PROPERTIES = {
transaction_hash?: TXN_HASH;
actual_fee?: FELT;
status?: TXN_STATUS;
transaction_hash: TXN_HASH;
actual_fee: FELT;
status: TXN_STATUS;
block_hash?: BLOCK_HASH;
block_number?: BLOCK_NUMBER;
type?: TXN_TYPE;
messages_sent: MSG_TO_L1;
events: EVENT;
type: TXN_TYPE;
messages_sent: Array<MSG_TO_L1>;
events: Array<EVENT>;
};

type PENDING_COMMON_RECEIPT_PROPERTIES = {
transaction_hash: TXN_HASH;
actual_fee: FELT;
type?: TXN_TYPE;
messages_sent: MSG_TO_L1;
events: EVENT;
messages_sent: Array<MSG_TO_L1>;
events: Array<EVENT>;
};

type INVOKE_TXN_RECEIPT = COMMON_RECEIPT_PROPERTIES;
Expand Down Expand Up @@ -321,7 +321,7 @@ type FUNCTION_INVOCATION = FUNCTION_CALL & {
call_type: CALL_TYPE;
result: FELT;
calls: NESTED_CALL;
events: EVENT;
events: Array<EVENT>;
messages: MSG_TO_L1;
};
type NESTED_CALL = FUNCTION_INVOCATION;
Expand Down
3 changes: 1 addition & 2 deletions src/types/api/rpc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ADDRESS, FELT, OPENRPC } from './openrpc';
import { ADDRESS, FELT, OPENRPC } from './openrpcv2';

export namespace RPC {
export type Response = {
Expand Down Expand Up @@ -41,6 +41,5 @@ export namespace RPC {
export type InvokedTransaction = OPENRPC.InvokedTransaction;
export type DeclaredTransaction = OPENRPC.DeclaredTransaction;
export type DeployedTransaction = OPENRPC.DeployedTransaction;

export type Methods = OPENRPC.Methods;
}
11 changes: 9 additions & 2 deletions src/types/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,24 @@ export type FunctionAbi = {
name: string;
outputs: AbiEntry[];
stateMutability?: 'view';
type: 'function' | 'constructor';
type: FUNCTION_ABI_TYPE;
};

enum FUNCTION_ABI_TYPE {
'function',
'l1_handler',
}

export type StructAbi = {
members: (AbiEntry & { offset: number })[];
name: string;
size: number;
type: 'struct';
};

export type Abi = Array<FunctionAbi | StructAbi>;
export type Abi = Array<FunctionAbi | EventAbi | StructAbi>;

type EventAbi = any; // TODO: define

export type EntryPointsByType = object;
export type Program = Record<any, any>;
Expand Down

0 comments on commit 25b2d29

Please sign in to comment.