From 2fedd78ae3209372bb41f3fe109e8095630827e8 Mon Sep 17 00:00:00 2001 From: stanleyyuen <102275989+stanleyyconsensys@users.noreply.github.com> Date: Wed, 12 Jun 2024 18:25:50 +0800 Subject: [PATCH] chore: lint style --- packages/starknet-snap/src/createAccount.ts | 14 ++++- packages/starknet-snap/src/utils/snapUtils.ts | 2 +- .../starknet-snap/src/utils/starknetUtils.ts | 55 +++++++++++-------- .../test/src/sendTransaction.test.ts | 2 +- .../test/utils/snapUtils.test.ts | 8 ++- .../test/utils/starknetUtils.test.ts | 25 +++++++-- yarn.lock | 15 ++++- 7 files changed, 85 insertions(+), 36 deletions(-) diff --git a/packages/starknet-snap/src/createAccount.ts b/packages/starknet-snap/src/createAccount.ts index aa36f729..0ce385e9 100644 --- a/packages/starknet-snap/src/createAccount.ts +++ b/packages/starknet-snap/src/createAccount.ts @@ -108,9 +108,17 @@ export async function createAccount(params: ApiParams, silentMode = false, waitM } } - const deployResp = await deployAccount(network, contractAddress, contractCallData, publicKey, privateKey, undefined, { - maxFee: estimateDeployFee?.suggestedMaxFee, - }); + const deployResp = await deployAccount( + network, + contractAddress, + contractCallData, + publicKey, + privateKey, + undefined, + { + maxFee: estimateDeployFee?.suggestedMaxFee, + }, + ); if (deployResp.contract_address && deployResp.transaction_hash) { const userAccount: AccContract = { diff --git a/packages/starknet-snap/src/utils/snapUtils.ts b/packages/starknet-snap/src/utils/snapUtils.ts index 02dc23c6..f0c63ae5 100644 --- a/packages/starknet-snap/src/utils/snapUtils.ts +++ b/packages/starknet-snap/src/utils/snapUtils.ts @@ -538,7 +538,7 @@ export function getRPCUrl(chainId: string) { } export function getRPCCredentials(): string { - return process.env.ALCHEMY_API_KEY ?? '' + return process.env.ALCHEMY_API_KEY ?? ''; } export function getVoyagerUrl(chainId: string) { diff --git a/packages/starknet-snap/src/utils/starknetUtils.ts b/packages/starknet-snap/src/utils/starknetUtils.ts index 4b726421..654691e3 100644 --- a/packages/starknet-snap/src/utils/starknetUtils.ts +++ b/packages/starknet-snap/src/utils/starknetUtils.ts @@ -34,6 +34,7 @@ import { CairoVersion, InvocationsSignerDetails, ProviderInterface, + GetTransactionReceiptResponse, } from 'starknet'; import { Network, SnapState, Transaction, TransactionType } from '../types/snapState'; import { @@ -101,6 +102,16 @@ export const callContract = async ( ); }; +export const waitForTransaction = async ( + network: Network, + senderAddress: string, + privateKey: string | Uint8Array, + txnHash: num.BigNumberish, + cairoVersion?: CairoVersion, +): Promise => { + return getAccountInstance(network, senderAddress, privateKey, cairoVersion).waitForTransaction(txnHash); +}; + export const declareContract = async ( network: Network, senderAddress: string, @@ -109,9 +120,11 @@ export const declareContract = async ( invocationsDetails?: UniversalDetails, cairoVersion?: CairoVersion, ): Promise => { - return getAccountInstance(network, senderAddress, privateKey, cairoVersion).declare( - contractPayload, { ...invocationsDetails, skipValidate: false, blockIdentifier: 'latest' } - ); + return getAccountInstance(network, senderAddress, privateKey, cairoVersion).declare(contractPayload, { + ...invocationsDetails, + skipValidate: false, + blockIdentifier: 'latest', + }); }; export const estimateFee = async ( @@ -137,14 +150,11 @@ export const estimateFeeBulk = async ( invocationsDetails?: UniversalDetails, cairoVersion?: CairoVersion, ): Promise => { - return getAccountInstance(network, senderAddress, privateKey, cairoVersion).estimateFeeBulk( - txnInvocation, - { - ...invocationsDetails, - skipValidate: false, - blockIdentifier: 'latest', - }, - ); + return getAccountInstance(network, senderAddress, privateKey, cairoVersion).estimateFeeBulk(txnInvocation, { + ...invocationsDetails, + skipValidate: false, + blockIdentifier: 'latest', + }); }; export const executeTxn = async ( @@ -156,15 +166,11 @@ export const executeTxn = async ( invocationsDetails?: UniversalDetails, cairoVersion?: CairoVersion, ): Promise => { - return getAccountInstance(network, senderAddress, privateKey, cairoVersion).execute( - txnInvocation, - abis, - { - ...invocationsDetails, - skipValidate: false, - blockIdentifier: 'latest', - }, - ); + return getAccountInstance(network, senderAddress, privateKey, cairoVersion).execute(txnInvocation, abis, { + ...invocationsDetails, + skipValidate: false, + blockIdentifier: 'latest', + }); }; export const deployAccount = async ( @@ -210,7 +216,7 @@ export const estimateAccountDeployFee = async ( ...invocationsDetails, skipValidate: false, blockIdentifier: 'latest', - } + }, ); }; @@ -384,7 +390,12 @@ export const getMassagedTransactions = async ( contractAddress: txnResp.calldata?.[1] || txnResp.contract_address || txn.contract_address || '', // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore - contractFuncName: num.toBigInt(txnResp.calldata?.[2] || '') === bigIntTransferSelectorHex ? 'transfer' : num.toBigInt(txnResp.calldata?.[2] || '') === bigIntUpgradeSelectorHex ? 'upgrade' : txn.operations ?? '', + contractFuncName: + num.toBigInt(txnResp.calldata?.[2] || '') === bigIntTransferSelectorHex + ? 'transfer' + : num.toBigInt(txnResp.calldata?.[2] || '') === bigIntUpgradeSelectorHex + ? 'upgrade' + : txn.operations ?? '', // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore contractCallData: txnResp.calldata || [], diff --git a/packages/starknet-snap/test/src/sendTransaction.test.ts b/packages/starknet-snap/test/src/sendTransaction.test.ts index 31e0a282..9d684ce2 100644 --- a/packages/starknet-snap/test/src/sendTransaction.test.ts +++ b/packages/starknet-snap/test/src/sendTransaction.test.ts @@ -59,7 +59,6 @@ describe('Test function: sendTransaction', function () { beforeEach(async function () { walletStub.rpcStubs.snap_getBip44Entropy.callsFake(getBip44EntropyStub); apiParams.keyDeriver = await getAddressKeyDeriver(walletStub); - //sandbox.stub(utils, 'waitForTransaction').resolves({} as unknown as GetTransactionReceiptResponse); }); afterEach(function () { @@ -229,6 +228,7 @@ describe('Test function: sendTransaction', function () { executeTxnStub = sandbox.stub(utils, 'executeTxn').resolves(executeTxnResp); walletStub.rpcStubs.snap_manageState.resolves(state); walletStub.rpcStubs.snap_dialog.resolves(true); + sandbox.stub(utils, 'waitForTransaction').resolves({} as unknown as GetTransactionReceiptResponse); }); describe('when account is deployed', function () { diff --git a/packages/starknet-snap/test/utils/snapUtils.test.ts b/packages/starknet-snap/test/utils/snapUtils.test.ts index 90bd6e93..a88f1eb7 100644 --- a/packages/starknet-snap/test/utils/snapUtils.test.ts +++ b/packages/starknet-snap/test/utils/snapUtils.test.ts @@ -137,11 +137,15 @@ describe('getVoyagerCredentials', () => { describe('getRPCUrl', () => { it('returns Mainnet RPC URL if chain id is Mainnet', () => { - expect(getRPCUrl(constants.StarknetChainId.SN_MAIN)).to.be.equal('https://starknet-mainnet.g.alchemy.com/starknet/version/rpc/v0_7/'); + expect(getRPCUrl(constants.StarknetChainId.SN_MAIN)).to.be.equal( + 'https://starknet-mainnet.g.alchemy.com/starknet/version/rpc/v0_7/', + ); }); it('returns Sepolia RPC URL if chain id is not either Mainnet or Sepolia', () => { - expect(getRPCUrl('0x534e5f474f45524c49')).to.be.equal('https://starknet-sepolia.g.alchemy.com/starknet/version/rpc/v0_7/'); + expect(getRPCUrl('0x534e5f474f45524c49')).to.be.equal( + 'https://starknet-sepolia.g.alchemy.com/starknet/version/rpc/v0_7/', + ); }); it('returns Sepolia RPC URL if chain id is Sepolia', () => { diff --git a/packages/starknet-snap/test/utils/starknetUtils.test.ts b/packages/starknet-snap/test/utils/starknetUtils.test.ts index d4b22d21..7ef654f2 100644 --- a/packages/starknet-snap/test/utils/starknetUtils.test.ts +++ b/packages/starknet-snap/test/utils/starknetUtils.test.ts @@ -14,7 +14,7 @@ import { account3, } from '../constants.test'; import { SnapState } from '../../src/types/snapState'; -import { Calldata, num, Account, Provider } from 'starknet'; +import { Calldata, num, Account, Provider, GetTransactionReceiptResponse } from 'starknet'; import { hexToString } from '../../src/utils/formatterUtils'; chai.use(sinonChai); @@ -271,7 +271,7 @@ describe('Test function: getVersion', function () { const expected = '0.3.0'; beforeEach(function () { - callContractStub = sandbox.stub(utils, 'callContract').callsFake(async () => ([expected])); + callContractStub = sandbox.stub(utils, 'callContract').callsFake(async () => [expected]); }); afterEach(function () { @@ -294,7 +294,7 @@ describe('Test function: getOwner', function () { const expected = 'pk'; beforeEach(function () { - callContractStub = sandbox.stub(utils, 'callContract').callsFake(async () => ([expected])); + callContractStub = sandbox.stub(utils, 'callContract').callsFake(async () => [expected]); }); afterEach(function () { @@ -317,7 +317,7 @@ describe('Test function: getBalance', function () { const expected = 'pk'; beforeEach(function () { - callContractStub = sandbox.stub(utils, 'callContract').callsFake(async () => ([expected])); + callContractStub = sandbox.stub(utils, 'callContract').callsFake(async () => [expected]); }); afterEach(function () { @@ -579,3 +579,20 @@ describe('Test function: getCorrectContractAddress', function () { }); }); }); + +describe('Test function: waitForTransaction', function () { + const walletStub = new WalletMock(); + const userAddress = '0x27f204588cadd08a7914f6a9808b34de0cbfc4cb53aa053663e7fd3a34dbc26'; + + afterEach(function () { + walletStub.reset(); + sandbox.restore(); + }); + + it('pass parameter to waitForTransaction correctly', async function () { + const stub = sandbox.stub(utils, 'waitForTransaction'); + stub.resolves({} as unknown as GetTransactionReceiptResponse); + await utils.waitForTransaction(STARKNET_SEPOLIA_TESTNET_NETWORK, userAddress, 'pk', 'txHash'); + expect(stub).to.have.been.calledWith(STARKNET_SEPOLIA_TESTNET_NETWORK, userAddress, 'pk', 'txHash'); + }); +}); diff --git a/yarn.lock b/yarn.lock index 27a2cd54..c86b0207 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3248,7 +3248,7 @@ __metadata: "@consensys/starknet-snap@file:../starknet-snap::locator=wallet-ui%40workspace%3Apackages%2Fwallet-ui": version: 2.7.0 - resolution: "@consensys/starknet-snap@file:../starknet-snap#../starknet-snap::hash=099522&locator=wallet-ui%40workspace%3Apackages%2Fwallet-ui" + resolution: "@consensys/starknet-snap@file:../starknet-snap#../starknet-snap::hash=2d7b8f&locator=wallet-ui%40workspace%3Apackages%2Fwallet-ui" dependencies: "@metamask/snaps-sdk": 3.0.1 async-mutex: ^0.3.2 @@ -3257,7 +3257,7 @@ __metadata: ethers: ^5.5.1 starknet: 6.7.0 starknet_v4.22.0: "npm:starknet@4.22.0" - checksum: a53e3b5b9b53448473b4b362ebdda3d1ecb97321baa7db52393c0b43e9454ccb81d641bd2a86956896a591c23f237566d4c131034806c2609de79f9f1a12ba41 + checksum: b5bada5353623f8523c7c0ab3493b8da94446f0320b2195be81f442e4822f9c2b265da93e90a464c12c68491bb12d08438da446d263f4363fb820df1fbf7c0f1 languageName: node linkType: hard @@ -25348,6 +25348,15 @@ __metadata: languageName: node linkType: hard +"semver@npm:^7.5.2": + version: 7.6.2 + resolution: "semver@npm:7.6.2" + bin: + semver: bin/semver.js + checksum: 40f6a95101e8d854357a644da1b8dd9d93ce786d5c6a77227bc69dbb17bea83d0d1d1d7c4cd5920a6df909f48e8bd8a5909869535007f90278289f2451d0292d + languageName: node + linkType: hard + "semver@npm:^7.5.4": version: 7.6.0 resolution: "semver@npm:7.6.0" @@ -28383,7 +28392,7 @@ __metadata: react-scripts: 5.0.1 redux-persist: ^6.0.0 rimraf: ^3.0.2 - semver: ^7.3.7 + semver: ^7.5.2 starknet: ^4.22.0 styled-components: ^5.3.5 toastr2: ^3.0.0-alpha.18