Skip to content

Commit

Permalink
feat: support txv3 in estimateFee and estimateFeeBulk [SNAP] (#262)
Browse files Browse the repository at this point in the history
* feat: support txv3 in estimateFee and estimateFeeBulk

* chore: lint + prettier

* chore: lint + prettier

* refactor: add transactionVersion request params only in required RPC

* fix: pr review comment

* fix: forced tx v2 in upgrade account

* fix: use TRANSACTION_VERSION in snaps starknetUtils.ts

Co-authored-by: Stanley Yuen <102275989+stanleyyconsensys@users.noreply.github.com>

---------

Co-authored-by: Stanley Yuen <102275989+stanleyyconsensys@users.noreply.github.com>
  • Loading branch information
khanti42 and stanleyyconsensys authored Aug 15, 2024
1 parent 1922348 commit d2dcbad
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 8 deletions.
8 changes: 4 additions & 4 deletions packages/starknet-snap/src/estimateFee.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { EstimateFee, Invocations } from 'starknet';
import type { Invocations } from 'starknet';
import { TransactionType } from 'starknet';

import type {
Expand Down Expand Up @@ -37,6 +37,7 @@ export async function estimateFee(params: ApiParamsWithKeyDeriver) {
);
const { senderAddress } = requestParamsObj;
const network = getNetworkFromChainId(state, requestParamsObj.chainId);
const { transactionVersion } = requestParamsObj;

if (
!contractAddress ||
Expand Down Expand Up @@ -115,15 +116,14 @@ export async function estimateFee(params: ApiParamsWithKeyDeriver) {
senderAddress,
senderPrivateKey,
bulkTransactions,
transactionVersion,
);
logger.log(
`estimateFee:\nestimateFeeBulk estimateBulkFeeResp: ${toJson(
estimateBulkFeeResp,
)}`,
);
const estimateFeeResp = addFeesFromAllTransactions(
estimateBulkFeeResp,
) as EstimateFee;
const estimateFeeResp = addFeesFromAllTransactions(estimateBulkFeeResp);

logger.log(`estimateFee:\nestimateFeeResp: ${toJson(estimateFeeResp)}`);

Expand Down
1 change: 1 addition & 0 deletions packages/starknet-snap/src/estimateFees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export async function estimateFees(params: ApiParamsWithKeyDeriver) {
senderAddress,
senderPrivateKey,
requestParamsObj.invocations,
requestParamsObj.transactionVersion,
requestParamsObj.invocationDetails,
);

Expand Down
3 changes: 2 additions & 1 deletion packages/starknet-snap/src/executeTxn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
ApiParamsWithKeyDeriver,
ExecuteTxnRequestParams,
} from './types/snapApi';
import { ACCOUNT_CLASS_HASH } from './utils/constants';
import { ACCOUNT_CLASS_HASH, TRANSACTION_VERSION } from './utils/constants';
import { logger } from './utils/logger';
import {
getNetworkFromChainId,
Expand Down Expand Up @@ -71,6 +71,7 @@ export async function executeTxn(params: ApiParamsWithKeyDeriver) {
senderAddress,
senderPrivateKey,
bulkTransactions,
TRANSACTION_VERSION,
requestParamsObj.invocationsDetails
? requestParamsObj.invocationsDetails
: undefined,
Expand Down
13 changes: 11 additions & 2 deletions packages/starknet-snap/src/types/snapApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
DeployAccountSignerDetails,
DeclareSignerDetails,
typedData,
constants,
} from 'starknet';

import type { SnapState, VoyagerTransactionType } from './snapState';
Expand Down Expand Up @@ -57,6 +58,12 @@ export type BaseRequestParams = {
debugLevel?: string;
};

export type TransactionVersionParams = {
transactionVersion?:
| typeof constants.TRANSACTION_VERSION.V2
| typeof constants.TRANSACTION_VERSION.V3;
};

export type CreateAccountRequestParams = {
addressIndex?: string | number;
deploy?: boolean;
Expand Down Expand Up @@ -121,7 +128,8 @@ export type EstimateFeeRequestParams = {
contractFuncName: string;
contractCallData?: string;
senderAddress: string;
} & BaseRequestParams;
} & BaseRequestParams &
TransactionVersionParams;

export type EstimateAccountDeployFeeRequestParams = {
addressIndex?: string | number;
Expand Down Expand Up @@ -176,7 +184,8 @@ export type EstimateFeesRequestParams = {
senderAddress: string;
invocations: Invocations;
invocationDetails?: EstimateFeeDetails;
} & BaseRequestParams;
} & BaseRequestParams &
TransactionVersionParams;

export type DeclareContractRequestParams = {
senderAddress: string;
Expand Down
1 change: 1 addition & 0 deletions packages/starknet-snap/src/upgradeAccContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export async function upgradeAccContract(params: ApiParamsWithKeyDeriver) {
contractAddress,
privateKey,
txnInvocation,
constants.TRANSACTION_VERSION.V2,
CAIRO_VERSION_LEGACY,
);
maxFee = numUtils.toBigInt(
Expand Down
2 changes: 2 additions & 0 deletions packages/starknet-snap/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ export const CAIRO_VERSION = '1';

export const CAIRO_VERSION_LEGACY = '0';

export const TRANSACTION_VERSION = constants.TRANSACTION_VERSION.V2;

export enum BlockIdentifierEnum {
Latest = 'latest',
Pending = 'pending',
Expand Down
16 changes: 15 additions & 1 deletion packages/starknet-snap/src/utils/starknetUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import {
CAIRO_VERSION_LEGACY,
ETHER_MAINNET,
ETHER_SEPOLIA_TESTNET,
TRANSACTION_VERSION,
BlockIdentifierEnum,
} from './constants';
import { DeployRequiredError, UpgradeRequiredError } from './exceptions';
Expand Down Expand Up @@ -131,6 +132,9 @@ export const getAccountInstance = (
userAddress: string,
privateKey: string | Uint8Array,
cairoVersion?: CairoVersion,
transactionVersion?:
| typeof constants.TRANSACTION_VERSION.V2
| typeof constants.TRANSACTION_VERSION.V3,
blockIdentifier?: BlockIdentifierEnum,
): Account => {
const provider = getProvider(network, blockIdentifier);
Expand All @@ -139,6 +143,7 @@ export const getAccountInstance = (
userAddress,
privateKey,
cairoVersion ?? CAIRO_VERSION,
transactionVersion ?? TRANSACTION_VERSION,
);
};

Expand Down Expand Up @@ -200,6 +205,9 @@ export const estimateFee = async (
senderAddress: string,
privateKey: string | Uint8Array,
txnInvocation: Call | Call[],
transactionVersion:
| typeof constants.TRANSACTION_VERSION.V2
| typeof constants.TRANSACTION_VERSION.V3 = TRANSACTION_VERSION,
cairoVersion?: CairoVersion,
invocationsDetails?: UniversalDetails,
): Promise<EstimateFee> => {
Expand All @@ -210,6 +218,7 @@ export const estimateFee = async (
senderAddress,
privateKey,
cairoVersion,
transactionVersion,
BlockIdentifierEnum.Latest,
).estimateInvokeFee(txnInvocation, {
...invocationsDetails,
Expand All @@ -223,6 +232,9 @@ export const estimateFeeBulk = async (
senderAddress: string,
privateKey: string | Uint8Array,
txnInvocation: Invocations,
transactionVersion:
| typeof constants.TRANSACTION_VERSION.V2
| typeof constants.TRANSACTION_VERSION.V3 = TRANSACTION_VERSION,
invocationsDetails?: UniversalDetails,
cairoVersion?: CairoVersion,
): Promise<EstimateFee[]> => {
Expand All @@ -233,6 +245,7 @@ export const estimateFeeBulk = async (
senderAddress,
privateKey,
cairoVersion,
transactionVersion,
BlockIdentifierEnum.Latest,
).estimateFeeBulk(txnInvocation, {
...invocationsDetails,
Expand Down Expand Up @@ -874,7 +887,7 @@ export const isAccountDeployed = async (network: Network, address: string) => {

export const addFeesFromAllTransactions = (
fees: EstimateFee[],
): Partial<EstimateFee> => {
): Pick<EstimateFee, 'suggestedMaxFee' | 'overall_fee'> => {
let overallFee = numUtils.toBigInt(0);
let suggestedMaxFee = numUtils.toBigInt(0);

Expand Down Expand Up @@ -1008,6 +1021,7 @@ export async function estimateAccountUpgradeFee(
contractAddress,
privateKey,
txnInvocation,
TRANSACTION_VERSION,
CAIRO_VERSION_LEGACY,
);
return numUtils.toBigInt(estFeeResp.suggestedMaxFee.toString(10) ?? '0');
Expand Down

0 comments on commit d2dcbad

Please sign in to comment.