starknet 2.7.2 → 3.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 (111) hide show
  1. package/.eslintrc +3 -1
  2. package/CHANGELOG.md +54 -0
  3. package/README.md +16 -14
  4. package/__mocks__/contract.json +33191 -0
  5. package/__mocks__/multicall.json +8139 -0
  6. package/__mocks__/typedDataExample.json +35 -0
  7. package/__tests__/account.test.ts +53 -87
  8. package/__tests__/accountContract.test.ts +161 -0
  9. package/__tests__/contract.test.ts +167 -30
  10. package/__tests__/jest.setup.ts +9 -0
  11. package/__tests__/provider.test.ts +19 -34
  12. package/__tests__/utils/address.test.ts +16 -0
  13. package/__tests__/utils/typedData.test.ts +1 -36
  14. package/account/default.d.ts +66 -0
  15. package/account/default.js +439 -0
  16. package/account/index.d.ts +2 -0
  17. package/account/index.js +27 -0
  18. package/account/interface.d.ts +83 -0
  19. package/account/interface.js +37 -0
  20. package/constants.d.ts +2 -0
  21. package/constants.js +4 -0
  22. package/contract.d.ts +71 -12
  23. package/contract.js +243 -89
  24. package/dist/account/default.d.ts +55 -0
  25. package/dist/account/default.js +271 -0
  26. package/dist/account/index.d.ts +2 -0
  27. package/dist/account/index.js +14 -0
  28. package/dist/account/interface.d.ts +69 -0
  29. package/dist/account/interface.js +27 -0
  30. package/dist/constants.d.ts +2 -0
  31. package/dist/constants.js +3 -1
  32. package/dist/contract.d.ts +71 -9
  33. package/dist/contract.js +214 -65
  34. package/dist/index.d.ts +2 -1
  35. package/dist/index.js +2 -1
  36. package/dist/provider/default.d.ts +27 -16
  37. package/dist/provider/default.js +157 -100
  38. package/dist/provider/interface.d.ts +29 -32
  39. package/dist/provider/utils.d.ts +21 -5
  40. package/dist/provider/utils.js +53 -10
  41. package/dist/signer/default.d.ts +7 -31
  42. package/dist/signer/default.js +25 -121
  43. package/dist/signer/index.d.ts +1 -1
  44. package/dist/signer/index.js +1 -1
  45. package/dist/signer/interface.d.ts +17 -18
  46. package/dist/signer/interface.js +2 -20
  47. package/dist/types/api.d.ts +147 -0
  48. package/dist/{types.js → types/api.js} +0 -0
  49. package/dist/types/index.d.ts +3 -0
  50. package/dist/types/index.js +15 -0
  51. package/dist/types/lib.d.ts +57 -0
  52. package/dist/types/lib.js +2 -0
  53. package/dist/types/signer.d.ts +4 -0
  54. package/dist/types/signer.js +2 -0
  55. package/dist/utils/address.d.ts +2 -0
  56. package/dist/utils/address.js +22 -0
  57. package/dist/utils/number.d.ts +2 -0
  58. package/dist/utils/number.js +32 -2
  59. package/dist/utils/stark.d.ts +2 -1
  60. package/dist/utils/stark.js +44 -1
  61. package/index.d.ts +2 -1
  62. package/index.js +2 -1
  63. package/package.json +9 -3
  64. package/provider/default.d.ts +45 -36
  65. package/provider/default.js +216 -201
  66. package/provider/interface.d.ts +36 -49
  67. package/provider/utils.d.ts +23 -8
  68. package/provider/utils.js +57 -11
  69. package/signer/default.d.ts +11 -31
  70. package/signer/default.js +52 -169
  71. package/signer/index.d.ts +1 -1
  72. package/signer/index.js +1 -1
  73. package/signer/interface.d.ts +21 -18
  74. package/signer/interface.js +3 -32
  75. package/src/account/default.ts +151 -0
  76. package/src/account/index.ts +2 -0
  77. package/src/account/interface.ts +91 -0
  78. package/src/constants.ts +2 -0
  79. package/src/contract.ts +246 -77
  80. package/src/index.ts +2 -1
  81. package/src/provider/default.ts +141 -110
  82. package/src/provider/interface.ts +36 -52
  83. package/src/provider/utils.ts +60 -13
  84. package/src/signer/default.ts +33 -76
  85. package/src/signer/index.ts +1 -1
  86. package/src/signer/interface.ts +21 -20
  87. package/src/types/api.ts +171 -0
  88. package/src/types/index.ts +3 -0
  89. package/src/types/lib.ts +73 -0
  90. package/src/types/signer.ts +5 -0
  91. package/src/utils/address.ts +23 -0
  92. package/src/utils/number.ts +12 -1
  93. package/src/utils/stark.ts +13 -1
  94. package/types/api.d.ts +162 -0
  95. package/{types.js → types/api.js} +0 -0
  96. package/types/index.d.ts +3 -0
  97. package/types/index.js +28 -0
  98. package/types/lib.d.ts +64 -0
  99. package/types/lib.js +2 -0
  100. package/types/signer.d.ts +4 -0
  101. package/types/signer.js +2 -0
  102. package/utils/address.d.ts +2 -0
  103. package/utils/address.js +22 -0
  104. package/utils/number.d.ts +4 -0
  105. package/utils/number.js +54 -2
  106. package/utils/stark.d.ts +2 -1
  107. package/utils/stark.js +64 -1
  108. package/__tests__/signer.test.ts +0 -119
  109. package/dist/types.d.ts +0 -109
  110. package/src/types.ts +0 -131
  111. package/types.d.ts +0 -116
@@ -1,6 +1,31 @@
1
1
  "use strict";
2
+ var __read = (this && this.__read) || function (o, n) {
3
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
4
+ if (!m) return o;
5
+ var i = m.call(o), r, ar = [], e;
6
+ try {
7
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
8
+ }
9
+ catch (error) { e = { error: error }; }
10
+ finally {
11
+ try {
12
+ if (r && !r.done && (m = i["return"])) m.call(i);
13
+ }
14
+ finally { if (e) throw e.error; }
15
+ }
16
+ return ar;
17
+ };
18
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
19
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
20
+ if (ar || !(i in from)) {
21
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
22
+ ar[i] = from[i];
23
+ }
24
+ }
25
+ return to.concat(ar || Array.prototype.slice.call(from));
26
+ };
2
27
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.formatSignature = exports.makeAddress = exports.randomAddress = exports.getSelectorFromName = exports.compressProgram = void 0;
28
+ exports.compileCalldata = exports.formatSignature = exports.makeAddress = exports.randomAddress = exports.getSelectorFromName = exports.compressProgram = void 0;
4
29
  var pako_1 = require("pako");
5
30
  var ellipticCurve_1 = require("./ellipticCurve");
6
31
  var encode_1 = require("./encode");
@@ -52,3 +77,21 @@ function formatSignature(sig) {
52
77
  }
53
78
  }
54
79
  exports.formatSignature = formatSignature;
80
+ function compileCalldata(args) {
81
+ return Object.values(args).flatMap(function (value) {
82
+ if (Array.isArray(value))
83
+ return __spreadArray([(0, number_1.toBN)(value.length).toString()], __read(value.map(function (x) { return (0, number_1.toBN)(x).toString(); })), false);
84
+ if (typeof value === 'object' && 'type' in value)
85
+ return Object.entries(value)
86
+ .filter(function (_a) {
87
+ var _b = __read(_a, 1), k = _b[0];
88
+ return k !== 'type';
89
+ })
90
+ .map(function (_a) {
91
+ var _b = __read(_a, 2), v = _b[1];
92
+ return (0, number_1.toBN)(v).toString();
93
+ });
94
+ return (0, number_1.toBN)(value).toString();
95
+ });
96
+ }
97
+ exports.compileCalldata = compileCalldata;
package/index.d.ts CHANGED
@@ -4,7 +4,7 @@
4
4
  export * from './types';
5
5
  export * from './contract';
6
6
  export * from './provider';
7
- export * from './signer';
7
+ export * from './account';
8
8
  /**
9
9
  * Utils
10
10
  */
@@ -18,3 +18,4 @@ export * as ec from './utils/ellipticCurve';
18
18
  export * as uint256 from './utils/uint256';
19
19
  export * as shortString from './utils/shortString';
20
20
  export * as typedData from './utils/typedData';
21
+ export * from './utils/address';
package/index.js CHANGED
@@ -61,7 +61,7 @@ exports.typedData =
61
61
  __exportStar(require('./types'), exports);
62
62
  __exportStar(require('./contract'), exports);
63
63
  __exportStar(require('./provider'), exports);
64
- __exportStar(require('./signer'), exports);
64
+ __exportStar(require('./account'), exports);
65
65
  /**
66
66
  * Utils
67
67
  */
@@ -75,3 +75,4 @@ exports.ec = __importStar(require('./utils/ellipticCurve'));
75
75
  exports.uint256 = __importStar(require('./utils/uint256'));
76
76
  exports.shortString = __importStar(require('./utils/shortString'));
77
77
  exports.typedData = __importStar(require('./utils/typedData'));
78
+ __exportStar(require('./utils/address'), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starknet",
3
- "version": "2.7.2",
3
+ "version": "3.1.0",
4
4
  "description": "JavaScript library for StarkNet",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -23,7 +23,7 @@
23
23
  "zk",
24
24
  "rollup"
25
25
  ],
26
- "repository": "github:seanjameshan/starknet.js",
26
+ "repository": "github:0xs34n/starknet.js",
27
27
  "author": "Sean Han",
28
28
  "license": "MIT",
29
29
  "devDependencies": {
@@ -44,6 +44,7 @@
44
44
  "@types/url-join": "^4.0.1",
45
45
  "@typescript-eslint/eslint-plugin": "^5.0.0",
46
46
  "@typescript-eslint/parser": "^5.0.0",
47
+ "axios-logger": "^2.6.0",
47
48
  "eslint": "^7.32.0",
48
49
  "eslint-config-airbnb-base": "^14.2.1",
49
50
  "eslint-config-airbnb-typescript": "^14.0.1",
@@ -76,7 +77,12 @@
76
77
  "*.{ts,js,md,yml,json}": "prettier --write"
77
78
  },
78
79
  "jest": {
79
- "testTimeout": 800000
80
+ "testMatch": [
81
+ "**/__tests__/**/(*.)+(spec|test).[jt]s?(x)"
82
+ ],
83
+ "setupFilesAfterEnv": [
84
+ "./__tests__/jest.setup.ts"
85
+ ]
80
86
  },
81
87
  "importSort": {
82
88
  ".js, .jsx, .ts, .tsx": {
@@ -1,19 +1,21 @@
1
1
  import {
2
+ Abi,
2
3
  AddTransactionResponse,
3
- BlockNumber,
4
+ Call,
4
5
  CallContractResponse,
5
- CallContractTransaction,
6
- CompiledContract,
6
+ DeployContractPayload,
7
+ Endpoints,
7
8
  GetBlockResponse,
8
9
  GetCodeResponse,
9
10
  GetContractAddressesResponse,
10
11
  GetTransactionResponse,
11
12
  GetTransactionStatusResponse,
12
- Signature,
13
- Transaction,
13
+ Invocation,
14
+ TransactionReceipt,
14
15
  } from '../types';
15
16
  import { BigNumberish } from '../utils/number';
16
17
  import { ProviderInterface } from './interface';
18
+ import { BlockIdentifier } from './utils';
17
19
  declare type NetworkName = 'mainnet-alpha' | 'goerli-alpha';
18
20
  declare type ProviderOptions =
19
21
  | {
@@ -30,6 +32,20 @@ export declare class Provider implements ProviderInterface {
30
32
  protected static getNetworkFromName(
31
33
  name: NetworkName
32
34
  ): 'https://alpha-mainnet.starknet.io' | 'https://alpha4.starknet.io';
35
+ private getFetchUrl;
36
+ private getFetchMethod;
37
+ private getQueryString;
38
+ private getHeaders;
39
+ protected fetchEndpoint<T extends keyof Endpoints>(
40
+ endpoint: T,
41
+ ...[query, request]: Endpoints[T]['QUERY'] extends never
42
+ ? Endpoints[T]['REQUEST'] extends never
43
+ ? []
44
+ : [undefined, Endpoints[T]['REQUEST']]
45
+ : Endpoints[T]['REQUEST'] extends never
46
+ ? [Endpoints[T]['QUERY']]
47
+ : [Endpoints[T]['QUERY'], Endpoints[T]['REQUEST']]
48
+ ): Promise<Endpoints[T]['RESPONSE']>;
33
49
  /**
34
50
  * Gets the smart contract address on the goerli testnet.
35
51
  *
@@ -48,9 +64,8 @@ export declare class Provider implements ProviderInterface {
48
64
  * @returns the result of the function on the smart contract.
49
65
  */
50
66
  callContract(
51
- invokeTransaction: CallContractTransaction,
52
- blockHash?: BigNumberish,
53
- blockNumber?: BlockNumber
67
+ { contractAddress, entrypoint, calldata }: Call,
68
+ blockIdentifier?: BlockIdentifier
54
69
  ): Promise<CallContractResponse>;
55
70
  /**
56
71
  * Gets the block information
@@ -61,7 +76,7 @@ export declare class Provider implements ProviderInterface {
61
76
  * @param blockNumber
62
77
  * @returns the block object { block_number, previous_block_number, state_root, status, timestamp, transaction_receipts, transactions }
63
78
  */
64
- getBlock(blockHash?: BigNumberish, blockNumber?: BlockNumber): Promise<GetBlockResponse>;
79
+ getBlock(blockIdentifier?: BlockIdentifier): Promise<GetBlockResponse>;
65
80
  /**
66
81
  * Gets the code of the deployed contract.
67
82
  *
@@ -72,11 +87,7 @@ export declare class Provider implements ProviderInterface {
72
87
  * @param blockNumber
73
88
  * @returns Bytecode and ABI of compiled contract
74
89
  */
75
- getCode(
76
- contractAddress: string,
77
- blockHash?: BigNumberish,
78
- blockNumber?: BlockNumber
79
- ): Promise<GetCodeResponse>;
90
+ getCode(contractAddress: string, blockIdentifier?: BlockIdentifier): Promise<GetCodeResponse>;
80
91
  /**
81
92
  * Gets the contract's storage variable at a specific key.
82
93
  *
@@ -91,8 +102,7 @@ export declare class Provider implements ProviderInterface {
91
102
  getStorageAt(
92
103
  contractAddress: string,
93
104
  key: number,
94
- blockHash?: BigNumberish,
95
- blockNumber?: BlockNumber
105
+ blockIdentifier?: BlockIdentifier
96
106
  ): Promise<object>;
97
107
  /**
98
108
  * Gets the status of a transaction.
@@ -104,23 +114,30 @@ export declare class Provider implements ProviderInterface {
104
114
  */
105
115
  getTransactionStatus(txHash: BigNumberish): Promise<GetTransactionStatusResponse>;
106
116
  /**
107
- * Gets the transaction information from a tx id.
117
+ * Gets the transaction receipt from a tx hash or tx id.
108
118
  *
109
- * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L54-L58)
119
+ * [Reference] (https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L104-L111)
110
120
  *
111
121
  * @param txHash
112
- * @returns the transacton object { transaction_id, status, transaction, block_number?, block_number?, transaction_index?, transaction_failure_reason? }
122
+ * @param txId
123
+ * @returns the transaction receipt object
113
124
  */
114
- getTransaction(txHash: BigNumberish): Promise<GetTransactionResponse>;
125
+ getTransactionReceipt({
126
+ txHash,
127
+ txId,
128
+ }: {
129
+ txHash?: BigNumberish;
130
+ txId?: BigNumberish;
131
+ }): Promise<TransactionReceipt>;
115
132
  /**
116
- * Invoke a function on the starknet contract
133
+ * Gets the transaction information from a tx id.
117
134
  *
118
- * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
135
+ * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L54-L58)
119
136
  *
120
- * @param transaction - transaction to be invoked
121
- * @returns a confirmation of invoking a function on the starknet contract
137
+ * @param txHash
138
+ * @returns the transacton object { transaction_id, status, transaction, block_number?, block_number?, transaction_index?, transaction_failure_reason? }
122
139
  */
123
- addTransaction(transaction: Transaction): Promise<AddTransactionResponse>;
140
+ getTransaction(txHash: BigNumberish): Promise<GetTransactionResponse>;
124
141
  /**
125
142
  * Deploys a given compiled contract (json) to starknet
126
143
  *
@@ -128,13 +145,10 @@ export declare class Provider implements ProviderInterface {
128
145
  * @param address - (optional, defaults to a random address) the address where the contract should be deployed (alpha)
129
146
  * @returns a confirmation of sending a transaction on the starknet contract
130
147
  */
131
- deployContract(
132
- contract: CompiledContract | string,
133
- constructorCalldata?: string[],
134
- addressSalt?: BigNumberish
135
- ): Promise<AddTransactionResponse>;
148
+ deployContract(payload: DeployContractPayload, _abi?: Abi): Promise<AddTransactionResponse>;
136
149
  /**
137
150
  * Invokes a function on starknet
151
+ * @deprecated This method wont be supported as soon as fees are mandatory
138
152
  *
139
153
  * @param contractAddress - target contract address for invoke
140
154
  * @param entrypointSelector - target entrypoint selector for
@@ -142,12 +156,7 @@ export declare class Provider implements ProviderInterface {
142
156
  * @param signature - (optional) signature to send along
143
157
  * @returns response from addTransaction
144
158
  */
145
- invokeFunction(
146
- contractAddress: string,
147
- entrypointSelector: string,
148
- calldata?: string[],
149
- signature?: Signature
150
- ): Promise<AddTransactionResponse>;
159
+ invokeFunction(invocation: Invocation, _abi?: Abi): Promise<AddTransactionResponse>;
151
160
  waitForTx(txHash: BigNumberish, retryInterval?: number): Promise<void>;
152
161
  }
153
162
  export {};