From 8e86c0e813924fa5cf8f708acd708f9bea1bb7c3 Mon Sep 17 00:00:00 2001 From: Antun Badurina Date: Mon, 13 Jun 2022 17:17:32 +0200 Subject: [PATCH] fix: account tests --- __tests__/account.test.ts | 109 ++++++++++++++++++++++++++----------- __tests__/contract.test.ts | 4 +- __tests__/fixtures.ts | 19 +++---- 3 files changed, 87 insertions(+), 45 deletions(-) diff --git a/__tests__/account.test.ts b/__tests__/account.test.ts index 8ae43681f..94759bf63 100644 --- a/__tests__/account.test.ts +++ b/__tests__/account.test.ts @@ -1,32 +1,23 @@ import { isBN } from 'bn.js'; import typedDataExample from '../__mocks__/typedDataExample.json'; -import { Account, Contract, defaultProvider, ec, number, stark } from '../src'; +import { Account, Contract, Provider, defaultProvider, ec, number, stark } from '../src'; import { toBN } from '../src/utils/number'; -import { compiledArgentAccount, compiledErc20, compiledTestDapp } from './fixtures'; +import { + compiledErc20, + compiledOpenZeppelinAccount, + compiledTestDapp, + getTestAccount, +} from './fixtures'; describe('deploy and test Wallet', () => { - const privateKey = stark.randomAddress(); - - const starkKeyPair = ec.getKeyPair(privateKey); - const starkKeyPub = ec.getStarkKey(starkKeyPair); - let account: Account; + const account: Account = getTestAccount(); let erc20: Contract; let erc20Address: string; let dapp: Contract; beforeAll(async () => { - const accountResponse = await defaultProvider.deployContract({ - contract: compiledArgentAccount, - addressSalt: starkKeyPub, - }); - const contract = new Contract(compiledArgentAccount.abi, accountResponse.address); - expect(accountResponse.code).toBe('TRANSACTION_RECEIVED'); - - const initializeResponse = await contract.initialize(starkKeyPub, '0'); - expect(initializeResponse.code).toBe('TRANSACTION_RECEIVED'); - - account = new Account(defaultProvider, accountResponse.address, starkKeyPair); + expect(account).toBeInstanceOf(Account); const erc20Response = await defaultProvider.deployContract({ contract: compiledErc20, @@ -35,14 +26,24 @@ describe('deploy and test Wallet', () => { erc20 = new Contract(compiledErc20.abi, erc20Address); expect(erc20Response.code).toBe('TRANSACTION_RECEIVED'); - const mintResponse = await erc20.mint(account.address, '1000'); + await defaultProvider.waitForTransaction(erc20Response.transaction_hash); + + const mintResponse = await account.execute({ + contractAddress: erc20Address, + entrypoint: 'mint', + calldata: [account.address, '1000'], + }); + expect(mintResponse.code).toBe('TRANSACTION_RECEIVED'); + await defaultProvider.waitForTransaction(mintResponse.transaction_hash); + const dappResponse = await defaultProvider.deployContract({ contract: compiledTestDapp, }); dapp = new Contract(compiledTestDapp.abi, dappResponse.address); expect(dappResponse.code).toBe('TRANSACTION_RECEIVED'); + await defaultProvider.waitForTransaction(dappResponse.transaction_hash); }); @@ -56,20 +57,6 @@ describe('deploy and test Wallet', () => { expect(typeof unit).toBe('string'); }); - test('same wallet address', () => { - expect(account.address).toBe(account.address); - }); - - test('read nonce', async () => { - const { result } = await account.callContract({ - contractAddress: account.address, - entrypoint: 'get_nonce', - }); - const nonce = result[0]; - - expect(number.toBN(nonce).toString()).toStrictEqual(number.toBN(0).toString()); - }); - test('read balance of wallet', async () => { const { res } = await erc20.balance_of(account.address); @@ -139,4 +126,60 @@ describe('deploy and test Wallet', () => { expect(await account.verifyMessage(typedDataExample, signature)).toBe(true); }); + + describe('new deployed account', () => { + let newAccount: Account; + + beforeAll(async () => { + const starkKeyPair = ec.genKeyPair(); + const starkKeyPub = ec.getStarkKey(starkKeyPair); + + const accountResponse = await defaultProvider.deployContract({ + contract: compiledOpenZeppelinAccount, + constructorCalldata: [starkKeyPub], + }); + + await defaultProvider.waitForTransaction(accountResponse.transaction_hash); + + newAccount = new Account(defaultProvider, accountResponse.address, starkKeyPair); + }); + + test('read nonce', async () => { + const { result } = await account.callContract({ + contractAddress: newAccount.address, + entrypoint: 'get_nonce', + }); + const nonce = result[0]; + + expect(number.toBN(nonce).toString()).toStrictEqual(number.toBN(0).toString()); + }); + }); + + describe('Contract interaction with Account', () => { + const wallet = stark.randomAddress(); + + beforeAll(async () => { + const mintResponse = await account.execute({ + contractAddress: erc20Address, + entrypoint: 'mint', + calldata: [wallet, '1000'], + }); + + expect(mintResponse.code).toBe('TRANSACTION_RECEIVED'); + + await defaultProvider.waitForTransaction(mintResponse.transaction_hash); + }); + + test('change from provider to account', async () => { + expect(erc20.providerOrAccount instanceof Provider); + erc20.connect(account); + expect(erc20.providerOrAccount instanceof Account); + }); + + test('estimate gas fee for `mint`', async () => { + const res = await erc20.estimateFee.mint(wallet, '10'); + expect(res).toHaveProperty('amount'); + expect(res).toHaveProperty('unit'); + }); + }); }); diff --git a/__tests__/contract.test.ts b/__tests__/contract.test.ts index c22af400b..c0fb9af3a 100644 --- a/__tests__/contract.test.ts +++ b/__tests__/contract.test.ts @@ -8,7 +8,7 @@ import { compiledErc20, compiledMulticall, compiledTypeTransformation, - testAccount, + getTestAccount, } from './fixtures'; describe('class Contract {}', () => { @@ -194,7 +194,7 @@ describe('class Contract {}', () => { }); describe('Contract interaction with Account', () => { - const account = testAccount; + const account = getTestAccount(); let erc20: Contract; let erc20Address: string; diff --git a/__tests__/fixtures.ts b/__tests__/fixtures.ts index 7b0c61bf5..cf0f4b623 100644 --- a/__tests__/fixtures.ts +++ b/__tests__/fixtures.ts @@ -15,15 +15,14 @@ export const compiledTestDapp = readContract('TestDapp'); const DEFAULT_TEST_ACCOUNT_ADDRESS = '0x6c0a3ca4f79e978f3b7005898aaa49bef4a24aeaa5f10c6a97887516400197e'; -const testAccountAddress = process.env.TEST_ACCOUNT_ADDRESS || DEFAULT_TEST_ACCOUNT_ADDRESS; -const testAccountPrivateKey = process.env.TEST_ACCOUNT_PRIVATE_KEY; -if (!testAccountPrivateKey) { - throw new Error('TEST_ACCOUNT_PRIVATE_KEY is not set'); -} +export const getTestAccount = () => { + const testAccountAddress = process.env.TEST_ACCOUNT_ADDRESS || DEFAULT_TEST_ACCOUNT_ADDRESS; + const testAccountPrivateKey = process.env.TEST_ACCOUNT_PRIVATE_KEY; -export const testAccount = new Account( - defaultProvider, - testAccountAddress, - ec.getKeyPair(testAccountPrivateKey) -); + if (!testAccountPrivateKey) { + throw new Error('TEST_ACCOUNT_PRIVATE_KEY is not set'); + } + + return new Account(defaultProvider, testAccountAddress, ec.getKeyPair(testAccountPrivateKey)); +};