Skip to content

Commit

Permalink
fix: pr review
Browse files Browse the repository at this point in the history
  • Loading branch information
janek26 committed Oct 22, 2021
1 parent f3eb0f5 commit 457a852
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 39 deletions.
11 changes: 8 additions & 3 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"node": true,
"jest": true
},
"extends": ["airbnb-base", "prettier", "plugin:prettier/recommended"],
"extends": ["airbnb-base", "prettier", "plugin:prettier/recommended", "plugin:import/typescript"],
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
Expand All @@ -18,7 +18,12 @@
"plugins": ["@typescript-eslint"],
"rules": {
"camelcase": "off",
"import/no-unresolved": "off",
"import/extensions": "off"
"import/extensions": [
"error",
"ignorePackages",
{
"ts": "never"
}
]
}
}
17 changes: 0 additions & 17 deletions __tests__/compression.test.ts

This file was deleted.

12 changes: 8 additions & 4 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ describe('starknet endpoints', () => {
contract_address: randomAddress(),
contract_definition: contractDefinition,
});
expect(response.code).toBe('TRANSACTION_RECEIVED');
expect(response.tx_id).toBeGreaterThan(0);

// I want to show the tx number to the tester, so he/she can trace the transaction in the explorer.
// eslint-disable-next-line no-console
console.log(response);
expect(response.code).toBe('TRANSACTION_RECEIVED');
console.log('txId:', response.tx_id);
});
test('deployContract()', async () => {
const inputContract = compiledArgentAccount as unknown as CompiledContract;
Expand All @@ -55,10 +57,12 @@ describe('starknet endpoints', () => {
inputContract,
makeAddress('0x20b5B1b8aFd65F1FCB755a449000cFC4aBCA0D40')
);
expect(response.code).toBe('TRANSACTION_RECEIVED');
expect(response.tx_id).toBeGreaterThan(0);

// I want to show the tx number to the tester, so he/she can trace the transaction in the explorer.
// eslint-disable-next-line no-console
console.log(response);
expect(response.code).toBe('TRANSACTION_RECEIVED');
console.log('txId:', response.tx_id);
});
});

Expand Down
15 changes: 15 additions & 0 deletions __tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { compressProgram } from '..';
import compiledArgentAccount from '../__mocks__/ArgentAccount.json';

describe('compressProgram()', () => {
test('compresses a contract program', () => {
const inputContract = compiledArgentAccount as any;

const compressed = compressProgram(JSON.stringify(inputContract.program));

expect(compressed).toMatchSnapshot();
});
test('throws Error when no contract program is provided', () => {
expect(() => compressProgram('test')).toThrow();
});
});
21 changes: 9 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import axios from 'axios';
import { gzip } from 'pako';
import { randomAddress, btoaUniversal } from './utils';
import { randomAddress, compressProgram } from './utils';
import type {
GetBlockResponse,
GetCode,
GetContractAddressesResponse,
GetTransactionResponse,
GetTransactionStatusResponse,
CompressedProgram,
Transaction,
AddTransactionResponse,
CompiledContract,
Expand Down Expand Up @@ -183,21 +181,20 @@ export function addTransaction(tx: Transaction): Promise<AddTransactionResponse>
});
}

export function compressProgram(program: string): CompressedProgram {
const json = JSON.parse(program);
const stringified = JSON.stringify(json);
const compressedProgram = gzip(stringified);
const base64 = btoaUniversal(compressedProgram);
return base64;
}

/**
* Deploys a given compiled contract (json) to starknet
*
* @param contract - a json object containing the compiled contract
* @param address - (optional, defaults to a random address) the address where the contract should be deployed (alpha)
* @returns a confirmation of sending a transaction on the starknet contract
*/
export function deployContract(
contract: CompiledContract,
address: string = randomAddress()
): Promise<AddTransactionResponse> {
const contractDefinition = {
...contract,
program: compressProgram(JSON.stringify(contract.program)),
program: compressProgram(contract.program),
};

return addTransaction({
Expand Down
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ export interface Abi {
outputs: { name: string; type: string }[];
type: string;
}
export type EntryPointsByType = any;
export type Program = any;
export type EntryPointsByType = object;
export type Program = object;

export interface CompiledContract {
abi: Abi;
entry_points_by_type: EntryPointsByType;
program: Program;
}

export interface CompressedCompiledContract extends CompiledContract {
export interface CompressedCompiledContract extends Omit<CompiledContract, 'program'> {
program: CompressedProgram;
}

Expand Down
9 changes: 9 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { gzip } from 'pako';
import { CompressedProgram, Program } from './types';
import { CONTRACT_ADDRESS_LOWER_BOUND, CONTRACT_ADDRESS_UPPER_BOUND } from './constants';

const isBrowser = typeof window !== 'undefined';
Expand All @@ -19,3 +21,10 @@ export function randomAddress(): string {
export function makeAddress(input: string): string {
return `0x${input.replace(/^0x/, '').toLowerCase()}`;
}

export function compressProgram(jsonProgram: Program): CompressedProgram {
const stringified = JSON.stringify(jsonProgram);
const compressedProgram = gzip(stringified);
const base64 = btoaUniversal(compressedProgram);
return base64;
}

0 comments on commit 457a852

Please sign in to comment.