Skip to content

Commit

Permalink
feat: r p.deployCo. C tests, CF fixtures, cleanHex, DeclareDeploy, F …
Browse files Browse the repository at this point in the history
…Provider, seq.waitFor ret.txRec
  • Loading branch information
tabaktoni committed Nov 24, 2022
1 parent 605c2a9 commit bb50f53
Show file tree
Hide file tree
Showing 13 changed files with 197 additions and 164 deletions.
1 change: 0 additions & 1 deletion __tests__/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ describe('deploy and test Wallet', () => {
beforeAll(async () => {
expect(account).toBeInstanceOf(Account);

// New Stuff
const declareDeploy = await account.declareDeploy({
contract: compiledErc20,
classHash: '0x54328a1075b8820eb43caf0caa233923148c983742402dcfc38541dd843d01a',
Expand Down
116 changes: 63 additions & 53 deletions __tests__/contract.test.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,62 @@
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 { encodeShortString } from '../src/utils/shortString';
import { compileCalldata } from '../src/utils/stark';
import {
compiledErc20,
compiledMulticall,
compiledTypeTransformation,
getERC20DeployPayload,
getTestAccount,
getTestProvider,
} from './fixtures';

const provider = getTestProvider();

describe('class Contract {}', () => {
const wallet = stark.randomAddress();
const account = getTestAccount(provider);

describe('Basic Interaction', () => {
let erc20: Contract;
let contract: Contract;
let erc20Contract: Contract;
let multicallContract: Contract;

beforeAll(async () => {
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
const { deploy } = await account.declareDeploy({
contract: compiledErc20,
classHash: '0x54328a1075b8820eb43caf0caa233923148c983742402dcfc38541dd843d01a',
constructorCalldata: [encodeShortString('Token'), encodeShortString('ERC20'), wallet],
});

const { transaction_hash: m_transaction_hash, contract_address: multicallAddress } =
await provider.deployContract({
contract: compiledMulticall,
});
erc20Contract = new Contract(compiledErc20.abi, deploy.contract_address!, provider);

contract = new Contract(compiledMulticall.abi, multicallAddress!, provider);
const { deploy: multicallDeploy } = await account.declareDeploy({
contract: compiledMulticall,
classHash: '0x06f94f3229a8d9c1d51cb84f1f5ec306c8552a805e307540727dda53c4936b43',
});

await provider.waitForTransaction(m_transaction_hash);
multicallContract = new Contract(
compiledMulticall.abi,
multicallDeploy.contract_address!,
provider
);
});

test('populate transaction for initial balance of that account', async () => {
const res = await erc20.populateTransaction.balanceOf(wallet);
const res = await erc20Contract.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', '0'])).rejects.toThrow();
expect(erc20Contract.estimateFee.mint(wallet, ['10', '0'])).rejects.toThrow();
});

test('read initial balance of that account', async () => {
const result = await erc20.balanceOf(wallet);
const result = await erc20Contract.balanceOf(wallet);
const [res] = result;
expect(res.low).toStrictEqual(toBN(1000));
expect(res).toStrictEqual(result.balance);
Expand All @@ -65,17 +66,17 @@ describe('class Contract {}', () => {
const args1 = { user: wallet };
const args2 = {};
const calls = [
erc20.address,
erc20Contract.address,
getSelectorFromName('balanceOf'),
Object.keys(args1).length,
...compileCalldata(args1),

erc20.address,
erc20Contract.address,
getSelectorFromName('decimals'),
Object.keys(args2).length,
...compileCalldata(args2),
];
const result = await contract.aggregate(calls);
const result = await multicallContract.aggregate(calls);
const [block_number, res] = result;
expect(isBN(block_number));
expect(Array.isArray(res));
Expand All @@ -86,36 +87,43 @@ describe('class Contract {}', () => {
});

describe('Type Transformation', () => {
let contract: Contract;
let typeTransContract: Contract;

beforeAll(async () => {
const { transaction_hash, contract_address } = await provider.deployContract({
const { deploy } = await account.declareDeploy({
contract: compiledTypeTransformation,
classHash: '0x022a0e662b13d18a2aaa3ee54ae290de6569621b549022c18169c6e7893809ea',
});
contract = new Contract(compiledTypeTransformation.abi, contract_address!, provider);
await provider.waitForTransaction(transaction_hash);

typeTransContract = new Contract(
compiledTypeTransformation.abi,
deploy.contract_address!,
provider
);
});

describe('Request Type Transformation', () => {
test('Parsing the felt in request', async () => {
return expect(contract.request_felt(3)).resolves.not.toThrow();
return expect(typeTransContract.request_felt(3)).resolves.not.toThrow();
});

test('Parsing the array of felt in request', async () => {
return expect(contract.request_array_of_felts([1, 2])).resolves.not.toThrow();
return expect(typeTransContract.request_array_of_felts([1, 2])).resolves.not.toThrow();
});

test('Parsing the struct in request', async () => {
return expect(contract.request_struct({ x: 1, y: 2 })).resolves.not.toThrow();
return expect(typeTransContract.request_struct({ x: 1, y: 2 })).resolves.not.toThrow();
});

test('Parsing the array of structs in request', async () => {
return expect(contract.request_array_of_structs([{ x: 1, y: 2 }])).resolves.not.toThrow();
return expect(
typeTransContract.request_array_of_structs([{ x: 1, y: 2 }])
).resolves.not.toThrow();
});

test('Parsing the nested structs in request', async () => {
return expect(
contract.request_nested_structs({
typeTransContract.request_nested_structs({
p1: { x: 1, y: 2 },
p2: { x: 3, y: 4 },
extra: 5,
Expand All @@ -124,43 +132,45 @@ describe('class Contract {}', () => {
});

test('Parsing the tuple in request', async () => {
return expect(contract.request_tuple([1, 2])).resolves.not.toThrow();
return expect(typeTransContract.request_tuple([1, 2])).resolves.not.toThrow();
});

test('Parsing the multiple types in request', async () => {
return expect(contract.request_mixed_types(2, { x: 1, y: 2 }, [1])).resolves.not.toThrow();
return expect(
typeTransContract.request_mixed_types(2, { x: 1, y: 2 }, [1])
).resolves.not.toThrow();
});
});

describe('Response Type Transformation', () => {
test('Parsing the felt in response', async () => {
const { res } = await contract.get_felt();
const { res } = await typeTransContract.get_felt();
expect(res).toStrictEqual(toBN(4));
});

test('Parsing the array of felt in response', async () => {
const result = await contract.get_array_of_felts();
const result = await typeTransContract.get_array_of_felts();
const [res] = result;
expect(res).toStrictEqual([toBN(4), toBN(5)]);
expect(res).toStrictEqual(result.res);
});

test('Parsing the array of structs in response', async () => {
const result = await contract.get_struct();
const result = await typeTransContract.get_struct();
const [res] = result;
expect(res).toStrictEqual({ x: toBN(1), y: toBN(2) });
expect(res).toStrictEqual(result.res);
});

test('Parsing the array of structs in response', async () => {
const result = await contract.get_array_of_structs();
const result = await typeTransContract.get_array_of_structs();
const [res] = result;
expect(res).toStrictEqual([{ x: toBN(1), y: toBN(2) }]);
expect(res).toStrictEqual(result.res);
});

test('Parsing the nested structs in response', async () => {
const result = await contract.get_nested_structs();
const result = await typeTransContract.get_nested_structs();
const [res] = result;
expect(res).toStrictEqual({
p1: { x: toBN(1), y: toBN(2) },
Expand All @@ -171,14 +181,14 @@ describe('class Contract {}', () => {
});

test('Parsing the tuple in response', async () => {
const result = await contract.get_tuple();
const result = await typeTransContract.get_tuple();
const [res] = result;
expect(res).toStrictEqual([toBN(1), toBN(2), toBN(3)]);
expect(res).toStrictEqual(result.res);
});

test('Parsing the multiple types in response', async () => {
const result = await contract.get_mixed_types();
const result = await typeTransContract.get_mixed_types();
const [tuple, number, array, point] = result;
expect(tuple).toStrictEqual([toBN(1), toBN(2)]);
expect(number).toStrictEqual(toBN(3));
Expand All @@ -196,26 +206,26 @@ describe('class Contract {}', () => {
describe('class ContractFactory {}', () => {
let erc20Address: string;
const wallet = stark.randomAddress();
let erc20DeployPayload: DeployContractPayload;
const account = getTestAccount(provider);
let constructorCalldata;

beforeAll(async () => {
erc20DeployPayload = getERC20DeployPayload(wallet);

const { contract_address, transaction_hash } = await provider.deployContract(
erc20DeployPayload
);
constructorCalldata = [encodeShortString('Token'), encodeShortString('ERC20'), wallet];

await provider.waitForTransaction(transaction_hash);
erc20Address = contract_address;
await account.declareDeploy({
contract: compiledErc20,
classHash: '0x54328a1075b8820eb43caf0caa233923148c983742402dcfc38541dd843d01a',
constructorCalldata,
});
});
test('deployment of new contract', async () => {
const factory = new ContractFactory(compiledErc20, provider);
const erc20 = await factory.deploy(erc20DeployPayload.constructorCalldata);
const erc20 = await factory.deploy(constructorCalldata);
expect(erc20 instanceof Contract);
});
test('wait for deployment transaction', async () => {
const factory = new ContractFactory(compiledErc20, provider);
const contract = await factory.deploy(erc20DeployPayload.constructorCalldata);
const contract = await factory.deploy(constructorCalldata);
expect(contract.deployed()).resolves.not.toThrow();
});
test('attach new contract', async () => {
Expand Down
23 changes: 12 additions & 11 deletions __tests__/defaultProvider.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BlockNumber, GetBlockResponse, stark } from '../src';
import { toBN } from '../src/utils/number';
import { erc20ClassHash, getERC20DeployPayload, getTestProvider } from './fixtures';
import { encodeShortString } from '../src/utils/shortString';
import { compiledErc20, erc20ClassHash, getTestAccount, getTestProvider } from './fixtures';

const { compileCalldata } = stark;

Expand All @@ -9,29 +10,29 @@ const testProvider = getTestProvider();
describe('defaultProvider', () => {
let exampleTransactionHash: string;
let erc20ContractAddress: string;

let exampleBlock: GetBlockResponse;
let exampleBlockNumber: BlockNumber;
let exampleBlockHash: string;
const wallet = stark.randomAddress();
const account = getTestAccount(testProvider);

beforeAll(async () => {
const erc20DeployPayload = getERC20DeployPayload(wallet);
const { deploy } = await account.declareDeploy({
contract: compiledErc20,
classHash: '0x54328a1075b8820eb43caf0caa233923148c983742402dcfc38541dd843d01a',
constructorCalldata: [encodeShortString('Token'), encodeShortString('ERC20'), wallet],
});

const { contract_address, transaction_hash } = await testProvider.deployContract(
erc20DeployPayload
);
await testProvider.waitForTransaction(transaction_hash);
exampleTransactionHash = transaction_hash;
erc20ContractAddress = contract_address;
exampleTransactionHash = deploy.transaction_hash;
erc20ContractAddress = deploy.contract_address;

exampleBlock = await testProvider.getBlock('latest');
exampleBlockHash = exampleBlock.block_hash;
exampleBlockNumber = exampleBlock.block_number;
});

describe('endpoints', () => {
test('deployContract()', () => {
test('declareDeploy()', () => {
expect(erc20ContractAddress).toBeTruthy();
expect(exampleTransactionHash).toBeTruthy();
});
Expand Down Expand Up @@ -76,7 +77,7 @@ describe('defaultProvider', () => {

test('getNonce()', async () => {
const nonce = await testProvider.getNonce(erc20ContractAddress);
return expect(nonce).toEqual('0x0');
return expect(toBN(nonce)).toEqual(toBN('0x0'));
});

test('getClassAt(contractAddress, blockNumber="latest")', async () => {
Expand Down
Loading

0 comments on commit bb50f53

Please sign in to comment.