starknet 0.1.4 → 1.1.0

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/docs-bot.yml +25 -0
  6. package/.github/workflows/pr.yml +23 -0
  7. package/.github/workflows/release.yml +39 -0
  8. package/.husky/commit-msg +4 -0
  9. package/.husky/pre-commit +4 -0
  10. package/{.prettierrc.json → .prettierrc} +0 -1
  11. package/.releaserc +28 -0
  12. package/CHANGELOG.md +36 -0
  13. package/CONTRIBUTING.md +52 -0
  14. package/README.md +35 -11
  15. package/__mocks__/ArgentAccount.json +92620 -0
  16. package/__tests__/__snapshots__/utils.browser.test.ts.snap +5 -0
  17. package/__tests__/__snapshots__/utils.test.ts.snap +5 -0
  18. package/__tests__/index.test.ts +49 -4
  19. package/__tests__/utils.browser.test.ts +30 -0
  20. package/__tests__/utils.test.ts +35 -0
  21. package/constants.d.ts +3 -0
  22. package/constants.js +9 -0
  23. package/dist/constants.d.ts +3 -0
  24. package/dist/constants.js +6 -0
  25. package/dist/index.d.ts +22 -8
  26. package/dist/index.js +50 -4
  27. package/dist/types.d.ts +89 -0
  28. package/dist/types.js +2 -0
  29. package/dist/utils.d.ts +21 -0
  30. package/dist/utils.js +44 -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 +66 -20
  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/index.js ADDED
@@ -0,0 +1,261 @@
1
+ 'use strict';
2
+ var __assign =
3
+ (this && this.__assign) ||
4
+ function () {
5
+ __assign =
6
+ Object.assign ||
7
+ function (t) {
8
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
9
+ s = arguments[i];
10
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
11
+ }
12
+ return t;
13
+ };
14
+ return __assign.apply(this, arguments);
15
+ };
16
+ var __createBinding =
17
+ (this && this.__createBinding) ||
18
+ (Object.create
19
+ ? function (o, m, k, k2) {
20
+ if (k2 === undefined) k2 = k;
21
+ Object.defineProperty(o, k2, {
22
+ enumerable: true,
23
+ get: function () {
24
+ return m[k];
25
+ },
26
+ });
27
+ }
28
+ : function (o, m, k, k2) {
29
+ if (k2 === undefined) k2 = k;
30
+ o[k2] = m[k];
31
+ });
32
+ var __exportStar =
33
+ (this && this.__exportStar) ||
34
+ function (m, exports) {
35
+ for (var p in m)
36
+ if (p !== 'default' && !Object.prototype.hasOwnProperty.call(exports, p))
37
+ __createBinding(exports, m, p);
38
+ };
39
+ var __importDefault =
40
+ (this && this.__importDefault) ||
41
+ function (mod) {
42
+ return mod && mod.__esModule ? mod : { default: mod };
43
+ };
44
+ Object.defineProperty(exports, '__esModule', { value: true });
45
+ exports.deployContract =
46
+ exports.addTransaction =
47
+ exports.getTransaction =
48
+ exports.getTransactionStatus =
49
+ exports.getStorageAt =
50
+ exports.getCode =
51
+ exports.getBlock =
52
+ exports.callContract =
53
+ exports.getContractAddresses =
54
+ void 0;
55
+ var axios_1 = __importDefault(require('axios'));
56
+ var utils_1 = require('./utils');
57
+ var API_URL = 'https://alpha2.starknet.io';
58
+ var FEEDER_GATEWAY_URL = API_URL + '/feeder_gateway';
59
+ var GATEWAY_URL = API_URL + '/gateway';
60
+ /**
61
+ * Gets the smart contract address on the goerli testnet.
62
+ *
63
+ * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L13-L15)
64
+ * @returns starknet smart contract addresses
65
+ */
66
+ function getContractAddresses() {
67
+ return new Promise(function (resolve, reject) {
68
+ axios_1.default
69
+ .get(FEEDER_GATEWAY_URL + '/get_contract_addresses')
70
+ .then(function (resp) {
71
+ resolve(resp.data);
72
+ })
73
+ .catch(reject);
74
+ });
75
+ }
76
+ exports.getContractAddresses = getContractAddresses;
77
+ // TODO: add proper type
78
+ /**
79
+ * Calls a function on the StarkNet contract.
80
+ *
81
+ * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L17-L25)
82
+ *
83
+ * @param invokeTx - transaction to be invoked (WIP)
84
+ * @param blockId
85
+ * @returns the result of the function on the smart contract.
86
+ */
87
+ function callContract(invokeTx, blockId) {
88
+ return new Promise(function (resolve, reject) {
89
+ axios_1.default
90
+ .post(FEEDER_GATEWAY_URL + '/call_contract?blockId=' + blockId, invokeTx)
91
+ .then(function (resp) {
92
+ resolve(resp.data);
93
+ })
94
+ .catch(reject);
95
+ });
96
+ }
97
+ exports.callContract = callContract;
98
+ /**
99
+ * Gets the block information from a block ID.
100
+ *
101
+ * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L27-L31)
102
+ *
103
+ * @param blockId
104
+ * @returns the block object { block_id, previous_block_id, state_root, status, timestamp, transaction_receipts, transactions }
105
+ */
106
+ function getBlock(blockId) {
107
+ return new Promise(function (resolve, reject) {
108
+ axios_1.default
109
+ .get(FEEDER_GATEWAY_URL + '/get_block?blockId=' + blockId)
110
+ .then(function (resp) {
111
+ resolve(resp.data);
112
+ })
113
+ .catch(reject);
114
+ });
115
+ }
116
+ exports.getBlock = getBlock;
117
+ /**
118
+ * Gets the code of the deployed contract.
119
+ *
120
+ * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L33-L36)
121
+ *
122
+ * @param contractAddress
123
+ * @param blockId
124
+ * @returns Bytecode and ABI of compiled contract
125
+ */
126
+ function getCode(contractAddress, blockId) {
127
+ return new Promise(function (resolve, reject) {
128
+ axios_1.default
129
+ .get(
130
+ FEEDER_GATEWAY_URL + '/get_code?contractAddress=' + contractAddress + '&blockId=' + blockId
131
+ )
132
+ .then(function (resp) {
133
+ resolve(resp.data);
134
+ })
135
+ .catch(reject);
136
+ });
137
+ }
138
+ exports.getCode = getCode;
139
+ // TODO: add proper type
140
+ /**
141
+ * Gets the contract's storage variable at a specific key.
142
+ *
143
+ * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L38-L46)
144
+ *
145
+ * @param contractAddress
146
+ * @param key - from getStorageVarAddress('<STORAGE_VARIABLE_NAME>') (WIP)
147
+ * @param blockId
148
+ * @returns the value of the storage variable
149
+ */
150
+ function getStorageAt(contractAddress, key, blockId) {
151
+ return new Promise(function (resolve, reject) {
152
+ axios_1.default
153
+ .get(
154
+ FEEDER_GATEWAY_URL +
155
+ '/get_storage_at?contractAddress=' +
156
+ contractAddress +
157
+ '&key=' +
158
+ key +
159
+ '&blockId=' +
160
+ blockId
161
+ )
162
+ .then(function (resp) {
163
+ resolve(resp.data);
164
+ })
165
+ .catch(reject);
166
+ });
167
+ }
168
+ exports.getStorageAt = getStorageAt;
169
+ /**
170
+ * Gets the status of a transaction.
171
+ *
172
+ * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L48-L52)
173
+ *
174
+ * @param txId
175
+ * @returns the transaction status object { block_id, tx_status: NOT_RECEIVED | RECEIVED | PENDING | REJECTED | ACCEPTED_ONCHAIN }
176
+ */
177
+ function getTransactionStatus(txId) {
178
+ return new Promise(function (resolve, reject) {
179
+ axios_1.default
180
+ .get(FEEDER_GATEWAY_URL + '/get_transaction_status?transactionId=' + txId)
181
+ .then(function (resp) {
182
+ resolve(resp.data);
183
+ })
184
+ .catch(reject);
185
+ });
186
+ }
187
+ exports.getTransactionStatus = getTransactionStatus;
188
+ /**
189
+ * Gets the transaction information from a tx id.
190
+ *
191
+ * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L54-L58)
192
+ *
193
+ * @param txId
194
+ * @returns the transacton object { transaction_id, status, transaction, block_id?, block_number?, transaction_index?, transaction_failure_reason? }
195
+ */
196
+ function getTransaction(txId) {
197
+ return new Promise(function (resolve, reject) {
198
+ axios_1.default
199
+ .get(FEEDER_GATEWAY_URL + '/get_transaction?transactionId=' + txId)
200
+ .then(function (resp) {
201
+ resolve(resp.data);
202
+ })
203
+ .catch(reject);
204
+ });
205
+ }
206
+ exports.getTransaction = getTransaction;
207
+ /**
208
+ * Invoke a function on the starknet contract
209
+ *
210
+ * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
211
+ *
212
+ * @param tx - transaction to be invoked (WIP)
213
+ * @returns a confirmation of invoking a function on the starknet contract
214
+ */
215
+ function addTransaction(tx) {
216
+ return new Promise(function (resolve, reject) {
217
+ axios_1.default
218
+ .post(GATEWAY_URL + '/add_transaction', tx)
219
+ .then(function (resp) {
220
+ resolve(resp.data);
221
+ })
222
+ .catch(reject);
223
+ });
224
+ }
225
+ exports.addTransaction = addTransaction;
226
+ /**
227
+ * Deploys a given compiled contract (json) to starknet
228
+ *
229
+ * @param contract - a json object containing the compiled contract
230
+ * @param address - (optional, defaults to a random address) the address where the contract should be deployed (alpha)
231
+ * @returns a confirmation of sending a transaction on the starknet contract
232
+ */
233
+ function deployContract(contract, address) {
234
+ if (address === void 0) {
235
+ address = (0, utils_1.randomAddress)();
236
+ }
237
+ var parsedContract = typeof contract === 'string' ? utils_1.JsonParser.parse(contract) : contract;
238
+ var contractDefinition = __assign(__assign({}, parsedContract), {
239
+ program: (0, utils_1.compressProgram)(parsedContract.program),
240
+ });
241
+ return addTransaction({
242
+ type: 'DEPLOY',
243
+ contract_address: address,
244
+ contract_definition: contractDefinition,
245
+ });
246
+ }
247
+ exports.deployContract = deployContract;
248
+ __exportStar(require('./utils'), exports);
249
+ __exportStar(require('./types'), exports);
250
+ exports.default = {
251
+ getContractAddresses: getContractAddresses,
252
+ callContract: callContract,
253
+ getBlock: getBlock,
254
+ getCode: getCode,
255
+ getStorageAt: getStorageAt,
256
+ getTransactionStatus: getTransactionStatus,
257
+ getTransaction: getTransaction,
258
+ addTransaction: addTransaction,
259
+ compressProgram: utils_1.compressProgram,
260
+ deployContract: deployContract,
261
+ };
package/package.json CHANGED
@@ -1,15 +1,19 @@
1
1
  {
2
2
  "name": "starknet",
3
- "version": "0.1.4",
3
+ "version": "1.1.0",
4
4
  "description": "JavaScript library for StarkNet",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "scripts": {
8
- "prepare": "npm run build",
8
+ "prepare": "npm run build && husky install",
9
9
  "build": "tsc",
10
+ "pretest": "npm run lint",
10
11
  "test": "jest",
12
+ "posttest": "npm run format",
11
13
  "test:watch": "jest --watch",
12
- "docs": "typedoc --theme ./node_modules/typedoc-github-wiki-theme/dist --readme none --name 'StarkNet.js Docs' --excludeNotDocumented"
14
+ "docs": "typedoc --readme none --name 'StarkNet.js Docs' --excludeNotDocumented --disableSources",
15
+ "format": "prettier --loglevel warn --write \"**/*.{ts,js,md,yml,json}\"",
16
+ "lint": "eslint . --cache --fix --ext .ts"
13
17
  },
14
18
  "keywords": [
15
19
  "starknet",
@@ -24,22 +28,42 @@
24
28
  "devDependencies": {
25
29
  "@babel/preset-env": "^7.15.8",
26
30
  "@babel/preset-typescript": "^7.15.0",
31
+ "@commitlint/cli": "^13.2.1",
32
+ "@commitlint/config-conventional": "^13.2.0",
33
+ "@semantic-release/changelog": "^6.0.0",
34
+ "@semantic-release/commit-analyzer": "^9.0.1",
35
+ "@semantic-release/git": "^10.0.0",
36
+ "@semantic-release/npm": "^8.0.2",
37
+ "@semantic-release/release-notes-generator": "^10.0.2",
27
38
  "@types/jest": "^27.0.2",
39
+ "@types/json-bigint": "^1.0.1",
40
+ "@types/pako": "^1.0.2",
28
41
  "@typescript-eslint/eslint-plugin": "^5.0.0",
29
42
  "@typescript-eslint/parser": "^5.0.0",
30
43
  "eslint": "^7.32.0",
31
44
  "eslint-config-airbnb-base": "^14.2.1",
45
+ "eslint-config-airbnb-typescript": "^14.0.1",
32
46
  "eslint-config-prettier": "^8.3.0",
33
47
  "eslint-plugin-import": "^2.25.2",
34
48
  "eslint-plugin-prettier": "^4.0.0",
49
+ "husky": "^7.0.2",
35
50
  "jest": "^27.3.0",
51
+ "lint-staged": "^11.2.3",
36
52
  "prettier": "^2.4.1",
37
53
  "typedoc": "^0.22.6",
38
- "typedoc-github-wiki-theme": "^0.6.0",
39
54
  "typedoc-plugin-markdown": "^3.11.3",
40
55
  "typescript": "^4.4.4"
41
56
  },
42
57
  "dependencies": {
43
- "axios": "^0.23.0"
58
+ "axios": "^0.23.0",
59
+ "json-bigint": "^1.0.0",
60
+ "pako": "^2.0.4"
61
+ },
62
+ "lint-staged": {
63
+ "*.ts": "eslint --cache --fix",
64
+ "*.{ts,js,md,yml,json}": "prettier --write"
65
+ },
66
+ "jest": {
67
+ "testTimeout": 20000
44
68
  }
45
69
  }
@@ -0,0 +1,3 @@
1
+ export const CONTRACT_ADDRESS_BITS = 251;
2
+ export const CONTRACT_ADDRESS_LOWER_BOUND = 1;
3
+ export const CONTRACT_ADDRESS_UPPER_BOUND = 2 ** CONTRACT_ADDRESS_BITS;
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
21
  * [Reference](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
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
+ }