starknet 2.0.1 → 2.0.2

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,10 @@
1
+ ## [2.0.2](https://github.com/seanjameshan/starknet.js/compare/v2.0.1...v2.0.2) (2021-11-22)
2
+
3
+ ### Bug Fixes
4
+
5
+ - correctly parse structs in Starknet return types ([5a4a318](https://github.com/seanjameshan/starknet.js/commit/5a4a318dad4c78fe84540ad92063fc1879317ac1))
6
+ - make Typescript compiler happy with constant types ([aedd895](https://github.com/seanjameshan/starknet.js/commit/aedd895a62e6018dd1d7330b004d54360007967f))
7
+
1
8
  ## [2.0.1](https://github.com/seanjameshan/starknet.js/compare/v2.0.0...v2.0.1) (2021-11-18)
2
9
 
3
10
  ### Bug Fixes
package/constants.d.ts CHANGED
@@ -1,9 +1,9 @@
1
- /// <reference types="bn.js" />
1
+ import BN from 'bn.js';
2
2
  export { IS_BROWSER } from './utils/encode';
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');
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;
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
@@ -1,5 +1,5 @@
1
1
  import { Provider } from './provider';
2
- import { Abi } from './types';
2
+ import { Abi, StructAbi } from './types';
3
3
  import { BigNumberish } from './utils/number';
4
4
  export declare type Args = {
5
5
  [inputName: string]: string | string[];
@@ -9,6 +9,9 @@ export declare function compileCalldata(args: Args): Calldata;
9
9
  export declare class Contract {
10
10
  connectedTo: string | null;
11
11
  abi: Abi[];
12
+ structs: {
13
+ [name: string]: StructAbi;
14
+ };
12
15
  provider: Provider;
13
16
  /**
14
17
  * Contract class to handle contract methods
@@ -19,6 +22,7 @@ export declare class Contract {
19
22
  constructor(abi: Abi[], address?: string | null, provider?: Provider);
20
23
  connect(address: string): Contract;
21
24
  private validateMethodAndArgs;
25
+ private parseResponseField;
22
26
  private parseResponse;
23
27
  invoke(
24
28
  method: string,
package/contract.js CHANGED
@@ -238,6 +238,14 @@ var Contract = /** @class */ (function () {
238
238
  this.connectedTo = address;
239
239
  this.provider = provider;
240
240
  this.abi = abi;
241
+ this.structs = abi
242
+ .filter(function (abiEntry) {
243
+ return abiEntry.type === 'struct';
244
+ })
245
+ .reduce(function (acc, abiEntry) {
246
+ var _a;
247
+ return __assign(__assign({}, acc), ((_a = {}), (_a[abiEntry.name] = abiEntry), _a));
248
+ }, {});
241
249
  }
242
250
  Contract.prototype.connect = function (address) {
243
251
  this.connectedTo = address;
@@ -250,9 +258,9 @@ var Contract = /** @class */ (function () {
250
258
  // ensure provided method exists
251
259
  var invokeableFunctionNames = this.abi
252
260
  .filter(function (abi) {
261
+ if (abi.type !== 'function') return false;
253
262
  var isView = abi.stateMutability === 'view';
254
- var isFunction = abi.type === 'function';
255
- return isFunction && type === 'INVOKE' ? !isView : isView;
263
+ return type === 'INVOKE' ? !isView : isView;
256
264
  })
257
265
  .map(function (abi) {
258
266
  return abi.name;
@@ -263,7 +271,7 @@ var Contract = /** @class */ (function () {
263
271
  );
264
272
  // ensure args match abi type
265
273
  var methodAbi = this.abi.find(function (abi) {
266
- return abi.name === method;
274
+ return abi.name === method && abi.type === 'function';
267
275
  });
268
276
  methodAbi.inputs.forEach(function (input) {
269
277
  if (args[input.name] !== undefined) {
@@ -291,14 +299,31 @@ var Contract = /** @class */ (function () {
291
299
  }
292
300
  });
293
301
  };
302
+ Contract.prototype.parseResponseField = function (element, responseIterator) {
303
+ var _this = this;
304
+ var entries = [];
305
+ if (['felt', 'felt*'].includes(element.type)) {
306
+ return responseIterator.next().value;
307
+ }
308
+ if (element.type in this.structs) {
309
+ entries = this.structs[element.type].members;
310
+ } else if ('outputs' in element) {
311
+ entries = element.outputs;
312
+ }
313
+ return entries.reduce(function (acc, member) {
314
+ var _a;
315
+ return __assign(
316
+ __assign({}, acc),
317
+ ((_a = {}), (_a[member.name] = _this.parseResponseField(member, responseIterator)), _a)
318
+ );
319
+ }, {});
320
+ };
294
321
  Contract.prototype.parseResponse = function (method, response) {
295
322
  var methodAbi = this.abi.find(function (abi) {
296
323
  return abi.name === method;
297
324
  });
298
- return methodAbi.outputs.reduce(function (acc, output, i) {
299
- var _a;
300
- return __assign(__assign({}, acc), ((_a = {}), (_a[output.name] = response[i]), _a));
301
- }, {});
325
+ var responseIterator = response.flat()[Symbol.iterator]();
326
+ return this.parseResponseField(methodAbi, responseIterator);
302
327
  };
303
328
  Contract.prototype.invoke = function (method, args, signature) {
304
329
  if (args === void 0) {
@@ -1,9 +1,9 @@
1
- /// <reference types="bn.js" />
1
+ import BN from 'bn.js';
2
2
  export { IS_BROWSER } from './utils/encode';
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");
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;
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,5 +1,5 @@
1
1
  import { Provider } from './provider';
2
- import { Abi } from './types';
2
+ import { Abi, StructAbi } from './types';
3
3
  import { BigNumberish } from './utils/number';
4
4
  export declare type Args = {
5
5
  [inputName: string]: string | string[];
@@ -9,6 +9,9 @@ export declare function compileCalldata(args: Args): Calldata;
9
9
  export declare class Contract {
10
10
  connectedTo: string | null;
11
11
  abi: Abi[];
12
+ structs: {
13
+ [name: string]: StructAbi;
14
+ };
12
15
  provider: Provider;
13
16
  /**
14
17
  * Contract class to handle contract methods
@@ -19,6 +22,7 @@ export declare class Contract {
19
22
  constructor(abi: Abi[], address?: string | null, provider?: Provider);
20
23
  connect(address: string): Contract;
21
24
  private validateMethodAndArgs;
25
+ private parseResponseField;
22
26
  private parseResponse;
23
27
  invoke(method: string, args?: Args, signature?: [BigNumberish, BigNumberish]): Promise<import("./types").AddTransactionResponse>;
24
28
  call(method: string, args?: Args): Promise<Args>;
package/dist/contract.js CHANGED
@@ -119,6 +119,12 @@ var Contract = /** @class */ (function () {
119
119
  this.connectedTo = address;
120
120
  this.provider = provider;
121
121
  this.abi = abi;
122
+ this.structs = abi
123
+ .filter(function (abiEntry) { return abiEntry.type === 'struct'; })
124
+ .reduce(function (acc, abiEntry) {
125
+ var _a;
126
+ return (__assign(__assign({}, acc), (_a = {}, _a[abiEntry.name] = abiEntry, _a)));
127
+ }, {});
122
128
  }
123
129
  Contract.prototype.connect = function (address) {
124
130
  this.connectedTo = address;
@@ -129,14 +135,15 @@ var Contract = /** @class */ (function () {
129
135
  // ensure provided method exists
130
136
  var invokeableFunctionNames = this.abi
131
137
  .filter(function (abi) {
138
+ if (abi.type !== 'function')
139
+ return false;
132
140
  var isView = abi.stateMutability === 'view';
133
- var isFunction = abi.type === 'function';
134
- return isFunction && type === 'INVOKE' ? !isView : isView;
141
+ return type === 'INVOKE' ? !isView : isView;
135
142
  })
136
143
  .map(function (abi) { return abi.name; });
137
144
  (0, minimalistic_assert_1.default)(invokeableFunctionNames.includes(method), (type === 'INVOKE' ? 'invokeable' : 'viewable') + " method not found in abi");
138
145
  // ensure args match abi type
139
- var methodAbi = this.abi.find(function (abi) { return abi.name === method; });
146
+ var methodAbi = this.abi.find(function (abi) { return abi.name === method && abi.type === 'function'; });
140
147
  methodAbi.inputs.forEach(function (input) {
141
148
  if (args[input.name] !== undefined) {
142
149
  if (input.type === 'felt') {
@@ -153,13 +160,28 @@ var Contract = /** @class */ (function () {
153
160
  }
154
161
  });
155
162
  };
156
- Contract.prototype.parseResponse = function (method, response) {
157
- var methodAbi = this.abi.find(function (abi) { return abi.name === method; });
158
- return methodAbi.outputs.reduce(function (acc, output, i) {
163
+ Contract.prototype.parseResponseField = function (element, responseIterator) {
164
+ var _this = this;
165
+ var entries = [];
166
+ if (['felt', 'felt*'].includes(element.type)) {
167
+ return responseIterator.next().value;
168
+ }
169
+ if (element.type in this.structs) {
170
+ entries = this.structs[element.type].members;
171
+ }
172
+ else if ('outputs' in element) {
173
+ entries = element.outputs;
174
+ }
175
+ return entries.reduce(function (acc, member) {
159
176
  var _a;
160
- return __assign(__assign({}, acc), (_a = {}, _a[output.name] = response[i], _a));
177
+ return (__assign(__assign({}, acc), (_a = {}, _a[member.name] = _this.parseResponseField(member, responseIterator), _a)));
161
178
  }, {});
162
179
  };
180
+ Contract.prototype.parseResponse = function (method, response) {
181
+ var methodAbi = this.abi.find(function (abi) { return abi.name === method; });
182
+ var responseIterator = response.flat()[Symbol.iterator]();
183
+ return this.parseResponseField(methodAbi, responseIterator);
184
+ };
163
185
  Contract.prototype.invoke = function (method, args, signature) {
164
186
  if (args === void 0) { args = {}; }
165
187
  // ensure contract is connected
package/dist/types.d.ts CHANGED
@@ -11,19 +11,26 @@ export declare type TxStatus = '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;
14
- export declare type Abi = {
15
- inputs: {
16
- name: string;
17
- type: 'felt' | 'felt*';
18
- }[];
14
+ export declare type AbiEntry = {
19
15
  name: string;
20
- outputs: {
21
- name: string;
22
- type: 'felt' | 'felt*';
23
- }[];
16
+ type: 'felt' | 'felt*' | string;
17
+ };
18
+ export declare type FunctionAbi = {
19
+ inputs: AbiEntry[];
20
+ name: string;
21
+ outputs: AbiEntry[];
24
22
  stateMutability?: 'view';
25
23
  type: 'function';
26
24
  };
25
+ export declare type StructAbi = {
26
+ members: (AbiEntry & {
27
+ offset: number;
28
+ })[];
29
+ name: string;
30
+ size: number;
31
+ type: 'struct';
32
+ };
33
+ export declare type Abi = FunctionAbi | StructAbi;
27
34
  export declare type EntryPointsByType = object;
28
35
  export declare type Program = object;
29
36
  export declare type CompiledContract = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starknet",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "description": "JavaScript library for StarkNet",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/constants.ts CHANGED
@@ -1,11 +1,13 @@
1
+ import BN from 'bn.js';
2
+
1
3
  import { toBN } from './utils/number';
2
4
 
3
5
  export { IS_BROWSER } from './utils/encode';
4
6
 
5
- export const ZERO = toBN(0);
6
- export const ONE = toBN(1);
7
- export const TWO = toBN(2);
8
- export const MASK_250 = TWO.pow(toBN(250)).sub(ONE); // 2 ** 250 - 1
7
+ export const ZERO: BN = toBN(0);
8
+ export const ONE: BN = toBN(1);
9
+ export const TWO: BN = toBN(2);
10
+ export const MASK_250: BN = TWO.pow(toBN(250)).sub(ONE); // 2 ** 250 - 1
9
11
 
10
12
  /**
11
13
  * 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
package/src/contract.ts CHANGED
@@ -2,7 +2,7 @@ import BN from 'bn.js';
2
2
  import assert from 'minimalistic-assert';
3
3
 
4
4
  import { Provider, defaultProvider } from './provider';
5
- import { Abi } from './types';
5
+ import { Abi, AbiEntry, FunctionAbi, StructAbi } from './types';
6
6
  import { BigNumberish, toBN } from './utils/number';
7
7
  import { getSelectorFromName } from './utils/stark';
8
8
 
@@ -39,6 +39,8 @@ export class Contract {
39
39
 
40
40
  abi: Abi[];
41
41
 
42
+ structs: { [name: string]: StructAbi };
43
+
42
44
  provider: Provider;
43
45
 
44
46
  /**
@@ -51,6 +53,15 @@ export class Contract {
51
53
  this.connectedTo = address;
52
54
  this.provider = provider;
53
55
  this.abi = abi;
56
+ this.structs = abi
57
+ .filter((abiEntry) => abiEntry.type === 'struct')
58
+ .reduce(
59
+ (acc, abiEntry) => ({
60
+ ...acc,
61
+ [abiEntry.name]: abiEntry,
62
+ }),
63
+ {}
64
+ );
54
65
  }
55
66
 
56
67
  public connect(address: string): Contract {
@@ -62,9 +73,9 @@ export class Contract {
62
73
  // ensure provided method exists
63
74
  const invokeableFunctionNames = this.abi
64
75
  .filter((abi) => {
76
+ if (abi.type !== 'function') return false;
65
77
  const isView = abi.stateMutability === 'view';
66
- const isFunction = abi.type === 'function';
67
- return isFunction && type === 'INVOKE' ? !isView : isView;
78
+ return type === 'INVOKE' ? !isView : isView;
68
79
  })
69
80
  .map((abi) => abi.name);
70
81
  assert(
@@ -73,7 +84,9 @@ export class Contract {
73
84
  );
74
85
 
75
86
  // ensure args match abi type
76
- const methodAbi = this.abi.find((abi) => abi.name === method)!;
87
+ const methodAbi = this.abi.find(
88
+ (abi) => abi.name === method && abi.type === 'function'
89
+ ) as FunctionAbi;
77
90
  methodAbi.inputs.forEach((input) => {
78
91
  if (args[input.name] !== undefined) {
79
92
  if (input.type === 'felt') {
@@ -102,14 +115,32 @@ export class Contract {
102
115
  });
103
116
  }
104
117
 
105
- private parseResponse(method: string, response: (string | string[])[]): Args {
106
- const methodAbi = this.abi.find((abi) => abi.name === method)!;
107
- return methodAbi.outputs.reduce((acc, output, i) => {
108
- return {
118
+ private parseResponseField(
119
+ element: AbiEntry | FunctionAbi,
120
+ responseIterator: Iterator<string>
121
+ ): Args {
122
+ let entries: AbiEntry[] = [];
123
+ if (['felt', 'felt*'].includes(element.type)) {
124
+ return responseIterator.next().value;
125
+ }
126
+ if (element.type in this.structs) {
127
+ entries = this.structs[element.type].members;
128
+ } else if ('outputs' in element) {
129
+ entries = element.outputs;
130
+ }
131
+ return entries.reduce(
132
+ (acc, member) => ({
109
133
  ...acc,
110
- [output.name]: response[i],
111
- };
112
- }, {} as Args);
134
+ [member.name]: this.parseResponseField(member, responseIterator),
135
+ }),
136
+ {}
137
+ );
138
+ }
139
+
140
+ private parseResponse(method: string, response: string[]): Args {
141
+ const methodAbi = this.abi.find((abi) => abi.name === method) as FunctionAbi;
142
+ const responseIterator = response.flat()[Symbol.iterator]();
143
+ return this.parseResponseField(methodAbi, responseIterator);
113
144
  }
114
145
 
115
146
  public invoke(method: string, args: Args = {}, signature?: [BigNumberish, BigNumberish]) {
package/src/types.ts CHANGED
@@ -16,13 +16,25 @@ export type Type = 'DEPLOY' | 'INVOKE_FUNCTION';
16
16
  export type EntryPointType = 'EXTERNAL';
17
17
  export type CompressedProgram = string;
18
18
 
19
- export type Abi = {
20
- inputs: { name: string; type: 'felt' | 'felt*' }[];
19
+ export type AbiEntry = { name: string; type: 'felt' | 'felt*' | string };
20
+
21
+ export type FunctionAbi = {
22
+ inputs: AbiEntry[];
21
23
  name: string;
22
- outputs: { name: string; type: 'felt' | 'felt*' }[];
24
+ outputs: AbiEntry[];
23
25
  stateMutability?: 'view';
24
26
  type: 'function';
25
27
  };
28
+
29
+ export type StructAbi = {
30
+ members: (AbiEntry & { offset: number })[];
31
+ name: string;
32
+ size: number;
33
+ type: 'struct';
34
+ };
35
+
36
+ export type Abi = FunctionAbi | StructAbi;
37
+
26
38
  export type EntryPointsByType = object;
27
39
  export type Program = object;
28
40
 
package/tsconfig.json CHANGED
@@ -105,11 +105,12 @@
105
105
  "include": ["src/**/*"],
106
106
  "exclude": ["node_modules"],
107
107
  "typedocOptions": {
108
- "entryPoints": ["src/starknet.ts", "src/types.ts"],
108
+ "entryPoints": "src/index.ts",
109
+ "entryPointStrategy": "expand",
109
110
  "out": "docs",
110
111
  "githubPages": false,
111
112
  "readme": "./README.md",
112
113
  "name": "StarkNet.js Docs",
113
- "sort": "source-order"
114
+ "sort": "required-first"
114
115
  }
115
116
  }
package/types.d.ts CHANGED
@@ -17,19 +17,26 @@ export declare type TxStatus = 'TRANSACTION_RECEIVED';
17
17
  export declare type Type = 'DEPLOY' | 'INVOKE_FUNCTION';
18
18
  export declare type EntryPointType = 'EXTERNAL';
19
19
  export declare type CompressedProgram = string;
20
- export declare type Abi = {
21
- inputs: {
22
- name: string;
23
- type: 'felt' | 'felt*';
24
- }[];
20
+ export declare type AbiEntry = {
25
21
  name: string;
26
- outputs: {
27
- name: string;
28
- type: 'felt' | 'felt*';
29
- }[];
22
+ type: 'felt' | 'felt*' | string;
23
+ };
24
+ export declare type FunctionAbi = {
25
+ inputs: AbiEntry[];
26
+ name: string;
27
+ outputs: AbiEntry[];
30
28
  stateMutability?: 'view';
31
29
  type: 'function';
32
30
  };
31
+ export declare type StructAbi = {
32
+ members: (AbiEntry & {
33
+ offset: number;
34
+ })[];
35
+ name: string;
36
+ size: number;
37
+ type: 'struct';
38
+ };
39
+ export declare type Abi = FunctionAbi | StructAbi;
33
40
  export declare type EntryPointsByType = object;
34
41
  export declare type Program = object;
35
42
  export declare type CompiledContract = {