From b2df4c7deea96aaefcdf90e640bca67a91b6dab7 Mon Sep 17 00:00:00 2001 From: Dhruv Kelawala Date: Fri, 7 Oct 2022 16:40:03 +0530 Subject: [PATCH] feat: add and update tests --- __tests__/account.test.ts | 56 ++++++++++++++++------------- __tests__/contract.test.ts | 39 ++++++++++++-------- __tests__/defaultProvider.test.ts | 34 +++++------------- __tests__/sequencerProvider.test.ts | 27 ++++++++------ 4 files changed, 83 insertions(+), 73 deletions(-) diff --git a/__tests__/account.test.ts b/__tests__/account.test.ts index 0e17a5e89..49d079a64 100644 --- a/__tests__/account.test.ts +++ b/__tests__/account.test.ts @@ -3,7 +3,13 @@ import { isBN } from 'bn.js'; import typedDataExample from '../__mocks__/typedDataExample.json'; import { Account, Contract, Provider, number, stark } from '../src'; import { toBN } from '../src/utils/number'; -import { compiledErc20, compiledTestDapp, getTestAccount, getTestProvider } from './fixtures'; +import { + compiledErc20, + compiledTestDapp, + getERC20DeployPayload, + getTestAccount, + getTestProvider, +} from './fixtures'; describe('deploy and test Wallet', () => { const provider = getTestProvider(); @@ -15,26 +21,18 @@ describe('deploy and test Wallet', () => { beforeAll(async () => { expect(account).toBeInstanceOf(Account); - const erc20Response = await provider.deployContract({ - contract: compiledErc20, - }); + const erc20DeployPayload = getERC20DeployPayload(account.address); + + const erc20Response = await provider.deployContract(erc20DeployPayload); erc20Address = erc20Response.contract_address; erc20 = new Contract(compiledErc20.abi, erc20Address, provider); await provider.waitForTransaction(erc20Response.transaction_hash); - const mintResponse = await account.execute({ - contractAddress: erc20Address, - entrypoint: 'mint', - calldata: [account.address, '1000'], - }); - - await provider.waitForTransaction(mintResponse.transaction_hash); + const x = await erc20.balanceOf(account.address); - const x = await erc20.balance_of(account.address); - - expect(number.toBN(x.res as string).toString()).toStrictEqual(number.toBN(1000).toString()); + expect(number.toBN(x[0].low).toString()).toStrictEqual(number.toBN(1000).toString()); const dappResponse = await provider.deployContract({ contract: compiledTestDapp, @@ -45,34 +43,34 @@ describe('deploy and test Wallet', () => { }); test('estimate fee', async () => { - const { overall_fee } = await account.estimateFee({ + const { overall_fee } = await account.estimateInvokeFee({ contractAddress: erc20Address, entrypoint: 'transfer', - calldata: [erc20.address, '10'], + calldata: [erc20.address, '10', '0'], }); expect(isBN(overall_fee)).toBe(true); }); test('read balance of wallet', async () => { - const x = await erc20.balance_of(account.address); + const x = await erc20.balanceOf(account.address); - expect(number.toBN(x.res as string).toString()).toStrictEqual(number.toBN(1000).toString()); + expect(number.toBN(x[0].low).toString()).toStrictEqual(number.toBN(1000).toString()); }); test('execute by wallet owner', async () => { const { transaction_hash } = await account.execute({ contractAddress: erc20Address, entrypoint: 'transfer', - calldata: [erc20.address, '10'], + calldata: [erc20.address, '10', '0'], }); await provider.waitForTransaction(transaction_hash); }); test('read balance of wallet after transfer', async () => { - const { res } = await erc20.balance_of(account.address); + const { balance } = await erc20.balanceOf(account.address); - expect(res).toStrictEqual(toBN(990)); + expect(balance.low).toStrictEqual(toBN(990)); }); test('execute with custom nonce', async () => { @@ -82,7 +80,7 @@ describe('deploy and test Wallet', () => { { contractAddress: erc20Address, entrypoint: 'transfer', - calldata: [account.address, '10'], + calldata: [account.address, '10', '0'], }, undefined, { nonce } @@ -130,7 +128,7 @@ describe('deploy and test Wallet', () => { const mintResponse = await account.execute({ contractAddress: erc20Address, entrypoint: 'mint', - calldata: [wallet, '1000'], + calldata: [wallet, '1000', '0'], }); await provider.waitForTransaction(mintResponse.transaction_hash); @@ -143,8 +141,18 @@ describe('deploy and test Wallet', () => { }); test('estimate gas fee for `mint`', async () => { - const res = await erc20.estimateFee.mint(wallet, '10'); + const res = await erc20.estimateFee.mint(wallet, ['10', '0']); expect(res).toHaveProperty('overall_fee'); }); + + test('Declare Account contract', async () => { + const declareTx = await account.declare({ + contract: compiledErc20, + classHash: '0x54328a1075b8820eb43caf0caa233923148c983742402dcfc38541dd843d01a', + }); + await provider.waitForTransaction(declareTx.transaction_hash); + + expect(declareTx.class_hash).toBeDefined(); + }); }); }); diff --git a/__tests__/contract.test.ts b/__tests__/contract.test.ts index 455a0546a..e196d4684 100644 --- a/__tests__/contract.test.ts +++ b/__tests__/contract.test.ts @@ -1,6 +1,7 @@ import { isBN } from 'bn.js'; import { Contract, ContractFactory, stark } from '../src'; +import { DeployContractPayload } from '../src/types/lib'; import { getSelectorFromName } from '../src/utils/hash'; import { BigNumberish, toBN } from '../src/utils/number'; import { compileCalldata } from '../src/utils/stark'; @@ -8,6 +9,7 @@ import { compiledErc20, compiledMulticall, compiledTypeTransformation, + getERC20DeployPayload, getTestProvider, } from './fixtures'; @@ -21,9 +23,12 @@ describe('class Contract {}', () => { let contract: Contract; beforeAll(async () => { - const { transaction_hash, contract_address } = await provider.deployContract({ - contract: compiledErc20, - }); + const erc20DeployPayload = getERC20DeployPayload(wallet); + + const { contract_address, transaction_hash } = await provider.deployContract( + erc20DeployPayload + ); + erc20 = new Contract(compiledErc20.abi, contract_address!, provider); await provider.waitForTransaction(transaction_hash); // Deploy Multicall @@ -39,21 +44,21 @@ describe('class Contract {}', () => { }); test('populate transaction for initial balance of that account', async () => { - const res = await erc20.populateTransaction.balance_of(wallet); + const res = await erc20.populateTransaction.balanceOf(wallet); expect(res).toHaveProperty('contractAddress'); expect(res).toHaveProperty('entrypoint'); expect(res).toHaveProperty('calldata'); }); test('estimate gas fee for `mint` should fail when connected to the provider', async () => { - expect(erc20.estimateFee.mint(wallet, '10')).rejects.toThrow(); + expect(erc20.estimateFee.mint(wallet, ['10', '0'])).rejects.toThrow(); }); test('read initial balance of that account', async () => { - const result = await erc20.balance_of(wallet); + const result = await erc20.balanceOf(wallet); const [res] = result; - expect(res).toStrictEqual(toBN(0)); - expect(res).toStrictEqual(result.res); + expect(res.low).toStrictEqual(toBN(1000)); + expect(res).toStrictEqual(result.balance); }); test('read balance in a multicall', async () => { @@ -61,7 +66,7 @@ describe('class Contract {}', () => { const args2 = {}; const calls = [ erc20.address, - getSelectorFromName('balance_of'), + getSelectorFromName('balanceOf'), Object.keys(args1).length, ...compileCalldata(args1), @@ -190,21 +195,27 @@ describe('class Contract {}', () => { describe('class ContractFactory {}', () => { let erc20Address: string; + const wallet = stark.randomAddress(); + let erc20DeployPayload: DeployContractPayload; + beforeAll(async () => { - const { transaction_hash, contract_address } = await provider.deployContract({ - contract: compiledErc20, - }); + erc20DeployPayload = getERC20DeployPayload(wallet); + + const { contract_address, transaction_hash } = await provider.deployContract( + erc20DeployPayload + ); + await provider.waitForTransaction(transaction_hash); erc20Address = contract_address; }); test('deployment of new contract', async () => { const factory = new ContractFactory(compiledErc20, provider); - const erc20 = await factory.deploy(); + const erc20 = await factory.deploy(erc20DeployPayload.constructorCalldata); expect(erc20 instanceof Contract); }); test('wait for deployment transaction', async () => { const factory = new ContractFactory(compiledErc20, provider); - const contract = await factory.deploy(); + const contract = await factory.deploy(erc20DeployPayload.constructorCalldata); expect(contract.deployed()).resolves.not.toThrow(); }); test('attach new contract', async () => { diff --git a/__tests__/defaultProvider.test.ts b/__tests__/defaultProvider.test.ts index 08e04c3e5..7bbd1fc51 100644 --- a/__tests__/defaultProvider.test.ts +++ b/__tests__/defaultProvider.test.ts @@ -10,6 +10,7 @@ import { compiledErc20, compiledOpenZeppelinAccount, describeIfNotDevnet, + getERC20DeployPayload, getTestProvider, } from './fixtures'; @@ -24,11 +25,14 @@ describe('defaultProvider', () => { let exampleBlock: GetBlockResponse; let exampleBlockNumber: BlockNumber; let exampleBlockHash: string; + const wallet = stark.randomAddress(); beforeAll(async () => { - const { transaction_hash, contract_address } = await testProvider.deployContract({ - contract: compiledErc20, - }); + const erc20DeployPayload = getERC20DeployPayload(wallet); + + const { contract_address, transaction_hash } = await testProvider.deployContract( + erc20DeployPayload + ); await testProvider.waitForTransaction(transaction_hash); exampleTransactionHash = transaction_hash; exampleContractAddress = contract_address; @@ -101,7 +105,7 @@ describe('defaultProvider', () => { return expect( testProvider.callContract({ contractAddress: exampleContractAddress, - entrypoint: 'balance_of', + entrypoint: 'balanceOf', calldata: compileCalldata({ user: '0x9ff64f4ab0e1fe88df4465ade98d1ea99d5732761c39279b8e1374fa943e9b', }), @@ -123,15 +127,6 @@ describe('defaultProvider', () => { }); describe('addTransaction()', () => { - test('declareContract()', async () => { - const response = await testProvider.declareContract({ - contract: compiledErc20, - }); - - expect(response.transaction_hash).toBeDefined(); - expect(response.class_hash).toBeDefined(); - }); - test('deployContract()', async () => { const response = await testProvider.deployContract({ contract: compiledOpenZeppelinAccount, @@ -221,6 +216,7 @@ describe('defaultProvider', () => { expect(transaction.transaction_hash).toBeTruthy(); expect(transaction.contract_address).toBeTruthy(); expect(Array.isArray(transaction.calldata)).toBe(true); + // expect(transaction.entry_point_selector).toBeTruthy(); expect(Array.isArray(transaction.signature)).toBe(true); expect(transaction.max_fee).toBeTruthy(); }); @@ -260,12 +256,7 @@ describe('defaultProvider', () => { beforeAll(async () => { deployResponse = await provider.deployContract({ contract: compiledErc20 }); - console.log( - '🚀 ~ file: defaultProvider.test.ts ~ line 264 ~ beforeAll ~ deployResponse', - deployResponse - ); contractAddress = deployResponse.contract_address; - declareResponse = await provider.declareContract({ contract: compiledErc20 }); await Promise.all([ provider.waitForTransaction(deployResponse.transaction_hash), provider.waitForTransaction(declareResponse.transaction_hash), @@ -280,13 +271,6 @@ describe('defaultProvider', () => { }); }); - describe('declareContract', () => { - test('response', async () => { - expect(declareResponse.class_hash).toBeTruthy(); - expect(declareResponse.transaction_hash).toBeTruthy(); - }); - }); - describe('getClassAt', () => { test('response', async () => { const classResponse = await provider.getClassAt(contractAddress, blockNumber); diff --git a/__tests__/sequencerProvider.test.ts b/__tests__/sequencerProvider.test.ts index 19d1bc941..910e9a6af 100644 --- a/__tests__/sequencerProvider.test.ts +++ b/__tests__/sequencerProvider.test.ts @@ -4,6 +4,7 @@ import { compiledErc20, describeIfNotDevnet, describeIfSequencer, + getERC20DeployPayload, getTestProvider, } from './fixtures'; @@ -17,7 +18,7 @@ describeIfSequencer('SequencerProvider', () => { sequencerProvider = getTestProvider() as SequencerProvider; customSequencerProvider = new Provider({ sequencer: { - baseUrl: 'https://alpha4.starknet.io', + baseUrl: 'http://127.0.0.1:5050/', feederGatewayUrl: 'feeder_gateway', gatewayUrl: 'gateway', }, // Similar to arguements used in docs @@ -26,11 +27,15 @@ describeIfSequencer('SequencerProvider', () => { describe('Gateway specific methods', () => { let exampleTransactionHash: string; + const wallet = stark.randomAddress(); beforeAll(async () => { - const { transaction_hash, contract_address } = await sequencerProvider.deployContract({ - contract: compiledErc20, - }); + const erc20DeployPayload = getERC20DeployPayload(wallet); + + const { contract_address, transaction_hash } = await sequencerProvider.deployContract( + erc20DeployPayload + ); + await sequencerProvider.waitForTransaction(transaction_hash); exampleTransactionHash = transaction_hash; exampleContractAddress = contract_address; @@ -67,19 +72,21 @@ describeIfSequencer('SequencerProvider', () => { const wallet = stark.randomAddress(); beforeAll(async () => { - const { contract_address, transaction_hash } = await customSequencerProvider.deployContract({ - contract: compiledErc20, - }); + const erc20DeployPayload = getERC20DeployPayload(wallet); + + const { contract_address, transaction_hash } = await customSequencerProvider.deployContract( + erc20DeployPayload + ); await customSequencerProvider.waitForTransaction(transaction_hash); erc20 = new Contract(compiledErc20.abi, contract_address, customSequencerProvider); }); test('Check ERC20 balance using Custom Sequencer Provider', async () => { - const result = await erc20.balance_of(wallet); + const result = await erc20.balanceOf(wallet); const [res] = result; - expect(res).toStrictEqual(toBN(0)); - expect(res).toStrictEqual(result.res); + expect(res.low).toStrictEqual(toBN(1000)); + expect(res).toStrictEqual(result.balance); }); }); });