starknet 2.1.1 → 2.4.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 (47) hide show
  1. package/CHANGELOG.md +45 -0
  2. package/__tests__/signer.test.ts +17 -0
  3. package/__tests__/utils/shortString.test.ts +22 -0
  4. package/__tests__/utils/uint256.test.ts +32 -0
  5. package/constants.d.ts +5 -5
  6. package/contract.d.ts +7 -1
  7. package/contract.js +23 -5
  8. package/dist/constants.d.ts +5 -5
  9. package/dist/contract.d.ts +4 -1
  10. package/dist/contract.js +19 -5
  11. package/dist/index.d.ts +2 -0
  12. package/dist/index.js +3 -1
  13. package/dist/provider/default.d.ts +5 -5
  14. package/dist/provider/default.js +18 -17
  15. package/dist/provider/interface.d.ts +4 -4
  16. package/dist/signer/default.d.ts +2 -2
  17. package/dist/signer/default.js +20 -15
  18. package/dist/signer/interface.d.ts +2 -2
  19. package/dist/types.d.ts +4 -2
  20. package/dist/utils/shortString.d.ts +4 -0
  21. package/dist/utils/shortString.js +26 -0
  22. package/dist/utils/uint256.d.ts +11 -0
  23. package/dist/utils/uint256.js +28 -0
  24. package/index.d.ts +2 -0
  25. package/index.js +5 -1
  26. package/package.json +5 -2
  27. package/provider/default.d.ts +8 -5
  28. package/provider/default.js +57 -38
  29. package/provider/interface.d.ts +4 -4
  30. package/signer/default.d.ts +2 -2
  31. package/signer/default.js +18 -12
  32. package/signer/interface.d.ts +2 -2
  33. package/src/constants.ts +4 -6
  34. package/src/contract.ts +15 -12
  35. package/src/index.ts +2 -0
  36. package/src/provider/default.ts +34 -23
  37. package/src/provider/interface.ts +4 -4
  38. package/src/signer/default.ts +23 -14
  39. package/src/signer/interface.ts +4 -2
  40. package/src/types.ts +4 -2
  41. package/src/utils/shortString.ts +21 -0
  42. package/src/utils/uint256.ts +32 -0
  43. package/types.d.ts +4 -2
  44. package/utils/shortString.d.ts +4 -0
  45. package/utils/shortString.js +34 -0
  46. package/utils/uint256.d.ts +11 -0
  47. package/utils/uint256.js +38 -0
package/CHANGELOG.md CHANGED
@@ -1,9 +1,54 @@
1
+ # [2.4.0](https://github.com/seanjameshan/starknet.js/compare/v2.3.1...v2.4.0) (2021-12-09)
2
+
3
+ ### Features
4
+
5
+ - allow custom nonce ([17666de](https://github.com/seanjameshan/starknet.js/commit/17666de94db6875bbf8e88555773b8862a4a32cf))
6
+
7
+ ## [2.3.1](https://github.com/seanjameshan/starknet.js/compare/v2.3.0...v2.3.1) (2021-12-01)
8
+
9
+ ### Bug Fixes
10
+
11
+ - allow structs in calldata ([fd2bdc0](https://github.com/seanjameshan/starknet.js/commit/fd2bdc0a1756544e4162fa5baaa7d3aec6f97bee))
12
+ - struct compiling ([84617ae](https://github.com/seanjameshan/starknet.js/commit/84617aefe1218f421ca446b2a3c9959252d326e6))
13
+ - struct data ([313dff5](https://github.com/seanjameshan/starknet.js/commit/313dff57f54050747c65b32b3378762c349d9c8c))
14
+ - uint256 type ([2e05902](https://github.com/seanjameshan/starknet.js/commit/2e05902268f76bda2ae9b4e75782fa78876c2f00))
15
+
16
+ # [2.3.0](https://github.com/seanjameshan/starknet.js/compare/v2.2.0...v2.3.0) (2021-12-01)
17
+
18
+ ### Features
19
+
20
+ - add compileStructToCalldata ([e5bdb18](https://github.com/seanjameshan/starknet.js/commit/e5bdb1890e0bc53b03d4b145069cf7fbc639e830))
21
+ - **utils:** support shortstring and uint256 ([f7ff057](https://github.com/seanjameshan/starknet.js/commit/f7ff05753d9bc39b31bdd4e7f893ee04cab77823))
22
+
23
+ # [2.2.0](https://github.com/seanjameshan/starknet.js/compare/v2.1.0...v2.2.0) (2021-11-30)
24
+
25
+ ### Bug Fixes
26
+
27
+ - bump version ([02c8d07](https://github.com/seanjameshan/starknet.js/commit/02c8d0772e42c81e35a3a841169eb25cde68716e))
28
+ - correctly parse structs in Starknet return types ([5a4a318](https://github.com/seanjameshan/starknet.js/commit/5a4a318dad4c78fe84540ad92063fc1879317ac1))
29
+ - make Typescript compiler happy with constant types ([aedd895](https://github.com/seanjameshan/starknet.js/commit/aedd895a62e6018dd1d7330b004d54360007967f))
30
+ - use urljoin ([4f1a040](https://github.com/seanjameshan/starknet.js/commit/4f1a04090f26f8e8565c516921d5d3332b6a4649))
31
+
32
+ ### Features
33
+
34
+ - bump version ([fd22f41](https://github.com/seanjameshan/starknet.js/commit/fd22f41e39ec1c7f71c32019309f82ad0f4d66a9))
35
+
36
+ ## [2.1.1](https://github.com/seanjameshan/starknet.js/compare/v2.1.0...v2.1.1) (2021-11-30)
37
+
38
+ ### Bug Fixes
39
+
40
+ - bump version ([02c8d07](https://github.com/seanjameshan/starknet.js/commit/02c8d0772e42c81e35a3a841169eb25cde68716e))
41
+ - correctly parse structs in Starknet return types ([5a4a318](https://github.com/seanjameshan/starknet.js/commit/5a4a318dad4c78fe84540ad92063fc1879317ac1))
42
+ - make Typescript compiler happy with constant types ([aedd895](https://github.com/seanjameshan/starknet.js/commit/aedd895a62e6018dd1d7330b004d54360007967f))
43
+ - use urljoin ([4f1a040](https://github.com/seanjameshan/starknet.js/commit/4f1a04090f26f8e8565c516921d5d3332b6a4649))
44
+
1
45
  ## [2.1.1](https://github.com/seanjameshan/starknet.js/compare/v2.1.0...v2.1.1) (2021-11-30)
2
46
 
3
47
  ### Bug Fixes
4
48
 
5
49
  - correctly parse structs in Starknet return types ([5a4a318](https://github.com/seanjameshan/starknet.js/commit/5a4a318dad4c78fe84540ad92063fc1879317ac1))
6
50
  - make Typescript compiler happy with constant types ([aedd895](https://github.com/seanjameshan/starknet.js/commit/aedd895a62e6018dd1d7330b004d54360007967f))
51
+ - use urljoin ([4f1a040](https://github.com/seanjameshan/starknet.js/commit/4f1a04090f26f8e8565c516921d5d3332b6a4649))
7
52
 
8
53
  # [2.1.0](https://github.com/seanjameshan/starknet.js/compare/v2.0.1...v2.1.0) (2021-11-30)
9
54
 
@@ -99,4 +99,21 @@ describe('deploy and test Wallet', () => {
99
99
 
100
100
  expect(number.toBN(res as string).toString()).toStrictEqual(number.toBN(990).toString());
101
101
  });
102
+ test('execute with custom nonce', async () => {
103
+ const { result } = await signer.callContract({
104
+ contract_address: signer.address,
105
+ entry_point_selector: stark.getSelectorFromName('get_nonce'),
106
+ });
107
+ const nonce = parseInt(result[0], 10);
108
+ const { code, transaction_hash } = await signer.addTransaction({
109
+ type: 'INVOKE_FUNCTION',
110
+ contract_address: erc20Address,
111
+ entry_point_selector: stark.getSelectorFromName('transfer'),
112
+ calldata: [erc20Address, '10'],
113
+ nonce,
114
+ });
115
+
116
+ expect(code).toBe('TRANSACTION_RECEIVED');
117
+ await defaultProvider.waitForTx(transaction_hash);
118
+ });
102
119
  });
@@ -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.
package/contract.d.ts CHANGED
@@ -2,7 +2,13 @@ import { Provider } from './provider';
2
2
  import { Abi, StructAbi } from './types';
3
3
  import { BigNumberish } from './utils/number';
4
4
  export declare type Args = {
5
- [inputName: string]: string | string[];
5
+ [inputName: string]:
6
+ | string
7
+ | string[]
8
+ | {
9
+ type: 'struct';
10
+ [k: string]: BigNumberish;
11
+ };
6
12
  };
7
13
  export declare type Calldata = string[];
8
14
  export declare function compileCalldata(args: Args): Calldata;
package/contract.js CHANGED
@@ -216,6 +216,18 @@ function compileCalldata(args) {
216
216
  ),
217
217
  false
218
218
  );
219
+ if (typeof value === 'object' && 'type' in value)
220
+ return Object.entries(value)
221
+ .filter(function (_a) {
222
+ var _b = __read(_a, 1),
223
+ k = _b[0];
224
+ return k !== 'type';
225
+ })
226
+ .map(function (_a) {
227
+ var _b = __read(_a, 2),
228
+ v = _b[1];
229
+ return (0, number_1.toBN)(v).toString();
230
+ });
219
231
  return (0, number_1.toBN)(value).toString();
220
232
  });
221
233
  }
@@ -274,22 +286,28 @@ var Contract = /** @class */ (function () {
274
286
  return abi.name === method && abi.type === 'function';
275
287
  });
276
288
  methodAbi.inputs.forEach(function (input) {
277
- if (args[input.name] !== undefined) {
289
+ var arg = args[input.name];
290
+ if (arg !== undefined) {
278
291
  if (input.type === 'felt') {
279
292
  (0, minimalistic_assert_1.default)(
280
- typeof args[input.name] === 'string',
293
+ typeof arg === 'string',
281
294
  'arg ' + input.name + ' should be a felt (string)'
282
295
  );
283
296
  (0, minimalistic_assert_1.default)(
284
- isFelt(args[input.name]),
297
+ isFelt(arg),
285
298
  'arg ' + input.name + ' should be decimal or hexadecimal'
286
299
  );
300
+ } else if (typeof arg === 'object' && 'type' in arg) {
301
+ (0, minimalistic_assert_1.default)(
302
+ arg.type === 'struct',
303
+ 'arg ' + input.name + ' should be a struct'
304
+ );
287
305
  } else {
288
306
  (0, minimalistic_assert_1.default)(
289
- Array.isArray(args[input.name]),
307
+ Array.isArray(arg),
290
308
  'arg ' + input.name + ' should be a felt* (string[])'
291
309
  );
292
- args[input.name].forEach(function (felt, i) {
310
+ arg.forEach(function (felt, i) {
293
311
  (0,
294
312
  minimalistic_assert_1.default)(typeof felt === 'string', 'arg ' + input.name + '[' + i + '] should be a felt (string) as part of a felt* (string[])');
295
313
  (0,
@@ -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.
@@ -2,7 +2,10 @@ import { Provider } from './provider';
2
2
  import { Abi, StructAbi } from './types';
3
3
  import { BigNumberish } from './utils/number';
4
4
  export declare type Args = {
5
- [inputName: string]: string | string[];
5
+ [inputName: string]: string | string[] | {
6
+ type: 'struct';
7
+ [k: string]: BigNumberish;
8
+ };
6
9
  };
7
10
  export declare type Calldata = string[];
8
11
  export declare function compileCalldata(args: Args): Calldata;
package/dist/contract.js CHANGED
@@ -101,6 +101,16 @@ function compileCalldata(args) {
101
101
  return Object.values(args).flatMap(function (value) {
102
102
  if (Array.isArray(value))
103
103
  return __spreadArray([(0, number_1.toBN)(value.length).toString()], __read(value.map(function (x) { return (0, number_1.toBN)(x).toString(); })), false);
104
+ if (typeof value === 'object' && 'type' in value)
105
+ return Object.entries(value)
106
+ .filter(function (_a) {
107
+ var _b = __read(_a, 1), k = _b[0];
108
+ return k !== 'type';
109
+ })
110
+ .map(function (_a) {
111
+ var _b = __read(_a, 2), v = _b[1];
112
+ return (0, number_1.toBN)(v).toString();
113
+ });
104
114
  return (0, number_1.toBN)(value).toString();
105
115
  });
106
116
  }
@@ -145,14 +155,18 @@ var Contract = /** @class */ (function () {
145
155
  // ensure args match abi type
146
156
  var methodAbi = this.abi.find(function (abi) { return abi.name === method && abi.type === 'function'; });
147
157
  methodAbi.inputs.forEach(function (input) {
148
- if (args[input.name] !== undefined) {
158
+ var arg = args[input.name];
159
+ if (arg !== undefined) {
149
160
  if (input.type === 'felt') {
150
- (0, minimalistic_assert_1.default)(typeof args[input.name] === 'string', "arg " + input.name + " should be a felt (string)");
151
- (0, minimalistic_assert_1.default)(isFelt(args[input.name]), "arg " + input.name + " should be decimal or hexadecimal");
161
+ (0, minimalistic_assert_1.default)(typeof arg === 'string', "arg " + input.name + " should be a felt (string)");
162
+ (0, minimalistic_assert_1.default)(isFelt(arg), "arg " + input.name + " should be decimal or hexadecimal");
163
+ }
164
+ else if (typeof arg === 'object' && 'type' in arg) {
165
+ (0, minimalistic_assert_1.default)(arg.type === 'struct', "arg " + input.name + " should be a struct");
152
166
  }
153
167
  else {
154
- (0, minimalistic_assert_1.default)(Array.isArray(args[input.name]), "arg " + input.name + " should be a felt* (string[])");
155
- args[input.name].forEach(function (felt, i) {
168
+ (0, minimalistic_assert_1.default)(Array.isArray(arg), "arg " + input.name + " should be a felt* (string[])");
169
+ arg.forEach(function (felt, i) {
156
170
  (0, minimalistic_assert_1.default)(typeof felt === 'string', "arg " + input.name + "[" + i + "] should be a felt (string) as part of a felt* (string[])");
157
171
  (0, minimalistic_assert_1.default)(isFelt(felt), "arg " + input.name + "[" + i + "] should be decimal or hexadecimal as part of a felt* (string[])");
158
172
  });
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"));
@@ -12,7 +12,7 @@ export declare class Provider implements ProviderInterface {
12
12
  feederGatewayUrl: string;
13
13
  gatewayUrl: string;
14
14
  constructor(optionsOrProvider?: ProviderOptions | Provider);
15
- protected static getNetworkFromName(name: NetworkName): "http://alpha-mainnet.starknet.io/" | "https://alpha4.starknet.io";
15
+ protected static getNetworkFromName(name: NetworkName): "https://alpha-mainnet.starknet.io" | "https://alpha4.starknet.io";
16
16
  /**
17
17
  * Gets the smart contract address on the goerli testnet.
18
18
  *
@@ -25,11 +25,11 @@ export declare class Provider implements ProviderInterface {
25
25
  *
26
26
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L17-L25)
27
27
  *
28
- * @param invokeTx - transaction to be invoked
28
+ * @param invokeTransaction - transaction to be invoked
29
29
  * @param blockId
30
30
  * @returns the result of the function on the smart contract.
31
31
  */
32
- callContract(invokeTx: CallContractTransaction, blockId?: number): Promise<CallContractResponse>;
32
+ callContract(invokeTransaction: CallContractTransaction, blockId?: number): Promise<CallContractResponse>;
33
33
  /**
34
34
  * Gets the block information from a block ID.
35
35
  *
@@ -83,10 +83,10 @@ export declare class Provider implements ProviderInterface {
83
83
  *
84
84
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
85
85
  *
86
- * @param tx - transaction to be invoked
86
+ * @param transaction - transaction to be invoked
87
87
  * @returns a confirmation of invoking a function on the starknet contract
88
88
  */
89
- addTransaction(tx: Transaction): Promise<AddTransactionResponse>;
89
+ addTransaction(transaction: Transaction): Promise<AddTransactionResponse>;
90
90
  /**
91
91
  * Deploys a given compiled contract (json) to starknet
92
92
  *
@@ -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");
@@ -71,14 +72,14 @@ var Provider = /** @class */ (function () {
71
72
  ? optionsOrProvider.baseUrl
72
73
  : Provider.getNetworkFromName(optionsOrProvider.network);
73
74
  this.baseUrl = baseUrl;
74
- this.feederGatewayUrl = baseUrl + "/feeder_gateway";
75
- 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');
76
77
  }
77
78
  }
78
79
  Provider.getNetworkFromName = function (name) {
79
80
  switch (name) {
80
81
  case 'mainnet-alpha':
81
- return 'http://alpha-mainnet.starknet.io/';
82
+ return 'https://alpha-mainnet.starknet.io';
82
83
  case 'georli-alpha':
83
84
  default:
84
85
  return 'https://alpha4.starknet.io';
@@ -95,7 +96,7 @@ var Provider = /** @class */ (function () {
95
96
  var data;
96
97
  return __generator(this, function (_a) {
97
98
  switch (_a.label) {
98
- 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'))];
99
100
  case 1:
100
101
  data = (_a.sent()).data;
101
102
  return [2 /*return*/, data];
@@ -108,16 +109,16 @@ var Provider = /** @class */ (function () {
108
109
  *
109
110
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L17-L25)
110
111
  *
111
- * @param invokeTx - transaction to be invoked
112
+ * @param invokeTransaction - transaction to be invoked
112
113
  * @param blockId
113
114
  * @returns the result of the function on the smart contract.
114
115
  */
115
- Provider.prototype.callContract = function (invokeTx, blockId) {
116
+ Provider.prototype.callContract = function (invokeTransaction, blockId) {
116
117
  return __awaiter(this, void 0, void 0, function () {
117
118
  var data;
118
119
  return __generator(this, function (_a) {
119
120
  switch (_a.label) {
120
- 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: [] }, invokeTransaction))];
121
122
  case 1:
122
123
  data = (_a.sent()).data;
123
124
  return [2 /*return*/, data];
@@ -138,7 +139,7 @@ var Provider = /** @class */ (function () {
138
139
  var data;
139
140
  return __generator(this, function (_a) {
140
141
  switch (_a.label) {
141
- 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')))];
142
143
  case 1:
143
144
  data = (_a.sent()).data;
144
145
  return [2 /*return*/, data];
@@ -160,7 +161,7 @@ var Provider = /** @class */ (function () {
160
161
  var data;
161
162
  return __generator(this, function (_a) {
162
163
  switch (_a.label) {
163
- 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')))];
164
165
  case 1:
165
166
  data = (_a.sent()).data;
166
167
  return [2 /*return*/, data];
@@ -184,7 +185,7 @@ var Provider = /** @class */ (function () {
184
185
  var data;
185
186
  return __generator(this, function (_a) {
186
187
  switch (_a.label) {
187
- 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')))];
188
189
  case 1:
189
190
  data = (_a.sent()).data;
190
191
  return [2 /*return*/, data];
@@ -207,7 +208,7 @@ var Provider = /** @class */ (function () {
207
208
  switch (_a.label) {
208
209
  case 0:
209
210
  txHashBn = (0, number_1.toBN)(txHash);
210
- 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)))];
211
212
  case 1:
212
213
  data = (_a.sent()).data;
213
214
  return [2 /*return*/, data];
@@ -230,7 +231,7 @@ var Provider = /** @class */ (function () {
230
231
  switch (_a.label) {
231
232
  case 0:
232
233
  txHashBn = (0, number_1.toBN)(txHash);
233
- 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)))];
234
235
  case 1:
235
236
  data = (_a.sent()).data;
236
237
  return [2 /*return*/, data];
@@ -243,18 +244,18 @@ var Provider = /** @class */ (function () {
243
244
  *
244
245
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
245
246
  *
246
- * @param tx - transaction to be invoked
247
+ * @param transaction - transaction to be invoked
247
248
  * @returns a confirmation of invoking a function on the starknet contract
248
249
  */
249
- Provider.prototype.addTransaction = function (tx) {
250
+ Provider.prototype.addTransaction = function (transaction) {
250
251
  return __awaiter(this, void 0, void 0, function () {
251
252
  var signature, contract_address_salt, data;
252
253
  return __generator(this, function (_a) {
253
254
  switch (_a.label) {
254
255
  case 0:
255
- signature = tx.type === 'INVOKE_FUNCTION' && (0, stark_1.formatSignature)(tx.signature);
256
- contract_address_salt = tx.type === 'DEPLOY' && (0, number_1.toHex)((0, number_1.toBN)(tx.contract_address_salt));
257
- 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' } })];
256
+ signature = transaction.type === 'INVOKE_FUNCTION' && (0, stark_1.formatSignature)(transaction.signature);
257
+ contract_address_salt = transaction.type === 'DEPLOY' && (0, number_1.toHex)((0, number_1.toBN)(transaction.contract_address_salt));
258
+ return [4 /*yield*/, axios_1.default.post((0, url_join_1.default)(this.gatewayUrl, 'add_transaction'), (0, json_1.stringify)(__assign(__assign(__assign({}, transaction), (Array.isArray(signature) && { signature: signature })), (contract_address_salt && { contract_address_salt: contract_address_salt }))), { headers: { 'Content-Type': 'application/json' } })];
258
259
  case 1:
259
260
  data = (_a.sent()).data;
260
261
  return [2 /*return*/, data];
@@ -16,11 +16,11 @@ export declare abstract class ProviderInterface {
16
16
  *
17
17
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L17-L25)
18
18
  *
19
- * @param invokeTx - transaction to be invoked
19
+ * @param invokeTransaction - transaction to be invoked
20
20
  * @param blockId
21
21
  * @returns the result of the function on the smart contract.
22
22
  */
23
- abstract callContract(invokeTx: CallContractTransaction, blockId?: number): Promise<CallContractResponse>;
23
+ abstract callContract(invokeTransaction: CallContractTransaction, blockId?: number): Promise<CallContractResponse>;
24
24
  /**
25
25
  * Gets the block information from a block ID.
26
26
  *
@@ -74,10 +74,10 @@ export declare abstract class ProviderInterface {
74
74
  *
75
75
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
76
76
  *
77
- * @param tx - transaction to be invoked
77
+ * @param transaction - transaction to be invoked
78
78
  * @returns a confirmation of invoking a function on the starknet contract
79
79
  */
80
- abstract addTransaction(tx: Transaction): Promise<AddTransactionResponse>;
80
+ abstract addTransaction(transaction: Transaction): Promise<AddTransactionResponse>;
81
81
  /**
82
82
  * Deploys a given compiled contract (json) to starknet
83
83
  *
@@ -10,8 +10,8 @@ export declare class Signer extends Provider implements SignerInterface {
10
10
  *
11
11
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
12
12
  *
13
- * @param tx - transaction to be invoked
13
+ * @param transaction - transaction to be invoked
14
14
  * @returns a confirmation of invoking a function on the starknet contract
15
15
  */
16
- addTransaction(tx: Transaction): Promise<AddTransactionResponse>;
16
+ addTransaction(transaction: Transaction): Promise<AddTransactionResponse>;
17
17
  }
@@ -100,34 +100,39 @@ var Signer = /** @class */ (function (_super) {
100
100
  *
101
101
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
102
102
  *
103
- * @param tx - transaction to be invoked
103
+ * @param transaction - transaction to be invoked
104
104
  * @returns a confirmation of invoking a function on the starknet contract
105
105
  */
106
- Signer.prototype.addTransaction = function (tx) {
106
+ Signer.prototype.addTransaction = function (transaction) {
107
107
  return __awaiter(this, void 0, void 0, function () {
108
- var result, nonceBn, calldataDecimal, msgHash, _a, r, s;
108
+ var nonceBn, result, calldataDecimal, msgHash, _a, r, s;
109
109
  return __generator(this, function (_b) {
110
110
  switch (_b.label) {
111
111
  case 0:
112
- if (tx.type === 'DEPLOY')
113
- return [2 /*return*/, _super.prototype.addTransaction.call(this, tx)];
114
- (0, minimalistic_assert_1.default)(!tx.signature, "Adding signatures to a signer tx currently isn't supported");
115
- return [4 /*yield*/, this.callContract({
116
- contract_address: this.address,
117
- entry_point_selector: (0, stark_1.getSelectorFromName)('get_nonce'),
118
- })];
119
- case 1:
112
+ if (transaction.type === 'DEPLOY')
113
+ return [2 /*return*/, _super.prototype.addTransaction.call(this, transaction)];
114
+ (0, minimalistic_assert_1.default)(!transaction.signature, "Adding signatures to a signer transaction currently isn't supported");
115
+ if (!transaction.nonce) return [3 /*break*/, 1];
116
+ nonceBn = (0, number_1.toBN)(transaction.nonce);
117
+ return [3 /*break*/, 3];
118
+ case 1: return [4 /*yield*/, this.callContract({
119
+ contract_address: this.address,
120
+ entry_point_selector: (0, stark_1.getSelectorFromName)('get_nonce'),
121
+ })];
122
+ case 2:
120
123
  result = (_b.sent()).result;
121
124
  nonceBn = (0, number_1.toBN)(result[0]);
122
- calldataDecimal = (tx.calldata || []).map(function (x) { return (0, number_1.toBN)(x).toString(); });
123
- msgHash = (0, encode_1.addHexPrefix)((0, hash_1.hashMessage)(this.address, tx.contract_address, tx.entry_point_selector, calldataDecimal, nonceBn.toString()));
125
+ _b.label = 3;
126
+ case 3:
127
+ calldataDecimal = (transaction.calldata || []).map(function (x) { return (0, number_1.toBN)(x).toString(); });
128
+ msgHash = (0, encode_1.addHexPrefix)((0, hash_1.hashMessage)(this.address, transaction.contract_address, transaction.entry_point_selector, calldataDecimal, nonceBn.toString()));
124
129
  _a = (0, ellipticCurve_1.sign)(this.keyPair, msgHash), r = _a.r, s = _a.s;
125
130
  return [2 /*return*/, _super.prototype.addTransaction.call(this, {
126
131
  type: 'INVOKE_FUNCTION',
127
132
  entry_point_selector: (0, stark_1.getSelectorFromName)('execute'),
128
133
  calldata: __spreadArray(__spreadArray([
129
- tx.contract_address,
130
- tx.entry_point_selector,
134
+ transaction.contract_address,
135
+ transaction.entry_point_selector,
131
136
  calldataDecimal.length.toString()
132
137
  ], __read(calldataDecimal), false), [
133
138
  nonceBn.toString(),
@@ -7,8 +7,8 @@ export declare abstract class SignerInterface extends Provider {
7
7
  *
8
8
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
9
9
  *
10
- * @param tx - transaction to be invoked
10
+ * @param transaction - transaction to be invoked
11
11
  * @returns a confirmation of invoking a function on the starknet contract
12
12
  */
13
- abstract addTransaction(tx: Transaction): Promise<AddTransactionResponse>;
13
+ abstract addTransaction(transaction: Transaction): Promise<AddTransactionResponse>;
14
14
  }
package/dist/types.d.ts CHANGED
@@ -7,7 +7,7 @@ export declare type GetContractAddressesResponse = {
7
7
  GpsStatementVerifier: string;
8
8
  };
9
9
  export declare type Status = 'NOT_RECEIVED' | 'RECEIVED' | 'PENDING' | 'REJECTED' | 'ACCEPTED_ONCHAIN';
10
- export declare type TxStatus = 'TRANSACTION_RECEIVED';
10
+ export declare type TransactionStatus = 'TRANSACTION_RECEIVED';
11
11
  export declare type Type = 'DEPLOY' | 'INVOKE_FUNCTION';
12
12
  export declare type EntryPointType = 'EXTERNAL';
13
13
  export declare type CompressedProgram = string;
@@ -44,6 +44,7 @@ export declare type DeployTransaction = {
44
44
  contract_definition: CompressedCompiledContract;
45
45
  contract_address_salt: BigNumberish;
46
46
  constructor_calldata: string[];
47
+ nonce?: BigNumberish;
47
48
  };
48
49
  export declare type InvokeFunctionTransaction = {
49
50
  type: 'INVOKE_FUNCTION';
@@ -52,6 +53,7 @@ export declare type InvokeFunctionTransaction = {
52
53
  entry_point_type?: EntryPointType;
53
54
  entry_point_selector: string;
54
55
  calldata?: string[];
56
+ nonce?: BigNumberish;
55
57
  };
56
58
  export declare type CallContractTransaction = Omit<InvokeFunctionTransaction, 'type'>;
57
59
  export declare type Transaction = DeployTransaction | InvokeFunctionTransaction;
@@ -100,7 +102,7 @@ export declare type GetTransactionResponse = {
100
102
  transaction_hash: string;
101
103
  };
102
104
  export declare type AddTransactionResponse = {
103
- code: TxStatus;
105
+ code: TransactionStatus;
104
106
  transaction_hash: string;
105
107
  address?: string;
106
108
  };
@@ -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;