@airgap/ethereum 0.13.45-beta.1 → 0.13.45-beta.3

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 (82) hide show
  1. package/package.json +5 -5
  2. package/v0/index.js +10 -10
  3. package/v0/index.js.map +1 -1
  4. package/v0/protocol/BaseEthereumProtocol.d.ts +0 -1
  5. package/v0/protocol/BaseEthereumProtocol.js +460 -872
  6. package/v0/protocol/BaseEthereumProtocol.js.map +1 -1
  7. package/v0/protocol/EthereumAddress.d.ts +0 -1
  8. package/v0/protocol/EthereumAddress.js +26 -17
  9. package/v0/protocol/EthereumAddress.js.map +1 -1
  10. package/v0/protocol/EthereumClassicProtocol.js +6 -23
  11. package/v0/protocol/EthereumClassicProtocol.js.map +1 -1
  12. package/v0/protocol/EthereumCryptoClient.js +66 -125
  13. package/v0/protocol/EthereumCryptoClient.js.map +1 -1
  14. package/v0/protocol/EthereumProtocol.js +9 -28
  15. package/v0/protocol/EthereumProtocol.js.map +1 -1
  16. package/v0/protocol/EthereumProtocolOptions.d.ts +1 -1
  17. package/v0/protocol/EthereumProtocolOptions.js +36 -117
  18. package/v0/protocol/EthereumProtocolOptions.js.map +1 -1
  19. package/v0/protocol/EthereumRopstenProtocol.js +6 -23
  20. package/v0/protocol/EthereumRopstenProtocol.js.map +1 -1
  21. package/v0/protocol/clients/info-clients/EtherscanInfoClient.js +86 -186
  22. package/v0/protocol/clients/info-clients/EtherscanInfoClient.js.map +1 -1
  23. package/v0/protocol/clients/info-clients/InfoClient.js +3 -4
  24. package/v0/protocol/clients/info-clients/InfoClient.js.map +1 -1
  25. package/v0/protocol/clients/node-clients/AirGapNodeClient.js +126 -298
  26. package/v0/protocol/clients/node-clients/AirGapNodeClient.js.map +1 -1
  27. package/v0/protocol/clients/node-clients/NodeClient.js +3 -4
  28. package/v0/protocol/clients/node-clients/NodeClient.js.map +1 -1
  29. package/v0/protocol/erc20/ERC20.js +4 -4
  30. package/v0/protocol/erc20/ERC20.js.map +1 -1
  31. package/v0/protocol/erc20/GenericERC20.js +161 -300
  32. package/v0/protocol/erc20/GenericERC20.js.map +1 -1
  33. package/v0/protocol/utils/utils.js +52 -55
  34. package/v0/protocol/utils/utils.js.map +1 -1
  35. package/v0/serializer/validators/transaction-validator.js +23 -31
  36. package/v0/serializer/validators/transaction-validator.js.map +1 -1
  37. package/v0/serializer/validators/validators.js +25 -73
  38. package/v0/serializer/validators/validators.js.map +1 -1
  39. package/v1/block-explorer/EtherscanBlockExplorer.js +12 -61
  40. package/v1/block-explorer/EtherscanBlockExplorer.js.map +1 -1
  41. package/v1/clients/crypto/EthereumCryptoClient.js +69 -124
  42. package/v1/clients/crypto/EthereumCryptoClient.js.map +1 -1
  43. package/v1/clients/info/EthereumInfoClient.d.ts +1 -1
  44. package/v1/clients/info/EthereumInfoClient.js +3 -4
  45. package/v1/clients/info/EthereumInfoClient.js.map +1 -1
  46. package/v1/clients/info/EtherscanInfoClient.js +84 -162
  47. package/v1/clients/info/EtherscanInfoClient.js.map +1 -1
  48. package/v1/clients/node/HttpEthereumNodeClient.d.ts +2 -2
  49. package/v1/clients/node/HttpEthereumNodeClient.js +164 -375
  50. package/v1/clients/node/HttpEthereumNodeClient.js.map +1 -1
  51. package/v1/data/EthereumAddress.js +29 -20
  52. package/v1/data/EthereumAddress.js.map +1 -1
  53. package/v1/index.js +14 -14
  54. package/v1/index.js.map +1 -1
  55. package/v1/module/EthereumModule.js +43 -112
  56. package/v1/module/EthereumModule.js.map +1 -1
  57. package/v1/module.js +2 -3
  58. package/v1/module.js.map +1 -1
  59. package/v1/protocol/EthereumBaseProtocol.js +467 -788
  60. package/v1/protocol/EthereumBaseProtocol.js.map +1 -1
  61. package/v1/protocol/EthereumProtocol.js +15 -46
  62. package/v1/protocol/EthereumProtocol.js.map +1 -1
  63. package/v1/protocol/erc20/ERC20Protocol.js +213 -360
  64. package/v1/protocol/erc20/ERC20Protocol.js.map +1 -1
  65. package/v1/protocol/erc20/ERC20Token.d.ts +1 -1
  66. package/v1/protocol/erc20/ERC20Token.js +35 -113
  67. package/v1/protocol/erc20/ERC20Token.js.map +1 -1
  68. package/v1/serializer/v3/schemas/converter/transaction-converter.js +25 -42
  69. package/v1/serializer/v3/schemas/converter/transaction-converter.js.map +1 -1
  70. package/v1/serializer/v3/serializer-companion.js +71 -148
  71. package/v1/serializer/v3/serializer-companion.js.map +1 -1
  72. package/v1/serializer/v3/validators/transaction-validator.js +14 -17
  73. package/v1/serializer/v3/validators/transaction-validator.js.map +1 -1
  74. package/v1/types/crypto.d.ts +1 -1
  75. package/v1/types/protocol.d.ts +1 -1
  76. package/v1/types/transaction.d.ts +1 -1
  77. package/v1/utils/EthereumUtils.js +57 -60
  78. package/v1/utils/EthereumUtils.js.map +1 -1
  79. package/v1/utils/key.js +28 -19
  80. package/v1/utils/key.js.map +1 -1
  81. package/v1/utils/protocol.js +33 -21
  82. package/v1/utils/protocol.js.map +1 -1
@@ -1,121 +1,56 @@
1
1
  "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
- return new (P || (P = Promise))(function (resolve, reject) {
20
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
21
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
22
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
23
- step((generator = generator.apply(thisArg, _arguments || [])).next());
24
- });
25
- };
26
- var __generator = (this && this.__generator) || function (thisArg, body) {
27
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
28
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
29
- function verb(n) { return function (v) { return step([n, v]); }; }
30
- function step(op) {
31
- if (f) throw new TypeError("Generator is already executing.");
32
- while (_) try {
33
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
34
- if (y = 0, t) op = [op[0] & 2, t.value];
35
- switch (op[0]) {
36
- case 0: case 1: t = op; break;
37
- case 4: _.label++; return { value: op[1], done: false };
38
- case 5: _.label++; y = op[1]; op = [0]; continue;
39
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
40
- default:
41
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
42
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
43
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
44
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
45
- if (t[2]) _.ops.pop();
46
- _.trys.pop(); continue;
47
- }
48
- op = body.call(thisArg, _);
49
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
50
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
51
- }
52
- };
53
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
54
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
55
- if (ar || !(i in from)) {
56
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
57
- ar[i] = from[i];
58
- }
59
- }
60
- return to.concat(ar || Array.prototype.slice.call(from));
61
- };
62
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
63
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
64
4
  };
65
5
  Object.defineProperty(exports, "__esModule", { value: true });
66
6
  exports.HttpEthereumNodeClient = exports.EthereumRPCDataTransfer = exports.EthereumRPCDataBalanceOf = exports.EthereumRPCData = exports.EthereumRPCBody = void 0;
67
7
  // tslint:disable: max-classes-per-file
68
- var RPCBody_1 = require("@airgap/coinlib-core/data/RPCBody");
69
- var axios_0_19_0_1 = __importDefault(require("@airgap/coinlib-core/dependencies/src/axios-0.19.0"));
70
- var bignumber_1 = require("@airgap/coinlib-core/dependencies/src/bignumber.js-9.0.0/bignumber");
71
- var errors_1 = require("@airgap/coinlib-core/errors");
72
- var coinlib_error_1 = require("@airgap/coinlib-core/errors/coinlib-error");
73
- var coinlib_core_1 = require("@airgap/coinlib-core");
74
- var EthereumUtils_1 = require("../../utils/EthereumUtils");
75
- var EthereumRPCBody = /** @class */ (function (_super) {
76
- __extends(EthereumRPCBody, _super);
77
- function EthereumRPCBody() {
78
- return _super !== null && _super.apply(this, arguments) || this;
79
- }
80
- EthereumRPCBody.prototype.toRPCBody = function () {
8
+ const RPCBody_1 = require("@airgap/coinlib-core/data/RPCBody");
9
+ const axios_0_19_0_1 = __importDefault(require("@airgap/coinlib-core/dependencies/src/axios-0.19.0"));
10
+ const bignumber_1 = require("@airgap/coinlib-core/dependencies/src/bignumber.js-9.0.0/bignumber");
11
+ const errors_1 = require("@airgap/coinlib-core/errors");
12
+ const coinlib_error_1 = require("@airgap/coinlib-core/errors/coinlib-error");
13
+ const coinlib_core_1 = require("@airgap/coinlib-core");
14
+ const EthereumUtils_1 = require("../../utils/EthereumUtils");
15
+ class EthereumRPCBody extends RPCBody_1.RPCBody {
16
+ static { this.blockEarliest = 'earliest'; }
17
+ static { this.blockLatest = 'latest'; }
18
+ static { this.blockPending = 'pending'; }
19
+ toRPCBody() {
81
20
  return JSON.stringify(this.toJSON());
82
- };
83
- EthereumRPCBody.prototype.toJSON = function () {
21
+ }
22
+ toJSON() {
84
23
  return {
85
24
  jsonrpc: this.jsonrpc,
86
25
  method: this.method,
87
26
  params: this.params,
88
27
  id: this.id
89
28
  };
90
- };
91
- EthereumRPCBody.blockEarliest = 'earliest';
92
- EthereumRPCBody.blockLatest = 'latest';
93
- EthereumRPCBody.blockPending = 'pending';
94
- return EthereumRPCBody;
95
- }(RPCBody_1.RPCBody));
29
+ }
30
+ }
96
31
  exports.EthereumRPCBody = EthereumRPCBody;
97
- var EthereumRPCData = /** @class */ (function () {
98
- function EthereumRPCData(methodSignature) {
32
+ class EthereumRPCData {
33
+ // 2 chars = 1 byte hence to get to 32 bytes we need 64 chars
34
+ static { this.parametersLength = 64; }
35
+ constructor(methodSignature) {
99
36
  this.methodSignature = methodSignature;
100
37
  }
101
- EthereumRPCData.prototype.abiEncoded = function () {
102
- var hash = EthereumUtils_1.EthereumUtils.sha3(this.methodSignature);
38
+ abiEncoded() {
39
+ const hash = EthereumUtils_1.EthereumUtils.sha3(this.methodSignature);
103
40
  if (hash === null) {
104
41
  return '';
105
42
  }
106
- return "0x".concat(hash.slice(2, 10));
107
- };
108
- EthereumRPCData.addLeadingZeroPadding = function (value, targetLength) {
109
- if (targetLength === void 0) { targetLength = EthereumRPCData.parametersLength; }
110
- var result = value;
43
+ return `0x${hash.slice(2, 10)}`;
44
+ }
45
+ static addLeadingZeroPadding(value, targetLength = EthereumRPCData.parametersLength) {
46
+ let result = value;
111
47
  while (result.length < targetLength || result.length % 2 !== 0) {
112
48
  result = '0' + result;
113
49
  }
114
50
  return result;
115
- };
116
- EthereumRPCData.removeLeadingZeroPadding = function (value, isAddress) {
117
- if (isAddress === void 0) { isAddress = false; }
118
- var result = value;
51
+ }
52
+ static removeLeadingZeroPadding(value, isAddress = false) {
53
+ let result = value;
119
54
  if (isAddress) {
120
55
  return value.slice(-40);
121
56
  }
@@ -126,325 +61,179 @@ var EthereumRPCData = /** @class */ (function () {
126
61
  return result;
127
62
  }
128
63
  return result;
129
- };
130
- EthereumRPCData.abiDecoded = function (value, encodedType) {
64
+ }
65
+ static abiDecoded(value, encodedType) {
131
66
  switch (encodedType) {
132
67
  case 'bytes':
133
68
  if (value.startsWith('0x')) {
134
69
  value = value.slice(2);
135
70
  }
136
- var buffer = Buffer.from(value, 'hex');
137
- var offset = new bignumber_1.BigNumber(buffer.slice(0, 32).toString('hex'), 16).toNumber();
138
- var length_1 = new bignumber_1.BigNumber(buffer.slice(offset, offset + 32).toString('hex'), 16).toNumber();
139
- return buffer.slice(offset + 32, offset + 32 + length_1).toString('hex');
71
+ const buffer = Buffer.from(value, 'hex');
72
+ const offset = new bignumber_1.BigNumber(buffer.slice(0, 32).toString('hex'), 16).toNumber();
73
+ const length = new bignumber_1.BigNumber(buffer.slice(offset, offset + 32).toString('hex'), 16).toNumber();
74
+ return buffer.slice(offset + 32, offset + 32 + length).toString('hex');
140
75
  default:
141
76
  (0, coinlib_core_1.assertNever)(encodedType);
142
77
  throw new errors_1.UnsupportedError(coinlib_error_1.Domain.ETHEREUM, 'Unsupported ABI encoded type');
143
78
  }
144
- };
145
- // 2 chars = 1 byte hence to get to 32 bytes we need 64 chars
146
- EthereumRPCData.parametersLength = 64;
147
- return EthereumRPCData;
148
- }());
79
+ }
80
+ }
149
81
  exports.EthereumRPCData = EthereumRPCData;
150
- var EthereumRPCDataBalanceOf = /** @class */ (function (_super) {
151
- __extends(EthereumRPCDataBalanceOf, _super);
152
- function EthereumRPCDataBalanceOf(address) {
153
- var _this = _super.call(this, "".concat(EthereumRPCDataBalanceOf.methodName, "(address)")) || this;
154
- _this.address = address;
155
- return _this;
82
+ class EthereumRPCDataBalanceOf extends EthereumRPCData {
83
+ static { this.methodName = 'balanceOf'; }
84
+ constructor(address) {
85
+ super(`${EthereumRPCDataBalanceOf.methodName}(address)`);
86
+ this.address = address;
156
87
  }
157
- EthereumRPCDataBalanceOf.prototype.abiEncoded = function () {
158
- var srcAddress = this.address;
88
+ abiEncoded() {
89
+ let srcAddress = this.address;
159
90
  if (srcAddress.startsWith('0x')) {
160
91
  srcAddress = srcAddress.slice(2);
161
92
  }
162
- return _super.prototype.abiEncoded.call(this) + EthereumRPCData.addLeadingZeroPadding(srcAddress);
163
- };
164
- EthereumRPCDataBalanceOf.methodName = 'balanceOf';
165
- return EthereumRPCDataBalanceOf;
166
- }(EthereumRPCData));
93
+ return super.abiEncoded() + EthereumRPCData.addLeadingZeroPadding(srcAddress);
94
+ }
95
+ }
167
96
  exports.EthereumRPCDataBalanceOf = EthereumRPCDataBalanceOf;
168
- var EthereumRPCDataTransfer = /** @class */ (function (_super) {
169
- __extends(EthereumRPCDataTransfer, _super);
170
- function EthereumRPCDataTransfer(toAddressOrData, amount) {
171
- var _this = _super.call(this, "".concat(EthereumRPCDataTransfer.methodName, "(address,uint256)")) || this;
97
+ class EthereumRPCDataTransfer extends EthereumRPCData {
98
+ static { this.methodName = 'transfer'; }
99
+ constructor(toAddressOrData, amount) {
100
+ super(`${EthereumRPCDataTransfer.methodName}(address,uint256)`);
172
101
  if (amount) {
173
- var toAddress = toAddressOrData;
174
- _this.recipient = toAddress;
175
- _this.amount = amount;
102
+ const toAddress = toAddressOrData;
103
+ this.recipient = toAddress;
104
+ this.amount = amount;
176
105
  }
177
106
  else {
178
- var data = toAddressOrData;
179
- var methodID = _super.prototype.abiEncoded.call(_this);
107
+ const data = toAddressOrData;
108
+ const methodID = super.abiEncoded();
180
109
  if (!data.startsWith(methodID)) {
181
110
  throw new errors_1.InvalidValueError(coinlib_error_1.Domain.ETHEREUM, 'unexpected method ID');
182
111
  }
183
- var params = data.slice(methodID.length);
184
- var recipient = EthereumRPCData.removeLeadingZeroPadding(params.slice(0, EthereumRPCData.parametersLength), true);
185
- var parsedAmount = EthereumRPCData.removeLeadingZeroPadding(params.slice(EthereumRPCData.parametersLength));
186
- _this.recipient = "0x".concat(recipient);
187
- _this.amount = "0x".concat(parsedAmount);
112
+ const params = data.slice(methodID.length);
113
+ const recipient = EthereumRPCData.removeLeadingZeroPadding(params.slice(0, EthereumRPCData.parametersLength), true);
114
+ const parsedAmount = EthereumRPCData.removeLeadingZeroPadding(params.slice(EthereumRPCData.parametersLength));
115
+ this.recipient = `0x${recipient}`;
116
+ this.amount = `0x${parsedAmount}`;
188
117
  }
189
- return _this;
190
118
  }
191
- EthereumRPCDataTransfer.prototype.abiEncoded = function () {
192
- var dstAddress = this.recipient;
119
+ abiEncoded() {
120
+ let dstAddress = this.recipient;
193
121
  if (dstAddress.startsWith('0x')) {
194
122
  dstAddress = dstAddress.slice(2);
195
123
  }
196
- var transferAmount = this.amount;
124
+ let transferAmount = this.amount;
197
125
  if (transferAmount.startsWith('0x')) {
198
126
  transferAmount = transferAmount.slice(2);
199
127
  }
200
- return (_super.prototype.abiEncoded.call(this) +
128
+ return (super.abiEncoded() +
201
129
  EthereumRPCData.addLeadingZeroPadding(dstAddress.toLowerCase()) +
202
130
  EthereumRPCData.addLeadingZeroPadding(transferAmount.toLowerCase()));
203
- };
204
- EthereumRPCDataTransfer.methodName = 'transfer';
205
- return EthereumRPCDataTransfer;
206
- }(EthereumRPCData));
131
+ }
132
+ }
207
133
  exports.EthereumRPCDataTransfer = EthereumRPCDataTransfer;
208
- var HttpEthereumNodeClient = /** @class */ (function () {
209
- function HttpEthereumNodeClient(baseURL, headers) {
134
+ class HttpEthereumNodeClient {
135
+ constructor(baseURL, headers) {
210
136
  this.baseURL = baseURL;
211
137
  this.headers = headers;
212
138
  }
213
- HttpEthereumNodeClient.prototype.fetchBalance = function (address) {
214
- return __awaiter(this, void 0, void 0, function () {
215
- var body, response;
216
- return __generator(this, function (_a) {
217
- switch (_a.label) {
218
- case 0:
219
- body = new EthereumRPCBody('eth_getBalance', [address, EthereumRPCBody.blockLatest]);
220
- return [4 /*yield*/, this.send(body)];
221
- case 1:
222
- response = _a.sent();
223
- return [2 /*return*/, new bignumber_1.BigNumber(response.result)];
224
- }
225
- });
226
- });
227
- };
228
- HttpEthereumNodeClient.prototype.fetchTransactionCount = function (address) {
229
- return __awaiter(this, void 0, void 0, function () {
230
- var body, response;
231
- return __generator(this, function (_a) {
232
- switch (_a.label) {
233
- case 0:
234
- body = new EthereumRPCBody('eth_getTransactionCount', [address, EthereumRPCBody.blockLatest]);
235
- return [4 /*yield*/, this.send(body)];
236
- case 1:
237
- response = _a.sent();
238
- return [2 /*return*/, new bignumber_1.BigNumber(response.result).toNumber()];
239
- }
240
- });
241
- });
242
- };
243
- HttpEthereumNodeClient.prototype.sendSignedTransaction = function (transaction) {
244
- return __awaiter(this, void 0, void 0, function () {
245
- var body;
246
- return __generator(this, function (_a) {
247
- switch (_a.label) {
248
- case 0:
249
- body = new EthereumRPCBody('eth_sendRawTransaction', [transaction]);
250
- return [4 /*yield*/, this.send(body)];
251
- case 1: return [2 /*return*/, (_a.sent()).result];
252
- }
253
- });
254
- });
255
- };
256
- HttpEthereumNodeClient.prototype.getTransactionStatus = function (transactionHash) {
257
- return __awaiter(this, void 0, void 0, function () {
258
- var body, response;
259
- return __generator(this, function (_a) {
260
- switch (_a.label) {
261
- case 0:
262
- body = new EthereumRPCBody('eth_getTransactionReceipt', [transactionHash]);
263
- return [4 /*yield*/, this.send(body)];
264
- case 1:
265
- response = _a.sent();
266
- return [2 /*return*/, response.result.status === '0x1' ? { type: 'applied' } : { type: 'failed' }];
267
- }
268
- });
269
- });
270
- };
271
- HttpEthereumNodeClient.prototype.callBalanceOf = function (contractAddress, address) {
272
- return __awaiter(this, void 0, void 0, function () {
273
- var body, response;
274
- return __generator(this, function (_a) {
275
- switch (_a.label) {
276
- case 0:
277
- body = this.balanceOfBody(contractAddress, address);
278
- return [4 /*yield*/, this.send(body)];
279
- case 1:
280
- response = _a.sent();
281
- return [2 /*return*/, new bignumber_1.BigNumber(response.result)];
282
- }
283
- });
284
- });
285
- };
286
- HttpEthereumNodeClient.prototype.callBalanceOfOnContracts = function (contractAddresses, address) {
287
- return __awaiter(this, void 0, void 0, function () {
288
- var bodies, responses, result;
289
- var _this = this;
290
- return __generator(this, function (_a) {
291
- switch (_a.label) {
292
- case 0:
293
- bodies = contractAddresses.map(function (contractAddress, index) { return _this.balanceOfBody(contractAddress, address, index); });
294
- return [4 /*yield*/, this.batchSend(bodies)];
295
- case 1:
296
- responses = _a.sent();
297
- result = {};
298
- responses.forEach(function (response) {
299
- var _a;
300
- result[contractAddresses[response.id]] = new bignumber_1.BigNumber((_a = response.result) !== null && _a !== void 0 ? _a : 0);
301
- });
302
- return [2 /*return*/, result];
303
- }
304
- });
139
+ async fetchBalance(address) {
140
+ const body = new EthereumRPCBody('eth_getBalance', [address, EthereumRPCBody.blockLatest]);
141
+ const response = await this.send(body);
142
+ return new bignumber_1.BigNumber(response.result);
143
+ }
144
+ async fetchTransactionCount(address) {
145
+ const body = new EthereumRPCBody('eth_getTransactionCount', [address, EthereumRPCBody.blockLatest]);
146
+ const response = await this.send(body);
147
+ return new bignumber_1.BigNumber(response.result).toNumber();
148
+ }
149
+ async sendSignedTransaction(transaction) {
150
+ const body = new EthereumRPCBody('eth_sendRawTransaction', [transaction]);
151
+ return (await this.send(body)).result;
152
+ }
153
+ async getTransactionStatus(transactionHash) {
154
+ const body = new EthereumRPCBody('eth_getTransactionReceipt', [transactionHash]);
155
+ const response = await this.send(body);
156
+ return response.result.status === '0x1' ? { type: 'applied' } : { type: 'failed' };
157
+ }
158
+ async callBalanceOf(contractAddress, address) {
159
+ const body = this.balanceOfBody(contractAddress, address);
160
+ const response = await this.send(body);
161
+ return new bignumber_1.BigNumber(response.result);
162
+ }
163
+ async callBalanceOfOnContracts(contractAddresses, address) {
164
+ const bodies = contractAddresses.map((contractAddress, index) => this.balanceOfBody(contractAddress, address, index));
165
+ const responses = await this.batchSend(bodies);
166
+ const result = {};
167
+ responses.forEach((response) => {
168
+ result[contractAddresses[response.id]] = new bignumber_1.BigNumber(response.result ?? 0);
305
169
  });
306
- };
307
- HttpEthereumNodeClient.prototype.balanceOfBody = function (contractAddress, address, id) {
308
- if (id === void 0) { id = 0; }
309
- var data = new EthereumRPCDataBalanceOf(address);
170
+ return result;
171
+ }
172
+ balanceOfBody(contractAddress, address, id = 0) {
173
+ const data = new EthereumRPCDataBalanceOf(address);
310
174
  return this.contractCallBody(contractAddress, data, [EthereumRPCBody.blockLatest], id);
311
- };
312
- HttpEthereumNodeClient.prototype.estimateTransactionGas = function (fromAddress, toAddress, amount, data, gas) {
313
- return __awaiter(this, void 0, void 0, function () {
314
- var body, response;
315
- return __generator(this, function (_a) {
316
- switch (_a.label) {
317
- case 0:
318
- body = new EthereumRPCBody('eth_estimateGas', [{ from: fromAddress, to: toAddress, gas: gas, value: amount, data: data }]);
319
- return [4 /*yield*/, this.send(body)];
320
- case 1:
321
- response = _a.sent();
322
- return [2 /*return*/, new bignumber_1.BigNumber(response.result)];
323
- }
324
- });
325
- });
326
- };
327
- HttpEthereumNodeClient.prototype.estimateTransferGas = function (contractAddress, fromAddress, toAddress, hexAmount) {
328
- return __awaiter(this, void 0, void 0, function () {
329
- var data, result;
330
- return __generator(this, function (_a) {
331
- data = new EthereumRPCDataTransfer(toAddress, hexAmount);
332
- result = this.estimateTransactionGas(fromAddress, contractAddress, undefined, data.abiEncoded());
333
- return [2 /*return*/, result];
334
- });
335
- });
336
- };
337
- HttpEthereumNodeClient.prototype.getGasPrice = function () {
338
- return __awaiter(this, void 0, void 0, function () {
339
- var body, response;
340
- return __generator(this, function (_a) {
341
- switch (_a.label) {
342
- case 0:
343
- body = new EthereumRPCBody('eth_gasPrice', []);
344
- return [4 /*yield*/, this.send(body)];
345
- case 1:
346
- response = _a.sent();
347
- return [2 /*return*/, new bignumber_1.BigNumber(response.result)];
348
- }
349
- });
350
- });
351
- };
352
- HttpEthereumNodeClient.prototype.getContractName = function (contractAddress) {
353
- return __awaiter(this, void 0, void 0, function () {
354
- var data, body, response, result;
355
- return __generator(this, function (_a) {
356
- switch (_a.label) {
357
- case 0:
358
- data = new EthereumRPCData('name()');
359
- body = this.contractCallBody(contractAddress, data, [EthereumRPCBody.blockLatest]);
360
- return [4 /*yield*/, this.send(body)];
361
- case 1:
362
- response = _a.sent();
363
- if (!response.result) {
364
- return [2 /*return*/, undefined];
365
- }
366
- result = EthereumRPCData.abiDecoded(response.result, 'bytes');
367
- return [2 /*return*/, EthereumUtils_1.EthereumUtils.hexToUtf8(result)];
368
- }
369
- });
370
- });
371
- };
372
- HttpEthereumNodeClient.prototype.getContractSymbol = function (contractAddress) {
373
- return __awaiter(this, void 0, void 0, function () {
374
- var data, body, response, result;
375
- return __generator(this, function (_a) {
376
- switch (_a.label) {
377
- case 0:
378
- data = new EthereumRPCData('symbol()');
379
- body = this.contractCallBody(contractAddress, data, [EthereumRPCBody.blockLatest]);
380
- return [4 /*yield*/, this.send(body)];
381
- case 1:
382
- response = _a.sent();
383
- if (!response.result) {
384
- return [2 /*return*/, undefined];
385
- }
386
- result = EthereumRPCData.abiDecoded(response.result, 'bytes');
387
- return [2 /*return*/, EthereumUtils_1.EthereumUtils.hexToUtf8(result)];
388
- }
389
- });
390
- });
391
- };
392
- HttpEthereumNodeClient.prototype.getContractDecimals = function (contractAddress) {
393
- return __awaiter(this, void 0, void 0, function () {
394
- var data, body, response;
395
- return __generator(this, function (_a) {
396
- switch (_a.label) {
397
- case 0:
398
- data = new EthereumRPCData('decimals()');
399
- body = this.contractCallBody(contractAddress, data, [EthereumRPCBody.blockLatest]);
400
- return [4 /*yield*/, this.send(body)];
401
- case 1:
402
- response = _a.sent();
403
- if (!response.result) {
404
- return [2 /*return*/, undefined];
405
- }
406
- return [2 /*return*/, EthereumUtils_1.EthereumUtils.hexToNumber(response.result).toNumber()];
407
- }
408
- });
409
- });
410
- };
411
- HttpEthereumNodeClient.prototype.contractCallBody = function (contractAddress, data, extraParams, id, jsonrpc) {
412
- if (extraParams === void 0) { extraParams = []; }
413
- return new EthereumRPCBody('eth_call', __spreadArray([{ to: contractAddress, data: data.abiEncoded() }], extraParams, true), id, jsonrpc);
414
- };
415
- HttpEthereumNodeClient.prototype.send = function (body) {
416
- return __awaiter(this, void 0, void 0, function () {
417
- var response;
418
- return __generator(this, function (_a) {
419
- switch (_a.label) {
420
- case 0: return [4 /*yield*/, axios_0_19_0_1.default.post(this.baseURL, body.toRPCBody(), { headers: this.headers }).catch(function (error) {
421
- throw new errors_1.NetworkError(coinlib_error_1.Domain.ETHEREUM, error);
422
- })];
423
- case 1:
424
- response = _a.sent();
425
- return [2 /*return*/, response.data];
426
- }
427
- });
175
+ }
176
+ async estimateTransactionGas(fromAddress, toAddress, amount, data, gas) {
177
+ const body = new EthereumRPCBody('eth_estimateGas', [{ from: fromAddress, to: toAddress, gas, value: amount, data }]);
178
+ const response = await this.send(body);
179
+ return new bignumber_1.BigNumber(response.result);
180
+ }
181
+ async estimateTransferGas(contractAddress, fromAddress, toAddress, hexAmount) {
182
+ const data = new EthereumRPCDataTransfer(toAddress, hexAmount);
183
+ const result = this.estimateTransactionGas(fromAddress, contractAddress, undefined, data.abiEncoded());
184
+ return result;
185
+ }
186
+ async getGasPrice() {
187
+ const body = new EthereumRPCBody('eth_gasPrice', []);
188
+ const response = await this.send(body);
189
+ return new bignumber_1.BigNumber(response.result);
190
+ }
191
+ async getContractName(contractAddress) {
192
+ const data = new EthereumRPCData('name()');
193
+ const body = this.contractCallBody(contractAddress, data, [EthereumRPCBody.blockLatest]);
194
+ const response = await this.send(body);
195
+ if (!response.result) {
196
+ return undefined;
197
+ }
198
+ const result = EthereumRPCData.abiDecoded(response.result, 'bytes');
199
+ return EthereumUtils_1.EthereumUtils.hexToUtf8(result);
200
+ }
201
+ async getContractSymbol(contractAddress) {
202
+ const data = new EthereumRPCData('symbol()');
203
+ const body = this.contractCallBody(contractAddress, data, [EthereumRPCBody.blockLatest]);
204
+ const response = await this.send(body);
205
+ if (!response.result) {
206
+ return undefined;
207
+ }
208
+ const result = EthereumRPCData.abiDecoded(response.result, 'bytes');
209
+ return EthereumUtils_1.EthereumUtils.hexToUtf8(result);
210
+ }
211
+ async getContractDecimals(contractAddress) {
212
+ const data = new EthereumRPCData('decimals()');
213
+ const body = this.contractCallBody(contractAddress, data, [EthereumRPCBody.blockLatest]);
214
+ const response = await this.send(body);
215
+ if (!response.result) {
216
+ return undefined;
217
+ }
218
+ return EthereumUtils_1.EthereumUtils.hexToNumber(response.result).toNumber();
219
+ }
220
+ contractCallBody(contractAddress, data, extraParams = [], id, jsonrpc) {
221
+ return new EthereumRPCBody('eth_call', [{ to: contractAddress, data: data.abiEncoded() }, ...extraParams], id, jsonrpc);
222
+ }
223
+ async send(body) {
224
+ const response = await axios_0_19_0_1.default.post(this.baseURL, body.toRPCBody(), { headers: this.headers }).catch((error) => {
225
+ throw new errors_1.NetworkError(coinlib_error_1.Domain.ETHEREUM, error);
428
226
  });
429
- };
430
- HttpEthereumNodeClient.prototype.batchSend = function (bodies) {
431
- return __awaiter(this, void 0, void 0, function () {
432
- var response;
433
- return __generator(this, function (_a) {
434
- switch (_a.label) {
435
- case 0: return [4 /*yield*/, axios_0_19_0_1.default
436
- .post(this.baseURL, JSON.stringify(bodies.map(function (body) { return body.toJSON(); })), { headers: this.headers })
437
- .catch(function (error) {
438
- throw new errors_1.NetworkError(coinlib_error_1.Domain.ETHEREUM, error);
439
- })];
440
- case 1:
441
- response = _a.sent();
442
- return [2 /*return*/, response.data];
443
- }
444
- });
227
+ return response.data;
228
+ }
229
+ async batchSend(bodies) {
230
+ const response = await axios_0_19_0_1.default
231
+ .post(this.baseURL, JSON.stringify(bodies.map((body) => body.toJSON())), { headers: this.headers })
232
+ .catch((error) => {
233
+ throw new errors_1.NetworkError(coinlib_error_1.Domain.ETHEREUM, error);
445
234
  });
446
- };
447
- return HttpEthereumNodeClient;
448
- }());
235
+ return response.data;
236
+ }
237
+ }
449
238
  exports.HttpEthereumNodeClient = HttpEthereumNodeClient;
450
239
  //# sourceMappingURL=HttpEthereumNodeClient.js.map