Skip to content

Commit

Permalink
fix: json formatting and deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
janek26 committed Oct 23, 2021
1 parent 3665897 commit 0fe7e3d
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 17 deletions.
4 changes: 3 additions & 1 deletion __tests__/__snapshots__/utils.browser.test.ts.snap

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion __tests__/__snapshots__/utils.test.ts.snap

Large diffs are not rendered by default.

14 changes: 12 additions & 2 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import starknet, { CompiledContract, compressProgram, randomAddress, makeAddress } from '..';
import compiledArgentAccount from '../__mocks__/ArgentAccount.json';
import fs from 'fs';
import starknet, {
CompiledContract,
compressProgram,
randomAddress,
makeAddress,
JsonParser,
} from '..';

const compiledArgentAccount = JsonParser.parse(
fs.readFileSync('./__mocks__/ArgentAccount.json').toString('ascii')
);

describe('starknet endpoints', () => {
describe('feeder gateway endpoints', () => {
Expand Down
15 changes: 13 additions & 2 deletions __tests__/utils.browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
* @jest-environment jsdom
*/

import { compressProgram, isBrowser } from '..';
import compiledArgentAccount from '../__mocks__/ArgentAccount.json';
import fs from 'fs';
import { compressProgram, isBrowser, JsonParser } from '..';

const compiledArgentAccount = JsonParser.parse(
fs.readFileSync('./__mocks__/ArgentAccount.json').toString('ascii')
);

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

const compressed = compressProgram(inputContract.program);

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

const compressed = compressProgram(inputProgram);

expect(compressed).toMatchSnapshot();
});
});
20 changes: 15 additions & 5 deletions __tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import { compressProgram, makeAddress, isBrowser } from '..';
import compiledArgentAccount from '../__mocks__/ArgentAccount.json';
import fs from 'fs';
import { compressProgram, makeAddress, isBrowser, JsonParser } from '..';

const compiledArgentAccount = JsonParser.parse(
fs.readFileSync('./__mocks__/ArgentAccount.json').toString('ascii')
);

test('isNode', () => {
expect(isBrowser).toBe(false);
});
describe('compressProgram()', () => {
test('compresses a contract program', () => {
const inputContract = compiledArgentAccount as any;
const inputProgram = compiledArgentAccount.program;

const compressed = compressProgram(inputProgram);

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

const compressed = compressProgram(inputContract.program);
const compressed = compressProgram(inputProgram);

expect(compressed).toMatchSnapshot();
});
// there's basically no error case with almost all types being supported
});
describe('makeAddress()', () => {
test('test on eth address', () => {
Expand Down
43 changes: 43 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@semantic-release/npm": "^8.0.2",
"@semantic-release/release-notes-generator": "^10.0.2",
"@types/jest": "^27.0.2",
"@types/json-bigint": "^1.0.1",
"@types/pako": "^1.0.2",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
Expand All @@ -54,6 +55,7 @@
},
"dependencies": {
"axios": "^0.23.0",
"json-bigint": "^1.0.0",
"pako": "^2.0.4"
},
"lint-staged": {
Expand Down
10 changes: 6 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from 'axios';
import { randomAddress, compressProgram } from './utils';
import { randomAddress, compressProgram, JsonParser } from './utils';
import type {
GetBlockResponse,
GetCode,
Expand Down Expand Up @@ -189,12 +189,14 @@ export function addTransaction(tx: Transaction): Promise<AddTransactionResponse>
* @returns a confirmation of sending a transaction on the starknet contract
*/
export function deployContract(
contract: CompiledContract,
contract: CompiledContract | string,
address: string = randomAddress()
): Promise<AddTransactionResponse> {
const parsedContract =
typeof contract === 'string' ? (JsonParser.parse(contract) as CompiledContract) : contract;
const contractDefinition = {
...contract,
program: compressProgram(contract.program),
...parsedContract,
program: compressProgram(parsedContract.program),
};

return addTransaction({
Expand Down
11 changes: 9 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { gzip } from 'pako';
import Json from 'json-bigint';
import { CompressedProgram, Program } from './types';
import { CONTRACT_ADDRESS_LOWER_BOUND, CONTRACT_ADDRESS_UPPER_BOUND } from './constants';

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

export const JsonParser = Json({
alwaysParseAsBig: true,
useNativeBigInt: true,
});

/**
* Function to compress compiled cairo program
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/services/api/gateway/transaction.py#L54-L58)
* @param jsonProgram - json file representing the compiled cairo program
* @returns Compressed cairo program
*/
export function compressProgram(jsonProgram: Program): CompressedProgram {
const stringified = JSON.stringify(jsonProgram);
export function compressProgram(jsonProgram: Program | string): CompressedProgram {
const stringified =
typeof jsonProgram === 'string' ? jsonProgram : JsonParser.stringify(jsonProgram);
const compressedProgram = gzip(stringified);
const base64 = btoaUniversal(compressedProgram);
return base64;
Expand Down

0 comments on commit 0fe7e3d

Please sign in to comment.