Skip to content

Commit

Permalink
feat(contract): adding parse request data on call and invoke
Browse files Browse the repository at this point in the history
  • Loading branch information
MilGard91 committed Feb 11, 2022
1 parent 60d5eb6 commit e6d46dc
Show file tree
Hide file tree
Showing 10 changed files with 28,335 additions and 10,274 deletions.
38,247 changes: 28,072 additions & 10,175 deletions __mocks__/contract.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion __tests__/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
Account,
CompiledContract,
Contract,
compileCalldata,
defaultProvider,
ec,
json,
Expand All @@ -14,6 +13,8 @@ import {
} from '../src';
import { toBN } from '../src/utils/number';

const { compileCalldata } = stark;

const compiledArgentAccount: CompiledContract = json.parse(
fs.readFileSync('./__mocks__/ArgentAccount.json').toString('ascii')
);
Expand Down
3 changes: 2 additions & 1 deletion __tests__/accountContract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import fs from 'fs';
import {
CompiledContract,
Contract,
compileCalldata,
defaultProvider,
ec,
encode,
Expand All @@ -13,6 +12,8 @@ import {
stark,
} from '../src';

const { compileCalldata } = stark;

describe('getStarkAccountFromPrivateKey()', () => {
test('it works with valid privateKey', () => {
const privateKey = '0xb696427c0d79c5d28a1fa6f748bae1b98b3f4b86bd1a2505bab144673c856fa9';
Expand Down
116 changes: 83 additions & 33 deletions __tests__/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('class Contract {}', () => {
expect(res).toStrictEqual(toBN(10));
});
});
describe('Response Type Transformation', () => {
describe('Type Transformation', () => {
let contract: Contract;
beforeAll(async () => {
const { code, transaction_hash, address } = await defaultProvider.deployContract({
Expand All @@ -55,40 +55,90 @@ describe('class Contract {}', () => {
expect(code).toBe('TRANSACTION_RECEIVED');
await defaultProvider.waitForTx(transaction_hash);
});
test('Parsing the felt in response', async () => {
const { res } = await contract.call('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');
expect(res).toStrictEqual([toBN(4), toBN(5)]);
});
test('Parsing the array of structs in response', async () => {
const { res } = await contract.call('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');
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');
expect(res).toStrictEqual({
p1: { x: toBN(1), y: toBN(2) },
p2: { x: toBN(3), y: toBN(4) },
extra: toBN(5),
describe('Request Type Transformation', () => {
test('Parsing the felt in request', async () => {
return expect(contract.call('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();
});
test('Parsing the struct in request', async () => {
return expect(
contract.call('request_struct', {
str: { x: 1, y: 2 },
})
).resolves.not.toThrow();
});
test('Parsing the array of structs in request', async () => {
return expect(
contract.call('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', {
str: {
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.call('request_tuple', { tup: [1, 2] })).resolves.not.toThrow();
});
test('Parsing the multiple types in request', async () => {
return expect(
contract.call('request_mixed_types', {
num: 2,
point: {
x: 1,
y: 2,
},
arr: [1],
})
).resolves.not.toThrow();
});
});
test('Parsing the tuple in response', async () => {
const { res } = await contract.call('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_all');
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) });
describe('Response Type Transformation', () => {
test('Parsing the felt in response', async () => {
const { res } = await contract.call('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');
expect(res).toStrictEqual([toBN(4), toBN(5)]);
});
test('Parsing the array of structs in response', async () => {
const { res } = await contract.call('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');
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');
expect(res).toStrictEqual({
p1: { x: toBN(1), y: toBN(2) },
p2: { x: toBN(3), y: toBN(4) },
extra: toBN(5),
});
});
test('Parsing the tuple in response', async () => {
const { res } = await contract.call('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');
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) });
});
});
});
});
4 changes: 3 additions & 1 deletion __tests__/provider.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import fs from 'fs';

import { CompiledContract, compileCalldata, defaultProvider, json, stark } from '../src';
import { CompiledContract, defaultProvider, json, stark } from '../src';

const { compileCalldata } = stark;

const compiledArgentAccount = json.parse(
fs.readFileSync('./__mocks__/ArgentAccount.json').toString('ascii')
Expand Down
3 changes: 1 addition & 2 deletions src/account/default.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { compileCalldata } from '../contract';
import { Provider } from '../provider';
import { Signer, SignerInterface } from '../signer';
import {
Expand All @@ -10,7 +9,7 @@ import {
Signature,
} from '../types';
import { BigNumberish, bigNumberishArrayToDecimalStringArray, toBN, toHex } from '../utils/number';
import { getSelectorFromName } from '../utils/stark';
import { compileCalldata, getSelectorFromName } from '../utils/stark';
import { TypedData, getMessageHash } from '../utils/typedData';
import { AccountInterface } from './interface';

Expand Down
Loading

0 comments on commit e6d46dc

Please sign in to comment.