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

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 +1 -0
  5. package/v0/protocol/BaseEthereumProtocol.js +872 -460
  6. package/v0/protocol/BaseEthereumProtocol.js.map +1 -1
  7. package/v0/protocol/EthereumAddress.d.ts +1 -0
  8. package/v0/protocol/EthereumAddress.js +17 -26
  9. package/v0/protocol/EthereumAddress.js.map +1 -1
  10. package/v0/protocol/EthereumClassicProtocol.js +23 -6
  11. package/v0/protocol/EthereumClassicProtocol.js.map +1 -1
  12. package/v0/protocol/EthereumCryptoClient.js +125 -66
  13. package/v0/protocol/EthereumCryptoClient.js.map +1 -1
  14. package/v0/protocol/EthereumProtocol.js +28 -9
  15. package/v0/protocol/EthereumProtocol.js.map +1 -1
  16. package/v0/protocol/EthereumProtocolOptions.d.ts +1 -1
  17. package/v0/protocol/EthereumProtocolOptions.js +117 -36
  18. package/v0/protocol/EthereumProtocolOptions.js.map +1 -1
  19. package/v0/protocol/EthereumRopstenProtocol.js +23 -6
  20. package/v0/protocol/EthereumRopstenProtocol.js.map +1 -1
  21. package/v0/protocol/clients/info-clients/EtherscanInfoClient.js +186 -86
  22. package/v0/protocol/clients/info-clients/EtherscanInfoClient.js.map +1 -1
  23. package/v0/protocol/clients/info-clients/InfoClient.js +4 -3
  24. package/v0/protocol/clients/info-clients/InfoClient.js.map +1 -1
  25. package/v0/protocol/clients/node-clients/AirGapNodeClient.js +298 -126
  26. package/v0/protocol/clients/node-clients/AirGapNodeClient.js.map +1 -1
  27. package/v0/protocol/clients/node-clients/NodeClient.js +4 -3
  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 +300 -161
  32. package/v0/protocol/erc20/GenericERC20.js.map +1 -1
  33. package/v0/protocol/utils/utils.js +55 -52
  34. package/v0/protocol/utils/utils.js.map +1 -1
  35. package/v0/serializer/validators/transaction-validator.js +31 -23
  36. package/v0/serializer/validators/transaction-validator.js.map +1 -1
  37. package/v0/serializer/validators/validators.js +73 -25
  38. package/v0/serializer/validators/validators.js.map +1 -1
  39. package/v1/block-explorer/EtherscanBlockExplorer.js +61 -12
  40. package/v1/block-explorer/EtherscanBlockExplorer.js.map +1 -1
  41. package/v1/clients/crypto/EthereumCryptoClient.js +124 -69
  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 +4 -3
  45. package/v1/clients/info/EthereumInfoClient.js.map +1 -1
  46. package/v1/clients/info/EtherscanInfoClient.js +162 -84
  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 +375 -164
  50. package/v1/clients/node/HttpEthereumNodeClient.js.map +1 -1
  51. package/v1/data/EthereumAddress.js +20 -29
  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 +112 -43
  56. package/v1/module/EthereumModule.js.map +1 -1
  57. package/v1/module.js +3 -2
  58. package/v1/module.js.map +1 -1
  59. package/v1/protocol/EthereumBaseProtocol.js +788 -467
  60. package/v1/protocol/EthereumBaseProtocol.js.map +1 -1
  61. package/v1/protocol/EthereumProtocol.js +46 -15
  62. package/v1/protocol/EthereumProtocol.js.map +1 -1
  63. package/v1/protocol/erc20/ERC20Protocol.js +360 -213
  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 +113 -35
  67. package/v1/protocol/erc20/ERC20Token.js.map +1 -1
  68. package/v1/serializer/v3/schemas/converter/transaction-converter.js +42 -25
  69. package/v1/serializer/v3/schemas/converter/transaction-converter.js.map +1 -1
  70. package/v1/serializer/v3/serializer-companion.js +148 -71
  71. package/v1/serializer/v3/serializer-companion.js.map +1 -1
  72. package/v1/serializer/v3/validators/transaction-validator.js +17 -14
  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 +60 -57
  78. package/v1/utils/EthereumUtils.js.map +1 -1
  79. package/v1/utils/key.js +19 -28
  80. package/v1/utils/key.js.map +1 -1
  81. package/v1/utils/protocol.js +21 -33
  82. package/v1/utils/protocol.js.map +1 -1
@@ -1,56 +1,121 @@
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
+ };
2
62
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
63
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
64
  };
5
65
  Object.defineProperty(exports, "__esModule", { value: true });
6
66
  exports.HttpEthereumNodeClient = exports.EthereumRPCDataTransfer = exports.EthereumRPCDataBalanceOf = exports.EthereumRPCData = exports.EthereumRPCBody = void 0;
7
67
  // tslint:disable: max-classes-per-file
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() {
20
- return JSON.stringify(this.toJSON());
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;
21
79
  }
22
- toJSON() {
80
+ EthereumRPCBody.prototype.toRPCBody = function () {
81
+ return JSON.stringify(this.toJSON());
82
+ };
83
+ EthereumRPCBody.prototype.toJSON = function () {
23
84
  return {
24
85
  jsonrpc: this.jsonrpc,
25
86
  method: this.method,
26
87
  params: this.params,
27
88
  id: this.id
28
89
  };
29
- }
30
- }
90
+ };
91
+ EthereumRPCBody.blockEarliest = 'earliest';
92
+ EthereumRPCBody.blockLatest = 'latest';
93
+ EthereumRPCBody.blockPending = 'pending';
94
+ return EthereumRPCBody;
95
+ }(RPCBody_1.RPCBody));
31
96
  exports.EthereumRPCBody = EthereumRPCBody;
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) {
97
+ var EthereumRPCData = /** @class */ (function () {
98
+ function EthereumRPCData(methodSignature) {
36
99
  this.methodSignature = methodSignature;
37
100
  }
38
- abiEncoded() {
39
- const hash = EthereumUtils_1.EthereumUtils.sha3(this.methodSignature);
101
+ EthereumRPCData.prototype.abiEncoded = function () {
102
+ var hash = EthereumUtils_1.EthereumUtils.sha3(this.methodSignature);
40
103
  if (hash === null) {
41
104
  return '';
42
105
  }
43
- return `0x${hash.slice(2, 10)}`;
44
- }
45
- static addLeadingZeroPadding(value, targetLength = EthereumRPCData.parametersLength) {
46
- let result = value;
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;
47
111
  while (result.length < targetLength || result.length % 2 !== 0) {
48
112
  result = '0' + result;
49
113
  }
50
114
  return result;
51
- }
52
- static removeLeadingZeroPadding(value, isAddress = false) {
53
- let result = value;
115
+ };
116
+ EthereumRPCData.removeLeadingZeroPadding = function (value, isAddress) {
117
+ if (isAddress === void 0) { isAddress = false; }
118
+ var result = value;
54
119
  if (isAddress) {
55
120
  return value.slice(-40);
56
121
  }
@@ -61,179 +126,325 @@ class EthereumRPCData {
61
126
  return result;
62
127
  }
63
128
  return result;
64
- }
65
- static abiDecoded(value, encodedType) {
129
+ };
130
+ EthereumRPCData.abiDecoded = function (value, encodedType) {
66
131
  switch (encodedType) {
67
132
  case 'bytes':
68
133
  if (value.startsWith('0x')) {
69
134
  value = value.slice(2);
70
135
  }
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');
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');
75
140
  default:
76
141
  (0, coinlib_core_1.assertNever)(encodedType);
77
142
  throw new errors_1.UnsupportedError(coinlib_error_1.Domain.ETHEREUM, 'Unsupported ABI encoded type');
78
143
  }
79
- }
80
- }
144
+ };
145
+ // 2 chars = 1 byte hence to get to 32 bytes we need 64 chars
146
+ EthereumRPCData.parametersLength = 64;
147
+ return EthereumRPCData;
148
+ }());
81
149
  exports.EthereumRPCData = EthereumRPCData;
82
- class EthereumRPCDataBalanceOf extends EthereumRPCData {
83
- static { this.methodName = 'balanceOf'; }
84
- constructor(address) {
85
- super(`${EthereumRPCDataBalanceOf.methodName}(address)`);
86
- this.address = address;
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;
87
156
  }
88
- abiEncoded() {
89
- let srcAddress = this.address;
157
+ EthereumRPCDataBalanceOf.prototype.abiEncoded = function () {
158
+ var srcAddress = this.address;
90
159
  if (srcAddress.startsWith('0x')) {
91
160
  srcAddress = srcAddress.slice(2);
92
161
  }
93
- return super.abiEncoded() + EthereumRPCData.addLeadingZeroPadding(srcAddress);
94
- }
95
- }
162
+ return _super.prototype.abiEncoded.call(this) + EthereumRPCData.addLeadingZeroPadding(srcAddress);
163
+ };
164
+ EthereumRPCDataBalanceOf.methodName = 'balanceOf';
165
+ return EthereumRPCDataBalanceOf;
166
+ }(EthereumRPCData));
96
167
  exports.EthereumRPCDataBalanceOf = EthereumRPCDataBalanceOf;
97
- class EthereumRPCDataTransfer extends EthereumRPCData {
98
- static { this.methodName = 'transfer'; }
99
- constructor(toAddressOrData, amount) {
100
- super(`${EthereumRPCDataTransfer.methodName}(address,uint256)`);
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;
101
172
  if (amount) {
102
- const toAddress = toAddressOrData;
103
- this.recipient = toAddress;
104
- this.amount = amount;
173
+ var toAddress = toAddressOrData;
174
+ _this.recipient = toAddress;
175
+ _this.amount = amount;
105
176
  }
106
177
  else {
107
- const data = toAddressOrData;
108
- const methodID = super.abiEncoded();
178
+ var data = toAddressOrData;
179
+ var methodID = _super.prototype.abiEncoded.call(_this);
109
180
  if (!data.startsWith(methodID)) {
110
181
  throw new errors_1.InvalidValueError(coinlib_error_1.Domain.ETHEREUM, 'unexpected method ID');
111
182
  }
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}`;
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);
117
188
  }
189
+ return _this;
118
190
  }
119
- abiEncoded() {
120
- let dstAddress = this.recipient;
191
+ EthereumRPCDataTransfer.prototype.abiEncoded = function () {
192
+ var dstAddress = this.recipient;
121
193
  if (dstAddress.startsWith('0x')) {
122
194
  dstAddress = dstAddress.slice(2);
123
195
  }
124
- let transferAmount = this.amount;
196
+ var transferAmount = this.amount;
125
197
  if (transferAmount.startsWith('0x')) {
126
198
  transferAmount = transferAmount.slice(2);
127
199
  }
128
- return (super.abiEncoded() +
200
+ return (_super.prototype.abiEncoded.call(this) +
129
201
  EthereumRPCData.addLeadingZeroPadding(dstAddress.toLowerCase()) +
130
202
  EthereumRPCData.addLeadingZeroPadding(transferAmount.toLowerCase()));
131
- }
132
- }
203
+ };
204
+ EthereumRPCDataTransfer.methodName = 'transfer';
205
+ return EthereumRPCDataTransfer;
206
+ }(EthereumRPCData));
133
207
  exports.EthereumRPCDataTransfer = EthereumRPCDataTransfer;
134
- class HttpEthereumNodeClient {
135
- constructor(baseURL, headers) {
208
+ var HttpEthereumNodeClient = /** @class */ (function () {
209
+ function HttpEthereumNodeClient(baseURL, headers) {
136
210
  this.baseURL = baseURL;
137
211
  this.headers = headers;
138
212
  }
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);
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
+ });
169
226
  });
170
- return result;
171
- }
172
- balanceOfBody(contractAddress, address, id = 0) {
173
- const data = new EthereumRPCDataBalanceOf(address);
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
+ });
305
+ });
306
+ };
307
+ HttpEthereumNodeClient.prototype.balanceOfBody = function (contractAddress, address, id) {
308
+ if (id === void 0) { id = 0; }
309
+ var data = new EthereumRPCDataBalanceOf(address);
174
310
  return this.contractCallBody(contractAddress, data, [EthereumRPCBody.blockLatest], id);
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);
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
+ });
226
325
  });
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);
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
+ });
234
335
  });
235
- return response.data;
236
- }
237
- }
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
+ });
428
+ });
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
+ });
445
+ });
446
+ };
447
+ return HttpEthereumNodeClient;
448
+ }());
238
449
  exports.HttpEthereumNodeClient = HttpEthereumNodeClient;
239
450
  //# sourceMappingURL=HttpEthereumNodeClient.js.map