starknet 2.0.2 → 2.3.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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,51 @@
1
+ # [2.3.0](https://github.com/seanjameshan/starknet.js/compare/v2.2.0...v2.3.0) (2021-12-01)
2
+
3
+ ### Features
4
+
5
+ - add compileStructToCalldata ([e5bdb18](https://github.com/seanjameshan/starknet.js/commit/e5bdb1890e0bc53b03d4b145069cf7fbc639e830))
6
+ - **utils:** support shortstring and uint256 ([f7ff057](https://github.com/seanjameshan/starknet.js/commit/f7ff05753d9bc39b31bdd4e7f893ee04cab77823))
7
+
8
+ # [2.2.0](https://github.com/seanjameshan/starknet.js/compare/v2.1.0...v2.2.0) (2021-11-30)
9
+
10
+ ### Bug Fixes
11
+
12
+ - bump version ([02c8d07](https://github.com/seanjameshan/starknet.js/commit/02c8d0772e42c81e35a3a841169eb25cde68716e))
13
+ - correctly parse structs in Starknet return types ([5a4a318](https://github.com/seanjameshan/starknet.js/commit/5a4a318dad4c78fe84540ad92063fc1879317ac1))
14
+ - make Typescript compiler happy with constant types ([aedd895](https://github.com/seanjameshan/starknet.js/commit/aedd895a62e6018dd1d7330b004d54360007967f))
15
+ - use urljoin ([4f1a040](https://github.com/seanjameshan/starknet.js/commit/4f1a04090f26f8e8565c516921d5d3332b6a4649))
16
+
17
+ ### Features
18
+
19
+ - bump version ([fd22f41](https://github.com/seanjameshan/starknet.js/commit/fd22f41e39ec1c7f71c32019309f82ad0f4d66a9))
20
+
21
+ ## [2.1.1](https://github.com/seanjameshan/starknet.js/compare/v2.1.0...v2.1.1) (2021-11-30)
22
+
23
+ ### Bug Fixes
24
+
25
+ - bump version ([02c8d07](https://github.com/seanjameshan/starknet.js/commit/02c8d0772e42c81e35a3a841169eb25cde68716e))
26
+ - correctly parse structs in Starknet return types ([5a4a318](https://github.com/seanjameshan/starknet.js/commit/5a4a318dad4c78fe84540ad92063fc1879317ac1))
27
+ - make Typescript compiler happy with constant types ([aedd895](https://github.com/seanjameshan/starknet.js/commit/aedd895a62e6018dd1d7330b004d54360007967f))
28
+ - use urljoin ([4f1a040](https://github.com/seanjameshan/starknet.js/commit/4f1a04090f26f8e8565c516921d5d3332b6a4649))
29
+
30
+ ## [2.1.1](https://github.com/seanjameshan/starknet.js/compare/v2.1.0...v2.1.1) (2021-11-30)
31
+
32
+ ### Bug Fixes
33
+
34
+ - correctly parse structs in Starknet return types ([5a4a318](https://github.com/seanjameshan/starknet.js/commit/5a4a318dad4c78fe84540ad92063fc1879317ac1))
35
+ - make Typescript compiler happy with constant types ([aedd895](https://github.com/seanjameshan/starknet.js/commit/aedd895a62e6018dd1d7330b004d54360007967f))
36
+ - use urljoin ([4f1a040](https://github.com/seanjameshan/starknet.js/commit/4f1a04090f26f8e8565c516921d5d3332b6a4649))
37
+
38
+ # [2.1.0](https://github.com/seanjameshan/starknet.js/compare/v2.0.1...v2.1.0) (2021-11-30)
39
+
40
+ ### Bug Fixes
41
+
42
+ - deps ([020ba39](https://github.com/seanjameshan/starknet.js/commit/020ba3948f03e41fc96c7c002b613250d73fbda6))
43
+ - transaction status pending ([198d82b](https://github.com/seanjameshan/starknet.js/commit/198d82b30dd8c0c978cfdd2d56cb5a7e5131cb6a))
44
+
45
+ ### Features
46
+
47
+ - support mainnet ([de07149](https://github.com/seanjameshan/starknet.js/commit/de07149ad6521edc9f79e2b0e9c82bf40f32fe02))
48
+
1
49
  ## [2.0.2](https://github.com/seanjameshan/starknet.js/compare/v2.0.1...v2.0.2) (2021-11-22)
2
50
 
3
51
  ### Bug Fixes
package/README.md CHANGED
@@ -44,14 +44,53 @@ $ npm install starknet
44
44
 
45
45
  Import `starknet` and use the [API](https://www.starknetjs.com/modules.html)
46
46
 
47
+ The following code is used to build a [simple AMM example](https://starkfin.netlify.app/) from the [cairo docs](https://www.cairo-lang.org/docs/hello_starknet/amm.html)
48
+
47
49
  ```javascript
48
- import { defaultProvider } from 'starknet';
50
+ import { defaultProvider, stark } from 'starknet';
51
+ const { getSelectorFromName } = stark;
52
+
53
+ const CONTRACT_ADDRESS =
54
+ "0x03e19baa6cb2078631bcdb34844f3f7879449a544c9ce722681a54af08cff4b9";
55
+
56
+ /**
57
+ * addTransaction() example
58
+ **/
59
+
60
+ /** Reset the liquidity pool **/
61
+ const addTokenResponse = await provider.addTransaction({
62
+ type: "INVOKE_FUNCTION",
63
+ contract_address: CONTRACT_ADDRESS,
64
+ entry_point_selector: getSelectorFromName("init_pool"),
65
+ calldata: ["1000000", "1000000"],
66
+ });
67
+ console.log(addTokenResponse);
68
+
69
+ /**
70
+ * callContract() example
71
+ **/
49
72
 
50
- defaultProvider.getContractAddresses().then((data) => {
51
- console.log(data);
73
+ /** Get the balance of the liquidity pool of token A **/
74
+ const poolBalanceTokenA = await callContract({
75
+ contract_address: CONTRACT_ADDRESS,
76
+ entry_point_selector: getSelectorFromName("get_pool_token_balance"),
77
+ calldata: ["1"],
52
78
  });
79
+ const balanceA = poolBalanceTokenA.result[0];
80
+ console.log('token a liquidity pool balance: ', parseInt(balanceA, 16));
81
+
82
+ /** Get the balance of the liquidity pool of token B **/
83
+ const poolBalanceTokenB = await callContract({
84
+ contract_address: CONTRACT_ADDRESS,
85
+ entry_point_selector: getSelectorFromName("get_pool_token_balance"),
86
+ calldata: ["2"],
87
+ });
88
+ const balanceB = poolBalanceTokenB.result[0];
89
+ console.log('token b liquidity pool balance: ', parseInt(balanceB, 16));
53
90
  ```
54
91
 
92
+ For more information about **signing transactions**, please take a look at this [pull request](https://github.com/seanjameshan/starknet.js/pull/51)
93
+
55
94
  ## 🌐 API
56
95
 
57
96
  [Click Here](https://www.starknetjs.com/modules.html)
@@ -60,6 +99,7 @@ defaultProvider.getContractAddresses().then((data) => {
60
99
 
61
100
  - [Argent X - the first StarkNet wallet](https://github.com/argentlabs/argent-x)
62
101
  - [React + Starknet.js boilerplate](https://github.com/fracek/starknet-react-example)
102
+ - [AMM Demo](https://www.starknetswap.com/)
63
103
 
64
104
  ## ✏️ Contributing
65
105
 
@@ -67,7 +107,7 @@ If you consider to contribute to this project please read [CONTRIBUTING.md](http
67
107
 
68
108
  ## ❤️ Special Thanks
69
109
 
70
- Special thanks to all the [contributors](https://github.com/seanjameshan/starknet.js/graphs/contributors), and especially Janek ([@janek26](https://github.com/janek26)) from [Argent](https://github.com/argentlabs) for driving the development of Starknet.js.
110
+ Special thanks to all the [contributors](https://github.com/seanjameshan/starknet.js/graphs/contributors), especially to Janek ([@janek26](https://github.com/janek26)) from [Argent](https://github.com/argentlabs) for driving the development of Starknet.js.
71
111
 
72
112
  This library would not be possible without these rockstars.
73
113
 
@@ -49,17 +49,17 @@ describe('defaultProvider', () => {
49
49
  )
50
50
  ).resolves.not.toThrow();
51
51
  });
52
- test('getTransactionStatus()', () => {
52
+ test('getTransactionStatus()', async () => {
53
53
  return expect(
54
54
  defaultProvider.getTransactionStatus(
55
- '0x774f7856b1ce6d5ce023a18cd5a06ab67e3a6d81c7bfcd01f99f32243c2d2ef'
55
+ '0x72add9621ecdcb07405a4f943fe410bf57003ca250400f01ce70f8a6fc72147'
56
56
  )
57
57
  ).resolves.not.toThrow();
58
58
  });
59
59
  test('getTransaction()', async () => {
60
60
  return expect(
61
61
  defaultProvider.getTransaction(
62
- '0x774f7856b1ce6d5ce023a18cd5a06ab67e3a6d81c7bfcd01f99f32243c2d2ef'
62
+ '0x72add9621ecdcb07405a4f943fe410bf57003ca250400f01ce70f8a6fc72147'
63
63
  )
64
64
  ).resolves.not.toThrow();
65
65
  });
@@ -1,4 +1,4 @@
1
- import { getKeyPair, getStarkKey, sign, verify, ec } from '../../src/utils/ellipticCurve';
1
+ import { ec, getKeyPair, getStarkKey, sign, verify } from '../../src/utils/ellipticCurve';
2
2
  import { removeHexPrefix } from '../../src/utils/encode';
3
3
  import { hashCalldata, hashMessage, pedersen } from '../../src/utils/hash';
4
4
  import { toBN, toHex } from '../../src/utils/number';
@@ -0,0 +1,22 @@
1
+ import { decodeShortString, encodeShortString } from '../../src/utils/shortString';
2
+
3
+ describe('shortString', () => {
4
+ test('should convert string to number', () => {
5
+ expect(encodeShortString('hello')).toMatchInlineSnapshot(`"0x68656c6c6f"`);
6
+ });
7
+ test('should convert number to string', () => {
8
+ expect(decodeShortString('0x68656c6c6f')).toMatchInlineSnapshot(`"hello"`);
9
+ });
10
+ test('should throw if string is too long', () => {
11
+ expect(() =>
12
+ encodeShortString('hello world hello world hello world hello world hello world hello world')
13
+ ).toThrowErrorMatchingInlineSnapshot(
14
+ `"hello world hello world hello world hello world hello world hello world is too long"`
15
+ );
16
+ });
17
+ test('should throw if string contains non ascii chars', () => {
18
+ expect(() => encodeShortString('hello\uD83D\uDE00')).toThrowErrorMatchingInlineSnapshot(
19
+ `"hello😀 is not an ASCII string"`
20
+ );
21
+ });
22
+ });
@@ -0,0 +1,32 @@
1
+ import { ONE } from '../../src/constants';
2
+ import { toBN } from '../../src/utils/number';
3
+ import { UINT_128_MAX, UINT_256_MAX, bnToUint256, uint256ToBN } from '../../src/utils/uint256';
4
+
5
+ describe('cairo uint256', () => {
6
+ test('should convert 0 from BN to uint256 struct', () => {
7
+ const uint256 = bnToUint256('0');
8
+ expect(uint256).toMatchInlineSnapshot(`
9
+ Object {
10
+ "high": "0x0",
11
+ "low": "0x0",
12
+ }
13
+ `);
14
+ });
15
+ test('should convert 0 from uint256 to BN', () => {
16
+ expect(uint256ToBN({ low: '0x0', high: '0x0' }).toString()).toMatchInlineSnapshot(`"0"`);
17
+ });
18
+ test('should convert BN over 2^128 to uint256 struct', () => {
19
+ const uint256 = bnToUint256(UINT_128_MAX.add(ONE));
20
+ expect(uint256).toMatchInlineSnapshot(`
21
+ Object {
22
+ "high": "0x1",
23
+ "low": "0x0",
24
+ }
25
+ `);
26
+ });
27
+ test('should throw if BN over uint256 range', () => {
28
+ expect(() => bnToUint256(UINT_256_MAX.add(toBN(1)))).toThrowErrorMatchingInlineSnapshot(
29
+ `"Number is too large"`
30
+ );
31
+ });
32
+ });
package/constants.d.ts CHANGED
@@ -1,9 +1,9 @@
1
- import BN from 'bn.js';
1
+ /// <reference types="bn.js" />
2
2
  export { IS_BROWSER } from './utils/encode';
3
- export declare const ZERO: BN;
4
- export declare const ONE: BN;
5
- export declare const TWO: BN;
6
- export declare const MASK_250: BN;
3
+ export declare const ZERO: import('bn.js');
4
+ export declare const ONE: import('bn.js');
5
+ export declare const TWO: import('bn.js');
6
+ export declare const MASK_250: import('bn.js');
7
7
  /**
8
8
  * The following is taken from https://github.com/starkware-libs/starkex-resources/blob/master/crypto/starkware/crypto/signature/pedersen_params.json but converted to hex, because JS is very bad handling big integers by default
9
9
  * Please do not edit until the JSON changes.
@@ -1,9 +1,9 @@
1
- import BN from 'bn.js';
1
+ /// <reference types="bn.js" />
2
2
  export { IS_BROWSER } from './utils/encode';
3
- export declare const ZERO: BN;
4
- export declare const ONE: BN;
5
- export declare const TWO: BN;
6
- export declare const MASK_250: BN;
3
+ export declare const ZERO: import("bn.js");
4
+ export declare const ONE: import("bn.js");
5
+ export declare const TWO: import("bn.js");
6
+ export declare const MASK_250: import("bn.js");
7
7
  /**
8
8
  * The following is taken from https://github.com/starkware-libs/starkex-resources/blob/master/crypto/starkware/crypto/signature/pedersen_params.json but converted to hex, because JS is very bad handling big integers by default
9
9
  * Please do not edit until the JSON changes.
package/dist/index.d.ts CHANGED
@@ -15,3 +15,5 @@ export * as json from './utils/json';
15
15
  export * as number from './utils/number';
16
16
  export * as stark from './utils/stark';
17
17
  export * as ec from './utils/ellipticCurve';
18
+ export * as uint256 from './utils/uint256';
19
+ export * as shortString from './utils/shortString';
package/dist/index.js CHANGED
@@ -22,7 +22,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
22
  return result;
23
23
  };
24
24
  Object.defineProperty(exports, "__esModule", { value: true });
25
- exports.ec = exports.stark = exports.number = exports.json = exports.hash = exports.encode = exports.constants = void 0;
25
+ exports.shortString = exports.uint256 = exports.ec = exports.stark = exports.number = exports.json = exports.hash = exports.encode = exports.constants = void 0;
26
26
  /**
27
27
  * Main
28
28
  */
@@ -40,3 +40,5 @@ exports.json = __importStar(require("./utils/json"));
40
40
  exports.number = __importStar(require("./utils/number"));
41
41
  exports.stark = __importStar(require("./utils/stark"));
42
42
  exports.ec = __importStar(require("./utils/ellipticCurve"));
43
+ exports.uint256 = __importStar(require("./utils/uint256"));
44
+ exports.shortString = __importStar(require("./utils/shortString"));
@@ -1,16 +1,18 @@
1
1
  import { AddTransactionResponse, CallContractResponse, CallContractTransaction, CompiledContract, GetBlockResponse, GetCodeResponse, GetContractAddressesResponse, GetTransactionResponse, GetTransactionStatusResponse, Transaction } from '../types';
2
2
  import { BigNumberish } from '../utils/number';
3
3
  import { ProviderInterface } from './interface';
4
- declare type NetworkName = 'alpha';
5
- interface ProviderOptions {
6
- network?: NetworkName;
7
- }
4
+ declare type NetworkName = 'mainnet-alpha' | 'georli-alpha';
5
+ declare type ProviderOptions = {
6
+ network: NetworkName;
7
+ } | {
8
+ baseUrl: string;
9
+ };
8
10
  export declare class Provider implements ProviderInterface {
9
11
  baseUrl: string;
10
12
  feederGatewayUrl: string;
11
13
  gatewayUrl: string;
12
14
  constructor(optionsOrProvider?: ProviderOptions | Provider);
13
- protected static getNetworkFromName(name: NetworkName): string;
15
+ protected static getNetworkFromName(name: NetworkName): "https://alpha-mainnet.starknet.io" | "https://alpha4.starknet.io";
14
16
  /**
15
17
  * Gets the smart contract address on the goerli testnet.
16
18
  *
@@ -52,6 +52,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
52
52
  Object.defineProperty(exports, "__esModule", { value: true });
53
53
  exports.Provider = void 0;
54
54
  var axios_1 = __importDefault(require("axios"));
55
+ var url_join_1 = __importDefault(require("url-join"));
55
56
  var json_1 = require("../utils/json");
56
57
  var number_1 = require("../utils/number");
57
58
  var stark_1 = require("../utils/stark");
@@ -60,22 +61,26 @@ function wait(delay) {
60
61
  }
61
62
  var Provider = /** @class */ (function () {
62
63
  function Provider(optionsOrProvider) {
64
+ if (optionsOrProvider === void 0) { optionsOrProvider = { network: 'georli-alpha' }; }
63
65
  if (optionsOrProvider instanceof Provider) {
64
66
  this.baseUrl = optionsOrProvider.baseUrl;
65
67
  this.feederGatewayUrl = optionsOrProvider.feederGatewayUrl;
66
68
  this.gatewayUrl = optionsOrProvider.gatewayUrl;
67
69
  }
68
70
  else {
69
- var _a = (optionsOrProvider || {}).network, network = _a === void 0 ? 'alpha' : _a;
70
- var baseUrl = Provider.getNetworkFromName(network);
71
+ var baseUrl = 'baseUrl' in optionsOrProvider
72
+ ? optionsOrProvider.baseUrl
73
+ : Provider.getNetworkFromName(optionsOrProvider.network);
71
74
  this.baseUrl = baseUrl;
72
- this.feederGatewayUrl = baseUrl + "/feeder_gateway";
73
- this.gatewayUrl = baseUrl + "/gateway";
75
+ this.feederGatewayUrl = (0, url_join_1.default)(baseUrl, 'feeder_gateway');
76
+ this.gatewayUrl = (0, url_join_1.default)(baseUrl, 'gateway');
74
77
  }
75
78
  }
76
79
  Provider.getNetworkFromName = function (name) {
77
80
  switch (name) {
78
- case 'alpha':
81
+ case 'mainnet-alpha':
82
+ return 'https://alpha-mainnet.starknet.io';
83
+ case 'georli-alpha':
79
84
  default:
80
85
  return 'https://alpha4.starknet.io';
81
86
  }
@@ -91,7 +96,7 @@ var Provider = /** @class */ (function () {
91
96
  var data;
92
97
  return __generator(this, function (_a) {
93
98
  switch (_a.label) {
94
- case 0: return [4 /*yield*/, axios_1.default.get(this.feederGatewayUrl + "/get_contract_addresses")];
99
+ case 0: return [4 /*yield*/, axios_1.default.get((0, url_join_1.default)(this.feederGatewayUrl, 'get_contract_addresses'))];
95
100
  case 1:
96
101
  data = (_a.sent()).data;
97
102
  return [2 /*return*/, data];
@@ -113,7 +118,7 @@ var Provider = /** @class */ (function () {
113
118
  var data;
114
119
  return __generator(this, function (_a) {
115
120
  switch (_a.label) {
116
- case 0: return [4 /*yield*/, axios_1.default.post(this.feederGatewayUrl + "/call_contract?blockId=" + (blockId !== null && blockId !== void 0 ? blockId : 'null'), __assign({ signature: [], calldata: [] }, invokeTx))];
121
+ case 0: return [4 /*yield*/, axios_1.default.post((0, url_join_1.default)(this.feederGatewayUrl, 'call_contract', "?blockId=" + (blockId !== null && blockId !== void 0 ? blockId : 'null')), __assign({ signature: [], calldata: [] }, invokeTx))];
117
122
  case 1:
118
123
  data = (_a.sent()).data;
119
124
  return [2 /*return*/, data];
@@ -134,7 +139,7 @@ var Provider = /** @class */ (function () {
134
139
  var data;
135
140
  return __generator(this, function (_a) {
136
141
  switch (_a.label) {
137
- case 0: return [4 /*yield*/, axios_1.default.get(this.feederGatewayUrl + "/get_block?blockId=" + (blockId !== null && blockId !== void 0 ? blockId : 'null'))];
142
+ case 0: return [4 /*yield*/, axios_1.default.get((0, url_join_1.default)(this.feederGatewayUrl, 'get_block', "?blockId=" + (blockId !== null && blockId !== void 0 ? blockId : 'null')))];
138
143
  case 1:
139
144
  data = (_a.sent()).data;
140
145
  return [2 /*return*/, data];
@@ -156,7 +161,7 @@ var Provider = /** @class */ (function () {
156
161
  var data;
157
162
  return __generator(this, function (_a) {
158
163
  switch (_a.label) {
159
- case 0: return [4 /*yield*/, axios_1.default.get(this.feederGatewayUrl + "/get_code?contractAddress=" + contractAddress + "&blockId=" + (blockId !== null && blockId !== void 0 ? blockId : 'null'))];
164
+ case 0: return [4 /*yield*/, axios_1.default.get((0, url_join_1.default)(this.feederGatewayUrl, 'get_code', "?contractAddress=" + contractAddress + "&blockId=" + (blockId !== null && blockId !== void 0 ? blockId : 'null')))];
160
165
  case 1:
161
166
  data = (_a.sent()).data;
162
167
  return [2 /*return*/, data];
@@ -180,7 +185,7 @@ var Provider = /** @class */ (function () {
180
185
  var data;
181
186
  return __generator(this, function (_a) {
182
187
  switch (_a.label) {
183
- case 0: return [4 /*yield*/, axios_1.default.get(this.feederGatewayUrl + "/get_storage_at?contractAddress=" + contractAddress + "&key=" + key + "&blockId=" + (blockId !== null && blockId !== void 0 ? blockId : 'null'))];
188
+ case 0: return [4 /*yield*/, axios_1.default.get((0, url_join_1.default)(this.feederGatewayUrl, 'get_storage_at', "?contractAddress=" + contractAddress + "&key=" + key + "&blockId=" + (blockId !== null && blockId !== void 0 ? blockId : 'null')))];
184
189
  case 1:
185
190
  data = (_a.sent()).data;
186
191
  return [2 /*return*/, data];
@@ -203,7 +208,7 @@ var Provider = /** @class */ (function () {
203
208
  switch (_a.label) {
204
209
  case 0:
205
210
  txHashBn = (0, number_1.toBN)(txHash);
206
- return [4 /*yield*/, axios_1.default.get(this.feederGatewayUrl + "/get_transaction_status?transactionHash=" + (0, number_1.toHex)(txHashBn))];
211
+ return [4 /*yield*/, axios_1.default.get((0, url_join_1.default)(this.feederGatewayUrl, 'get_transaction_status', "?transactionHash=" + (0, number_1.toHex)(txHashBn)))];
207
212
  case 1:
208
213
  data = (_a.sent()).data;
209
214
  return [2 /*return*/, data];
@@ -226,7 +231,7 @@ var Provider = /** @class */ (function () {
226
231
  switch (_a.label) {
227
232
  case 0:
228
233
  txHashBn = (0, number_1.toBN)(txHash);
229
- return [4 /*yield*/, axios_1.default.get(this.feederGatewayUrl + "/get_transaction?transactionHash=" + (0, number_1.toHex)(txHashBn))];
234
+ return [4 /*yield*/, axios_1.default.get((0, url_join_1.default)(this.feederGatewayUrl, 'get_transaction', "?transactionHash=" + (0, number_1.toHex)(txHashBn)))];
230
235
  case 1:
231
236
  data = (_a.sent()).data;
232
237
  return [2 /*return*/, data];
@@ -250,7 +255,7 @@ var Provider = /** @class */ (function () {
250
255
  case 0:
251
256
  signature = tx.type === 'INVOKE_FUNCTION' && (0, stark_1.formatSignature)(tx.signature);
252
257
  contract_address_salt = tx.type === 'DEPLOY' && (0, number_1.toHex)((0, number_1.toBN)(tx.contract_address_salt));
253
- return [4 /*yield*/, axios_1.default.post(this.gatewayUrl + "/add_transaction", (0, json_1.stringify)(__assign(__assign(__assign({}, tx), (Array.isArray(signature) && { signature: signature })), (contract_address_salt && { contract_address_salt: contract_address_salt }))), { headers: { 'Content-Type': 'application/json' } })];
258
+ return [4 /*yield*/, axios_1.default.post((0, url_join_1.default)(this.gatewayUrl, 'add_transaction'), (0, json_1.stringify)(__assign(__assign(__assign({}, tx), (Array.isArray(signature) && { signature: signature })), (contract_address_salt && { contract_address_salt: contract_address_salt }))), { headers: { 'Content-Type': 'application/json' } })];
254
259
  case 1:
255
260
  data = (_a.sent()).data;
256
261
  return [2 /*return*/, data];
@@ -296,14 +301,13 @@ var Provider = /** @class */ (function () {
296
301
  });
297
302
  };
298
303
  Provider.prototype.waitForTx = function (txHash, retryInterval) {
299
- if (retryInterval === void 0) { retryInterval = 5000; }
304
+ if (retryInterval === void 0) { retryInterval = 8000; }
300
305
  return __awaiter(this, void 0, void 0, function () {
301
- var onchain, firstRun, res;
306
+ var onchain, res;
302
307
  return __generator(this, function (_a) {
303
308
  switch (_a.label) {
304
309
  case 0:
305
310
  onchain = false;
306
- firstRun = true;
307
311
  _a.label = 1;
308
312
  case 1:
309
313
  if (!!onchain) return [3 /*break*/, 4];
@@ -315,16 +319,17 @@ var Provider = /** @class */ (function () {
315
319
  return [4 /*yield*/, this.getTransactionStatus(txHash)];
316
320
  case 3:
317
321
  res = _a.sent();
318
- if (res.tx_status === 'ACCEPTED_ONCHAIN' || res.tx_status === 'PENDING') {
322
+ if (res.tx_status === 'ACCEPTED_ONCHAIN' ||
323
+ (res.tx_status === 'PENDING' && res.block_hash !== 'pending') // This is needed as of today. In the future there will be a different status for pending transactions.
324
+ ) {
319
325
  onchain = true;
320
326
  }
321
327
  else if (res.tx_status === 'REJECTED') {
322
328
  throw Error('REJECTED');
323
329
  }
324
- else if (res.tx_status === 'NOT_RECEIVED' && !firstRun) {
330
+ else if (res.tx_status === 'NOT_RECEIVED') {
325
331
  throw Error('NOT_RECEIVED');
326
332
  }
327
- firstRun = false;
328
333
  return [3 /*break*/, 1];
329
334
  case 4: return [2 /*return*/];
330
335
  }
package/dist/types.d.ts CHANGED
@@ -61,14 +61,14 @@ export declare type CallContractResponse = {
61
61
  export declare type GetBlockResponse = {
62
62
  sequence_number: number;
63
63
  state_root: string;
64
- block_id: number;
64
+ block_hash: string;
65
65
  transactions: {
66
66
  [txHash: string]: Transaction;
67
67
  };
68
68
  timestamp: number;
69
69
  transaction_receipts: {
70
70
  [txHash: string]: {
71
- block_id: number;
71
+ block_hash: string;
72
72
  transaction_hash: string;
73
73
  l2_to_l1_messages: {
74
74
  to_address: string;
@@ -80,7 +80,7 @@ export declare type GetBlockResponse = {
80
80
  transaction_index: number;
81
81
  };
82
82
  };
83
- previous_block_id: number;
83
+ previous_block_hash: string;
84
84
  status: Status;
85
85
  };
86
86
  export declare type GetCodeResponse = {
@@ -89,12 +89,12 @@ export declare type GetCodeResponse = {
89
89
  };
90
90
  export declare type GetTransactionStatusResponse = {
91
91
  tx_status: Status;
92
- block_id: number;
92
+ block_hash: string;
93
93
  };
94
94
  export declare type GetTransactionResponse = {
95
95
  status: Status;
96
96
  transaction: Transaction;
97
- block_id: number;
97
+ block_hash: string;
98
98
  block_number: number;
99
99
  transaction_index: number;
100
100
  transaction_hash: string;
@@ -0,0 +1,4 @@
1
+ export declare function isASCII(str: string): boolean;
2
+ export declare function isShortString(str: string): boolean;
3
+ export declare function encodeShortString(str: string): string;
4
+ export declare function decodeShortString(str: string): string;
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.decodeShortString = exports.encodeShortString = exports.isShortString = exports.isASCII = void 0;
4
+ var encode_1 = require("./encode");
5
+ function isASCII(str) {
6
+ // eslint-disable-next-line no-control-regex
7
+ return /^[\x00-\x7F]*$/.test(str);
8
+ }
9
+ exports.isASCII = isASCII;
10
+ // function to check if string has less or equal 31 characters
11
+ function isShortString(str) {
12
+ return str.length <= 31;
13
+ }
14
+ exports.isShortString = isShortString;
15
+ function encodeShortString(str) {
16
+ if (!isASCII(str))
17
+ throw new Error(str + " is not an ASCII string");
18
+ if (!isShortString(str))
19
+ throw new Error(str + " is too long");
20
+ return (0, encode_1.addHexPrefix)(str.replace(/./g, function (char) { return char.charCodeAt(0).toString(16); }));
21
+ }
22
+ exports.encodeShortString = encodeShortString;
23
+ function decodeShortString(str) {
24
+ return (0, encode_1.removeHexPrefix)(str).replace(/.{2}/g, function (hex) { return String.fromCharCode(parseInt(hex, 16)); });
25
+ }
26
+ exports.decodeShortString = decodeShortString;
@@ -19,3 +19,6 @@ export declare function getSelectorFromName(funcName: string): string;
19
19
  export declare function randomAddress(): string;
20
20
  export declare function makeAddress(input: string): string;
21
21
  export declare function formatSignature(sig?: [BigNumberish, BigNumberish]): [string, string] | [];
22
+ export declare function compileStructToCalldata<S extends {
23
+ [k: string]: string;
24
+ }>(struct: S): string[];
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.formatSignature = exports.makeAddress = exports.randomAddress = exports.getSelectorFromName = exports.compressProgram = void 0;
3
+ exports.compileStructToCalldata = exports.formatSignature = exports.makeAddress = exports.randomAddress = exports.getSelectorFromName = exports.compressProgram = void 0;
4
4
  var pako_1 = require("pako");
5
5
  var ellipticCurve_1 = require("./ellipticCurve");
6
6
  var encode_1 = require("./encode");
@@ -52,3 +52,7 @@ function formatSignature(sig) {
52
52
  }
53
53
  }
54
54
  exports.formatSignature = formatSignature;
55
+ function compileStructToCalldata(struct) {
56
+ return Object.values(struct);
57
+ }
58
+ exports.compileStructToCalldata = compileStructToCalldata;
@@ -0,0 +1,11 @@
1
+ /// <reference types="bn.js" />
2
+ import { BigNumberish } from './number';
3
+ export interface Uint256 {
4
+ low: BigNumberish;
5
+ high: BigNumberish;
6
+ }
7
+ export declare function uint256ToBN(uint256: Uint256): import("bn.js");
8
+ export declare const UINT_128_MAX: import("bn.js");
9
+ export declare const UINT_256_MAX: import("bn.js");
10
+ export declare function isUint256(bn: BigNumberish): boolean;
11
+ export declare function bnToUint256(bignumber: BigNumberish): Uint256;
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.bnToUint256 = exports.isUint256 = exports.UINT_256_MAX = exports.UINT_128_MAX = exports.uint256ToBN = void 0;
4
+ var encode_1 = require("./encode");
5
+ var number_1 = require("./number");
6
+ // function to convert Uint256 to BN
7
+ function uint256ToBN(uint256) {
8
+ return (0, number_1.toBN)(uint256.high).shln(128).add((0, number_1.toBN)(uint256.low));
9
+ }
10
+ exports.uint256ToBN = uint256ToBN;
11
+ exports.UINT_128_MAX = (0, number_1.toBN)(1).shln(128).sub((0, number_1.toBN)(1));
12
+ exports.UINT_256_MAX = (0, number_1.toBN)(1).shln(256).sub((0, number_1.toBN)(1));
13
+ // function to check if BN is smaller or equal 2**256-1
14
+ function isUint256(bn) {
15
+ return (0, number_1.toBN)(bn).lte(exports.UINT_256_MAX);
16
+ }
17
+ exports.isUint256 = isUint256;
18
+ // function to convert BN to Uint256
19
+ function bnToUint256(bignumber) {
20
+ var bn = (0, number_1.toBN)(bignumber);
21
+ if (!isUint256(bn))
22
+ throw new Error('Number is too large');
23
+ return {
24
+ low: (0, encode_1.addHexPrefix)(bn.maskn(128).toString(16)),
25
+ high: (0, encode_1.addHexPrefix)(bn.shrn(128).toString(16)),
26
+ };
27
+ }
28
+ exports.bnToUint256 = bnToUint256;
package/index.d.ts CHANGED
@@ -15,3 +15,5 @@ export * as json from './utils/json';
15
15
  export * as number from './utils/number';
16
16
  export * as stark from './utils/stark';
17
17
  export * as ec from './utils/ellipticCurve';
18
+ export * as uint256 from './utils/uint256';
19
+ export * as shortString from './utils/shortString';
package/index.js CHANGED
@@ -44,7 +44,9 @@ var __importStar =
44
44
  return result;
45
45
  };
46
46
  Object.defineProperty(exports, '__esModule', { value: true });
47
- exports.ec =
47
+ exports.shortString =
48
+ exports.uint256 =
49
+ exports.ec =
48
50
  exports.stark =
49
51
  exports.number =
50
52
  exports.json =
@@ -69,3 +71,5 @@ exports.json = __importStar(require('./utils/json'));
69
71
  exports.number = __importStar(require('./utils/number'));
70
72
  exports.stark = __importStar(require('./utils/stark'));
71
73
  exports.ec = __importStar(require('./utils/ellipticCurve'));
74
+ exports.uint256 = __importStar(require('./utils/uint256'));
75
+ exports.shortString = __importStar(require('./utils/shortString'));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starknet",
3
- "version": "2.0.2",
3
+ "version": "2.3.0",
4
4
  "description": "JavaScript library for StarkNet",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -40,6 +40,7 @@
40
40
  "@types/json-bigint": "^1.0.1",
41
41
  "@types/minimalistic-assert": "^1.0.1",
42
42
  "@types/pako": "^1.0.2",
43
+ "@types/url-join": "^4.0.1",
43
44
  "@typescript-eslint/eslint-plugin": "^5.0.0",
44
45
  "@typescript-eslint/parser": "^5.0.0",
45
46
  "eslint": "^7.32.0",
@@ -65,7 +66,8 @@
65
66
  "hash.js": "^1.1.7",
66
67
  "json-bigint": "^1.0.0",
67
68
  "minimalistic-assert": "^1.0.1",
68
- "pako": "^2.0.4"
69
+ "pako": "^2.0.4",
70
+ "url-join": "^4.0.1"
69
71
  },
70
72
  "lint-staged": {
71
73
  "*.ts": "eslint --cache --fix",