starknet 0.1.2 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/.babelrc +6 -0
  2. package/.commitlintrc +12 -0
  3. package/.eslintignore +2 -0
  4. package/.eslintrc +20 -0
  5. package/.github/workflows/pr.yml +23 -0
  6. package/.github/workflows/release.yml +40 -0
  7. package/.husky/commit-msg +4 -0
  8. package/.husky/pre-commit +4 -0
  9. package/{.prettierrc.json → .prettierrc} +0 -1
  10. package/.releaserc +28 -0
  11. package/CHANGELOG.md +49 -0
  12. package/CONTRIBUTING.md +52 -0
  13. package/README.md +39 -11
  14. package/__mocks__/ArgentAccount.json +92620 -0
  15. package/__tests__/__snapshots__/utils.browser.test.ts.snap +5 -0
  16. package/__tests__/__snapshots__/utils.test.ts.snap +5 -0
  17. package/__tests__/index.test.ts +49 -4
  18. package/__tests__/utils.browser.test.ts +30 -0
  19. package/__tests__/utils.test.ts +35 -0
  20. package/constants.d.ts +3 -0
  21. package/constants.js +9 -0
  22. package/dist/constants.d.ts +3 -0
  23. package/dist/constants.js +6 -0
  24. package/dist/index.d.ts +23 -9
  25. package/dist/index.js +51 -5
  26. package/dist/types.d.ts +89 -0
  27. package/dist/types.js +2 -0
  28. package/dist/utils.d.ts +21 -0
  29. package/dist/utils.js +44 -0
  30. package/docs/README.md +235 -0
  31. package/index.d.ts +115 -0
  32. package/index.js +261 -0
  33. package/package.json +29 -5
  34. package/src/constants.ts +3 -0
  35. package/src/index.ts +67 -21
  36. package/src/types.ts +95 -0
  37. package/src/utils.ts +44 -0
  38. package/tsconfig.eslint.json +4 -0
  39. package/tsconfig.json +15 -13
  40. package/types.d.ts +94 -0
  41. package/types.js +2 -0
  42. package/utils.d.ts +29 -0
  43. package/utils.js +62 -0
  44. package/.eslintrc.json +0 -26
  45. package/babel.config.js +0 -3
  46. package/dist/__tests__/index.test.d.ts +0 -1
  47. package/dist/__tests__/index.test.js +0 -49
  48. package/docs/Home.md +0 -220
  49. package/docs/_Sidebar.md +0 -3
package/src/index.ts CHANGED
@@ -1,26 +1,38 @@
1
1
  import axios from 'axios';
2
+ import { randomAddress, compressProgram, JsonParser } from './utils';
3
+ import type {
4
+ GetBlockResponse,
5
+ GetCode,
6
+ GetContractAddressesResponse,
7
+ GetTransactionResponse,
8
+ GetTransactionStatusResponse,
9
+ Transaction,
10
+ AddTransactionResponse,
11
+ CompiledContract,
12
+ } from './types';
2
13
 
3
- const API_URL: string = 'https://alpha2.starknet.io/';
4
- const FEEDER_GATEWAY_URL: string = `${API_URL}/feeder_gateway`;
5
- const GATEWAY_URL: string = `${API_URL}/gateway`;
14
+ const API_URL = 'https://alpha2.starknet.io';
15
+ const FEEDER_GATEWAY_URL = `${API_URL}/feeder_gateway`;
16
+ const GATEWAY_URL = `${API_URL}/gateway`;
6
17
 
7
18
  /**
8
19
  * Gets the smart contract address on the goerli testnet.
9
20
  *
10
- * https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L13-L15
11
- * @returns starknet smart contract address
21
+ * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L13-L15)
22
+ * @returns starknet smart contract addresses
12
23
  */
13
- export function getContractAddresses(): Promise<object> {
24
+ export function getContractAddresses(): Promise<GetContractAddressesResponse> {
14
25
  return new Promise((resolve, reject) => {
15
26
  axios
16
- .get(`${FEEDER_GATEWAY_URL}/get_contract_addresses`)
17
- .then((resp: any) => {
27
+ .get<GetContractAddressesResponse>(`${FEEDER_GATEWAY_URL}/get_contract_addresses`)
28
+ .then((resp) => {
18
29
  resolve(resp.data);
19
30
  })
20
31
  .catch(reject);
21
32
  });
22
33
  }
23
34
 
35
+ // TODO: add proper type
24
36
  /**
25
37
  * Calls a function on the StarkNet contract.
26
38
  *
@@ -49,10 +61,10 @@ export function callContract(invokeTx: object, blockId: number): Promise<object>
49
61
  * @param blockId
50
62
  * @returns the block object { block_id, previous_block_id, state_root, status, timestamp, transaction_receipts, transactions }
51
63
  */
52
- export function getBlock(blockId: number): Promise<object> {
64
+ export function getBlock(blockId: number): Promise<GetBlockResponse> {
53
65
  return new Promise((resolve, reject) => {
54
66
  axios
55
- .get(`${FEEDER_GATEWAY_URL}/get_block?blockId=${blockId}`)
67
+ .get<GetBlockResponse>(`${FEEDER_GATEWAY_URL}/get_block?blockId=${blockId}`)
56
68
  .then((resp: any) => {
57
69
  resolve(resp.data);
58
70
  })
@@ -67,19 +79,22 @@ export function getBlock(blockId: number): Promise<object> {
67
79
  *
68
80
  * @param contractAddress
69
81
  * @param blockId
70
- * @returns ABI of compiled contract in JSON
82
+ * @returns Bytecode and ABI of compiled contract
71
83
  */
72
- export function getCode(contractAddress: string, blockId: number): Promise<object> {
84
+ export function getCode(contractAddress: string, blockId: number): Promise<GetCode> {
73
85
  return new Promise((resolve, reject) => {
74
86
  axios
75
- .get(`${FEEDER_GATEWAY_URL}/get_code?contractAddress=${contractAddress}&blockId=${blockId}`)
76
- .then((resp: any) => {
87
+ .get<GetCode>(
88
+ `${FEEDER_GATEWAY_URL}/get_code?contractAddress=${contractAddress}&blockId=${blockId}`
89
+ )
90
+ .then((resp) => {
77
91
  resolve(resp.data);
78
92
  })
79
93
  .catch(reject);
80
94
  });
81
95
  }
82
96
 
97
+ // TODO: add proper type
83
98
  /**
84
99
  * Gets the contract's storage variable at a specific key.
85
100
  *
@@ -115,11 +130,13 @@ export function getStorageAt(
115
130
  * @param txId
116
131
  * @returns the transaction status object { block_id, tx_status: NOT_RECEIVED | RECEIVED | PENDING | REJECTED | ACCEPTED_ONCHAIN }
117
132
  */
118
- export function getTransactionStatus(txId: number): Promise<object> {
133
+ export function getTransactionStatus(txId: number): Promise<GetTransactionStatusResponse> {
119
134
  return new Promise((resolve, reject) => {
120
135
  axios
121
- .get(`${FEEDER_GATEWAY_URL}/get_transaction_status?transactionId=${txId}`)
122
- .then((resp: any) => {
136
+ .get<GetTransactionStatusResponse>(
137
+ `${FEEDER_GATEWAY_URL}/get_transaction_status?transactionId=${txId}`
138
+ )
139
+ .then((resp) => {
123
140
  resolve(resp.data);
124
141
  })
125
142
  .catch(reject);
@@ -134,11 +151,11 @@ export function getTransactionStatus(txId: number): Promise<object> {
134
151
  * @param txId
135
152
  * @returns the transacton object { transaction_id, status, transaction, block_id?, block_number?, transaction_index?, transaction_failure_reason? }
136
153
  */
137
- export function getTransaction(txId: number): Promise<object> {
154
+ export function getTransaction(txId: number): Promise<GetTransactionResponse> {
138
155
  return new Promise((resolve, reject) => {
139
156
  axios
140
- .get(`${FEEDER_GATEWAY_URL}/get_transaction?transactionId=${txId}`)
141
- .then((resp: any) => {
157
+ .get<GetTransactionResponse>(`${FEEDER_GATEWAY_URL}/get_transaction?transactionId=${txId}`)
158
+ .then((resp) => {
142
159
  resolve(resp.data);
143
160
  })
144
161
  .catch(reject);
@@ -153,7 +170,7 @@ export function getTransaction(txId: number): Promise<object> {
153
170
  * @param tx - transaction to be invoked (WIP)
154
171
  * @returns a confirmation of invoking a function on the starknet contract
155
172
  */
156
- export function addTransaction(tx: object): Promise<object> {
173
+ export function addTransaction(tx: Transaction): Promise<AddTransactionResponse> {
157
174
  return new Promise((resolve, reject) => {
158
175
  axios
159
176
  .post(`${GATEWAY_URL}/add_transaction`, tx)
@@ -164,6 +181,33 @@ export function addTransaction(tx: object): Promise<object> {
164
181
  });
165
182
  }
166
183
 
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
+ */
191
+ export function deployContract(
192
+ contract: CompiledContract | string,
193
+ address: string = randomAddress()
194
+ ): Promise<AddTransactionResponse> {
195
+ const parsedContract =
196
+ typeof contract === 'string' ? (JsonParser.parse(contract) as CompiledContract) : contract;
197
+ const contractDefinition = {
198
+ ...parsedContract,
199
+ program: compressProgram(parsedContract.program),
200
+ };
201
+
202
+ return addTransaction({
203
+ type: 'DEPLOY',
204
+ contract_address: address,
205
+ contract_definition: contractDefinition,
206
+ });
207
+ }
208
+
209
+ export * from './utils';
210
+ export * from './types';
167
211
  export default {
168
212
  getContractAddresses,
169
213
  callContract,
@@ -173,4 +217,6 @@ export default {
173
217
  getTransactionStatus,
174
218
  getTransaction,
175
219
  addTransaction,
220
+ compressProgram,
221
+ deployContract,
176
222
  };
package/src/types.ts ADDED
@@ -0,0 +1,95 @@
1
+ export interface GetContractAddressesResponse {
2
+ Starknet: string;
3
+ GpsStatementVerifier: string;
4
+ }
5
+
6
+ export type Status = 'NOT_RECEIVED' | 'RECEIVED' | 'PENDING' | 'REJECTED' | 'ACCEPTED_ONCHAIN';
7
+ export type TxStatus = 'TRANSACTION_RECEIVED';
8
+ export type Type = 'DEPLOY' | 'INVOKE_FUNCTION';
9
+ export type EntryPointType = 'EXTERNAL';
10
+ export type CompressedProgram = string;
11
+
12
+ export interface Abi {
13
+ inputs: { name: string; type: string }[];
14
+ name: string;
15
+ outputs: { name: string; type: string }[];
16
+ type: string;
17
+ }
18
+ export type EntryPointsByType = object;
19
+ export type Program = object;
20
+
21
+ export interface CompiledContract {
22
+ abi: Abi;
23
+ entry_points_by_type: EntryPointsByType;
24
+ program: Program;
25
+ }
26
+
27
+ export interface CompressedCompiledContract extends Omit<CompiledContract, 'program'> {
28
+ program: CompressedProgram;
29
+ }
30
+
31
+ export interface DeployTransaction {
32
+ type: 'DEPLOY';
33
+ contract_definition: CompressedCompiledContract;
34
+ contract_address: string;
35
+ }
36
+
37
+ export interface InvokeFunctionTransaction {
38
+ type: 'INVOKE_FUNCTION';
39
+ contract_address: string;
40
+ entry_point_type?: EntryPointType;
41
+ entry_point_selector?: string;
42
+ calldata?: string[];
43
+ }
44
+
45
+ export type Transaction = DeployTransaction | InvokeFunctionTransaction;
46
+
47
+ export interface GetBlockResponse {
48
+ sequence_number: number;
49
+ state_root: string;
50
+ block_id: number;
51
+ transactions: {
52
+ [txid: string]: Transaction;
53
+ };
54
+ timestamp: number;
55
+ transaction_receipts: {
56
+ [txid: string]: {
57
+ block_id: number;
58
+ transaction_id: number;
59
+ l2_to_l1_messages: {
60
+ to_address: string;
61
+ payload: string[];
62
+ from_address: string;
63
+ }[];
64
+ block_number: number;
65
+ status: Status;
66
+ transaction_index: number;
67
+ };
68
+ };
69
+ previous_block_id: number;
70
+ status: Status;
71
+ }
72
+
73
+ export interface GetCode {
74
+ bytecode: string[];
75
+ abi: Abi[];
76
+ }
77
+
78
+ export interface GetTransactionStatusResponse {
79
+ tx_status: Status;
80
+ block_id: number;
81
+ }
82
+
83
+ export interface GetTransactionResponse {
84
+ transaction_index: number;
85
+ transaction: Transaction;
86
+ block_id: number;
87
+ block_number: number;
88
+ status: Status;
89
+ transaction_id: number;
90
+ }
91
+
92
+ export interface AddTransactionResponse {
93
+ code: TxStatus;
94
+ tx_id: number;
95
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,44 @@
1
+ import { gzip } from 'pako';
2
+ import Json from 'json-bigint';
3
+ import { CompressedProgram, Program } from './types';
4
+ import { CONTRACT_ADDRESS_LOWER_BOUND, CONTRACT_ADDRESS_UPPER_BOUND } from './constants';
5
+
6
+ export const isBrowser = typeof window !== 'undefined';
7
+
8
+ export const btoaUniversal = (b: ArrayBuffer): string =>
9
+ isBrowser ? btoa(String.fromCharCode.apply(null, b as any)) : Buffer.from(b).toString('base64');
10
+
11
+ export function randomIntFromInterval(min: number, max: number) {
12
+ return Math.floor(Math.random() * (max - min + 1) + min);
13
+ }
14
+
15
+ export function randomAddress(): string {
16
+ return `0x${randomIntFromInterval(
17
+ CONTRACT_ADDRESS_LOWER_BOUND,
18
+ CONTRACT_ADDRESS_UPPER_BOUND
19
+ ).toString(16)}`;
20
+ }
21
+
22
+ export function makeAddress(input: string): string {
23
+ return `0x${input.replace(/^0x/, '').toLowerCase()}`;
24
+ }
25
+
26
+ export const JsonParser = Json({
27
+ alwaysParseAsBig: true,
28
+ useNativeBigInt: true,
29
+ });
30
+
31
+ /**
32
+ * Function to compress compiled cairo program
33
+ *
34
+ * [Reference](https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/services/api/gateway/transaction.py#L54-L58)
35
+ * @param jsonProgram - json file representing the compiled cairo program
36
+ * @returns Compressed cairo program
37
+ */
38
+ export function compressProgram(jsonProgram: Program | string): CompressedProgram {
39
+ const stringified =
40
+ typeof jsonProgram === 'string' ? jsonProgram : JsonParser.stringify(jsonProgram);
41
+ const compressedProgram = gzip(stringified);
42
+ const base64 = btoaUniversal(compressedProgram);
43
+ return base64;
44
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "include": ["src/**/*", "__tests__/**/*"]
4
+ }
package/tsconfig.json CHANGED
@@ -11,8 +11,13 @@
11
11
  // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
12
12
 
13
13
  /* Language and Environment */
14
- "target": "es5", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
15
- "lib": ["es2017", "es7", "es6", "dom"], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
14
+ "target": "es5" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
15
+ "lib": [
16
+ "es2017",
17
+ "es7",
18
+ "es6",
19
+ "dom"
20
+ ] /* Specify a set of bundled library declaration files that describe the target runtime environment. */,
16
21
  // "jsx": "preserve", /* Specify what JSX code is generated. */
17
22
  // "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
18
23
  // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
@@ -24,7 +29,7 @@
24
29
  // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
25
30
 
26
31
  /* Modules */
27
- "module": "commonjs", /* Specify what module code is generated. */
32
+ "module": "commonjs" /* Specify what module code is generated. */,
28
33
  // "rootDir": "./", /* Specify the root folder within your source files. */
29
34
  // "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
30
35
  // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
@@ -42,12 +47,12 @@
42
47
  // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from `node_modules`. Only applicable with `allowJs`. */
43
48
 
44
49
  /* Emit */
45
- "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
50
+ "declaration": true /* Generate .d.ts files from TypeScript and JavaScript files in your project. */,
46
51
  // "declarationMap": true, /* Create sourcemaps for d.ts files. */
47
52
  // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
48
53
  // "sourceMap": true, /* Create source map files for emitted JavaScript files. */
49
54
  // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */
50
- "outDir": "dist", /* Specify an output folder for all emitted files. */
55
+ "outDir": "dist" /* Specify an output folder for all emitted files. */,
51
56
  // "removeComments": true, /* Disable emitting comments. */
52
57
  // "noEmit": true, /* Disable emitting files from a compilation. */
53
58
  // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
@@ -68,12 +73,12 @@
68
73
  /* Interop Constraints */
69
74
  // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
70
75
  // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
71
- "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */
76
+ "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */,
72
77
  // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
73
- "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
78
+ "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
74
79
 
75
80
  /* Type Checking */
76
- "strict": true, /* Enable all strict type-checking options. */
81
+ "strict": true /* Enable all strict type-checking options. */,
77
82
  // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied `any` type.. */
78
83
  // "strictNullChecks": true, /* When type checking, take into account `null` and `undefined`. */
79
84
  // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
@@ -95,13 +100,10 @@
95
100
 
96
101
  /* Completeness */
97
102
  // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
98
- "skipLibCheck": true /* Skip type checking all .d.ts files. */
103
+ "skipLibCheck": true /* Skip type checking all .d.ts files. */
99
104
  },
100
105
  "include": ["src/**/*"],
101
- "exclude": [
102
- "node_modules",
103
- "**/*.test.ts"
104
- ],
106
+ "exclude": ["node_modules"],
105
107
  "typedocOptions": {
106
108
  "entryPoints": ["src/index.ts"],
107
109
  "out": "docs"
package/types.d.ts ADDED
@@ -0,0 +1,94 @@
1
+ export interface GetContractAddressesResponse {
2
+ Starknet: string;
3
+ GpsStatementVerifier: string;
4
+ }
5
+ export declare type Status =
6
+ | 'NOT_RECEIVED'
7
+ | 'RECEIVED'
8
+ | 'PENDING'
9
+ | 'REJECTED'
10
+ | 'ACCEPTED_ONCHAIN';
11
+ export declare type TxStatus = 'TRANSACTION_RECEIVED';
12
+ export declare type Type = 'DEPLOY' | 'INVOKE_FUNCTION';
13
+ export declare type EntryPointType = 'EXTERNAL';
14
+ export declare type CompressedProgram = string;
15
+ export interface Abi {
16
+ inputs: {
17
+ name: string;
18
+ type: string;
19
+ }[];
20
+ name: string;
21
+ outputs: {
22
+ name: string;
23
+ type: string;
24
+ }[];
25
+ type: string;
26
+ }
27
+ export declare type EntryPointsByType = object;
28
+ export declare type Program = object;
29
+ export interface CompiledContract {
30
+ abi: Abi;
31
+ entry_points_by_type: EntryPointsByType;
32
+ program: Program;
33
+ }
34
+ export interface CompressedCompiledContract extends Omit<CompiledContract, 'program'> {
35
+ program: CompressedProgram;
36
+ }
37
+ export interface DeployTransaction {
38
+ type: 'DEPLOY';
39
+ contract_definition: CompressedCompiledContract;
40
+ contract_address: string;
41
+ }
42
+ export interface InvokeFunctionTransaction {
43
+ type: 'INVOKE_FUNCTION';
44
+ contract_address: string;
45
+ entry_point_type?: EntryPointType;
46
+ entry_point_selector?: string;
47
+ calldata?: string[];
48
+ }
49
+ export declare type Transaction = DeployTransaction | InvokeFunctionTransaction;
50
+ export interface GetBlockResponse {
51
+ sequence_number: number;
52
+ state_root: string;
53
+ block_id: number;
54
+ transactions: {
55
+ [txid: string]: Transaction;
56
+ };
57
+ timestamp: number;
58
+ transaction_receipts: {
59
+ [txid: string]: {
60
+ block_id: number;
61
+ transaction_id: number;
62
+ l2_to_l1_messages: {
63
+ to_address: string;
64
+ payload: string[];
65
+ from_address: string;
66
+ }[];
67
+ block_number: number;
68
+ status: Status;
69
+ transaction_index: number;
70
+ };
71
+ };
72
+ previous_block_id: number;
73
+ status: Status;
74
+ }
75
+ export interface GetCode {
76
+ bytecode: string[];
77
+ abi: Abi[];
78
+ }
79
+ export interface GetTransactionStatusResponse {
80
+ tx_status: Status;
81
+ block_id: number;
82
+ }
83
+ export interface GetTransactionResponse {
84
+ transaction_index: number;
85
+ transaction: Transaction;
86
+ block_id: number;
87
+ block_number: number;
88
+ status: Status;
89
+ transaction_id: number;
90
+ }
91
+ export interface AddTransactionResponse {
92
+ code: TxStatus;
93
+ tx_id: number;
94
+ }
package/types.js ADDED
@@ -0,0 +1,2 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
package/utils.d.ts ADDED
@@ -0,0 +1,29 @@
1
+ import { CompressedProgram, Program } from './types';
2
+ export declare const isBrowser: boolean;
3
+ export declare const btoaUniversal: (b: ArrayBuffer) => string;
4
+ export declare function randomIntFromInterval(min: number, max: number): number;
5
+ export declare function randomAddress(): string;
6
+ export declare function makeAddress(input: string): string;
7
+ export declare const JsonParser: {
8
+ parse: (text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined) => any;
9
+ stringify: {
10
+ (
11
+ value: any,
12
+ replacer?: ((this: any, key: string, value: any) => any) | undefined,
13
+ space?: string | number | undefined
14
+ ): string;
15
+ (
16
+ value: any,
17
+ replacer?: (string | number)[] | null | undefined,
18
+ space?: string | number | undefined
19
+ ): string;
20
+ };
21
+ };
22
+ /**
23
+ * Function to compress compiled cairo program
24
+ *
25
+ * [Reference](https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/services/api/gateway/transaction.py#L54-L58)
26
+ * @param jsonProgram - json file representing the compiled cairo program
27
+ * @returns Compressed cairo program
28
+ */
29
+ export declare function compressProgram(jsonProgram: Program | string): CompressedProgram;
package/utils.js ADDED
@@ -0,0 +1,62 @@
1
+ 'use strict';
2
+ var __importDefault =
3
+ (this && this.__importDefault) ||
4
+ function (mod) {
5
+ return mod && mod.__esModule ? mod : { default: mod };
6
+ };
7
+ Object.defineProperty(exports, '__esModule', { value: true });
8
+ exports.compressProgram =
9
+ exports.JsonParser =
10
+ exports.makeAddress =
11
+ exports.randomAddress =
12
+ exports.randomIntFromInterval =
13
+ exports.btoaUniversal =
14
+ exports.isBrowser =
15
+ void 0;
16
+ var pako_1 = require('pako');
17
+ var json_bigint_1 = __importDefault(require('json-bigint'));
18
+ var constants_1 = require('./constants');
19
+ exports.isBrowser = typeof window !== 'undefined';
20
+ var btoaUniversal = function (b) {
21
+ return exports.isBrowser
22
+ ? btoa(String.fromCharCode.apply(null, b))
23
+ : Buffer.from(b).toString('base64');
24
+ };
25
+ exports.btoaUniversal = btoaUniversal;
26
+ function randomIntFromInterval(min, max) {
27
+ return Math.floor(Math.random() * (max - min + 1) + min);
28
+ }
29
+ exports.randomIntFromInterval = randomIntFromInterval;
30
+ function randomAddress() {
31
+ return (
32
+ '0x' +
33
+ randomIntFromInterval(
34
+ constants_1.CONTRACT_ADDRESS_LOWER_BOUND,
35
+ constants_1.CONTRACT_ADDRESS_UPPER_BOUND
36
+ ).toString(16)
37
+ );
38
+ }
39
+ exports.randomAddress = randomAddress;
40
+ function makeAddress(input) {
41
+ return '0x' + input.replace(/^0x/, '').toLowerCase();
42
+ }
43
+ exports.makeAddress = makeAddress;
44
+ exports.JsonParser = (0, json_bigint_1.default)({
45
+ alwaysParseAsBig: true,
46
+ useNativeBigInt: true,
47
+ });
48
+ /**
49
+ * Function to compress compiled cairo program
50
+ *
51
+ * [Reference](https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/services/api/gateway/transaction.py#L54-L58)
52
+ * @param jsonProgram - json file representing the compiled cairo program
53
+ * @returns Compressed cairo program
54
+ */
55
+ function compressProgram(jsonProgram) {
56
+ var stringified =
57
+ typeof jsonProgram === 'string' ? jsonProgram : exports.JsonParser.stringify(jsonProgram);
58
+ var compressedProgram = (0, pako_1.gzip)(stringified);
59
+ var base64 = (0, exports.btoaUniversal)(compressedProgram);
60
+ return base64;
61
+ }
62
+ exports.compressProgram = compressProgram;
package/.eslintrc.json DELETED
@@ -1,26 +0,0 @@
1
- {
2
- "env": {
3
- "browser": true,
4
- "es2021": true,
5
- "node": true,
6
- "jest": true
7
- },
8
- "extends": [
9
- "airbnb-base",
10
- "prettier",
11
- "plugin:prettier/recommended",
12
- ],
13
- "globals": {
14
- "Atomics": "readonly",
15
- "SharedArrayBuffer": "readonly"
16
- },
17
- "parser": "@typescript-eslint/parser",
18
- "parserOptions": {
19
- "ecmaVersion": 12,
20
- "sourceType": "module"
21
- },
22
- "plugins": [
23
- "@typescript-eslint"
24
- ],
25
- "rules": {}
26
- }
package/babel.config.js DELETED
@@ -1,3 +0,0 @@
1
- module.exports = {
2
- presets: [['@babel/preset-env', { targets: { node: 'current' } }], '@babel/preset-typescript'],
3
- };
@@ -1 +0,0 @@
1
- export {};
@@ -1,49 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
- }) : (function(o, m, k, k2) {
6
- if (k2 === undefined) k2 = k;
7
- o[k2] = m[k];
8
- }));
9
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
- Object.defineProperty(o, "default", { enumerable: true, value: v });
11
- }) : function(o, v) {
12
- o["default"] = v;
13
- });
14
- var __importStar = (this && this.__importStar) || function (mod) {
15
- if (mod && mod.__esModule) return mod;
16
- var result = {};
17
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
- __setModuleDefault(result, mod);
19
- return result;
20
- };
21
- Object.defineProperty(exports, "__esModule", { value: true });
22
- var starknet = __importStar(require("../index"));
23
- describe('starknet endpoints', function () {
24
- describe('feeder gateway endpoints', function () {
25
- test('getContractAddresses()', function () {
26
- return expect(starknet.getContractAddresses()).resolves.not.toThrow();
27
- });
28
- xtest('callContract()', function () { });
29
- test('getBlock()', function () {
30
- return expect(starknet.getBlock(46500)).resolves.not.toThrow();
31
- });
32
- test('getCode()', function () {
33
- return expect(starknet.getCode('0x5f778a983bf8760ad37868f4c869d70247c5546044a7f0386df96d8043d4e9d', 46500)).resolves.not.toThrow();
34
- });
35
- test('getStorageAt()', function () {
36
- return expect(starknet.getStorageAt('0x5f778a983bf8760ad37868f4c869d70247c5546044a7f0386df96d8043d4e9d', 0, 46500)).resolves.not.toThrow();
37
- });
38
- test('getTransactionStatus()', function () {
39
- return expect(starknet.getTransactionStatus(286136)).resolves.not.toThrow();
40
- });
41
- test('getTransaction()', function () {
42
- return expect(starknet.getTransaction(286136)).resolves.not.toThrow();
43
- });
44
- xtest('addTransaction()', function () { });
45
- });
46
- describe('gateway endpoints', function () {
47
- xtest('addTransaction()', function () { });
48
- });
49
- });