Skip to content

Commit 0fe7e3d

Browse files
committed
fix: json formatting and deploy
1 parent 3665897 commit 0fe7e3d

File tree

9 files changed

+106
-17
lines changed

9 files changed

+106
-17
lines changed

__tests__/__snapshots__/utils.browser.test.ts.snap

Lines changed: 3 additions & 1 deletion
Large diffs are not rendered by default.

__tests__/__snapshots__/utils.test.ts.snap

Lines changed: 3 additions & 1 deletion
Large diffs are not rendered by default.

__tests__/index.test.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1-
import starknet, { CompiledContract, compressProgram, randomAddress, makeAddress } from '..';
2-
import compiledArgentAccount from '../__mocks__/ArgentAccount.json';
1+
import fs from 'fs';
2+
import starknet, {
3+
CompiledContract,
4+
compressProgram,
5+
randomAddress,
6+
makeAddress,
7+
JsonParser,
8+
} from '..';
9+
10+
const compiledArgentAccount = JsonParser.parse(
11+
fs.readFileSync('./__mocks__/ArgentAccount.json').toString('ascii')
12+
);
313

414
describe('starknet endpoints', () => {
515
describe('feeder gateway endpoints', () => {

__tests__/utils.browser.test.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22
* @jest-environment jsdom
33
*/
44

5-
import { compressProgram, isBrowser } from '..';
6-
import compiledArgentAccount from '../__mocks__/ArgentAccount.json';
5+
import fs from 'fs';
6+
import { compressProgram, isBrowser, JsonParser } from '..';
7+
8+
const compiledArgentAccount = JsonParser.parse(
9+
fs.readFileSync('./__mocks__/ArgentAccount.json').toString('ascii')
10+
);
711

812
test('isBrowser', () => {
913
expect(isBrowser).toBe(true);
@@ -14,6 +18,13 @@ describe('compressProgram()', () => {
1418

1519
const compressed = compressProgram(inputContract.program);
1620

21+
expect(compressed).toMatchSnapshot();
22+
});
23+
test('works with strings', () => {
24+
const inputProgram = JsonParser.stringify(compiledArgentAccount.program);
25+
26+
const compressed = compressProgram(inputProgram);
27+
1728
expect(compressed).toMatchSnapshot();
1829
});
1930
});

__tests__/utils.test.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
1-
import { compressProgram, makeAddress, isBrowser } from '..';
2-
import compiledArgentAccount from '../__mocks__/ArgentAccount.json';
1+
import fs from 'fs';
2+
import { compressProgram, makeAddress, isBrowser, JsonParser } from '..';
3+
4+
const compiledArgentAccount = JsonParser.parse(
5+
fs.readFileSync('./__mocks__/ArgentAccount.json').toString('ascii')
6+
);
37

48
test('isNode', () => {
59
expect(isBrowser).toBe(false);
610
});
711
describe('compressProgram()', () => {
812
test('compresses a contract program', () => {
9-
const inputContract = compiledArgentAccount as any;
13+
const inputProgram = compiledArgentAccount.program;
14+
15+
const compressed = compressProgram(inputProgram);
16+
17+
expect(compressed).toMatchSnapshot();
18+
});
19+
test('works with strings', () => {
20+
const inputProgram = JsonParser.stringify(compiledArgentAccount.program);
1021

11-
const compressed = compressProgram(inputContract.program);
22+
const compressed = compressProgram(inputProgram);
1223

1324
expect(compressed).toMatchSnapshot();
1425
});
15-
// there's basically no error case with almost all types being supported
1626
});
1727
describe('makeAddress()', () => {
1828
test('test on eth address', () => {

package-lock.json

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"@semantic-release/npm": "^8.0.2",
3737
"@semantic-release/release-notes-generator": "^10.0.2",
3838
"@types/jest": "^27.0.2",
39+
"@types/json-bigint": "^1.0.1",
3940
"@types/pako": "^1.0.2",
4041
"@typescript-eslint/eslint-plugin": "^5.0.0",
4142
"@typescript-eslint/parser": "^5.0.0",
@@ -54,6 +55,7 @@
5455
},
5556
"dependencies": {
5657
"axios": "^0.23.0",
58+
"json-bigint": "^1.0.0",
5759
"pako": "^2.0.4"
5860
},
5961
"lint-staged": {

src/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import axios from 'axios';
2-
import { randomAddress, compressProgram } from './utils';
2+
import { randomAddress, compressProgram, JsonParser } from './utils';
33
import type {
44
GetBlockResponse,
55
GetCode,
@@ -189,12 +189,14 @@ export function addTransaction(tx: Transaction): Promise<AddTransactionResponse>
189189
* @returns a confirmation of sending a transaction on the starknet contract
190190
*/
191191
export function deployContract(
192-
contract: CompiledContract,
192+
contract: CompiledContract | string,
193193
address: string = randomAddress()
194194
): Promise<AddTransactionResponse> {
195+
const parsedContract =
196+
typeof contract === 'string' ? (JsonParser.parse(contract) as CompiledContract) : contract;
195197
const contractDefinition = {
196-
...contract,
197-
program: compressProgram(contract.program),
198+
...parsedContract,
199+
program: compressProgram(parsedContract.program),
198200
};
199201

200202
return addTransaction({

src/utils.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { gzip } from 'pako';
2+
import Json from 'json-bigint';
23
import { CompressedProgram, Program } from './types';
34
import { CONTRACT_ADDRESS_LOWER_BOUND, CONTRACT_ADDRESS_UPPER_BOUND } from './constants';
45

@@ -22,15 +23,21 @@ export function makeAddress(input: string): string {
2223
return `0x${input.replace(/^0x/, '').toLowerCase()}`;
2324
}
2425

26+
export const JsonParser = Json({
27+
alwaysParseAsBig: true,
28+
useNativeBigInt: true,
29+
});
30+
2531
/**
2632
* Function to compress compiled cairo program
2733
*
2834
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/services/api/gateway/transaction.py#L54-L58)
2935
* @param jsonProgram - json file representing the compiled cairo program
3036
* @returns Compressed cairo program
3137
*/
32-
export function compressProgram(jsonProgram: Program): CompressedProgram {
33-
const stringified = JSON.stringify(jsonProgram);
38+
export function compressProgram(jsonProgram: Program | string): CompressedProgram {
39+
const stringified =
40+
typeof jsonProgram === 'string' ? jsonProgram : JsonParser.stringify(jsonProgram);
3441
const compressedProgram = gzip(stringified);
3542
const base64 = btoaUniversal(compressedProgram);
3643
return base64;

0 commit comments

Comments
 (0)