starknet 3.19.0 → 4.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 (46) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/README.md +1 -2
  3. package/__tests__/defaultProvider.test.ts +5 -0
  4. package/__tests__/jest.setup.ts +2 -3
  5. package/dist/provider/default.d.ts +2 -1
  6. package/dist/provider/default.js +7 -0
  7. package/dist/provider/interface.d.ts +2 -1
  8. package/dist/provider/rpc.d.ts +1 -0
  9. package/dist/provider/rpc.js +15 -2
  10. package/dist/provider/sequencer.d.ts +1 -0
  11. package/dist/provider/sequencer.js +10 -1
  12. package/dist/types/api/sequencer.d.ts +4 -4
  13. package/dist/types/provider.d.ts +3 -0
  14. package/dist/utils/fetchPonyfill.d.ts +2 -0
  15. package/dist/utils/fetchPonyfill.js +6 -0
  16. package/dist/utils/responseParser/rpc.d.ts +1 -0
  17. package/dist/utils/responseParser/rpc.js +3 -0
  18. package/dist/utils/responseParser/sequencer.d.ts +1 -0
  19. package/dist/utils/responseParser/sequencer.js +3 -0
  20. package/package.json +3 -6
  21. package/provider/default.d.ts +2 -1
  22. package/provider/default.js +7 -0
  23. package/provider/interface.d.ts +2 -1
  24. package/provider/rpc.d.ts +1 -0
  25. package/provider/rpc.js +15 -2
  26. package/provider/sequencer.d.ts +1 -0
  27. package/provider/sequencer.js +10 -1
  28. package/src/provider/default.ts +8 -0
  29. package/src/provider/interface.ts +6 -0
  30. package/src/provider/rpc.ts +10 -2
  31. package/src/provider/sequencer.ts +10 -0
  32. package/src/types/api/sequencer.ts +5 -5
  33. package/src/types/provider.ts +5 -0
  34. package/src/utils/fetchPonyfill.ts +4 -0
  35. package/src/utils/responseParser/rpc.ts +4 -0
  36. package/src/utils/responseParser/sequencer.ts +4 -0
  37. package/types/api/sequencer.d.ts +4 -4
  38. package/types/provider.d.ts +3 -0
  39. package/utils/fetchPonyfill.d.ts +2 -0
  40. package/utils/fetchPonyfill.js +6 -0
  41. package/utils/responseParser/rpc.d.ts +1 -0
  42. package/utils/responseParser/rpc.js +3 -0
  43. package/utils/responseParser/sequencer.d.ts +1 -0
  44. package/utils/responseParser/sequencer.js +3 -0
  45. package/www/docs/API/contract.md +1 -1
  46. package/www/docs/API/provider.md +1 -1
package/CHANGELOG.md CHANGED
@@ -1,3 +1,26 @@
1
+ # [4.1.0](https://github.com/0xs34n/starknet.js/compare/v4.0.1...v4.1.0) (2022-08-03)
2
+
3
+ ### Features
4
+
5
+ - get-code ([de6e597](https://github.com/0xs34n/starknet.js/commit/de6e5971de5155925defcf9249a8160dc3fdc9b7))
6
+
7
+ ## [4.0.1](https://github.com/0xs34n/starknet.js/compare/v4.0.0...v4.0.1) (2022-08-03)
8
+
9
+ ### Bug Fixes
10
+
11
+ - use custom fetch ponyfill ([16e9a53](https://github.com/0xs34n/starknet.js/commit/16e9a530d62942da75ed1bc30d0048a35f9f0152))
12
+ - use isomorphic-unfetch ([aa7af66](https://github.com/0xs34n/starknet.js/commit/aa7af6622d918ad0d17fe28bf1e73635895537c5))
13
+
14
+ # [4.0.0](https://github.com/0xs34n/starknet.js/compare/v3.19.0...v4.0.0) (2022-07-27)
15
+
16
+ ### Documentation
17
+
18
+ - v4 ([9e300a9](https://github.com/0xs34n/starknet.js/commit/9e300a98e587fc702ee3ef98441c457fe87e6c1e))
19
+
20
+ ### BREAKING CHANGES
21
+
22
+ - new provider and signer interfaces
23
+
1
24
  # [3.19.0](https://github.com/0xs34n/starknet.js/compare/v3.18.2...v3.19.0) (2022-07-25)
2
25
 
3
26
  ### Bug Fixes
package/README.md CHANGED
@@ -40,7 +40,7 @@ Install starknet with `npm`
40
40
 
41
41
  ```bash
42
42
  $ npm install starknet
43
- # or for pre-release features:
43
+ # or for starknet.js v4:
44
44
  $ npm install starknet@next
45
45
  ```
46
46
 
@@ -85,4 +85,3 @@ This library would not be possible without these rockstars.
85
85
  Copyright (c) 2022 0xs34n
86
86
 
87
87
  Licensed under the [MIT license](https://github.com/0xs34n/starknet.js/blob/main/LICENSE).
88
-
@@ -62,6 +62,11 @@ describe('defaultProvider', () => {
62
62
  return expect(block).toHaveProperty('block_number');
63
63
  });
64
64
 
65
+ test('getCode() -> { bytecode }', async () => {
66
+ const code = await testProvider.getCode(exampleContractAddress);
67
+ return expect(Array.isArray(code.bytecode)).toBe(true);
68
+ });
69
+
65
70
  describe('getStorageAt', () => {
66
71
  test('with "key" type of number', () => {
67
72
  return expect(testProvider.getStorageAt(exampleContractAddress, 0)).resolves.not.toThrow();
@@ -1,9 +1,8 @@
1
+ import 'isomorphic-fetch';
2
+
1
3
  /* eslint-disable no-console */
2
- import fetch from 'cross-fetch';
3
4
  import { register } from 'fetch-intercept';
4
5
 
5
- global.fetch = fetch;
6
-
7
6
  jest.setTimeout(50 * 60 * 1000);
8
7
 
9
8
  if (process.env.DEBUG === 'true') {
@@ -1,5 +1,5 @@
1
1
  import { StarknetChainId } from '../constants';
2
- import { BlockTag, Call, CallContractResponse, ContractClass, DeclareContractPayload, DeclareContractResponse, DeployContractPayload, DeployContractResponse, EstimateFeeResponse, GetBlockResponse, GetTransactionReceiptResponse, GetTransactionResponse, Invocation, InvocationsDetails, InvokeFunctionResponse } from '../types';
2
+ import { BlockTag, Call, CallContractResponse, ContractClass, DeclareContractPayload, DeclareContractResponse, DeployContractPayload, DeployContractResponse, EstimateFeeResponse, GetBlockResponse, GetCodeResponse, GetTransactionReceiptResponse, GetTransactionResponse, Invocation, InvocationsDetails, InvokeFunctionResponse } from '../types';
3
3
  import { BigNumberish } from '../utils/number';
4
4
  import { ProviderInterface } from './interface';
5
5
  import { RpcProviderOptions } from './rpc';
@@ -23,5 +23,6 @@ export declare class Provider implements ProviderInterface {
23
23
  invokeFunction(functionInvocation: Invocation, details: InvocationsDetails): Promise<InvokeFunctionResponse>;
24
24
  deployContract(payload: DeployContractPayload): Promise<DeployContractResponse>;
25
25
  declareContract(payload: DeclareContractPayload): Promise<DeclareContractResponse>;
26
+ getCode(contractAddress: string, blockIdentifier?: BlockIdentifier): Promise<GetCodeResponse>;
26
27
  waitForTransaction(txHash: BigNumberish, retryInterval?: number): Promise<void>;
27
28
  }
@@ -137,6 +137,13 @@ var Provider = /** @class */ (function () {
137
137
  });
138
138
  });
139
139
  };
140
+ Provider.prototype.getCode = function (contractAddress, blockIdentifier) {
141
+ return __awaiter(this, void 0, void 0, function () {
142
+ return __generator(this, function (_a) {
143
+ return [2 /*return*/, this.provider.getCode(contractAddress, blockIdentifier)];
144
+ });
145
+ });
146
+ };
140
147
  Provider.prototype.waitForTransaction = function (txHash, retryInterval) {
141
148
  return __awaiter(this, void 0, void 0, function () {
142
149
  return __generator(this, function (_a) {
@@ -1,5 +1,5 @@
1
1
  import { StarknetChainId } from '../constants';
2
- import type { BlockTag, Call, CallContractResponse, ContractClass, DeclareContractPayload, DeclareContractResponse, DeployContractPayload, DeployContractResponse, EstimateFeeResponse, GetBlockResponse, GetTransactionReceiptResponse, GetTransactionResponse, Invocation, InvocationsDetails, InvokeFunctionResponse } from '../types';
2
+ import type { BlockTag, Call, CallContractResponse, ContractClass, DeclareContractPayload, DeclareContractResponse, DeployContractPayload, DeployContractResponse, EstimateFeeResponse, GetBlockResponse, GetCodeResponse, GetTransactionReceiptResponse, GetTransactionResponse, Invocation, InvocationsDetails, InvokeFunctionResponse } from '../types';
3
3
  import type { BigNumberish } from '../utils/number';
4
4
  import { BlockIdentifier } from './utils';
5
5
  export declare abstract class ProviderInterface {
@@ -19,6 +19,7 @@ export declare abstract class ProviderInterface {
19
19
  * @returns the block object
20
20
  */
21
21
  abstract getBlock(blockIdentifier: BlockIdentifier): Promise<GetBlockResponse>;
22
+ abstract getCode(contractAddress: string, blockIdentifier?: BlockIdentifier): Promise<GetCodeResponse>;
22
23
  /**
23
24
  * Gets the contract class of the deployed contract.
24
25
  *
@@ -24,6 +24,7 @@ export declare class RpcProvider implements ProviderInterface {
24
24
  deployContract({ contract, constructorCalldata, addressSalt, }: DeployContractPayload): Promise<DeployContractResponse>;
25
25
  invokeFunction(functionInvocation: Invocation, details: InvocationsDetails): Promise<InvokeFunctionResponse>;
26
26
  callContract(call: Call, blockIdentifier?: BlockIdentifier): Promise<CallContractResponse>;
27
+ getCode(contractAddress: string, _blockIdentifier?: BlockIdentifier): Promise<RPC.GetCodeResponse>;
27
28
  waitForTransaction(txHash: BigNumberish, retryInterval?: number): Promise<void>;
28
29
  /**
29
30
  * Gets the transaction count from a block.
@@ -40,7 +40,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
40
40
  };
41
41
  Object.defineProperty(exports, "__esModule", { value: true });
42
42
  exports.RpcProvider = void 0;
43
- var cross_fetch_1 = __importDefault(require("cross-fetch"));
43
+ var fetchPonyfill_1 = __importDefault(require("../utils/fetchPonyfill"));
44
44
  var hash_1 = require("../utils/hash");
45
45
  var json_1 = require("../utils/json");
46
46
  var number_1 = require("../utils/number");
@@ -73,7 +73,7 @@ var RpcProvider = /** @class */ (function () {
73
73
  _c.label = 1;
74
74
  case 1:
75
75
  _c.trys.push([1, 4, , 5]);
76
- return [4 /*yield*/, (0, cross_fetch_1.default)(this.nodeUrl, {
76
+ return [4 /*yield*/, (0, fetchPonyfill_1.default)(this.nodeUrl, {
77
77
  method: 'POST',
78
78
  body: (0, json_1.stringify)(requestData),
79
79
  headers: {
@@ -248,6 +248,19 @@ var RpcProvider = /** @class */ (function () {
248
248
  });
249
249
  });
250
250
  };
251
+ RpcProvider.prototype.getCode = function (contractAddress, _blockIdentifier) {
252
+ return __awaiter(this, void 0, void 0, function () {
253
+ var result;
254
+ return __generator(this, function (_a) {
255
+ switch (_a.label) {
256
+ case 0: return [4 /*yield*/, this.fetchEndpoint('starknet_getCode', [contractAddress])];
257
+ case 1:
258
+ result = _a.sent();
259
+ return [2 /*return*/, this.responseParser.parseGetCodeResponse(result)];
260
+ }
261
+ });
262
+ });
263
+ };
251
264
  RpcProvider.prototype.waitForTransaction = function (txHash, retryInterval) {
252
265
  if (retryInterval === void 0) { retryInterval = 8000; }
253
266
  return __awaiter(this, void 0, void 0, function () {
@@ -37,6 +37,7 @@ export declare class SequencerProvider implements ProviderInterface {
37
37
  deployContract({ contract, constructorCalldata, addressSalt, }: DeployContractPayload): Promise<DeployContractResponse>;
38
38
  declareContract({ contract, }: DeclareContractPayload): Promise<DeclareContractResponse>;
39
39
  getEstimateFee(invocation: Invocation, blockIdentifier?: BlockIdentifier, invocationDetails?: InvocationsDetails): Promise<EstimateFeeResponse>;
40
+ getCode(contractAddress: string, blockIdentifier?: BlockIdentifier): Promise<Sequencer.GetCodeResponse>;
40
41
  waitForTransaction(txHash: BigNumberish, retryInterval?: number): Promise<void>;
41
42
  /**
42
43
  * Gets the status of a transaction.
@@ -58,6 +58,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
58
58
  exports.SequencerProvider = void 0;
59
59
  var url_join_1 = __importDefault(require("url-join"));
60
60
  var constants_1 = require("../constants");
61
+ var fetchPonyfill_1 = __importDefault(require("../utils/fetchPonyfill"));
61
62
  var hash_1 = require("../utils/hash");
62
63
  var json_1 = require("../utils/json");
63
64
  var number_1 = require("../utils/number");
@@ -180,7 +181,7 @@ var SequencerProvider = /** @class */ (function () {
180
181
  _c.label = 1;
181
182
  case 1:
182
183
  _c.trys.push([1, 4, , 5]);
183
- return [4 /*yield*/, fetch(url, {
184
+ return [4 /*yield*/, (0, fetchPonyfill_1.default)(url, {
184
185
  method: method,
185
186
  body: (0, json_1.stringify)(request),
186
187
  headers: headers,
@@ -354,6 +355,14 @@ var SequencerProvider = /** @class */ (function () {
354
355
  });
355
356
  });
356
357
  };
358
+ SequencerProvider.prototype.getCode = function (contractAddress, blockIdentifier) {
359
+ if (blockIdentifier === void 0) { blockIdentifier = 'pending'; }
360
+ return __awaiter(this, void 0, void 0, function () {
361
+ return __generator(this, function (_a) {
362
+ return [2 /*return*/, this.fetchEndpoint('get_code', { contractAddress: contractAddress, blockIdentifier: blockIdentifier }).then(this.responseParser.parseGetCodeResponse)];
363
+ });
364
+ });
365
+ };
357
366
  SequencerProvider.prototype.waitForTransaction = function (txHash, retryInterval) {
358
367
  if (retryInterval === void 0) { retryInterval = 8000; }
359
368
  return __awaiter(this, void 0, void 0, function () {
@@ -39,10 +39,6 @@ export declare type ExecutionResources = {
39
39
  };
40
40
  n_memory_holes: number;
41
41
  };
42
- export declare type GetCodeResponse = {
43
- bytecode: string[];
44
- abi: Abi;
45
- };
46
42
  export declare type GetTransactionTraceResponse = {
47
43
  function_invocation: {
48
44
  caller_address: string;
@@ -97,6 +93,10 @@ export declare namespace Sequencer {
97
93
  address?: string;
98
94
  class_hash?: string;
99
95
  };
96
+ type GetCodeResponse = {
97
+ bytecode: string[];
98
+ abi: Abi;
99
+ };
100
100
  interface InvokeFunctionTransactionResponse extends InvokeFunctionTransaction {
101
101
  transaction_hash: string;
102
102
  }
@@ -13,6 +13,9 @@ export interface GetBlockResponse {
13
13
  transactions: Array<string>;
14
14
  starknet_version?: string;
15
15
  }
16
+ export interface GetCodeResponse {
17
+ bytecode: string[];
18
+ }
16
19
  export declare type GetTransactionResponse = InvokeTransactionResponse & DeclareTransactionResponse;
17
20
  export interface CommonTransactionResponse {
18
21
  transaction_hash?: string;
@@ -0,0 +1,2 @@
1
+ declare const _default: any;
2
+ export default _default;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = (typeof window !== 'undefined' && window.fetch) || // use buildin fetch in browser if available
4
+ (typeof global !== 'undefined' && global.fetch) || // use buildin fetch in node, react-native and service worker if available
5
+ // eslint-disable-next-line global-require
6
+ require('isomorphic-fetch'); // ponyfill fetch in node and browsers that don't have it
@@ -5,6 +5,7 @@ export declare class RPCResponseParser extends ResponseParser {
5
5
  parseGetBlockResponse(res: RPC.GetBlockResponse): GetBlockResponse;
6
6
  parseGetTransactionResponse(res: RPC.GetTransactionResponse): GetTransactionResponse;
7
7
  parseGetTransactionReceiptResponse(res: RPC.GetTransactionReceiptResponse): GetTransactionReceiptResponse;
8
+ parseGetCodeResponse(res: RPC.GetCodeResponse): RPC.GetCodeResponse;
8
9
  parseFeeEstimateResponse(res: RPC.EstimateFeeResponse): EstimateFeeResponse;
9
10
  parseCallContractResponse(res: Array<string>): CallContractResponse;
10
11
  parseInvokeFunctionResponse(res: RPC.AddTransactionResponse): InvokeFunctionResponse;
@@ -62,6 +62,9 @@ var RPCResponseParser = /** @class */ (function (_super) {
62
62
  events: res.events,
63
63
  };
64
64
  };
65
+ RPCResponseParser.prototype.parseGetCodeResponse = function (res) {
66
+ return res;
67
+ };
65
68
  RPCResponseParser.prototype.parseFeeEstimateResponse = function (res) {
66
69
  return {
67
70
  overall_fee: (0, number_1.toBN)(res.overall_fee),
@@ -5,6 +5,7 @@ export declare class SequencerAPIResponseParser extends ResponseParser {
5
5
  parseGetBlockResponse(res: Sequencer.GetBlockResponse): GetBlockResponse;
6
6
  parseGetTransactionResponse(res: Sequencer.GetTransactionResponse): GetTransactionResponse;
7
7
  parseGetTransactionReceiptResponse(res: Sequencer.TransactionReceiptResponse): GetTransactionReceiptResponse;
8
+ parseGetCodeResponse(res: Sequencer.GetCodeResponse): Sequencer.GetCodeResponse;
8
9
  parseFeeEstimateResponse(res: Sequencer.EstimateFeeResponse): EstimateFeeResponse;
9
10
  parseCallContractResponse(res: Sequencer.CallContractResponse): CallContractResponse;
10
11
  parseInvokeFunctionResponse(res: Sequencer.AddTransactionResponse): InvokeFunctionResponse;
@@ -79,6 +79,9 @@ var SequencerAPIResponseParser = /** @class */ (function (_super) {
79
79
  l1_origin_message: undefined,
80
80
  };
81
81
  };
82
+ SequencerAPIResponseParser.prototype.parseGetCodeResponse = function (res) {
83
+ return res;
84
+ };
82
85
  SequencerAPIResponseParser.prototype.parseFeeEstimateResponse = function (res) {
83
86
  if ('overall_fee' in res) {
84
87
  var gasInfo = {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starknet",
3
- "version": "3.19.0",
3
+ "version": "4.1.0",
4
4
  "description": "JavaScript library for StarkNet",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -43,10 +43,8 @@
43
43
  "@types/minimalistic-assert": "^1.0.1",
44
44
  "@types/pako": "^2.0.0",
45
45
  "@types/url-join": "^4.0.1",
46
- "@types/whatwg-fetch": "^0.0.33",
47
46
  "@typescript-eslint/eslint-plugin": "^5.28.0",
48
47
  "@typescript-eslint/parser": "^5.28.0",
49
- "cross-fetch": "^3.1.5",
50
48
  "eslint": "^8.17.0",
51
49
  "eslint-config-airbnb-base": "^15.0.0",
52
50
  "eslint-config-airbnb-typescript": "^17.0.0",
@@ -62,16 +60,15 @@
62
60
  "prettier": "^2.7.0",
63
61
  "prettier-plugin-import-sort": "^0.0.7",
64
62
  "typedoc": "^0.22.17",
65
- "typescript": "^4.7.3",
66
- "whatwg-fetch": "^3.6.2"
63
+ "typescript": "^4.7.3"
67
64
  },
68
65
  "dependencies": {
69
66
  "@ethersproject/bytes": "^5.6.1",
70
67
  "bn.js": "^5.2.1",
71
- "cross-fetch": "^3.1.5",
72
68
  "elliptic": "^6.5.4",
73
69
  "ethereum-cryptography": "^1.0.3",
74
70
  "hash.js": "^1.1.7",
71
+ "isomorphic-fetch": "^3.0.0",
75
72
  "json-bigint": "^1.0.0",
76
73
  "minimalistic-assert": "^1.0.1",
77
74
  "pako": "^2.0.4",
@@ -1,5 +1,5 @@
1
1
  import { StarknetChainId } from '../constants';
2
- import { BlockTag, Call, CallContractResponse, ContractClass, DeclareContractPayload, DeclareContractResponse, DeployContractPayload, DeployContractResponse, EstimateFeeResponse, GetBlockResponse, GetTransactionReceiptResponse, GetTransactionResponse, Invocation, InvocationsDetails, InvokeFunctionResponse } from '../types';
2
+ import { BlockTag, Call, CallContractResponse, ContractClass, DeclareContractPayload, DeclareContractResponse, DeployContractPayload, DeployContractResponse, EstimateFeeResponse, GetBlockResponse, GetCodeResponse, GetTransactionReceiptResponse, GetTransactionResponse, Invocation, InvocationsDetails, InvokeFunctionResponse } from '../types';
3
3
  import { BigNumberish } from '../utils/number';
4
4
  import { ProviderInterface } from './interface';
5
5
  import { RpcProviderOptions } from './rpc';
@@ -23,5 +23,6 @@ export declare class Provider implements ProviderInterface {
23
23
  invokeFunction(functionInvocation: Invocation, details: InvocationsDetails): Promise<InvokeFunctionResponse>;
24
24
  deployContract(payload: DeployContractPayload): Promise<DeployContractResponse>;
25
25
  declareContract(payload: DeclareContractPayload): Promise<DeclareContractResponse>;
26
+ getCode(contractAddress: string, blockIdentifier?: BlockIdentifier): Promise<GetCodeResponse>;
26
27
  waitForTransaction(txHash: BigNumberish, retryInterval?: number): Promise<void>;
27
28
  }
@@ -137,6 +137,13 @@ var Provider = /** @class */ (function () {
137
137
  });
138
138
  });
139
139
  };
140
+ Provider.prototype.getCode = function (contractAddress, blockIdentifier) {
141
+ return __awaiter(this, void 0, void 0, function () {
142
+ return __generator(this, function (_a) {
143
+ return [2 /*return*/, this.provider.getCode(contractAddress, blockIdentifier)];
144
+ });
145
+ });
146
+ };
140
147
  Provider.prototype.waitForTransaction = function (txHash, retryInterval) {
141
148
  return __awaiter(this, void 0, void 0, function () {
142
149
  return __generator(this, function (_a) {
@@ -1,5 +1,5 @@
1
1
  import { StarknetChainId } from '../constants';
2
- import type { BlockTag, Call, CallContractResponse, ContractClass, DeclareContractPayload, DeclareContractResponse, DeployContractPayload, DeployContractResponse, EstimateFeeResponse, GetBlockResponse, GetTransactionReceiptResponse, GetTransactionResponse, Invocation, InvocationsDetails, InvokeFunctionResponse } from '../types';
2
+ import type { BlockTag, Call, CallContractResponse, ContractClass, DeclareContractPayload, DeclareContractResponse, DeployContractPayload, DeployContractResponse, EstimateFeeResponse, GetBlockResponse, GetCodeResponse, GetTransactionReceiptResponse, GetTransactionResponse, Invocation, InvocationsDetails, InvokeFunctionResponse } from '../types';
3
3
  import type { BigNumberish } from '../utils/number';
4
4
  import { BlockIdentifier } from './utils';
5
5
  export declare abstract class ProviderInterface {
@@ -19,6 +19,7 @@ export declare abstract class ProviderInterface {
19
19
  * @returns the block object
20
20
  */
21
21
  abstract getBlock(blockIdentifier: BlockIdentifier): Promise<GetBlockResponse>;
22
+ abstract getCode(contractAddress: string, blockIdentifier?: BlockIdentifier): Promise<GetCodeResponse>;
22
23
  /**
23
24
  * Gets the contract class of the deployed contract.
24
25
  *
package/provider/rpc.d.ts CHANGED
@@ -24,6 +24,7 @@ export declare class RpcProvider implements ProviderInterface {
24
24
  deployContract({ contract, constructorCalldata, addressSalt, }: DeployContractPayload): Promise<DeployContractResponse>;
25
25
  invokeFunction(functionInvocation: Invocation, details: InvocationsDetails): Promise<InvokeFunctionResponse>;
26
26
  callContract(call: Call, blockIdentifier?: BlockIdentifier): Promise<CallContractResponse>;
27
+ getCode(contractAddress: string, _blockIdentifier?: BlockIdentifier): Promise<RPC.GetCodeResponse>;
27
28
  waitForTransaction(txHash: BigNumberish, retryInterval?: number): Promise<void>;
28
29
  /**
29
30
  * Gets the transaction count from a block.
package/provider/rpc.js CHANGED
@@ -40,7 +40,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
40
40
  };
41
41
  Object.defineProperty(exports, "__esModule", { value: true });
42
42
  exports.RpcProvider = void 0;
43
- var cross_fetch_1 = __importDefault(require("cross-fetch"));
43
+ var fetchPonyfill_1 = __importDefault(require("../utils/fetchPonyfill"));
44
44
  var hash_1 = require("../utils/hash");
45
45
  var json_1 = require("../utils/json");
46
46
  var number_1 = require("../utils/number");
@@ -73,7 +73,7 @@ var RpcProvider = /** @class */ (function () {
73
73
  _c.label = 1;
74
74
  case 1:
75
75
  _c.trys.push([1, 4, , 5]);
76
- return [4 /*yield*/, (0, cross_fetch_1.default)(this.nodeUrl, {
76
+ return [4 /*yield*/, (0, fetchPonyfill_1.default)(this.nodeUrl, {
77
77
  method: 'POST',
78
78
  body: (0, json_1.stringify)(requestData),
79
79
  headers: {
@@ -248,6 +248,19 @@ var RpcProvider = /** @class */ (function () {
248
248
  });
249
249
  });
250
250
  };
251
+ RpcProvider.prototype.getCode = function (contractAddress, _blockIdentifier) {
252
+ return __awaiter(this, void 0, void 0, function () {
253
+ var result;
254
+ return __generator(this, function (_a) {
255
+ switch (_a.label) {
256
+ case 0: return [4 /*yield*/, this.fetchEndpoint('starknet_getCode', [contractAddress])];
257
+ case 1:
258
+ result = _a.sent();
259
+ return [2 /*return*/, this.responseParser.parseGetCodeResponse(result)];
260
+ }
261
+ });
262
+ });
263
+ };
251
264
  RpcProvider.prototype.waitForTransaction = function (txHash, retryInterval) {
252
265
  if (retryInterval === void 0) { retryInterval = 8000; }
253
266
  return __awaiter(this, void 0, void 0, function () {
@@ -37,6 +37,7 @@ export declare class SequencerProvider implements ProviderInterface {
37
37
  deployContract({ contract, constructorCalldata, addressSalt, }: DeployContractPayload): Promise<DeployContractResponse>;
38
38
  declareContract({ contract, }: DeclareContractPayload): Promise<DeclareContractResponse>;
39
39
  getEstimateFee(invocation: Invocation, blockIdentifier?: BlockIdentifier, invocationDetails?: InvocationsDetails): Promise<EstimateFeeResponse>;
40
+ getCode(contractAddress: string, blockIdentifier?: BlockIdentifier): Promise<Sequencer.GetCodeResponse>;
40
41
  waitForTransaction(txHash: BigNumberish, retryInterval?: number): Promise<void>;
41
42
  /**
42
43
  * Gets the status of a transaction.
@@ -58,6 +58,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
58
58
  exports.SequencerProvider = void 0;
59
59
  var url_join_1 = __importDefault(require("url-join"));
60
60
  var constants_1 = require("../constants");
61
+ var fetchPonyfill_1 = __importDefault(require("../utils/fetchPonyfill"));
61
62
  var hash_1 = require("../utils/hash");
62
63
  var json_1 = require("../utils/json");
63
64
  var number_1 = require("../utils/number");
@@ -180,7 +181,7 @@ var SequencerProvider = /** @class */ (function () {
180
181
  _c.label = 1;
181
182
  case 1:
182
183
  _c.trys.push([1, 4, , 5]);
183
- return [4 /*yield*/, fetch(url, {
184
+ return [4 /*yield*/, (0, fetchPonyfill_1.default)(url, {
184
185
  method: method,
185
186
  body: (0, json_1.stringify)(request),
186
187
  headers: headers,
@@ -354,6 +355,14 @@ var SequencerProvider = /** @class */ (function () {
354
355
  });
355
356
  });
356
357
  };
358
+ SequencerProvider.prototype.getCode = function (contractAddress, blockIdentifier) {
359
+ if (blockIdentifier === void 0) { blockIdentifier = 'pending'; }
360
+ return __awaiter(this, void 0, void 0, function () {
361
+ return __generator(this, function (_a) {
362
+ return [2 /*return*/, this.fetchEndpoint('get_code', { contractAddress: contractAddress, blockIdentifier: blockIdentifier }).then(this.responseParser.parseGetCodeResponse)];
363
+ });
364
+ });
365
+ };
357
366
  SequencerProvider.prototype.waitForTransaction = function (txHash, retryInterval) {
358
367
  if (retryInterval === void 0) { retryInterval = 8000; }
359
368
  return __awaiter(this, void 0, void 0, function () {
@@ -10,6 +10,7 @@ import {
10
10
  DeployContractResponse,
11
11
  EstimateFeeResponse,
12
12
  GetBlockResponse,
13
+ GetCodeResponse,
13
14
  GetTransactionReceiptResponse,
14
15
  GetTransactionResponse,
15
16
  Invocation,
@@ -103,6 +104,13 @@ export class Provider implements ProviderInterface {
103
104
  return this.provider.declareContract(payload);
104
105
  }
105
106
 
107
+ public async getCode(
108
+ contractAddress: string,
109
+ blockIdentifier?: BlockIdentifier
110
+ ): Promise<GetCodeResponse> {
111
+ return this.provider.getCode(contractAddress, blockIdentifier);
112
+ }
113
+
106
114
  public async waitForTransaction(txHash: BigNumberish, retryInterval?: number): Promise<void> {
107
115
  return this.provider.waitForTransaction(txHash, retryInterval);
108
116
  }
@@ -10,6 +10,7 @@ import type {
10
10
  DeployContractResponse,
11
11
  EstimateFeeResponse,
12
12
  GetBlockResponse,
13
+ GetCodeResponse,
13
14
  GetTransactionReceiptResponse,
14
15
  GetTransactionResponse,
15
16
  Invocation,
@@ -42,6 +43,11 @@ export abstract class ProviderInterface {
42
43
  */
43
44
  public abstract getBlock(blockIdentifier: BlockIdentifier): Promise<GetBlockResponse>;
44
45
 
46
+ public abstract getCode(
47
+ contractAddress: string,
48
+ blockIdentifier?: BlockIdentifier
49
+ ): Promise<GetCodeResponse>;
50
+
45
51
  /**
46
52
  * Gets the contract class of the deployed contract.
47
53
  *
@@ -1,5 +1,3 @@
1
- import fetch from 'cross-fetch';
2
-
3
1
  import { StarknetChainId } from '../constants';
4
2
  import {
5
3
  BlockTag,
@@ -18,6 +16,7 @@ import {
18
16
  InvokeFunctionResponse,
19
17
  } from '../types';
20
18
  import { RPC } from '../types/api';
19
+ import fetch from '../utils/fetchPonyfill';
21
20
  import { getSelectorFromName } from '../utils/hash';
22
21
  import { stringify } from '../utils/json';
23
22
  import {
@@ -214,6 +213,15 @@ export class RpcProvider implements ProviderInterface {
214
213
  return this.responseParser.parseCallContractResponse(result);
215
214
  }
216
215
 
216
+ public async getCode(
217
+ contractAddress: string,
218
+ _blockIdentifier?: BlockIdentifier
219
+ ): Promise<RPC.GetCodeResponse> {
220
+ const result = await this.fetchEndpoint('starknet_getCode', [contractAddress]);
221
+
222
+ return this.responseParser.parseGetCodeResponse(result);
223
+ }
224
+
217
225
  public async waitForTransaction(txHash: BigNumberish, retryInterval: number = 8000) {
218
226
  let onchain = false;
219
227
  let retries = 100;
@@ -24,6 +24,7 @@ import {
24
24
  GetTransactionTraceResponse,
25
25
  Sequencer,
26
26
  } from '../types/api';
27
+ import fetch from '../utils/fetchPonyfill';
27
28
  import { getSelectorFromName } from '../utils/hash';
28
29
  import { parse, parseAlwaysAsBig, stringify } from '../utils/json';
29
30
  import { BigNumberish, bigNumberishArrayToDecimalStringArray, toBN, toHex } from '../utils/number';
@@ -322,6 +323,15 @@ export class SequencerProvider implements ProviderInterface {
322
323
  ).then(this.responseParser.parseFeeEstimateResponse);
323
324
  }
324
325
 
326
+ public async getCode(
327
+ contractAddress: string,
328
+ blockIdentifier: BlockIdentifier = 'pending'
329
+ ): Promise<Sequencer.GetCodeResponse> {
330
+ return this.fetchEndpoint('get_code', { contractAddress, blockIdentifier }).then(
331
+ this.responseParser.parseGetCodeResponse
332
+ );
333
+ }
334
+
325
335
  public async waitForTransaction(txHash: BigNumberish, retryInterval: number = 8000) {
326
336
  let onchain = false;
327
337
 
@@ -53,11 +53,6 @@ export type ExecutionResources = {
53
53
  n_memory_holes: number;
54
54
  };
55
55
 
56
- export type GetCodeResponse = {
57
- bytecode: string[];
58
- abi: Abi;
59
- };
60
-
61
56
  export type GetTransactionTraceResponse = {
62
57
  function_invocation: {
63
58
  caller_address: string;
@@ -116,6 +111,11 @@ export namespace Sequencer {
116
111
  class_hash?: string;
117
112
  };
118
113
 
114
+ export type GetCodeResponse = {
115
+ bytecode: string[];
116
+ abi: Abi;
117
+ };
118
+
119
119
  export interface InvokeFunctionTransactionResponse extends InvokeFunctionTransaction {
120
120
  transaction_hash: string;
121
121
  }
@@ -16,6 +16,11 @@ export interface GetBlockResponse {
16
16
  starknet_version?: string;
17
17
  }
18
18
 
19
+ export interface GetCodeResponse {
20
+ bytecode: string[];
21
+ // abi: string; // is not consistent between rpc and sequencer (is it?), therefore not included in the provider interface
22
+ }
23
+
19
24
  export type GetTransactionResponse = InvokeTransactionResponse & DeclareTransactionResponse;
20
25
 
21
26
  export interface CommonTransactionResponse {
@@ -0,0 +1,4 @@
1
+ export default (typeof window !== 'undefined' && window.fetch) || // use buildin fetch in browser if available
2
+ (typeof global !== 'undefined' && global.fetch) || // use buildin fetch in node, react-native and service worker if available
3
+ // eslint-disable-next-line global-require
4
+ require('isomorphic-fetch'); // ponyfill fetch in node and browsers that don't have it
@@ -57,6 +57,10 @@ export class RPCResponseParser extends ResponseParser {
57
57
  };
58
58
  }
59
59
 
60
+ public parseGetCodeResponse(res: RPC.GetCodeResponse): RPC.GetCodeResponse {
61
+ return res;
62
+ }
63
+
60
64
  public parseFeeEstimateResponse(res: RPC.EstimateFeeResponse): EstimateFeeResponse {
61
65
  return {
62
66
  overall_fee: toBN(res.overall_fee),
@@ -70,6 +70,10 @@ export class SequencerAPIResponseParser extends ResponseParser {
70
70
  };
71
71
  }
72
72
 
73
+ public parseGetCodeResponse(res: Sequencer.GetCodeResponse): Sequencer.GetCodeResponse {
74
+ return res;
75
+ }
76
+
73
77
  public parseFeeEstimateResponse(res: Sequencer.EstimateFeeResponse): EstimateFeeResponse {
74
78
  if ('overall_fee' in res) {
75
79
  let gasInfo = {};
@@ -39,10 +39,6 @@ export declare type ExecutionResources = {
39
39
  };
40
40
  n_memory_holes: number;
41
41
  };
42
- export declare type GetCodeResponse = {
43
- bytecode: string[];
44
- abi: Abi;
45
- };
46
42
  export declare type GetTransactionTraceResponse = {
47
43
  function_invocation: {
48
44
  caller_address: string;
@@ -97,6 +93,10 @@ export declare namespace Sequencer {
97
93
  address?: string;
98
94
  class_hash?: string;
99
95
  };
96
+ type GetCodeResponse = {
97
+ bytecode: string[];
98
+ abi: Abi;
99
+ };
100
100
  interface InvokeFunctionTransactionResponse extends InvokeFunctionTransaction {
101
101
  transaction_hash: string;
102
102
  }
@@ -13,6 +13,9 @@ export interface GetBlockResponse {
13
13
  transactions: Array<string>;
14
14
  starknet_version?: string;
15
15
  }
16
+ export interface GetCodeResponse {
17
+ bytecode: string[];
18
+ }
16
19
  export declare type GetTransactionResponse = InvokeTransactionResponse & DeclareTransactionResponse;
17
20
  export interface CommonTransactionResponse {
18
21
  transaction_hash?: string;
@@ -0,0 +1,2 @@
1
+ declare const _default: any;
2
+ export default _default;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = (typeof window !== 'undefined' && window.fetch) || // use buildin fetch in browser if available
4
+ (typeof global !== 'undefined' && global.fetch) || // use buildin fetch in node, react-native and service worker if available
5
+ // eslint-disable-next-line global-require
6
+ require('isomorphic-fetch'); // ponyfill fetch in node and browsers that don't have it
@@ -5,6 +5,7 @@ export declare class RPCResponseParser extends ResponseParser {
5
5
  parseGetBlockResponse(res: RPC.GetBlockResponse): GetBlockResponse;
6
6
  parseGetTransactionResponse(res: RPC.GetTransactionResponse): GetTransactionResponse;
7
7
  parseGetTransactionReceiptResponse(res: RPC.GetTransactionReceiptResponse): GetTransactionReceiptResponse;
8
+ parseGetCodeResponse(res: RPC.GetCodeResponse): RPC.GetCodeResponse;
8
9
  parseFeeEstimateResponse(res: RPC.EstimateFeeResponse): EstimateFeeResponse;
9
10
  parseCallContractResponse(res: Array<string>): CallContractResponse;
10
11
  parseInvokeFunctionResponse(res: RPC.AddTransactionResponse): InvokeFunctionResponse;
@@ -62,6 +62,9 @@ var RPCResponseParser = /** @class */ (function (_super) {
62
62
  events: res.events,
63
63
  };
64
64
  };
65
+ RPCResponseParser.prototype.parseGetCodeResponse = function (res) {
66
+ return res;
67
+ };
65
68
  RPCResponseParser.prototype.parseFeeEstimateResponse = function (res) {
66
69
  return {
67
70
  overall_fee: (0, number_1.toBN)(res.overall_fee),
@@ -5,6 +5,7 @@ export declare class SequencerAPIResponseParser extends ResponseParser {
5
5
  parseGetBlockResponse(res: Sequencer.GetBlockResponse): GetBlockResponse;
6
6
  parseGetTransactionResponse(res: Sequencer.GetTransactionResponse): GetTransactionResponse;
7
7
  parseGetTransactionReceiptResponse(res: Sequencer.TransactionReceiptResponse): GetTransactionReceiptResponse;
8
+ parseGetCodeResponse(res: Sequencer.GetCodeResponse): Sequencer.GetCodeResponse;
8
9
  parseFeeEstimateResponse(res: Sequencer.EstimateFeeResponse): EstimateFeeResponse;
9
10
  parseCallContractResponse(res: Sequencer.CallContractResponse): CallContractResponse;
10
11
  parseInvokeFunctionResponse(res: Sequencer.AddTransactionResponse): InvokeFunctionResponse;
@@ -79,6 +79,9 @@ var SequencerAPIResponseParser = /** @class */ (function (_super) {
79
79
  l1_origin_message: undefined,
80
80
  };
81
81
  };
82
+ SequencerAPIResponseParser.prototype.parseGetCodeResponse = function (res) {
83
+ return res;
84
+ };
82
85
  SequencerAPIResponseParser.prototype.parseFeeEstimateResponse = function (res) {
83
86
  if ('overall_fee' in res) {
84
87
  var gasInfo = {};
@@ -22,7 +22,7 @@ contract.**address** => _string_
22
22
 
23
23
  The address the contract was constructed/connected with
24
24
 
25
- contract.**providerOrAcount** => ProviderInterface | AccountInterface_
25
+ contract.**providerOrAccount** => _ProviderInterface | AccountInterface_
26
26
 
27
27
  Provider or account that are used to interact with the network
28
28
 
@@ -175,7 +175,7 @@ Deploys a contract on Starknet
175
175
 
176
176
  <hr/>
177
177
 
178
- provider.\*\*waitForTransaction(txHash [ , retryInterval]) => _Promise < void >_
178
+ provider.**waitForTransaction**(txHash [ , retryInterval]) => _Promise < void >_
179
179
 
180
180
  Wait for the transaction to be accepted on L2 or L1.
181
181