Skip to content

Commit

Permalink
feat: add and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvkelawala committed Oct 7, 2022
1 parent 19ca7e8 commit b2df4c7
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 73 deletions.
56 changes: 32 additions & 24 deletions __tests__/account.test.ts
Expand Up @@ -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();
Expand All @@ -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,
Expand All @@ -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 () => {
Expand All @@ -82,7 +80,7 @@ describe('deploy and test Wallet', () => {
{
contractAddress: erc20Address,
entrypoint: 'transfer',
calldata: [account.address, '10'],
calldata: [account.address, '10', '0'],
},
undefined,
{ nonce }
Expand Down Expand Up @@ -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);
Expand All @@ -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();
});
});
});
39 changes: 25 additions & 14 deletions __tests__/contract.test.ts
@@ -1,13 +1,15 @@
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';
import {
compiledErc20,
compiledMulticall,
compiledTypeTransformation,
getERC20DeployPayload,
getTestProvider,
} from './fixtures';

Expand All @@ -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
Expand All @@ -39,29 +44,29 @@ 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 () => {
const args1 = { user: wallet };
const args2 = {};
const calls = [
erc20.address,
getSelectorFromName('balance_of'),
getSelectorFromName('balanceOf'),
Object.keys(args1).length,
...compileCalldata(args1),

Expand Down Expand Up @@ -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 () => {
Expand Down
34 changes: 9 additions & 25 deletions __tests__/defaultProvider.test.ts
Expand Up @@ -10,6 +10,7 @@ import {
compiledErc20,
compiledOpenZeppelinAccount,
describeIfNotDevnet,
getERC20DeployPayload,
getTestProvider,
} from './fixtures';

Expand All @@ -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;
Expand Down Expand Up @@ -101,7 +105,7 @@ describe('defaultProvider', () => {
return expect(
testProvider.callContract({
contractAddress: exampleContractAddress,
entrypoint: 'balance_of',
entrypoint: 'balanceOf',
calldata: compileCalldata({
user: '0x9ff64f4ab0e1fe88df4465ade98d1ea99d5732761c39279b8e1374fa943e9b',
}),
Expand All @@ -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,
Expand Down Expand Up @@ -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();
});
Expand Down Expand Up @@ -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),
Expand All @@ -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);
Expand Down
27 changes: 17 additions & 10 deletions __tests__/sequencerProvider.test.ts
Expand Up @@ -4,6 +4,7 @@ import {
compiledErc20,
describeIfNotDevnet,
describeIfSequencer,
getERC20DeployPayload,
getTestProvider,
} from './fixtures';

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

0 comments on commit b2df4c7

Please sign in to comment.