Skip to content

Commit 457a852

Browse files
committed
fix: pr review
1 parent f3eb0f5 commit 457a852

File tree

7 files changed

+52
-39
lines changed

7 files changed

+52
-39
lines changed

.eslintrc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"node": true,
66
"jest": true
77
},
8-
"extends": ["airbnb-base", "prettier", "plugin:prettier/recommended"],
8+
"extends": ["airbnb-base", "prettier", "plugin:prettier/recommended", "plugin:import/typescript"],
99
"globals": {
1010
"Atomics": "readonly",
1111
"SharedArrayBuffer": "readonly"
@@ -18,7 +18,12 @@
1818
"plugins": ["@typescript-eslint"],
1919
"rules": {
2020
"camelcase": "off",
21-
"import/no-unresolved": "off",
22-
"import/extensions": "off"
21+
"import/extensions": [
22+
"error",
23+
"ignorePackages",
24+
{
25+
"ts": "never"
26+
}
27+
]
2328
}
2429
}

__tests__/compression.test.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

__tests__/index.test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ describe('starknet endpoints', () => {
4343
contract_address: randomAddress(),
4444
contract_definition: contractDefinition,
4545
});
46+
expect(response.code).toBe('TRANSACTION_RECEIVED');
47+
expect(response.tx_id).toBeGreaterThan(0);
4648

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

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

__tests__/utils.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { compressProgram } from '..';
2+
import compiledArgentAccount from '../__mocks__/ArgentAccount.json';
3+
4+
describe('compressProgram()', () => {
5+
test('compresses a contract program', () => {
6+
const inputContract = compiledArgentAccount as any;
7+
8+
const compressed = compressProgram(JSON.stringify(inputContract.program));
9+
10+
expect(compressed).toMatchSnapshot();
11+
});
12+
test('throws Error when no contract program is provided', () => {
13+
expect(() => compressProgram('test')).toThrow();
14+
});
15+
});

src/index.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import axios from 'axios';
2-
import { gzip } from 'pako';
3-
import { randomAddress, btoaUniversal } from './utils';
2+
import { randomAddress, compressProgram } from './utils';
43
import type {
54
GetBlockResponse,
65
GetCode,
76
GetContractAddressesResponse,
87
GetTransactionResponse,
98
GetTransactionStatusResponse,
10-
CompressedProgram,
119
Transaction,
1210
AddTransactionResponse,
1311
CompiledContract,
@@ -183,21 +181,20 @@ export function addTransaction(tx: Transaction): Promise<AddTransactionResponse>
183181
});
184182
}
185183

186-
export function compressProgram(program: string): CompressedProgram {
187-
const json = JSON.parse(program);
188-
const stringified = JSON.stringify(json);
189-
const compressedProgram = gzip(stringified);
190-
const base64 = btoaUniversal(compressedProgram);
191-
return base64;
192-
}
193-
184+
/**
185+
* Deploys a given compiled contract (json) to starknet
186+
*
187+
* @param contract - a json object containing the compiled contract
188+
* @param address - (optional, defaults to a random address) the address where the contract should be deployed (alpha)
189+
* @returns a confirmation of sending a transaction on the starknet contract
190+
*/
194191
export function deployContract(
195192
contract: CompiledContract,
196193
address: string = randomAddress()
197194
): Promise<AddTransactionResponse> {
198195
const contractDefinition = {
199196
...contract,
200-
program: compressProgram(JSON.stringify(contract.program)),
197+
program: compressProgram(contract.program),
201198
};
202199

203200
return addTransaction({

src/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ export interface Abi {
1515
outputs: { name: string; type: string }[];
1616
type: string;
1717
}
18-
export type EntryPointsByType = any;
19-
export type Program = any;
18+
export type EntryPointsByType = object;
19+
export type Program = object;
2020

2121
export interface CompiledContract {
2222
abi: Abi;
2323
entry_points_by_type: EntryPointsByType;
2424
program: Program;
2525
}
2626

27-
export interface CompressedCompiledContract extends CompiledContract {
27+
export interface CompressedCompiledContract extends Omit<CompiledContract, 'program'> {
2828
program: CompressedProgram;
2929
}
3030

src/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { gzip } from 'pako';
2+
import { CompressedProgram, Program } from './types';
13
import { CONTRACT_ADDRESS_LOWER_BOUND, CONTRACT_ADDRESS_UPPER_BOUND } from './constants';
24

35
const isBrowser = typeof window !== 'undefined';
@@ -19,3 +21,10 @@ export function randomAddress(): string {
1921
export function makeAddress(input: string): string {
2022
return `0x${input.replace(/^0x/, '').toLowerCase()}`;
2123
}
24+
25+
export function compressProgram(jsonProgram: Program): CompressedProgram {
26+
const stringified = JSON.stringify(jsonProgram);
27+
const compressedProgram = gzip(stringified);
28+
const base64 = btoaUniversal(compressedProgram);
29+
return base64;
30+
}

0 commit comments

Comments
 (0)