@airgap/ethereum 0.13.45-beta.2 → 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.
- package/package.json +5 -5
- package/v0/index.js +10 -10
- package/v0/index.js.map +1 -1
- package/v0/protocol/BaseEthereumProtocol.d.ts +0 -1
- package/v0/protocol/BaseEthereumProtocol.js +460 -872
- package/v0/protocol/BaseEthereumProtocol.js.map +1 -1
- package/v0/protocol/EthereumAddress.d.ts +0 -1
- package/v0/protocol/EthereumAddress.js +26 -17
- package/v0/protocol/EthereumAddress.js.map +1 -1
- package/v0/protocol/EthereumClassicProtocol.js +6 -23
- package/v0/protocol/EthereumClassicProtocol.js.map +1 -1
- package/v0/protocol/EthereumCryptoClient.js +66 -125
- package/v0/protocol/EthereumCryptoClient.js.map +1 -1
- package/v0/protocol/EthereumProtocol.js +9 -28
- package/v0/protocol/EthereumProtocol.js.map +1 -1
- package/v0/protocol/EthereumProtocolOptions.d.ts +1 -1
- package/v0/protocol/EthereumProtocolOptions.js +36 -117
- package/v0/protocol/EthereumProtocolOptions.js.map +1 -1
- package/v0/protocol/EthereumRopstenProtocol.js +6 -23
- package/v0/protocol/EthereumRopstenProtocol.js.map +1 -1
- package/v0/protocol/clients/info-clients/EtherscanInfoClient.js +86 -186
- package/v0/protocol/clients/info-clients/EtherscanInfoClient.js.map +1 -1
- package/v0/protocol/clients/info-clients/InfoClient.js +3 -4
- package/v0/protocol/clients/info-clients/InfoClient.js.map +1 -1
- package/v0/protocol/clients/node-clients/AirGapNodeClient.js +126 -298
- package/v0/protocol/clients/node-clients/AirGapNodeClient.js.map +1 -1
- package/v0/protocol/clients/node-clients/NodeClient.js +3 -4
- package/v0/protocol/clients/node-clients/NodeClient.js.map +1 -1
- package/v0/protocol/erc20/ERC20.js +4 -4
- package/v0/protocol/erc20/ERC20.js.map +1 -1
- package/v0/protocol/erc20/GenericERC20.js +161 -300
- package/v0/protocol/erc20/GenericERC20.js.map +1 -1
- package/v0/protocol/utils/utils.js +52 -55
- package/v0/protocol/utils/utils.js.map +1 -1
- package/v0/serializer/validators/transaction-validator.js +23 -31
- package/v0/serializer/validators/transaction-validator.js.map +1 -1
- package/v0/serializer/validators/validators.js +25 -73
- package/v0/serializer/validators/validators.js.map +1 -1
- package/v1/block-explorer/EtherscanBlockExplorer.js +12 -61
- package/v1/block-explorer/EtherscanBlockExplorer.js.map +1 -1
- package/v1/clients/crypto/EthereumCryptoClient.js +69 -124
- package/v1/clients/crypto/EthereumCryptoClient.js.map +1 -1
- package/v1/clients/info/EthereumInfoClient.d.ts +1 -1
- package/v1/clients/info/EthereumInfoClient.js +3 -4
- package/v1/clients/info/EthereumInfoClient.js.map +1 -1
- package/v1/clients/info/EtherscanInfoClient.js +84 -162
- package/v1/clients/info/EtherscanInfoClient.js.map +1 -1
- package/v1/clients/node/HttpEthereumNodeClient.d.ts +2 -2
- package/v1/clients/node/HttpEthereumNodeClient.js +164 -375
- package/v1/clients/node/HttpEthereumNodeClient.js.map +1 -1
- package/v1/data/EthereumAddress.js +29 -20
- package/v1/data/EthereumAddress.js.map +1 -1
- package/v1/index.js +14 -14
- package/v1/index.js.map +1 -1
- package/v1/module/EthereumModule.js +43 -112
- package/v1/module/EthereumModule.js.map +1 -1
- package/v1/module.js +2 -3
- package/v1/module.js.map +1 -1
- package/v1/protocol/EthereumBaseProtocol.js +467 -788
- package/v1/protocol/EthereumBaseProtocol.js.map +1 -1
- package/v1/protocol/EthereumProtocol.js +15 -46
- package/v1/protocol/EthereumProtocol.js.map +1 -1
- package/v1/protocol/erc20/ERC20Protocol.js +213 -360
- package/v1/protocol/erc20/ERC20Protocol.js.map +1 -1
- package/v1/protocol/erc20/ERC20Token.d.ts +1 -1
- package/v1/protocol/erc20/ERC20Token.js +35 -113
- package/v1/protocol/erc20/ERC20Token.js.map +1 -1
- package/v1/serializer/v3/schemas/converter/transaction-converter.js +25 -42
- package/v1/serializer/v3/schemas/converter/transaction-converter.js.map +1 -1
- package/v1/serializer/v3/serializer-companion.js +71 -148
- package/v1/serializer/v3/serializer-companion.js.map +1 -1
- package/v1/serializer/v3/validators/transaction-validator.js +14 -17
- package/v1/serializer/v3/validators/transaction-validator.js.map +1 -1
- package/v1/types/crypto.d.ts +1 -1
- package/v1/types/protocol.d.ts +1 -1
- package/v1/types/transaction.d.ts +1 -1
- package/v1/utils/EthereumUtils.js +57 -60
- package/v1/utils/EthereumUtils.js.map +1 -1
- package/v1/utils/key.js +28 -19
- package/v1/utils/key.js.map +1 -1
- package/v1/utils/protocol.js +33 -21
- package/v1/utils/protocol.js.map +1 -1
|
@@ -1,30 +1,4 @@
|
|
|
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 __assign = (this && this.__assign) || function () {
|
|
18
|
-
__assign = Object.assign || function(t) {
|
|
19
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
20
|
-
s = arguments[i];
|
|
21
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
22
|
-
t[p] = s[p];
|
|
23
|
-
}
|
|
24
|
-
return t;
|
|
25
|
-
};
|
|
26
|
-
return __assign.apply(this, arguments);
|
|
27
|
-
};
|
|
28
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
29
3
|
if (k2 === undefined) k2 = k;
|
|
30
4
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
@@ -41,357 +15,236 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
|
41
15
|
}) : function(o, v) {
|
|
42
16
|
o["default"] = v;
|
|
43
17
|
});
|
|
44
|
-
var __importStar = (this && this.__importStar) || function (
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
return
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
62
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
63
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
64
|
-
function step(op) {
|
|
65
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
66
|
-
while (_) try {
|
|
67
|
-
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;
|
|
68
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
69
|
-
switch (op[0]) {
|
|
70
|
-
case 0: case 1: t = op; break;
|
|
71
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
72
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
73
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
74
|
-
default:
|
|
75
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
76
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
77
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
78
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
79
|
-
if (t[2]) _.ops.pop();
|
|
80
|
-
_.trys.pop(); continue;
|
|
81
|
-
}
|
|
82
|
-
op = body.call(thisArg, _);
|
|
83
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
84
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
85
|
-
}
|
|
86
|
-
};
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
87
35
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
88
36
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
89
37
|
};
|
|
90
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
91
39
|
exports.ERC20ProtocolImpl = void 0;
|
|
92
|
-
|
|
93
|
-
|
|
40
|
+
const coinlib_core_1 = require("@airgap/coinlib-core");
|
|
41
|
+
const bignumber_1 = __importDefault(require("@airgap/coinlib-core/dependencies/src/bignumber.js-9.0.0/bignumber"));
|
|
94
42
|
// @ts-ignore
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
43
|
+
const ethUtil = __importStar(require("@airgap/coinlib-core/dependencies/src/ethereumjs-util-5.2.0"));
|
|
44
|
+
const errors_1 = require("@airgap/coinlib-core/errors");
|
|
45
|
+
const hex_1 = require("@airgap/coinlib-core/utils/hex");
|
|
46
|
+
const module_kit_1 = require("@airgap/module-kit");
|
|
47
|
+
const tx_1 = require("@ethereumjs/tx");
|
|
48
|
+
const HttpEthereumNodeClient_1 = require("../../clients/node/HttpEthereumNodeClient");
|
|
49
|
+
const EthereumUtils_1 = require("../../utils/EthereumUtils");
|
|
50
|
+
const EthereumBaseProtocol_1 = require("../EthereumBaseProtocol");
|
|
51
|
+
const EthereumTransaction = require('@airgap/coinlib-core/dependencies/src/ethereumjs-tx-1.3.7/index');
|
|
104
52
|
// Implementation
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
var _this = _super.call(this, nodeClient, infoClient, {
|
|
53
|
+
class ERC20ProtocolImpl extends EthereumBaseProtocol_1.EthereumBaseProtocolImpl {
|
|
54
|
+
constructor(nodeClient, infoClient, options) {
|
|
55
|
+
super(nodeClient, infoClient, {
|
|
109
56
|
network: options.network,
|
|
110
57
|
name: options.name,
|
|
111
58
|
identifier: options.identifier,
|
|
112
59
|
units: options.units,
|
|
113
60
|
mainUnit: options.mainUnit
|
|
114
|
-
})
|
|
115
|
-
|
|
116
|
-
return _this;
|
|
61
|
+
});
|
|
62
|
+
this.contractAddress = options.contractAddress;
|
|
117
63
|
}
|
|
118
64
|
// Common
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
else {
|
|
167
|
-
typedTransaction = tx_1.TransactionFactory.fromSerializedData(Buffer.from(transaction.serialized, 'hex'));
|
|
168
|
-
data = typedTransaction.data.toString('hex');
|
|
169
|
-
}
|
|
170
|
-
tokenTransferDetails = new HttpEthereumNodeClient_1.EthereumRPCDataTransfer(data);
|
|
171
|
-
return [2 /*return*/, [
|
|
172
|
-
__assign(__assign({}, ethTransactionDetails[0]), { to: [ethUtil.toChecksumAddress(tokenTransferDetails.recipient)], amount: (0, module_kit_1.newAmount)(tokenTransferDetails.amount, 'blockchain') })
|
|
173
|
-
]];
|
|
174
|
-
});
|
|
175
|
-
});
|
|
176
|
-
};
|
|
65
|
+
async getDetailsFromTransaction(transaction, publicKey) {
|
|
66
|
+
const ethTransactionDetails = await super.getDetailsFromTransaction(transaction, publicKey);
|
|
67
|
+
switch (transaction.type) {
|
|
68
|
+
case 'signed':
|
|
69
|
+
return this.getDetailsFromSignedContractTransaction(transaction, ethTransactionDetails);
|
|
70
|
+
case 'unsigned':
|
|
71
|
+
return this.getDetailsFromUnsignedContractTransaction(transaction, ethTransactionDetails);
|
|
72
|
+
default:
|
|
73
|
+
(0, coinlib_core_1.assertNever)(transaction);
|
|
74
|
+
throw new errors_1.UnsupportedError(coinlib_core_1.Domain.ETHEREUM, 'Unsupported transaction type.');
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
async getDetailsFromSignedContractTransaction(transaction, ethTransactionDetails) {
|
|
78
|
+
if (ethTransactionDetails.length !== 1) {
|
|
79
|
+
throw new errors_1.ConditionViolationError(coinlib_core_1.Domain.ERC20, 'More than one ETH transaction detected.');
|
|
80
|
+
}
|
|
81
|
+
const extractedTx = new EthereumTransaction(transaction.serialized);
|
|
82
|
+
const tokenTransferDetails = new HttpEthereumNodeClient_1.EthereumRPCDataTransfer(`0x${extractedTx.data.toString('hex')}`);
|
|
83
|
+
return [
|
|
84
|
+
{
|
|
85
|
+
...ethTransactionDetails[0],
|
|
86
|
+
to: [ethUtil.toChecksumAddress(tokenTransferDetails.recipient)],
|
|
87
|
+
amount: (0, module_kit_1.newAmount)(tokenTransferDetails.amount, 'blockchain')
|
|
88
|
+
}
|
|
89
|
+
];
|
|
90
|
+
}
|
|
91
|
+
async getDetailsFromUnsignedContractTransaction(transaction, ethTransactionDetails) {
|
|
92
|
+
if (ethTransactionDetails.length !== 1) {
|
|
93
|
+
throw new errors_1.ConditionViolationError(coinlib_core_1.Domain.ERC20, 'More than one ETH transaction detected.');
|
|
94
|
+
}
|
|
95
|
+
let data;
|
|
96
|
+
if (transaction.ethereumType === 'raw') {
|
|
97
|
+
data = transaction.data;
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
const typedTransaction = tx_1.TransactionFactory.fromSerializedData(Buffer.from(transaction.serialized, 'hex'));
|
|
101
|
+
data = typedTransaction.data.toString('hex');
|
|
102
|
+
}
|
|
103
|
+
const tokenTransferDetails = new HttpEthereumNodeClient_1.EthereumRPCDataTransfer(data);
|
|
104
|
+
return [
|
|
105
|
+
{
|
|
106
|
+
...ethTransactionDetails[0],
|
|
107
|
+
to: [ethUtil.toChecksumAddress(tokenTransferDetails.recipient)],
|
|
108
|
+
amount: (0, module_kit_1.newAmount)(tokenTransferDetails.amount, 'blockchain')
|
|
109
|
+
}
|
|
110
|
+
];
|
|
111
|
+
}
|
|
177
112
|
// Offline
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
});
|
|
192
|
-
};
|
|
113
|
+
async signTransactionWithSecretKey(transaction, secretKey) {
|
|
114
|
+
if (transaction.ethereumType !== 'raw') {
|
|
115
|
+
// no v0 implementation
|
|
116
|
+
throw new errors_1.ConditionViolationError(coinlib_core_1.Domain.ERC20, 'Unsupported unsigned transaction type.');
|
|
117
|
+
}
|
|
118
|
+
const rawTransaction = {
|
|
119
|
+
...transaction,
|
|
120
|
+
data: !transaction.data || transaction.data === '0x'
|
|
121
|
+
? new HttpEthereumNodeClient_1.EthereumRPCDataTransfer(transaction.to, transaction.value).abiEncoded()
|
|
122
|
+
: transaction.data
|
|
123
|
+
};
|
|
124
|
+
return super.signTransactionWithSecretKey(rawTransaction, secretKey);
|
|
125
|
+
}
|
|
193
126
|
// Online
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
return acc.concat(current.transactions.map(function (tx) { return (__assign(__assign({}, tx), { amount: (0, module_kit_1.newAmount)(tx.amount.value, 'blockchain'), fee: (0, module_kit_1.newAmount)(tx.fee.value, 'blockchain'), network: _this.options.network })); }));
|
|
229
|
-
}, []);
|
|
230
|
-
var hasNext = transactions.length >= limit;
|
|
231
|
-
overallResolve({
|
|
232
|
-
transactions: transactions,
|
|
233
|
-
cursor: {
|
|
234
|
-
hasNext: hasNext,
|
|
235
|
-
page: hasNext ? page : undefined
|
|
236
|
-
}
|
|
237
|
-
});
|
|
238
|
-
})
|
|
239
|
-
.catch(overallReject);
|
|
240
|
-
})];
|
|
241
|
-
});
|
|
242
|
-
});
|
|
243
|
-
};
|
|
244
|
-
ERC20ProtocolImpl.prototype.getBalanceOfPublicKey = function (publicKey) {
|
|
245
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
246
|
-
var address;
|
|
247
|
-
return __generator(this, function (_a) {
|
|
248
|
-
switch (_a.label) {
|
|
249
|
-
case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
|
|
250
|
-
case 1:
|
|
251
|
-
address = _a.sent();
|
|
252
|
-
return [2 /*return*/, this.getBalanceOfAddress(address)];
|
|
253
|
-
}
|
|
254
|
-
});
|
|
255
|
-
});
|
|
256
|
-
};
|
|
257
|
-
ERC20ProtocolImpl.prototype.getBalanceOfAddress = function (address) {
|
|
258
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
259
|
-
return __generator(this, function (_a) {
|
|
260
|
-
return [2 /*return*/, this.getBalanceOfAddresses([address])];
|
|
261
|
-
});
|
|
262
|
-
});
|
|
263
|
-
};
|
|
264
|
-
ERC20ProtocolImpl.prototype.getBalanceOfAddresses = function (addresses) {
|
|
265
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
266
|
-
var balances, totalBalance;
|
|
267
|
-
var _this = this;
|
|
268
|
-
return __generator(this, function (_a) {
|
|
269
|
-
switch (_a.label) {
|
|
270
|
-
case 0: return [4 /*yield*/, Promise.all(addresses.map(function (address) {
|
|
271
|
-
return _this.nodeClient.callBalanceOf(_this.contractAddress, address);
|
|
272
|
-
}))];
|
|
273
|
-
case 1:
|
|
274
|
-
balances = _a.sent();
|
|
275
|
-
totalBalance = balances.reduce(function (a, b) { return a.plus(b); });
|
|
276
|
-
return [2 /*return*/, { total: (0, module_kit_1.newAmount)(totalBalance, 'blockchain') }];
|
|
277
|
-
}
|
|
278
|
-
});
|
|
279
|
-
});
|
|
280
|
-
};
|
|
281
|
-
ERC20ProtocolImpl.prototype.getTransactionMaxAmountWithPublicKey = function (publicKey, to, configuration) {
|
|
282
|
-
var _a;
|
|
283
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
284
|
-
var balance;
|
|
285
|
-
return __generator(this, function (_b) {
|
|
286
|
-
switch (_b.label) {
|
|
287
|
-
case 0: return [4 /*yield*/, this.getBalanceOfPublicKey(publicKey)];
|
|
288
|
-
case 1:
|
|
289
|
-
balance = _b.sent();
|
|
290
|
-
return [2 /*return*/, (_a = balance.transferable) !== null && _a !== void 0 ? _a : balance.total];
|
|
291
|
-
}
|
|
292
|
-
});
|
|
293
|
-
});
|
|
294
|
-
};
|
|
295
|
-
ERC20ProtocolImpl.prototype.prepareTransactionWithPublicKey = function (publicKey, details, configuration) {
|
|
296
|
-
var _a;
|
|
297
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
298
|
-
var fee, estimatedFee, wrappedFee, wrappedAmount, balance, wrappedBalance, address, ethBalance, wrappedEthBalance, estimatedGas, txCount, gasPrice, transaction;
|
|
299
|
-
return __generator(this, function (_b) {
|
|
300
|
-
switch (_b.label) {
|
|
301
|
-
case 0:
|
|
302
|
-
if (details.length !== 1) {
|
|
303
|
-
throw new errors_1.ConditionViolationError(coinlib_core_1.Domain.ETHEREUM, 'you cannot have 0 transaction details');
|
|
304
|
-
}
|
|
305
|
-
if (!((configuration === null || configuration === void 0 ? void 0 : configuration.fee) !== undefined)) return [3 /*break*/, 1];
|
|
306
|
-
fee = configuration.fee;
|
|
307
|
-
return [3 /*break*/, 3];
|
|
308
|
-
case 1: return [4 /*yield*/, this.getTransactionFeeWithPublicKey(publicKey, details)];
|
|
309
|
-
case 2:
|
|
310
|
-
estimatedFee = _b.sent();
|
|
311
|
-
fee = estimatedFee.medium;
|
|
312
|
-
_b.label = 3;
|
|
313
|
-
case 3:
|
|
314
|
-
wrappedFee = new bignumber_1.default((0, module_kit_1.newAmount)(fee).blockchain(this.feeUnits).value);
|
|
315
|
-
wrappedAmount = new bignumber_1.default((0, module_kit_1.newAmount)(details[0].amount).blockchain(this.units).value);
|
|
316
|
-
return [4 /*yield*/, this.getBalanceOfPublicKey(publicKey)];
|
|
317
|
-
case 4:
|
|
318
|
-
balance = _b.sent();
|
|
319
|
-
wrappedBalance = new bignumber_1.default((0, module_kit_1.newAmount)((_a = balance.transferable) !== null && _a !== void 0 ? _a : balance.total).blockchain(this.units).value);
|
|
320
|
-
if (!wrappedBalance.isGreaterThanOrEqualTo(wrappedAmount)) return [3 /*break*/, 11];
|
|
321
|
-
return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
|
|
322
|
-
case 5:
|
|
323
|
-
address = _b.sent();
|
|
324
|
-
return [4 /*yield*/, _super.prototype.getBalanceOfAddresses.call(this, [address])];
|
|
325
|
-
case 6:
|
|
326
|
-
ethBalance = _b.sent();
|
|
327
|
-
wrappedEthBalance = new bignumber_1.default((0, module_kit_1.newAmount)(ethBalance.total).blockchain(this.units).value);
|
|
328
|
-
return [4 /*yield*/, this.estimateGas(address, details[0].to, wrappedAmount)];
|
|
329
|
-
case 7:
|
|
330
|
-
estimatedGas = _b.sent();
|
|
331
|
-
if (!wrappedEthBalance.isGreaterThanOrEqualTo(wrappedFee)) return [3 /*break*/, 9];
|
|
332
|
-
return [4 /*yield*/, this.nodeClient.fetchTransactionCount(address)];
|
|
333
|
-
case 8:
|
|
334
|
-
txCount = _b.sent();
|
|
335
|
-
gasPrice = wrappedFee.isEqualTo(0)
|
|
336
|
-
? new bignumber_1.default(0)
|
|
337
|
-
: wrappedFee.div(estimatedGas).integerValue(bignumber_1.default.ROUND_CEIL);
|
|
338
|
-
transaction = (0, module_kit_1.newUnsignedTransaction)({
|
|
339
|
-
ethereumType: 'raw',
|
|
340
|
-
nonce: EthereumUtils_1.EthereumUtils.toHex(txCount),
|
|
341
|
-
gasLimit: EthereumUtils_1.EthereumUtils.toHex(estimatedGas.toFixed()),
|
|
342
|
-
gasPrice: EthereumUtils_1.EthereumUtils.toHex(gasPrice.toFixed()),
|
|
343
|
-
to: this.contractAddress,
|
|
344
|
-
value: EthereumUtils_1.EthereumUtils.toHex(new bignumber_1.default(0).toFixed()),
|
|
345
|
-
chainId: this.options.network.chainId,
|
|
346
|
-
data: new HttpEthereumNodeClient_1.EthereumRPCDataTransfer(details[0].to, EthereumUtils_1.EthereumUtils.toHex(wrappedAmount.toFixed())).abiEncoded()
|
|
347
|
-
});
|
|
348
|
-
return [2 /*return*/, transaction];
|
|
349
|
-
case 9: throw new errors_1.BalanceError(coinlib_core_1.Domain.ERC20, 'not enough ETH balance');
|
|
350
|
-
case 10: return [3 /*break*/, 12];
|
|
351
|
-
case 11: throw new errors_1.BalanceError(coinlib_core_1.Domain.ERC20, 'not enough token balance');
|
|
352
|
-
case 12: return [2 /*return*/];
|
|
353
|
-
}
|
|
354
|
-
});
|
|
127
|
+
async getTransactionsForPublicKey(publicKey, limit, cursor) {
|
|
128
|
+
const address = await this.getAddressFromPublicKey(publicKey);
|
|
129
|
+
return this.getTransactionsForAddress(address, limit, cursor);
|
|
130
|
+
}
|
|
131
|
+
async getTransactionsForAddress(address, limit, cursor) {
|
|
132
|
+
return this.getTransactionsForAddresses([address], limit, cursor);
|
|
133
|
+
}
|
|
134
|
+
async getTransactionsForAddresses(addresses, limit, cursor) {
|
|
135
|
+
return new Promise((overallResolve, overallReject) => {
|
|
136
|
+
const promises = [];
|
|
137
|
+
for (const address of addresses) {
|
|
138
|
+
promises.push(this.infoClient.fetchContractTransactions(this.contractAddress, address, limit, cursor));
|
|
139
|
+
}
|
|
140
|
+
Promise.all(promises)
|
|
141
|
+
.then((values) => {
|
|
142
|
+
const page = Math.max(...values.map((txResult) => txResult.cursor.page));
|
|
143
|
+
const transactions = values.reduce((acc, current) => {
|
|
144
|
+
return acc.concat(current.transactions.map((tx) => ({
|
|
145
|
+
...tx,
|
|
146
|
+
amount: (0, module_kit_1.newAmount)(tx.amount.value, 'blockchain'),
|
|
147
|
+
fee: (0, module_kit_1.newAmount)(tx.fee.value, 'blockchain'),
|
|
148
|
+
network: this.options.network
|
|
149
|
+
})));
|
|
150
|
+
}, []);
|
|
151
|
+
const hasNext = transactions.length >= limit;
|
|
152
|
+
overallResolve({
|
|
153
|
+
transactions,
|
|
154
|
+
cursor: {
|
|
155
|
+
hasNext,
|
|
156
|
+
page: hasNext ? page : undefined
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
})
|
|
160
|
+
.catch(overallReject);
|
|
355
161
|
});
|
|
356
|
-
}
|
|
162
|
+
}
|
|
163
|
+
async getBalanceOfPublicKey(publicKey) {
|
|
164
|
+
const address = await this.getAddressFromPublicKey(publicKey);
|
|
165
|
+
return this.getBalanceOfAddress(address);
|
|
166
|
+
}
|
|
167
|
+
async getBalanceOfAddress(address) {
|
|
168
|
+
return this.getBalanceOfAddresses([address]);
|
|
169
|
+
}
|
|
170
|
+
async getBalanceOfAddresses(addresses) {
|
|
171
|
+
const balances = await Promise.all(addresses.map((address) => {
|
|
172
|
+
return this.nodeClient.callBalanceOf(this.contractAddress, address);
|
|
173
|
+
}));
|
|
174
|
+
const totalBalance = balances.reduce((a, b) => a.plus(b));
|
|
175
|
+
return { total: (0, module_kit_1.newAmount)(totalBalance, 'blockchain') };
|
|
176
|
+
}
|
|
177
|
+
async getTransactionMaxAmountWithPublicKey(publicKey, to, configuration) {
|
|
178
|
+
const balance = await this.getBalanceOfPublicKey(publicKey);
|
|
179
|
+
return balance.transferable ?? balance.total;
|
|
180
|
+
}
|
|
181
|
+
async prepareTransactionWithPublicKey(publicKey, details, configuration) {
|
|
182
|
+
if (details.length !== 1) {
|
|
183
|
+
throw new errors_1.ConditionViolationError(coinlib_core_1.Domain.ETHEREUM, 'you cannot have 0 transaction details');
|
|
184
|
+
}
|
|
185
|
+
let fee;
|
|
186
|
+
if (configuration?.fee !== undefined) {
|
|
187
|
+
fee = configuration.fee;
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
const estimatedFee = await this.getTransactionFeeWithPublicKey(publicKey, details);
|
|
191
|
+
fee = estimatedFee.medium;
|
|
192
|
+
}
|
|
193
|
+
const wrappedFee = new bignumber_1.default((0, module_kit_1.newAmount)(fee).blockchain(this.feeUnits).value);
|
|
194
|
+
const wrappedAmount = new bignumber_1.default((0, module_kit_1.newAmount)(details[0].amount).blockchain(this.units).value);
|
|
195
|
+
const balance = await this.getBalanceOfPublicKey(publicKey);
|
|
196
|
+
const wrappedBalance = new bignumber_1.default((0, module_kit_1.newAmount)(balance.transferable ?? balance.total).blockchain(this.units).value);
|
|
197
|
+
if (wrappedBalance.isGreaterThanOrEqualTo(wrappedAmount)) {
|
|
198
|
+
const address = await this.getAddressFromPublicKey(publicKey);
|
|
199
|
+
const ethBalance = await super.getBalanceOfAddresses([address]);
|
|
200
|
+
const wrappedEthBalance = new bignumber_1.default((0, module_kit_1.newAmount)(ethBalance.total).blockchain(this.units).value);
|
|
201
|
+
const estimatedGas = await this.estimateGas(address, details[0].to, wrappedAmount);
|
|
202
|
+
if (wrappedEthBalance.isGreaterThanOrEqualTo(wrappedFee)) {
|
|
203
|
+
const txCount = await this.nodeClient.fetchTransactionCount(address);
|
|
204
|
+
const gasPrice = wrappedFee.isEqualTo(0)
|
|
205
|
+
? new bignumber_1.default(0)
|
|
206
|
+
: wrappedFee.div(estimatedGas).integerValue(bignumber_1.default.ROUND_CEIL);
|
|
207
|
+
const transaction = (0, module_kit_1.newUnsignedTransaction)({
|
|
208
|
+
ethereumType: 'raw',
|
|
209
|
+
nonce: EthereumUtils_1.EthereumUtils.toHex(txCount),
|
|
210
|
+
gasLimit: EthereumUtils_1.EthereumUtils.toHex(estimatedGas.toFixed()),
|
|
211
|
+
gasPrice: EthereumUtils_1.EthereumUtils.toHex(gasPrice.toFixed()),
|
|
212
|
+
to: this.contractAddress,
|
|
213
|
+
value: EthereumUtils_1.EthereumUtils.toHex(new bignumber_1.default(0).toFixed()),
|
|
214
|
+
chainId: this.options.network.chainId,
|
|
215
|
+
data: new HttpEthereumNodeClient_1.EthereumRPCDataTransfer(details[0].to, EthereumUtils_1.EthereumUtils.toHex(wrappedAmount.toFixed())).abiEncoded()
|
|
216
|
+
});
|
|
217
|
+
return transaction;
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
throw new errors_1.BalanceError(coinlib_core_1.Domain.ERC20, 'not enough ETH balance');
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
else {
|
|
224
|
+
throw new errors_1.BalanceError(coinlib_core_1.Domain.ERC20, 'not enough token balance');
|
|
225
|
+
}
|
|
226
|
+
}
|
|
357
227
|
// Custom
|
|
358
|
-
|
|
359
|
-
return
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
ERC20ProtocolImpl.prototype.estimateGas = function (fromAddress, toAddress, amount, _data) {
|
|
380
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
381
|
-
var hexAmount, blockchainAmount;
|
|
382
|
-
return __generator(this, function (_a) {
|
|
383
|
-
if (typeof amount === 'string' && (0, hex_1.isHex)(amount)) {
|
|
384
|
-
hexAmount = amount;
|
|
385
|
-
}
|
|
386
|
-
else {
|
|
387
|
-
blockchainAmount = (0, module_kit_1.isAmount)(amount) ? (0, module_kit_1.newAmount)(amount).blockchain(this.units) : (0, module_kit_1.newAmount)(amount, 'blockchain');
|
|
388
|
-
hexAmount = EthereumUtils_1.EthereumUtils.toHex(blockchainAmount.value);
|
|
389
|
-
}
|
|
390
|
-
return [2 /*return*/, this.nodeClient.estimateTransferGas(this.contractAddress, fromAddress, toAddress, hexAmount)];
|
|
391
|
-
});
|
|
392
|
-
});
|
|
393
|
-
};
|
|
394
|
-
return ERC20ProtocolImpl;
|
|
395
|
-
}(EthereumBaseProtocol_1.EthereumBaseProtocolImpl));
|
|
228
|
+
async name() {
|
|
229
|
+
return this.nodeClient.getContractName(this.contractAddress);
|
|
230
|
+
}
|
|
231
|
+
async symbol() {
|
|
232
|
+
return this.nodeClient.getContractSymbol(this.contractAddress);
|
|
233
|
+
}
|
|
234
|
+
async decimals() {
|
|
235
|
+
return this.nodeClient.getContractDecimals(this.contractAddress);
|
|
236
|
+
}
|
|
237
|
+
async estimateGas(fromAddress, toAddress, amount, _data) {
|
|
238
|
+
let hexAmount;
|
|
239
|
+
if (typeof amount === 'string' && (0, hex_1.isHex)(amount)) {
|
|
240
|
+
hexAmount = amount;
|
|
241
|
+
}
|
|
242
|
+
else {
|
|
243
|
+
const blockchainAmount = (0, module_kit_1.isAmount)(amount) ? (0, module_kit_1.newAmount)(amount).blockchain(this.units) : (0, module_kit_1.newAmount)(amount, 'blockchain');
|
|
244
|
+
hexAmount = EthereumUtils_1.EthereumUtils.toHex(blockchainAmount.value);
|
|
245
|
+
}
|
|
246
|
+
return this.nodeClient.estimateTransferGas(this.contractAddress, fromAddress, toAddress, hexAmount);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
396
249
|
exports.ERC20ProtocolImpl = ERC20ProtocolImpl;
|
|
397
250
|
//# sourceMappingURL=ERC20Protocol.js.map
|