diff --git a/__tests__/account.test.ts b/__tests__/account.test.ts index 9bd63d949..5ba113b6e 100644 --- a/__tests__/account.test.ts +++ b/__tests__/account.test.ts @@ -114,12 +114,12 @@ describe('deploy and test Wallet', () => { test('execute multiple transactions', async () => { const { code, transaction_hash } = await account.execute([ { - contractAddress: dapp.connectedTo, + contractAddress: dapp.address, entrypoint: 'set_number', calldata: ['47'], }, { - contractAddress: dapp.connectedTo, + contractAddress: dapp.address, entrypoint: 'increase_number', calldata: ['10'], }, diff --git a/__tests__/accountContract.test.ts b/__tests__/accountContract.test.ts index c0642b0d4..b40d52dd6 100644 --- a/__tests__/accountContract.test.ts +++ b/__tests__/accountContract.test.ts @@ -52,7 +52,7 @@ describe('deploy and test Wallet', () => { expect(erc20Response.code).toBe('TRANSACTION_RECEIVED'); const mintResponse = await erc20.invoke('mint', { - recipient: accountContract.connectedTo, + recipient: accountContract.address, amount: '1000', }); expect(mintResponse.code).toBe('TRANSACTION_RECEIVED'); @@ -67,7 +67,7 @@ describe('deploy and test Wallet', () => { test('read balance of wallet', async () => { const { res } = await erc20.call('balance_of', { - user: accountContract.connectedTo, + user: accountContract.address, }); expect(number.toBN(res as string).toString()).toStrictEqual(number.toBN(1000).toString()); @@ -79,7 +79,7 @@ describe('deploy and test Wallet', () => { const calls = [ { contractAddress: erc20Address, entrypoint: 'transfer', calldata: [erc20Address, '10'] }, ]; - const msgHash = hash.hashMulticall(accountContract.connectedTo, calls, nonce, '0'); + const msgHash = hash.hashMulticall(accountContract.address, calls, nonce, '0'); const { callArray, calldata } = transformCallsToMulticallArrays(calls); @@ -101,7 +101,7 @@ describe('deploy and test Wallet', () => { test('read balance of wallet after transfer', async () => { const { res } = await erc20.call('balance_of', { - user: accountContract.connectedTo, + user: accountContract.address, }); expect(number.toBN(res as string).toString()).toStrictEqual(number.toBN(990).toString()); diff --git a/__tests__/contract.test.ts b/__tests__/contract.test.ts index 006efcf3c..2b3261a64 100644 --- a/__tests__/contract.test.ts +++ b/__tests__/contract.test.ts @@ -1,10 +1,15 @@ import { isBN } from 'bn.js'; -import { Contract, defaultProvider, stark } from '../src'; +import { Account, Contract, Provider, defaultProvider, ec, stark } from '../src'; import { getSelectorFromName } from '../src/utils/hash'; import { BigNumberish, toBN } from '../src/utils/number'; import { compileCalldata } from '../src/utils/stark'; -import { compiledErc20, compiledMulticall, compiledTypeTransformation } from './fixtures'; +import { + compiledArgentAccount, + compiledErc20, + compiledMulticall, + compiledTypeTransformation, +} from './fixtures'; describe('class Contract {}', () => { const wallet = stark.randomAddress(); @@ -38,15 +43,21 @@ describe('class Contract {}', () => { await defaultProvider.waitForTransaction(m_transaction_hash); }); + test('populate transaction for initial balance of that account', async () => { + const res = await erc20.populateTransaction.balance_of({ user: wallet }); + expect(res).toHaveProperty('contractAddress'); + expect(res).toHaveProperty('entrypoint'); + expect(res).toHaveProperty('calldata'); + expect(res).toHaveProperty('signature'); + }); + test('read initial balance of that account', async () => { - const { res } = await erc20.call('balance_of', { - user: wallet, - }); + const { res } = await erc20.balance_of({ user: wallet }); expect(res).toStrictEqual(toBN(0)); }); test('add 10 test ERC20 to account', async () => { - const response = await erc20.invoke('mint', { + const response = await erc20.mint({ recipient: wallet, amount: '10', }); @@ -56,9 +67,7 @@ describe('class Contract {}', () => { }); test('read balance after mint of that account', async () => { - const { res } = await erc20.call('balance_of', { - user: wallet, - }); + const { res } = await erc20.balance_of({ user: wallet }); expect(res).toStrictEqual(toBN(10)); }); @@ -67,17 +76,17 @@ describe('class Contract {}', () => { const args1 = { user: wallet }; const args2 = {}; const calls = [ - erc20.connectedTo, + erc20.address, getSelectorFromName('balance_of'), Object.keys(args1).length, ...compileCalldata(args1), - erc20.connectedTo, + erc20.address, getSelectorFromName('decimals'), Object.keys(args2).length, ...compileCalldata(args2), ]; - const { block_number, result } = await contract.call('aggregate', { calls }); + const { block_number, result } = await contract.aggregate({ calls }); expect(isBN(block_number)); expect(Array.isArray(result)); (result as BigNumberish[]).forEach((el) => expect(isBN(el))); @@ -98,18 +107,16 @@ describe('class Contract {}', () => { describe('Request Type Transformation', () => { test('Parsing the felt in request', async () => { - return expect(contract.call('request_felt', { num: 3 })).resolves.not.toThrow(); + return expect(contract.request_felt({ num: 3 })).resolves.not.toThrow(); }); test('Parsing the array of felt in request', async () => { - return expect( - contract.call('request_array_of_felts', { arr: [1, 2] }) - ).resolves.not.toThrow(); + return expect(contract.request_array_of_felts({ arr: [1, 2] })).resolves.not.toThrow(); }); test('Parsing the struct in request', async () => { return expect( - contract.call('request_struct', { + contract.request_struct({ str: { x: 1, y: 2 }, }) ).resolves.not.toThrow(); @@ -117,13 +124,13 @@ describe('class Contract {}', () => { test('Parsing the array of structs in request', async () => { return expect( - contract.call('request_array_of_structs', { str: [{ x: 1, y: 2 }] }) + contract.request_array_of_structs({ str: [{ x: 1, y: 2 }] }) ).resolves.not.toThrow(); }); test('Parsing the nested structs in request', async () => { return expect( - contract.call('request_nested_structs', { + contract.request_nested_structs({ str: { p1: { x: 1, y: 2 }, p2: { x: 3, y: 4 }, @@ -134,12 +141,12 @@ describe('class Contract {}', () => { }); test('Parsing the tuple in request', async () => { - return expect(contract.call('request_tuple', { tup: [1, 2] })).resolves.not.toThrow(); + return expect(contract.request_tuple({ tup: [1, 2] })).resolves.not.toThrow(); }); test('Parsing the multiple types in request', async () => { return expect( - contract.call('request_mixed_types', { + contract.request_mixed_types({ num: 2, point: { x: 1, @@ -153,27 +160,27 @@ describe('class Contract {}', () => { describe('Response Type Transformation', () => { test('Parsing the felt in response', async () => { - const { res } = await contract.call('get_felt'); + const { res } = await contract.get_felt(); expect(res).toStrictEqual(toBN(4)); }); test('Parsing the array of felt in response', async () => { - const { res } = await contract.call('get_array_of_felts'); + const { res } = await contract.get_array_of_felts(); expect(res).toStrictEqual([toBN(4), toBN(5)]); }); test('Parsing the array of structs in response', async () => { - const { res } = await contract.call('get_struct'); + const { res } = await contract.get_struct(); expect(res).toStrictEqual({ x: toBN(1), y: toBN(2) }); }); test('Parsing the array of structs in response', async () => { - const { res } = await contract.call('get_array_of_structs'); + const { res } = await contract.get_array_of_structs(); expect(res).toStrictEqual([{ x: toBN(1), y: toBN(2) }]); }); test('Parsing the nested structs in response', async () => { - const { res } = await contract.call('get_nested_structs'); + const { res } = await contract.get_nested_structs(); expect(res).toStrictEqual({ p1: { x: toBN(1), y: toBN(2) }, p2: { x: toBN(3), y: toBN(4) }, @@ -182,12 +189,12 @@ describe('class Contract {}', () => { }); test('Parsing the tuple in response', async () => { - const { res } = await contract.call('get_tuple'); + const { res } = await contract.get_tuple(); expect(res).toStrictEqual([toBN(1), toBN(2), toBN(3)]); }); test('Parsing the multiple types in response', async () => { - const { tuple, number, array, point } = await contract.call('get_mixed_types'); + const { tuple, number, array, point } = await contract.get_mixed_types(); expect(tuple).toStrictEqual([toBN(1), toBN(2)]); expect(number).toStrictEqual(toBN(3)); expect(array).toStrictEqual([toBN(4)]); @@ -195,4 +202,81 @@ describe('class Contract {}', () => { }); }); }); + + describe('Contract interaction with Account', () => { + const privateKey = stark.randomAddress(); + + const starkKeyPair = ec.getKeyPair(privateKey); + const starkKeyPub = ec.getStarkKey(starkKeyPair); + let account: Account; + let erc20: Contract; + let erc20Address: string; + + 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({ + signer: starkKeyPub, + guardian: '0', + }); + expect(initializeResponse.code).toBe('TRANSACTION_RECEIVED'); + + account = new Account(defaultProvider, accountResponse.address, starkKeyPair); + + const erc20Response = await defaultProvider.deployContract({ + contract: compiledErc20, + }); + erc20Address = erc20Response.address; + erc20 = new Contract(compiledErc20.abi, erc20Address, defaultProvider); + await defaultProvider.waitForTransaction(erc20Response.transaction_hash); + expect(erc20Response.code).toBe('TRANSACTION_RECEIVED'); + + const mintResponse = await erc20.mint({ + recipient: account.address, + amount: '1000', + }); + + await defaultProvider.waitForTransaction(mintResponse.transaction_hash); + }); + + test('read balance of wallet', async () => { + const { res } = await erc20.balance_of({ + user: account.address, + }); + + expect(res).toStrictEqual(toBN(1000)); + }); + + test('change from provider to account', async () => { + expect(erc20.providerOrAccount instanceof Provider); + erc20.connect(account); + expect(erc20.providerOrAccount instanceof Account); + }); + + test('read balance of wallet', async () => { + const { res } = await erc20.balance_of({ + user: account.address, + }); + + expect(res).toStrictEqual(toBN(1000)); + }); + + test('invoke contract by wallet owner', async () => { + const { transaction_hash, code } = await erc20.transfer({ + recipient: toBN(erc20Address).toString(), + amount: 10, + }); + expect(code).toBe('TRANSACTION_RECEIVED'); + await defaultProvider.waitForTransaction(transaction_hash); + const { res } = await erc20.balance_of({ + user: account.address, + }); + expect(res).toStrictEqual(toBN(990)); + }); + }); });