Skip to content

Commit

Permalink
feat: rpc 0.6.0-rc3 implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
tabaktoni committed Nov 28, 2023
1 parent a596748 commit c2607ed
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 27 deletions.
19 changes: 18 additions & 1 deletion src/channel/rpc_0_6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ import { getHexStringArray, toHex, toStorageKey } from '../utils/num';
import { Block, getDefaultNodeUrl, isV3Tx, wait } from '../utils/provider';
import { decompressProgram, signatureToHexArray } from '../utils/stark';

/* function detailsToV3DefaultDetails(details: InvocationsDetailsWithNonce) {
if (!isV3Tx(details)) throw Error('detailsToV3Details: Transaction is not V3');
return {
...details,
resource_bounds: details.resourceBounds,
tip: toHex(details.tip || 0),
paymaster_data: details.paymasterData ? details.paymasterData.map((it) => toHex(it)) : [],
account_deployment_data: details.accountDeploymentData
? details.accountDeploymentData.map((it) => toHex(it))
: [],
nonce_data_availability_mode: details.nonceDataAvailabilityMode || 'L1',
fee_data_availability_mode: details.feeDataAvailabilityMode || 'L1',
};
} */

const defaultOptions = {
headers: { 'Content-Type': 'application/json' },
blockIdentifier: BlockTag.pending,
Expand Down Expand Up @@ -364,12 +380,13 @@ export class RpcChannel {

public getEstimateFee(
invocations: AccountInvocations,
{ blockIdentifier = this.blockIdentifier }: getEstimateFeeBulkOptions
{ blockIdentifier = this.blockIdentifier, skipValidate = false }: getEstimateFeeBulkOptions
) {
const block_id = new Block(blockIdentifier).identifier;
return this.fetchEndpoint('starknet_estimateFee', {
request: invocations.map((it) => this.buildTransaction(it, 'fee')),
block_id,
...(skipValidate && { simulation_flags: [RPC.V0_6.ESimulationFlag.SKIP_VALIDATE] }),
});
}

Expand Down
3 changes: 2 additions & 1 deletion src/provider/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ export abstract class ProviderInterface {
public abstract getEstimateFee(
invocation: Invocation,
details: InvocationsDetailsWithNonce,
blockIdentifier: BlockIdentifier
blockIdentifier?: BlockIdentifier,
skipValidate?: boolean
): Promise<EstimateFeeResponse>;

/**
Expand Down
20 changes: 12 additions & 8 deletions src/provider/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,15 +239,17 @@ export class RpcProvider implements ProviderInterface {
public async getEstimateFee(
invocation: Invocation,
invocationDetails: InvocationsDetailsWithNonce,
blockIdentifier?: BlockIdentifier
blockIdentifier?: BlockIdentifier,
skipValidate?: boolean
) {
return this.getInvokeEstimateFee(invocation, invocationDetails, blockIdentifier);
return this.getInvokeEstimateFee(invocation, invocationDetails, blockIdentifier, skipValidate);
}

public async getInvokeEstimateFee(
invocation: Invocation,
invocationDetails: InvocationsDetailsWithNonce,
blockIdentifier?: BlockIdentifier
blockIdentifier?: BlockIdentifier,
skipValidate?: boolean
) {
return this.channel
.getEstimateFee(
Expand All @@ -258,15 +260,16 @@ export class RpcProvider implements ProviderInterface {
...invocationDetails,
},
],
{ blockIdentifier }
{ blockIdentifier, skipValidate }
)
.then(this.responseParser.parseFeeEstimateResponse);
}

public async getDeclareEstimateFee(
invocation: DeclareContractTransaction,
details: InvocationsDetailsWithNonce,
blockIdentifier?: BlockIdentifier
blockIdentifier?: BlockIdentifier,
skipValidate?: boolean
) {
return this.channel
.getEstimateFee(
Expand All @@ -277,15 +280,16 @@ export class RpcProvider implements ProviderInterface {
...details,
},
],
{ blockIdentifier }
{ blockIdentifier, skipValidate }
)
.then(this.responseParser.parseFeeEstimateResponse);
}

public async getDeployAccountEstimateFee(
invocation: DeployAccountContractTransaction,
details: InvocationsDetailsWithNonce,
blockIdentifier?: BlockIdentifier
blockIdentifier?: BlockIdentifier,
skipValidate?: boolean
) {
return this.channel
.getEstimateFee(
Expand All @@ -296,7 +300,7 @@ export class RpcProvider implements ProviderInterface {
...details,
},
],
{ blockIdentifier }
{ blockIdentifier, skipValidate }
)
.then(this.responseParser.parseFeeEstimateResponse);
}
Expand Down
8 changes: 6 additions & 2 deletions src/types/api/rpcspec_0_6/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -554,15 +554,19 @@ export type TYPED_PARAMETER = {
type: string;
};

export type SIMULATION_FLAG_FOR_ESTIMATE_FEE = 'SKIP_VALIDATE';
export type PRICE_UNIT = 'WEI' | 'FRI';

export type FEE_ESTIMATE = {
gas_consumed: FELT;
gas_price: FELT;
overall_fee: FELT;
unit: PRICE_UNIT;
};

export type FEE_PAYMENT = {
amount: FELT;
unit: 'WEI' | 'STRK';
unit: PRICE_UNIT;
};

export type RESOURCE_BOUNDS_MAPPING = {
Expand All @@ -576,7 +580,7 @@ export type RESOURCE_BOUNDS = {
};

export type RESOURCE_PRICE = {
price_in_strk?: FELT;
price_in_fri: FELT;
price_in_wei: FELT;
};

Expand Down
14 changes: 9 additions & 5 deletions src/types/api/rpcspec_0_6/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ export interface BLOCK_NOT_FOUND {
message: 'Block not found';
}

export interface INVALID_TXN_HASH {
code: 25;
message: 'Invalid transaction hash';
}

export interface INVALID_BLOCK_HASH {
code: 26;
message: 'Invalid block hash';
Expand Down Expand Up @@ -84,6 +79,15 @@ export interface CONTRACT_ERROR {
};
}

export interface TRANSACTION_EXECUTION_ERROR {
code: 41;
message: 'Transaction execution error';
data: {
transaction_index: number;
execution_error: string;
};
}

export interface CLASS_ALREADY_DECLARED {
code: 51;
message: 'Class already declared';
Expand Down
8 changes: 5 additions & 3 deletions src/types/api/rpcspec_0_6/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
PENDING_STATE_UPDATE,
RESULT_PAGE_REQUEST,
SIMULATION_FLAG,
SIMULATION_FLAG_FOR_ESTIMATE_FEE,
STATE_UPDATE,
STORAGE_KEY,
TXN_HASH,
Expand Down Expand Up @@ -176,10 +177,11 @@ type ReadMethods = {
starknet_estimateFee: {
params: {
request: BROADCASTED_TXN[];
simulation_flags?: [SIMULATION_FLAG_FOR_ESTIMATE_FEE];
block_id: BLOCK_ID;
};
result: FeeEstimate[];
errors: Errors.CONTRACT_NOT_FOUND | Errors.CONTRACT_ERROR | Errors.BLOCK_NOT_FOUND;
errors: Errors.TRANSACTION_EXECUTION_ERROR | Errors.BLOCK_NOT_FOUND;
};

// Estimate the L2 fee of a message sent on L1
Expand Down Expand Up @@ -306,7 +308,7 @@ type TraceMethods = {
starknet_traceTransaction: {
params: { transaction_hash: TXN_HASH };
result: TransactionTrace;
errors: Errors.INVALID_TXN_HASH | Errors.NO_TRACE_AVAILABLE;
errors: Errors.TXN_HASH_NOT_FOUND | Errors.NO_TRACE_AVAILABLE;
};

// Returns the execution traces of all transactions included in the given block
Expand All @@ -324,6 +326,6 @@ type TraceMethods = {
simulation_flags: Array<SIMULATION_FLAG>;
};
result: SimulateTransactionResponse;
errors: Errors.CONTRACT_NOT_FOUND | Errors.CONTRACT_ERROR | Errors.BLOCK_NOT_FOUND;
errors: Errors.BLOCK_NOT_FOUND | Errors.TRANSACTION_EXECUTION_ERROR;
};
};
5 changes: 3 additions & 2 deletions src/types/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export type InvocationsDetails = {
version?: BigNumberish;
};

export type V3InvocationDetails = {
export type V3TransactionDetails = {
nonce: BigNumberish;
version: BigNumberish;
resourceBounds: V0_6.SPEC.RESOURCE_BOUNDS_MAPPING;
Expand All @@ -140,7 +140,7 @@ export type InvocationsDetailsWithNonce =
| (InvocationsDetails & {
nonce: BigNumberish;
})
| V3InvocationDetails;
| V3TransactionDetails;

export enum TransactionType {
DECLARE = 'DECLARE',
Expand Down Expand Up @@ -252,6 +252,7 @@ export type getContractVersionOptions = {

export type getEstimateFeeBulkOptions = {
blockIdentifier?: BlockIdentifier;
skipValidate?: boolean;
};

export interface CallStruct {
Expand Down
18 changes: 13 additions & 5 deletions src/utils/provider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { NetworkName, RPC_GOERLI_NODES, RPC_MAINNET_NODES } from '../constants';
import {
BN_FEE_TRANSACTION_VERSION_3,
HEX_STR_TRANSACTION_VERSION_3,
NetworkName,
RPC_GOERLI_NODES,
RPC_MAINNET_NODES,
} from '../constants';
import {
BigNumberish,
BlockIdentifier,
Expand All @@ -12,7 +18,7 @@ import {
RPC,
SequencerIdentifier,
SierraContractClass,
V3InvocationDetails,
V3TransactionDetails,
} from '../types';
import { isSierra } from './contract';
import { formatSpaces } from './hash';
Expand Down Expand Up @@ -180,7 +186,9 @@ export function defStateUpdate(
return pending(state);
}

export function isV3Tx(details: InvocationsDetailsWithNonce): details is V3InvocationDetails {
const version = details.version ? toHex(details.version) : '0x3';
return version === '0x3' || version === '0x100000000000000000000000000000003';
export function isV3Tx(details: InvocationsDetailsWithNonce): details is V3TransactionDetails {
const version = details.version ? toHex(details.version) : HEX_STR_TRANSACTION_VERSION_3;
return (
version === HEX_STR_TRANSACTION_VERSION_3 || version === toHex(BN_FEE_TRANSACTION_VERSION_3)
);
}

0 comments on commit c2607ed

Please sign in to comment.