Skip to content

Commit

Permalink
feat(contract): new contract methods signature
Browse files Browse the repository at this point in the history
  • Loading branch information
MilGard91 committed Mar 10, 2022
1 parent 9273f54 commit cc87943
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 199 deletions.
20 changes: 5 additions & 15 deletions __tests__/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ describe('deploy and test Wallet', () => {
const contract = new Contract(compiledArgentAccount.abi, accountResponse.address);
expect(accountResponse.code).toBe('TRANSACTION_RECEIVED');

const initializeResponse = await contract.invoke('initialize', {
signer: starkKeyPub,
guardian: '0',
});
const initializeResponse = await contract.initialize(starkKeyPub, '0');
expect(initializeResponse.code).toBe('TRANSACTION_RECEIVED');

account = new Account(defaultProvider, accountResponse.address, starkKeyPair);
Expand All @@ -36,10 +33,7 @@ describe('deploy and test Wallet', () => {
erc20 = new Contract(compiledErc20.abi, erc20Address);
expect(erc20Response.code).toBe('TRANSACTION_RECEIVED');

const mintResponse = await erc20.invoke('mint', {
recipient: account.address,
amount: '1000',
});
const mintResponse = await erc20.mint(account.address, '1000');
expect(mintResponse.code).toBe('TRANSACTION_RECEIVED');

const dappResponse = await defaultProvider.deployContract({
Expand All @@ -65,9 +59,7 @@ describe('deploy and test Wallet', () => {
});

test('read balance of wallet', async () => {
const { res } = await erc20.call('balance_of', {
user: account.address,
});
const { res } = await erc20.balance_of(account.address);

expect(number.toBN(res as string).toString()).toStrictEqual(number.toBN(1000).toString());
});
Expand All @@ -84,9 +76,7 @@ describe('deploy and test Wallet', () => {
});

test('read balance of wallet after transfer', async () => {
const { res } = await erc20.call('balance_of', {
user: account.address,
});
const { res } = await erc20.balance_of(account.address);

expect(number.toBN(res as string).toString()).toStrictEqual(number.toBN(990).toString());
});
Expand Down Expand Up @@ -128,7 +118,7 @@ describe('deploy and test Wallet', () => {
expect(code).toBe('TRANSACTION_RECEIVED');
await defaultProvider.waitForTransaction(transaction_hash);

const response = await dapp.call('get_number', { user: account.address });
const response = await dapp.get_number(account.address);
expect(toBN(response.number as string).toString()).toStrictEqual('57');
});

Expand Down
36 changes: 12 additions & 24 deletions __tests__/accountContract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ describe('deploy and test Wallet', () => {
accountContract = new Contract(compiledArgentAccount.abi, accountResponse.address);
expect(accountResponse.code).toBe('TRANSACTION_RECEIVED');

const initializeResponse = await accountContract.invoke('initialize', {
signer: starkKeyPub,
guardian: '0',
});
const initializeResponse = await accountContract.initialize(starkKeyPub, '0');
expect(initializeResponse.code).toBe('TRANSACTION_RECEIVED');

const erc20Response = await defaultProvider.deployContract({
Expand All @@ -51,30 +48,25 @@ describe('deploy and test Wallet', () => {
erc20 = new Contract(compiledErc20.abi, erc20Address);
expect(erc20Response.code).toBe('TRANSACTION_RECEIVED');

const mintResponse = await erc20.invoke('mint', {
recipient: accountContract.address,
amount: '1000',
});
const mintResponse = await erc20.mint(accountContract.address, '1000');
expect(mintResponse.code).toBe('TRANSACTION_RECEIVED');
await defaultProvider.waitForTransaction(mintResponse.transaction_hash);
});

test('read nonce', async () => {
const { nonce } = await accountContract.call('get_nonce');
const { nonce } = await accountContract.get_nonce();

expect(number.toBN(nonce as string).toString()).toStrictEqual(number.toBN(0).toString());
});

test('read balance of wallet', async () => {
const { res } = await erc20.call('balance_of', {
user: accountContract.address,
});
const { res } = await erc20.balance_of(accountContract.address);

expect(number.toBN(res as string).toString()).toStrictEqual(number.toBN(1000).toString());
expect(res).toStrictEqual(number.toBN(1000));
});

test('execute by wallet owner', async () => {
const nonce = (await accountContract.call('get_nonce')).nonce.toString();
const nonce = (await accountContract.get_nonce()).nonce.toString();

const calls = [
{ contractAddress: erc20Address, entrypoint: 'transfer', calldata: [erc20Address, '10'] },
Expand All @@ -84,13 +76,11 @@ describe('deploy and test Wallet', () => {
const { callArray, calldata } = transformCallsToMulticallArrays(calls);

const signature = ec.sign(starkKeyPair, msgHash);
const { code, transaction_hash } = await accountContract.invoke(
'__execute__',
{
call_array: callArray,
calldata,
nonce,
},
// eslint-disable-next-line no-underscore-dangle
const { code, transaction_hash } = await accountContract.__execute__(
callArray,
calldata,
nonce,
signature
);

Expand All @@ -100,9 +90,7 @@ describe('deploy and test Wallet', () => {
});

test('read balance of wallet after transfer', async () => {
const { res } = await erc20.call('balance_of', {
user: accountContract.address,
});
const { res } = await erc20.balance_of(accountContract.address);

expect(number.toBN(res as string).toString()).toStrictEqual(number.toBN(990).toString());
});
Expand Down
118 changes: 52 additions & 66 deletions __tests__/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ describe('class Contract {}', () => {
erc20 = new Contract(compiledErc20.abi, address, defaultProvider);
expect(code).toBe('TRANSACTION_RECEIVED');
await defaultProvider.waitForTransaction(transaction_hash);

// Deploy Multicall

const {
Expand All @@ -44,32 +43,32 @@ describe('class Contract {}', () => {
});

test('populate transaction for initial balance of that account', async () => {
const res = await erc20.populateTransaction.balance_of({ user: wallet });
const res = await erc20.populateTransaction.balance_of(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.balance_of({ user: wallet });
const result = await erc20.balance_of(wallet);
const [res] = result;
expect(res).toStrictEqual(toBN(0));
expect(res).toStrictEqual(result.res);
});

test('add 10 test ERC20 to account', async () => {
const response = await erc20.mint({
recipient: wallet,
amount: '10',
});
const response = await erc20.mint(wallet, '10');
expect(response.code).toBe('TRANSACTION_RECEIVED');

await defaultProvider.waitForTransaction(response.transaction_hash);
});

test('read balance after mint of that account', async () => {
const { res } = await erc20.balance_of({ user: wallet });

const result = await erc20.balance_of(wallet);
const [res] = result;
expect(res).toStrictEqual(toBN(10));
expect(res).toStrictEqual(result.res);
});

test('read balance in a multicall', async () => {
Expand All @@ -86,10 +85,13 @@ describe('class Contract {}', () => {
Object.keys(args2).length,
...compileCalldata(args2),
];
const { block_number, result } = await contract.aggregate({ calls });
const result = await contract.aggregate(calls);
const [block_number, res] = result;
expect(isBN(block_number));
expect(Array.isArray(result));
(result as BigNumberish[]).forEach((el) => expect(isBN(el)));
expect(Array.isArray(res));
(res as BigNumberish[]).forEach((el) => expect(isBN(el)));
expect(block_number).toStrictEqual(result.block_number);
expect(res).toStrictEqual(result.result);
});
});

Expand All @@ -107,54 +109,37 @@ describe('class Contract {}', () => {

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

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

test('Parsing the struct in request', async () => {
return expect(
contract.request_struct({
str: { x: 1, y: 2 },
})
).resolves.not.toThrow();
return expect(contract.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({ str: [{ x: 1, y: 2 }] })
).resolves.not.toThrow();
return expect(contract.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({
str: {
p1: { x: 1, y: 2 },
p2: { x: 3, y: 4 },
extra: 5,
},
p1: { x: 1, y: 2 },
p2: { x: 3, y: 4 },
extra: 5,
})
).resolves.not.toThrow();
});

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

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

Expand All @@ -165,40 +150,55 @@ describe('class Contract {}', () => {
});

test('Parsing the array of felt in response', async () => {
const { res } = await contract.get_array_of_felts();
const result = await contract.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 { res } = await contract.get_struct();
const result = await contract.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 { res } = await contract.get_array_of_structs();
const result = await contract.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 { res } = await contract.get_nested_structs();
const result = await contract.get_nested_structs();
const [res] = result;
expect(res).toStrictEqual({
p1: { x: toBN(1), y: toBN(2) },
p2: { x: toBN(3), y: toBN(4) },
extra: toBN(5),
});
expect(res).toStrictEqual(result.res);
});

test('Parsing the tuple in response', async () => {
const { res } = await contract.get_tuple();
const result = await contract.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 { tuple, number, array, point } = await contract.get_mixed_types();
const result = await contract.get_mixed_types();
const [tuple, number, array, point] = result;
expect(tuple).toStrictEqual([toBN(1), toBN(2)]);
expect(number).toStrictEqual(toBN(3));
expect(array).toStrictEqual([toBN(4)]);
expect(point).toStrictEqual({ x: toBN(1), y: toBN(2) });
expect(tuple).toStrictEqual(result.tuple);
expect(number).toStrictEqual(result.number);
expect(array).toStrictEqual(result.array);
expect(point).toStrictEqual(result.point);
});
});
});
Expand All @@ -220,10 +220,7 @@ describe('class Contract {}', () => {
const contract = new Contract(compiledArgentAccount.abi, accountResponse.address);
expect(accountResponse.code).toBe('TRANSACTION_RECEIVED');

const initializeResponse = await contract.initialize({
signer: starkKeyPub,
guardian: '0',
});
const initializeResponse = await contract.initialize(starkKeyPub, '0');
expect(initializeResponse.code).toBe('TRANSACTION_RECEIVED');

account = new Account(defaultProvider, accountResponse.address, starkKeyPair);
Expand All @@ -236,20 +233,16 @@ describe('class Contract {}', () => {
await defaultProvider.waitForTransaction(erc20Response.transaction_hash);
expect(erc20Response.code).toBe('TRANSACTION_RECEIVED');

const mintResponse = await erc20.mint({
recipient: account.address,
amount: '1000',
});
const mintResponse = await erc20.mint(account.address, '1000');

await defaultProvider.waitForTransaction(mintResponse.transaction_hash);
});

test('read balance of wallet', async () => {
const { res } = await erc20.balance_of({
user: account.address,
});

const result = await erc20.balance_of(account.address);
const [res] = result;
expect(res).toStrictEqual(toBN(1000));
expect(res).toStrictEqual(result.res);
});

test('change from provider to account', async () => {
Expand All @@ -259,23 +252,16 @@ describe('class Contract {}', () => {
});

test('read balance of wallet', async () => {
const { res } = await erc20.balance_of({
user: account.address,
});
const { res } = await erc20.balance_of(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,
});
const { transaction_hash, code } = await erc20.transfer(toBN(erc20Address).toString(), 10);
expect(code).toBe('TRANSACTION_RECEIVED');
await defaultProvider.waitForTransaction(transaction_hash);
const { res } = await erc20.balance_of({
user: account.address,
});
const { res } = await erc20.balance_of(account.address);
expect(res).toStrictEqual(toBN(990));
});
});
Expand Down
Loading

0 comments on commit cc87943

Please sign in to comment.