Skip to content

Commit

Permalink
feat: add tests and make RawArgs more flexible
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvkelawala committed Jan 24, 2023
1 parent 073be06 commit db9f274
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 22 deletions.
5 changes: 5 additions & 0 deletions __tests__/utils/__snapshots__/stark.test.ts.snap

Large diffs are not rendered by default.

65 changes: 65 additions & 0 deletions __tests__/utils/stark.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import fs from 'fs';

import { RawArgs, json, stark } from '../../src';
import { toBigInt, toHex } from '../../src/utils/number';

const compiledAccount = json.parse(fs.readFileSync('./__mocks__/Account.json').toString('ascii'));

describe('stark', () => {
describe('compressProgram()', () => {
test('compresses a contract program', () => {
const inputProgram = compiledAccount.program;

const compressed = stark.compressProgram(inputProgram);

expect(compressed).toMatchSnapshot();
});
test('works with strings', () => {
const inputProgram = json.stringify(compiledAccount.program);

const compressed = stark.compressProgram(inputProgram);

expect(compressed).toMatchSnapshot();
});
});

describe('compileCalldata() ', () => {
test('compiles BigNumberish[] calldata', () => {
const callData = ['0x000', 2n, 10000];

const compiled = stark.compileCalldata(callData);

expect(compiled).toEqual(['0', '2', '10000']);
});

test('compiles Object type calldata', () => {
const callData = {
a: '0x000',
b: 2n,
c: 10000,
d: [1n, 2n, '0x3'],
};

const compiled = stark.compileCalldata(callData);

expect(compiled).toEqual(['0', '2', '10000', '3', '1', '2', '3']);
});

test('compiles Struct type calldata', () => {
const calldata: RawArgs = {
a: {
type: 'struct',
x: 1n,
y: 2n,
z: 3n,
},
b: 10000000000,
c: ['1', '2', toBigInt(3), toHex(4)],
};

const compiled = stark.compileCalldata(calldata);

expect(compiled).toEqual(['1', '2', '3', '10000000000', '4', '1', '2', '3', '4']);
});
});
});
22 changes: 1 addition & 21 deletions __tests__/utils/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,13 @@
import fs from 'fs';

import { pedersen } from '@noble/curves/stark';

import { constants, hash, json, number, stark } from '../../src';
import { constants, hash, number, stark } from '../../src';
import { Block } from '../../src/provider/utils';

const { IS_BROWSER } = constants;

const compiledAccount = json.parse(fs.readFileSync('./__mocks__/Account.json').toString('ascii'));

test('isNode', () => {
expect(IS_BROWSER).toBe(false);
});
describe('compressProgram()', () => {
test('compresses a contract program', () => {
const inputProgram = compiledAccount.program;

const compressed = stark.compressProgram(inputProgram);

expect(compressed).toMatchSnapshot();
});
test('works with strings', () => {
const inputProgram = json.stringify(compiledAccount.program);

const compressed = stark.compressProgram(inputProgram);

expect(compressed).toMatchSnapshot();
});
});

describe('hexToDecimalString()', () => {
test('parse 0xa23', () => {
Expand Down
5 changes: 4 additions & 1 deletion src/types/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ export type RawCalldata = BigNumberish[];
export type AllowArray<T> = T | T[];
export type RawArgs =
| {
[inputName: string]: string | string[] | { type: 'struct'; [k: string]: BigNumberish };
[inputName: string]:
| BigNumberish
| BigNumberish[]
| { type: 'struct'; [k: string]: BigNumberish };
}
| BigNumberish[];

Expand Down

0 comments on commit db9f274

Please sign in to comment.