From 6619c620354f9443db6b5fe77df47da759e89931 Mon Sep 17 00:00:00 2001 From: Stanley Yuen <102275989+stanleyyconsensys@users.noreply.github.com> Date: Wed, 11 Dec 2024 16:45:39 +0800 Subject: [PATCH] chore: update RPCs - `starkNet_createAccount` , `starkNet_createAccountLegacy`, `starkNet_upgradeAccContract` to handle new txn state (#458) * feat: add stark scan client * chore: add starkscan config * chore: lint * chore: add interface * chore: support multiple txn * chore: update starkscan * chore: update stark scan client * chore: update contract func name * chore: fix test * chore: update data client * chore: re-structure starkscan type * chore: add test coverage * chore: factory and config * chore: add backward compatibility for transactions type * chore: add comment * chore: lint * chore: resolve review comment * chore: change dataVersion to enum * chore: lint * chore: update test helper and refactor ContractAddressFilter * chore: lint * chore: add test for dataVersion filter * chore: update txn state mgr test * chore: update search condition * chore: update starkscan to handle missing selector_name * chore: apply starkscan for list transaction * chore: update list transactions handle * chore: refactor execute txn * chore: refactor execute txn * chore: update create account and upgrade account * chore: lint * Revert "chore: lint" This reverts commit 12be249bf41178628a7b4738a310512fecf0a686. * Revert "chore: update create account and upgrade account" This reverts commit cf64931fe269d2c0ed14c9e3237056fe7079b351. * chore: update rpcs to handle v2 data * chore: update rpcs to handle new txn state data * chore: fix naming --------- Co-authored-by: khanti42 --- packages/starknet-snap/src/createAccount.ts | 21 +++++-------- .../starknet-snap/src/upgradeAccContract.ts | 23 +++++--------- .../src/utils/transaction.test.ts | 2 +- .../test/src/upgradeAccContract.test.ts | 31 ------------------- 4 files changed, 16 insertions(+), 61 deletions(-) diff --git a/packages/starknet-snap/src/createAccount.ts b/packages/starknet-snap/src/createAccount.ts index bdf7e4c2..d8704183 100644 --- a/packages/starknet-snap/src/createAccount.ts +++ b/packages/starknet-snap/src/createAccount.ts @@ -6,8 +6,7 @@ import type { ApiParamsWithKeyDeriver, CreateAccountRequestParams, } from './types/snapApi'; -import type { AccContract, Transaction } from './types/snapState'; -import { VoyagerTransactionType, TransactionStatus } from './types/snapState'; +import type { AccContract } from './types/snapState'; import { CAIRO_VERSION_LEGACY, CAIRO_VERSION } from './utils/constants'; import { logger } from './utils/logger'; import { toJson } from './utils/serializer'; @@ -26,6 +25,7 @@ import { waitForTransaction, estimateAccountDeployFee, } from './utils/starknetUtils'; +import { newDeployTransaction } from './utils/transaction'; /** * Create an starknet account. @@ -142,21 +142,14 @@ export async function createAccount( await upsertAccount(userAccount, wallet, saveMutex); - const txn: Transaction = { + const txn = newDeployTransaction({ txnHash: deployResp.transaction_hash, - txnType: VoyagerTransactionType.DEPLOY_ACCOUNT, chainId: network.chainId, senderAddress: deployResp.contract_address, - contractAddress: deployResp.contract_address, - contractFuncName: '', - contractCallData: [], - finalityStatus: TransactionStatus.RECEIVED, - executionStatus: TransactionStatus.RECEIVED, - status: '', - failureReason: '', - eventIds: [], - timestamp: Math.floor(Date.now() / 1000), - }; + // whenever create account is happen, we pay the fee in ETH, so txnVersion is 1 + // FIXME: it should allow to pay the fee in STRK + txnVersion: 1, + }); await upsertTransaction(txn, wallet, saveMutex); } diff --git a/packages/starknet-snap/src/upgradeAccContract.ts b/packages/starknet-snap/src/upgradeAccContract.ts index a6c16204..056b8003 100644 --- a/packages/starknet-snap/src/upgradeAccContract.ts +++ b/packages/starknet-snap/src/upgradeAccContract.ts @@ -5,8 +5,6 @@ import type { ApiParamsWithKeyDeriver, UpgradeTransactionRequestParams, } from './types/snapApi'; -import type { Transaction } from './types/snapState'; -import { TransactionStatus, VoyagerTransactionType } from './types/snapState'; import { ACCOUNT_CLASS_HASH, CAIRO_VERSION_LEGACY } from './utils/constants'; import { logger } from './utils/logger'; import { toJson } from './utils/serializer'; @@ -23,6 +21,7 @@ import { isAccountDeployed, estimateFee, } from './utils/starknetUtils'; +import { newInvokeTransaction } from './utils/transaction'; /** * @@ -145,21 +144,15 @@ export async function upgradeAccContract(params: ApiParamsWithKeyDeriver) { throw new Error(`Transaction hash is not found`); } - const txn: Transaction = { + const txn = newInvokeTransaction({ txnHash: txnResp.transaction_hash, - txnType: VoyagerTransactionType.INVOKE, - chainId: network.chainId, senderAddress: contractAddress, - contractAddress, - contractFuncName: 'upgrade', - contractCallData: CallData.compile(calldata), - finalityStatus: TransactionStatus.RECEIVED, - executionStatus: TransactionStatus.RECEIVED, - status: '', // DEPRECATED LATER - failureReason: '', - eventIds: [], - timestamp: Math.floor(Date.now() / 1000), - }; + chainId: network.chainId, + maxFee: maxFee.toString(10), + calls: [txnInvocation], + // whenever upgrade is happen, we pay the fee in ETH, so txnVersion is 1 + txnVersion: 1, + }); await upsertTransaction(txn, wallet, saveMutex); diff --git a/packages/starknet-snap/src/utils/transaction.test.ts b/packages/starknet-snap/src/utils/transaction.test.ts index 17f89eae..768bfa24 100644 --- a/packages/starknet-snap/src/utils/transaction.test.ts +++ b/packages/starknet-snap/src/utils/transaction.test.ts @@ -63,7 +63,7 @@ describe('feeTokenToTransactionVersion', () => { }); it.each([FeeToken.ETH, 'invalid_unit'])( - 'converts feeToken string to transaction version v1 if it not STRK - %s', + 'converts feeToken string to transaction version v1 if it is not STRK - %s', (txnVersion: string) => { expect(feeTokenToTransactionVersion(txnVersion)).toStrictEqual( constants.TRANSACTION_VERSION.V1, diff --git a/packages/starknet-snap/test/src/upgradeAccContract.test.ts b/packages/starknet-snap/test/src/upgradeAccContract.test.ts index 899f7ede..026b5fd4 100644 --- a/packages/starknet-snap/test/src/upgradeAccContract.test.ts +++ b/packages/starknet-snap/test/src/upgradeAccContract.test.ts @@ -280,36 +280,5 @@ describe('Test function: upgradeAccContract', function () { expect(result.message).to.be.include('Transaction hash is not found'); } }); - - it('should save transaction when execute transaction success', async function () { - executeTxnStub.resolves(sendTransactionResp); - estimateFeeStub.resolves(estimateFeeResp); - walletStub.rpcStubs.snap_dialog.resolves(true); - const address = ( - apiParams.requestParams as UpgradeTransactionRequestParams - ).contractAddress; - const calldata = CallData.compile({ - implementation: ACCOUNT_CLASS_HASH, - calldata: [0], - }); - const txn = { - txnHash: sendTransactionResp.transaction_hash, - txnType: VoyagerTransactionType.INVOKE, - chainId: STARKNET_SEPOLIA_TESTNET_NETWORK.chainId, - senderAddress: address, - contractAddress: address, - contractFuncName: 'upgrade', - contractCallData: CallData.compile(calldata), - finalityStatus: TransactionStatus.RECEIVED, - executionStatus: TransactionStatus.RECEIVED, - status: '', - failureReason: '', - eventIds: [], - }; - - const result = await upgradeAccContract(apiParams); - expect(result).to.be.equal(sendTransactionResp); - expect(upsertTransactionStub).to.calledOnceWith(sinon.match(txn)); - }); }); });