Skip to content

Commit

Permalink
fix: account tests
Browse files Browse the repository at this point in the history
  • Loading branch information
badurinantun committed Jun 13, 2022
1 parent f14fe11 commit 8e86c0e
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 45 deletions.
109 changes: 76 additions & 33 deletions __tests__/account.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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);
});

Expand All @@ -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);

Expand Down Expand Up @@ -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');
});
});
});
4 changes: 2 additions & 2 deletions __tests__/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
compiledErc20,
compiledMulticall,
compiledTypeTransformation,
testAccount,
getTestAccount,
} from './fixtures';

describe('class Contract {}', () => {
Expand Down Expand Up @@ -194,7 +194,7 @@ describe('class Contract {}', () => {
});

describe('Contract interaction with Account', () => {
const account = testAccount;
const account = getTestAccount();
let erc20: Contract;
let erc20Address: string;

Expand Down
19 changes: 9 additions & 10 deletions __tests__/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
};

0 comments on commit 8e86c0e

Please sign in to comment.