@airgap/icp 0.13.15-beta.0 → 0.13.15-beta.2
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 +8 -6
- package/v1/block-explorer/ICPBlockExplorer.js +16 -51
- package/v1/block-explorer/ICPBlockExplorer.js.map +1 -1
- package/v1/index.d.ts +2 -2
- package/v1/index.js +7 -5
- package/v1/index.js.map +1 -1
- package/v1/module/ICPModule.js +53 -94
- package/v1/module/ICPModule.js.map +1 -1
- package/v1/module.js +1 -1
- package/v1/module.js.map +1 -1
- package/v1/protocol/ICPGovernance.d.ts +38 -0
- package/v1/protocol/ICPGovernance.js +748 -0
- package/v1/protocol/ICPGovernance.js.map +1 -0
- package/v1/protocol/ICPImplementation.d.ts +27 -5
- package/v1/protocol/ICPImplementation.js +368 -250
- package/v1/protocol/ICPImplementation.js.map +1 -1
- package/v1/protocol/ICPProtocol.d.ts +29 -4
- package/v1/protocol/ICPProtocol.js +507 -320
- package/v1/protocol/ICPProtocol.js.map +1 -1
- package/v1/protocol/icrc/CkBTCProtocol.js +137 -223
- package/v1/protocol/icrc/CkBTCProtocol.js.map +1 -1
- package/v1/protocol/icrc/ICRC1Protocol.js +338 -553
- package/v1/protocol/icrc/ICRC1Protocol.js.map +1 -1
- package/v1/serializer/v3/schemas/converter/transaction-converter.js +17 -4
- package/v1/serializer/v3/schemas/converter/transaction-converter.js.map +1 -1
- package/v1/serializer/v3/schemas/generated/transaction-sign-request-icp.json +35 -6
- package/v1/serializer/v3/serializer-companion.js +86 -144
- package/v1/serializer/v3/serializer-companion.js.map +1 -1
- package/v1/serializer/v3/validators/transaction-validator.js +22 -60
- package/v1/serializer/v3/validators/transaction-validator.js.map +1 -1
- package/v1/serializer/v3/validators/validators.js +19 -58
- package/v1/serializer/v3/validators/validators.js.map +1 -1
- package/v1/types/governance.d.ts +2 -0
- package/v1/types/governance.js +268 -210
- package/v1/types/governance.js.map +1 -1
- package/v1/types/icrc/ledger.js +22 -23
- package/v1/types/icrc/ledger.js.map +1 -1
- package/v1/types/ledger.d.ts +1 -0
- package/v1/types/ledger.js +37 -38
- package/v1/types/ledger.js.map +1 -1
- package/v1/types/management_idl.js +6 -7
- package/v1/types/management_idl.js.map +1 -1
- package/v1/types/protocol.d.ts +1 -0
- package/v1/types/transaction.d.ts +21 -3
- package/v1/types/transaction.js +28 -0
- package/v1/types/transaction.js.map +1 -1
- package/v1/utils/account.js +42 -70
- package/v1/utils/account.js.map +1 -1
- package/v1/utils/actor.js +139 -322
- package/v1/utils/actor.js.map +1 -1
- package/v1/utils/auth.js +44 -134
- package/v1/utils/auth.js.map +1 -1
- package/v1/utils/bls.js +41 -109
- package/v1/utils/bls.js.map +1 -1
- package/v1/utils/buffer.js +47 -111
- package/v1/utils/buffer.js.map +1 -1
- package/v1/utils/cbor.js +50 -85
- package/v1/utils/cbor.js.map +1 -1
- package/v1/utils/certificate.js +116 -238
- package/v1/utils/certificate.js.map +1 -1
- package/v1/utils/convert.d.ts +1 -1
- package/v1/utils/convert.js +29 -49
- package/v1/utils/convert.js.map +1 -1
- package/v1/utils/der.js +37 -35
- package/v1/utils/der.js.map +1 -1
- package/v1/utils/errors.js +10 -30
- package/v1/utils/errors.js.map +1 -1
- package/v1/utils/hdkey.js +33 -33
- package/v1/utils/hdkey.js.map +1 -1
- package/v1/utils/http.d.ts +1 -1
- package/v1/utils/http.js +202 -426
- package/v1/utils/http.js.map +1 -1
- package/v1/utils/icrc1.js +24 -41
- package/v1/utils/icrc1.js.map +1 -1
- package/v1/utils/idl.js +780 -1159
- package/v1/utils/idl.js.map +1 -1
- package/v1/utils/leb128.js +29 -30
- package/v1/utils/leb128.js.map +1 -1
- package/v1/utils/polling.js +84 -234
- package/v1/utils/polling.js.map +1 -1
- package/v1/utils/principal.js +78 -126
- package/v1/utils/principal.js.map +1 -1
- package/v1/utils/secp256k1.js +68 -134
- package/v1/utils/secp256k1.js.map +1 -1
- package/v1/utils/transaction.js +7 -7
- package/v1/utils/transaction.js.map +1 -1
|
@@ -1,15 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
2
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
3
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
4
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -19,48 +8,25 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
19
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
9
|
});
|
|
21
10
|
};
|
|
22
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
24
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
-
function step(op) {
|
|
27
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
-
while (_) try {
|
|
29
|
-
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;
|
|
30
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
-
switch (op[0]) {
|
|
32
|
-
case 0: case 1: t = op; break;
|
|
33
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
-
default:
|
|
37
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
-
if (t[2]) _.ops.pop();
|
|
42
|
-
_.trys.pop(); continue;
|
|
43
|
-
}
|
|
44
|
-
op = body.call(thisArg, _);
|
|
45
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
11
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
50
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
51
13
|
};
|
|
52
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
53
15
|
exports.createICPProtocolOptions = exports.ICP_MAINNET_PROTOCOL_NETWORK = exports.createICPProtocol = exports.ICPProtocolImpl = exports.ICP_TRANSACTION_METADATA = exports.ICP_ACCOUNT_METADATA = exports.ICP_DERIVATION_PATH = void 0;
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
16
|
+
const coinlib_core_1 = require("@airgap/coinlib-core");
|
|
17
|
+
const index_1 = __importDefault(require("@airgap/coinlib-core/dependencies/src/axios-0.19.0/index"));
|
|
18
|
+
const bignumber_1 = __importDefault(require("@airgap/coinlib-core/dependencies/src/bignumber.js-9.0.0/bignumber"));
|
|
19
|
+
const errors_1 = require("@airgap/coinlib-core/errors");
|
|
20
|
+
const array_1 = require("@airgap/coinlib-core/utils/array");
|
|
21
|
+
const crypto_1 = require("@airgap/crypto");
|
|
22
|
+
const module_kit_1 = require("@airgap/module-kit");
|
|
23
|
+
const transaction_1 = require("../types/transaction");
|
|
24
|
+
const account_1 = require("../utils/account");
|
|
25
|
+
const principal_1 = require("../utils/principal");
|
|
26
|
+
const ICPGovernance_1 = require("./ICPGovernance");
|
|
27
|
+
const ICPImplementation_1 = require("./ICPImplementation");
|
|
62
28
|
// Implementation
|
|
63
|
-
exports.ICP_DERIVATION_PATH =
|
|
29
|
+
exports.ICP_DERIVATION_PATH = `m/44'/223'/0'/0/0`;
|
|
64
30
|
exports.ICP_ACCOUNT_METADATA = {
|
|
65
31
|
standardDerivationPath: exports.ICP_DERIVATION_PATH,
|
|
66
32
|
address: {
|
|
@@ -77,9 +43,8 @@ function ICP_TRANSACTION_METADATA() {
|
|
|
77
43
|
};
|
|
78
44
|
}
|
|
79
45
|
exports.ICP_TRANSACTION_METADATA = ICP_TRANSACTION_METADATA;
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
if (options === void 0) { options = {}; }
|
|
46
|
+
class ICPProtocolImpl {
|
|
47
|
+
constructor(options = {}) {
|
|
83
48
|
// Common
|
|
84
49
|
this.units = {
|
|
85
50
|
ICP: {
|
|
@@ -109,314 +74,536 @@ var ICPProtocolImpl = /** @class */ (function () {
|
|
|
109
74
|
};
|
|
110
75
|
this.options = createICPProtocolOptions(options.network);
|
|
111
76
|
}
|
|
112
|
-
|
|
113
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
114
|
-
return
|
|
115
|
-
return [2 /*return*/, this.metadata];
|
|
116
|
-
});
|
|
77
|
+
getMetadata() {
|
|
78
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
79
|
+
return this.metadata;
|
|
117
80
|
});
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
121
|
-
return
|
|
122
|
-
return [2 /*return*/, (0, ICPImplementation_1.getAddressFromPublicKey)(publicKey.value)];
|
|
123
|
-
});
|
|
81
|
+
}
|
|
82
|
+
getAddressFromPublicKey(publicKey) {
|
|
83
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
84
|
+
return (0, ICPImplementation_1.getAddressFromPublicKey)(publicKey.value);
|
|
124
85
|
});
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
to: [(0, convert_1.uint8ArrayToHexString)(args.to)],
|
|
138
|
-
isInbound: false,
|
|
139
|
-
amount: (0, module_kit_1.newAmount)(args.amount.e8s.toString(), 'blockchain'),
|
|
140
|
-
fee: (0, module_kit_1.newAmount)(args.fee.e8s.toString(), 'blockchain'),
|
|
141
|
-
network: this.options.network
|
|
142
|
-
}
|
|
143
|
-
]];
|
|
144
|
-
case 'unsigned':
|
|
145
|
-
unsignedTransactionInfo = (0, ICPImplementation_1.getInfoFromUnsignedTransaction)(transaction.transaction);
|
|
146
|
-
return [2 /*return*/, [
|
|
147
|
-
{
|
|
148
|
-
from: [(0, ICPImplementation_1.getAddressFromPublicKey)(_publicKey.value)],
|
|
149
|
-
to: [unsignedTransactionInfo.to],
|
|
150
|
-
isInbound: false,
|
|
151
|
-
amount: (0, module_kit_1.newAmount)(unsignedTransactionInfo.amount.toString(), 'blockchain'),
|
|
152
|
-
fee: (0, module_kit_1.newAmount)(unsignedTransactionInfo.fee.toString(), 'blockchain'),
|
|
153
|
-
network: this.options.network
|
|
154
|
-
}
|
|
155
|
-
]];
|
|
156
|
-
default:
|
|
157
|
-
(0, coinlib_core_1.assertNever)(transaction);
|
|
158
|
-
throw new errors_1.UnsupportedError(coinlib_core_1.Domain.ICP, 'Unsupported transaction type.');
|
|
159
|
-
}
|
|
160
|
-
return [2 /*return*/];
|
|
161
|
-
});
|
|
86
|
+
}
|
|
87
|
+
getDetailsFromTransaction(transaction, publicKey) {
|
|
88
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
89
|
+
switch (transaction.type) {
|
|
90
|
+
case 'signed':
|
|
91
|
+
return this.getDetailsFromSignedTransaction(transaction, publicKey);
|
|
92
|
+
case 'unsigned':
|
|
93
|
+
return this.getDetailsFromUnsignedTransaction(transaction, publicKey);
|
|
94
|
+
default:
|
|
95
|
+
(0, coinlib_core_1.assertNever)(transaction);
|
|
96
|
+
throw new errors_1.UnsupportedError(coinlib_core_1.Domain.ICP, 'Unsupported transaction type.');
|
|
97
|
+
}
|
|
162
98
|
});
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
99
|
+
}
|
|
100
|
+
getDetailsFromSignedTransaction(transaction, publicKey) {
|
|
101
|
+
const transactions = transaction.transactions.map(({ actionType, encoded }) => {
|
|
102
|
+
switch (actionType) {
|
|
103
|
+
case transaction_1.ICPActionType.TRANSFER:
|
|
104
|
+
return (0, ICPImplementation_1.getDetailsFromSignedTransactionTransfer)(encoded, publicKey.value, this.options.network);
|
|
105
|
+
case transaction_1.ICPActionType.GET_NEURON_INFO:
|
|
106
|
+
return (0, ICPGovernance_1.getDetailsFromSignedTransactionGetNeuronInfo)(publicKey.value, this.options.network);
|
|
107
|
+
case transaction_1.ICPActionType.TRANSFER_TO_SUBACCOUNT:
|
|
108
|
+
return (0, ICPGovernance_1.getDetailsFromSignedTransactionTransferToSubaccount)(encoded, publicKey.value, this.options.network);
|
|
109
|
+
case transaction_1.ICPActionType.CLAIM_GOVERNANCE:
|
|
110
|
+
return (0, ICPGovernance_1.getDetailsFromSignedTransactionClaimGovernance)(publicKey.value, this.options.network);
|
|
111
|
+
case transaction_1.ICPActionType.FOLLOW_NEURON:
|
|
112
|
+
return (0, ICPGovernance_1.getDetailsFromSignedTransactionFollowNeuron)(publicKey.value, this.options.network);
|
|
113
|
+
case transaction_1.ICPActionType.INCREASE_STAKE:
|
|
114
|
+
throw new Error('TODO: implement');
|
|
115
|
+
case transaction_1.ICPActionType.REMOVE_STAKE:
|
|
116
|
+
return (0, ICPGovernance_1.getDetailsFromSignedTransactionRemoveStake)(publicKey.value, this.options.network);
|
|
117
|
+
case transaction_1.ICPActionType.INCREASE_DELAY:
|
|
118
|
+
return (0, ICPGovernance_1.getDetailsFromSignedTransactionIncreaseDelay)(publicKey.value, this.options.network);
|
|
119
|
+
case transaction_1.ICPActionType.START_DISSOLVING:
|
|
120
|
+
return (0, ICPGovernance_1.getDetailsFromSignedTransactionStartDissolving)(publicKey.value, this.options.network);
|
|
121
|
+
case transaction_1.ICPActionType.STOP_DISSOLVING:
|
|
122
|
+
return (0, ICPGovernance_1.getDetailsFromSignedTransactionStopDissolving)(publicKey.value, this.options.network);
|
|
123
|
+
case transaction_1.ICPActionType.RESTAKE_MATURITY:
|
|
124
|
+
return (0, ICPGovernance_1.getDetailsFromSignedTransactionRestakeMaturity)(publicKey.value, this.options.network);
|
|
125
|
+
default:
|
|
126
|
+
(0, coinlib_core_1.assertNever)(actionType);
|
|
127
|
+
throw new errors_1.UnsupportedError(coinlib_core_1.Domain.ICP, 'Unsupported ICP action type');
|
|
128
|
+
}
|
|
169
129
|
});
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
130
|
+
return (0, array_1.flattenArray)(transactions);
|
|
131
|
+
}
|
|
132
|
+
getDetailsFromUnsignedTransaction(transaction, publicKey) {
|
|
133
|
+
const transactions = transaction.transactions.map(({ actionType, encoded }) => {
|
|
134
|
+
switch (actionType) {
|
|
135
|
+
case transaction_1.ICPActionType.TRANSFER:
|
|
136
|
+
return (0, ICPImplementation_1.getDetailsFromUnsignedTransactionTransfer)(encoded, publicKey.value, this.options.network);
|
|
137
|
+
case transaction_1.ICPActionType.GET_NEURON_INFO:
|
|
138
|
+
return (0, ICPGovernance_1.getDetailsFromUnsignedTransactionGetNeuronInfo)(publicKey.value, this.options.network);
|
|
139
|
+
case transaction_1.ICPActionType.TRANSFER_TO_SUBACCOUNT:
|
|
140
|
+
return (0, ICPGovernance_1.getDetailsFromUnsignedTransactionTransferToSubaccount)(encoded, publicKey.value, this.options.network);
|
|
141
|
+
case transaction_1.ICPActionType.CLAIM_GOVERNANCE:
|
|
142
|
+
return (0, ICPGovernance_1.getDetailsFromUnsignedTransactionClaimGovernance)(publicKey.value, this.options.network);
|
|
143
|
+
case transaction_1.ICPActionType.INCREASE_STAKE:
|
|
144
|
+
throw new Error('TODO: implement');
|
|
145
|
+
case transaction_1.ICPActionType.REMOVE_STAKE:
|
|
146
|
+
return (0, ICPGovernance_1.getDetailsFromUnsignedTransactionRemoveStake)(publicKey.value, this.options.network);
|
|
147
|
+
case transaction_1.ICPActionType.INCREASE_DELAY:
|
|
148
|
+
return (0, ICPGovernance_1.getDetailsFromUnsignedTransactionIncreaseDelay)(publicKey.value, this.options.network);
|
|
149
|
+
case transaction_1.ICPActionType.START_DISSOLVING:
|
|
150
|
+
return (0, ICPGovernance_1.getDetailsFromUnsignedTransactionStartDissolving)(publicKey.value, this.options.network);
|
|
151
|
+
case transaction_1.ICPActionType.STOP_DISSOLVING:
|
|
152
|
+
return (0, ICPGovernance_1.getDetailsFromUnsignedTransactionStopDissolving)(publicKey.value, this.options.network);
|
|
153
|
+
case transaction_1.ICPActionType.FOLLOW_NEURON:
|
|
154
|
+
return (0, ICPGovernance_1.getDetailsFromUnsignedTransactionFollowNeuron)(publicKey.value, this.options.network);
|
|
155
|
+
case transaction_1.ICPActionType.RESTAKE_MATURITY:
|
|
156
|
+
return (0, ICPGovernance_1.getDetailsFromUnsignedTransactionRestakeMaturity)(publicKey.value, this.options.network);
|
|
157
|
+
default:
|
|
158
|
+
(0, coinlib_core_1.assertNever)(actionType);
|
|
159
|
+
throw new errors_1.UnsupportedError(coinlib_core_1.Domain.ICP, 'Unsupported ICP action type');
|
|
160
|
+
}
|
|
182
161
|
});
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
return
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
162
|
+
return (0, array_1.flattenArray)(transactions);
|
|
163
|
+
}
|
|
164
|
+
getCryptoConfiguration() {
|
|
165
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
166
|
+
return this.cryptoConfiguration;
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
getKeyPairFromDerivative(derivative) {
|
|
170
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
171
|
+
const bip32Node = (0, crypto_1.encodeDerivative)('bip32', derivative);
|
|
172
|
+
const { publicKey, privateKey } = (0, ICPImplementation_1.getKeyPairFromExtendedSecretKey)(bip32Node.secretKey);
|
|
173
|
+
return {
|
|
174
|
+
secretKey: (0, module_kit_1.newSecretKey)(privateKey, 'hex'),
|
|
175
|
+
publicKey: (0, module_kit_1.newPublicKey)(publicKey, 'hex')
|
|
176
|
+
};
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
signTransactionWithSecretKey(transaction, secretKey) {
|
|
180
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
181
|
+
if (secretKey.format !== 'hex') {
|
|
182
|
+
throw new errors_1.ConditionViolationError(coinlib_core_1.Domain.ICP, 'Secret key is of an unexpected format.');
|
|
183
|
+
}
|
|
184
|
+
const transactions = yield Promise.all(transaction.transactions.map(({ encoded, actionType }) => __awaiter(this, void 0, void 0, function* () {
|
|
185
|
+
switch (actionType) {
|
|
186
|
+
case transaction_1.ICPActionType.TRANSFER:
|
|
187
|
+
return {
|
|
188
|
+
actionType,
|
|
189
|
+
encoded: yield (0, ICPImplementation_1.signTransactionTransfer)(encoded, secretKey.value, this.options.network.ledgerCanisterId),
|
|
190
|
+
requestType: 'update'
|
|
191
|
+
};
|
|
192
|
+
case transaction_1.ICPActionType.GET_NEURON_INFO:
|
|
193
|
+
return {
|
|
194
|
+
actionType,
|
|
195
|
+
encoded: yield (0, ICPGovernance_1.signTransactionGetNeuronInfo)(encoded, secretKey.value, this.options.network.governanceCanisterId),
|
|
196
|
+
requestType: 'query'
|
|
197
|
+
};
|
|
198
|
+
case transaction_1.ICPActionType.TRANSFER_TO_SUBACCOUNT:
|
|
199
|
+
return {
|
|
200
|
+
actionType,
|
|
201
|
+
encoded: yield (0, ICPGovernance_1.signTransactionTransferToSubaccount)(encoded, secretKey.value, this.options.network.ledgerCanisterId),
|
|
202
|
+
requestType: 'update'
|
|
203
|
+
};
|
|
204
|
+
case transaction_1.ICPActionType.CLAIM_GOVERNANCE:
|
|
205
|
+
return {
|
|
206
|
+
actionType,
|
|
207
|
+
encoded: yield (0, ICPGovernance_1.signTransactionClaimGovernance)(encoded, secretKey.value, this.options.network.ledgerCanisterId),
|
|
208
|
+
requestType: 'update'
|
|
209
|
+
};
|
|
210
|
+
case transaction_1.ICPActionType.INCREASE_STAKE:
|
|
211
|
+
throw new Error('TODO: implement');
|
|
212
|
+
case transaction_1.ICPActionType.REMOVE_STAKE:
|
|
213
|
+
return {
|
|
214
|
+
actionType,
|
|
215
|
+
encoded: yield (0, ICPGovernance_1.signTransactionRemoveStake)(encoded, secretKey.value, this.options.network.ledgerCanisterId),
|
|
216
|
+
requestType: 'update'
|
|
217
|
+
};
|
|
218
|
+
case transaction_1.ICPActionType.INCREASE_DELAY:
|
|
219
|
+
return {
|
|
220
|
+
actionType,
|
|
221
|
+
encoded: yield (0, ICPGovernance_1.signTransactionIncreaseDelay)(encoded, secretKey.value, this.options.network.ledgerCanisterId),
|
|
222
|
+
requestType: 'update'
|
|
223
|
+
};
|
|
224
|
+
case transaction_1.ICPActionType.START_DISSOLVING:
|
|
225
|
+
return {
|
|
226
|
+
actionType,
|
|
227
|
+
encoded: yield (0, ICPGovernance_1.signTransactionStartDissolving)(encoded, secretKey.value, this.options.network.ledgerCanisterId),
|
|
228
|
+
requestType: 'update'
|
|
229
|
+
};
|
|
230
|
+
case transaction_1.ICPActionType.STOP_DISSOLVING:
|
|
231
|
+
return {
|
|
232
|
+
actionType,
|
|
233
|
+
encoded: yield (0, ICPGovernance_1.signTransactionStopDissolving)(encoded, secretKey.value, this.options.network.ledgerCanisterId),
|
|
234
|
+
requestType: 'update'
|
|
235
|
+
};
|
|
236
|
+
case transaction_1.ICPActionType.FOLLOW_NEURON:
|
|
237
|
+
return {
|
|
238
|
+
actionType,
|
|
239
|
+
encoded: yield (0, ICPGovernance_1.signTransactionFollowNeuron)(encoded, secretKey.value, this.options.network.ledgerCanisterId),
|
|
240
|
+
requestType: 'update'
|
|
241
|
+
};
|
|
242
|
+
case transaction_1.ICPActionType.RESTAKE_MATURITY:
|
|
243
|
+
return {
|
|
244
|
+
actionType,
|
|
245
|
+
encoded: yield (0, ICPGovernance_1.signTransactionRestakeMaturity)(encoded, secretKey.value, this.options.network.ledgerCanisterId),
|
|
246
|
+
requestType: 'update'
|
|
247
|
+
};
|
|
248
|
+
default:
|
|
249
|
+
(0, coinlib_core_1.assertNever)(actionType);
|
|
250
|
+
throw new errors_1.UnsupportedError(coinlib_core_1.Domain.ICP, `ICP action type ${actionType} is not supported.`);
|
|
197
251
|
}
|
|
198
|
-
});
|
|
252
|
+
})));
|
|
253
|
+
return (0, module_kit_1.newSignedTransaction)({ transactions });
|
|
199
254
|
});
|
|
200
|
-
}
|
|
255
|
+
}
|
|
201
256
|
// Online
|
|
202
|
-
|
|
203
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
204
|
-
return
|
|
205
|
-
return [2 /*return*/, this.options.network];
|
|
206
|
-
});
|
|
257
|
+
getNetwork() {
|
|
258
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
259
|
+
return this.options.network;
|
|
207
260
|
});
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
211
|
-
|
|
212
|
-
return
|
|
213
|
-
switch (_a.label) {
|
|
214
|
-
case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
|
|
215
|
-
case 1:
|
|
216
|
-
address = _a.sent();
|
|
217
|
-
return [2 /*return*/, this.getTransactionsForAddress(address, limit, cursor)];
|
|
218
|
-
}
|
|
219
|
-
});
|
|
261
|
+
}
|
|
262
|
+
getTransactionsForPublicKey(publicKey, limit, cursor) {
|
|
263
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
264
|
+
const address = yield this.getAddressFromPublicKey(publicKey);
|
|
265
|
+
return this.getTransactionsForAddress(address, limit, cursor);
|
|
220
266
|
});
|
|
221
|
-
}
|
|
222
|
-
|
|
267
|
+
}
|
|
268
|
+
getTransactionsForAddress(address, limit, cursor) {
|
|
223
269
|
var _a;
|
|
224
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
return {
|
|
249
|
-
from: [obj.from_account_identifier],
|
|
250
|
-
to: [obj.to_account_identifier],
|
|
251
|
-
isInbound: address === obj.to_account_identifier,
|
|
252
|
-
amount: (0, module_kit_1.newAmount)(obj.amount, 'blockchain'),
|
|
253
|
-
fee: (0, module_kit_1.newAmount)(obj.fee, 'blockchain'),
|
|
254
|
-
network: _this.options.network,
|
|
255
|
-
timestamp: obj.created_at,
|
|
256
|
-
status: {
|
|
257
|
-
type: obj.transfer_type,
|
|
258
|
-
hash: obj.transaction_hash,
|
|
259
|
-
block: obj.block_height
|
|
260
|
-
}
|
|
261
|
-
};
|
|
262
|
-
});
|
|
263
|
-
return [2 /*return*/, {
|
|
264
|
-
transactions: transactions,
|
|
265
|
-
cursor: {
|
|
266
|
-
hasNext: next !== undefined,
|
|
267
|
-
next: next
|
|
268
|
-
}
|
|
269
|
-
}];
|
|
270
|
-
}
|
|
270
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
271
|
+
const endpoint = cursor === undefined ? `/accounts/${address}/transactions?limit=${limit}&offset=0` : cursor.next;
|
|
272
|
+
const url = endpoint !== undefined ? `${this.options.network.explorerUrl}${endpoint}` : undefined;
|
|
273
|
+
const response = url !== undefined ? yield index_1.default.get(url) : undefined;
|
|
274
|
+
const nodeTransactions = ((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.blocks) || [];
|
|
275
|
+
const tokens = endpoint.split('=');
|
|
276
|
+
const offset = parseInt(tokens[tokens.length - 1]);
|
|
277
|
+
tokens[tokens.length - 1] = (offset + limit).toString();
|
|
278
|
+
const next = tokens.join('=');
|
|
279
|
+
const transactions = nodeTransactions.map((obj) => {
|
|
280
|
+
return {
|
|
281
|
+
from: [obj.from_account_identifier],
|
|
282
|
+
to: [obj.to_account_identifier],
|
|
283
|
+
isInbound: address === obj.to_account_identifier,
|
|
284
|
+
amount: (0, module_kit_1.newAmount)(obj.amount, 'blockchain'),
|
|
285
|
+
fee: (0, module_kit_1.newAmount)(obj.fee, 'blockchain'),
|
|
286
|
+
network: this.options.network,
|
|
287
|
+
timestamp: obj.created_at,
|
|
288
|
+
status: {
|
|
289
|
+
type: obj.transfer_type,
|
|
290
|
+
hash: obj.transaction_hash,
|
|
291
|
+
block: obj.block_height
|
|
292
|
+
}
|
|
293
|
+
};
|
|
271
294
|
});
|
|
295
|
+
return {
|
|
296
|
+
transactions,
|
|
297
|
+
cursor: {
|
|
298
|
+
hasNext: next !== undefined,
|
|
299
|
+
next
|
|
300
|
+
}
|
|
301
|
+
};
|
|
272
302
|
});
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
276
|
-
|
|
277
|
-
return
|
|
278
|
-
address = (0, ICPImplementation_1.getAddressFromPublicKey)(publicKey.value);
|
|
279
|
-
return [2 /*return*/, this.getBalanceOfAddress(address)];
|
|
280
|
-
});
|
|
303
|
+
}
|
|
304
|
+
getBalanceOfPublicKey(publicKey) {
|
|
305
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
306
|
+
const address = (0, ICPImplementation_1.getAddressFromPublicKey)(publicKey.value);
|
|
307
|
+
return this.getBalanceOfAddress(address);
|
|
281
308
|
});
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
if (!address)
|
|
290
|
-
return [2 /*return*/, { total: (0, module_kit_1.newAmount)(0, 'blockchain') }];
|
|
291
|
-
return [4 /*yield*/, (0, ICPImplementation_1.getBalanceFromAddress)(address, this.options.network.rpcUrl, this.options.network.ledgerCanisterId)];
|
|
292
|
-
case 1:
|
|
293
|
-
balance = _a.sent();
|
|
294
|
-
return [2 /*return*/, { total: (0, module_kit_1.newAmount)(balance.toString(10), 'blockchain') }];
|
|
295
|
-
}
|
|
296
|
-
});
|
|
309
|
+
}
|
|
310
|
+
getBalanceOfAddress(address) {
|
|
311
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
312
|
+
if (!address)
|
|
313
|
+
return { total: (0, module_kit_1.newAmount)(0, 'blockchain') };
|
|
314
|
+
const balance = yield (0, ICPImplementation_1.getBalanceFromAddress)(address, this.options.network.rpcUrl, this.options.network.ledgerCanisterId);
|
|
315
|
+
return { total: (0, module_kit_1.newAmount)(balance.toString(10), 'blockchain') };
|
|
297
316
|
});
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
case 3:
|
|
313
|
-
feeEstimation = _a.sent();
|
|
314
|
-
fee = new bignumber_1.default((0, module_kit_1.newAmount)(feeEstimation.medium).value);
|
|
315
|
-
if (fee.gte(balanceBn)) {
|
|
316
|
-
fee = new bignumber_1.default(0);
|
|
317
|
-
}
|
|
318
|
-
_a.label = 4;
|
|
319
|
-
case 4:
|
|
320
|
-
amountWithoutFees = balanceBn.minus(fee);
|
|
321
|
-
if (amountWithoutFees.isNegative()) {
|
|
322
|
-
amountWithoutFees = new bignumber_1.default(0);
|
|
323
|
-
}
|
|
324
|
-
return [2 /*return*/, (0, module_kit_1.newAmount)(amountWithoutFees.toFixed(), 'blockchain')];
|
|
317
|
+
}
|
|
318
|
+
getTransactionMaxAmountWithPublicKey(publicKey, to, configuration) {
|
|
319
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
320
|
+
const balance = yield this.getBalanceOfPublicKey(publicKey);
|
|
321
|
+
const balanceBn = new bignumber_1.default((0, module_kit_1.newAmount)(balance.total).value);
|
|
322
|
+
let fee;
|
|
323
|
+
if ((configuration === null || configuration === void 0 ? void 0 : configuration.fee) !== undefined) {
|
|
324
|
+
fee = new bignumber_1.default((0, module_kit_1.newAmount)(configuration.fee).value);
|
|
325
|
+
}
|
|
326
|
+
else {
|
|
327
|
+
const feeEstimation = yield this.getTransactionFeeWithPublicKey(publicKey, []);
|
|
328
|
+
fee = new bignumber_1.default((0, module_kit_1.newAmount)(feeEstimation.medium).value);
|
|
329
|
+
if (fee.gte(balanceBn)) {
|
|
330
|
+
fee = new bignumber_1.default(0);
|
|
325
331
|
}
|
|
326
|
-
}
|
|
332
|
+
}
|
|
333
|
+
let amountWithoutFees = balanceBn.minus(fee);
|
|
334
|
+
if (amountWithoutFees.isNegative()) {
|
|
335
|
+
amountWithoutFees = new bignumber_1.default(0);
|
|
336
|
+
}
|
|
337
|
+
return (0, module_kit_1.newAmount)(amountWithoutFees.toFixed(), 'blockchain');
|
|
327
338
|
});
|
|
328
|
-
}
|
|
339
|
+
}
|
|
329
340
|
// TODO : Get default from chain
|
|
330
|
-
|
|
331
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
return [2 /*return*/, this.feeDefaults];
|
|
341
|
-
});
|
|
341
|
+
getTransactionFeeWithPublicKey(_publicKey, _details) {
|
|
342
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
343
|
+
// const ledger = LedgerCanister.create()
|
|
344
|
+
// const feeDefault = await ledger.transactionFee()
|
|
345
|
+
// return {
|
|
346
|
+
// low: newAmount(feeDefault.toString(10), 'ICP'),
|
|
347
|
+
// medium: newAmount(feeDefault.toString(10), 'ICP'),
|
|
348
|
+
// high: newAmount(feeDefault.toString(10), 'ICP')
|
|
349
|
+
// }
|
|
350
|
+
return this.feeDefaults;
|
|
342
351
|
});
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
})];
|
|
374
|
-
}
|
|
352
|
+
}
|
|
353
|
+
prepareTransactionWithPublicKey(publicKey, details, configuration) {
|
|
354
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
355
|
+
// Check balance
|
|
356
|
+
const balance = (0, module_kit_1.newAmount)((yield this.getBalanceOfPublicKey(publicKey)).total).blockchain(this.units);
|
|
357
|
+
const balanceBn = BigInt(balance.value);
|
|
358
|
+
const feeBn = (configuration === null || configuration === void 0 ? void 0 : configuration.fee) !== undefined
|
|
359
|
+
? BigInt((0, module_kit_1.newAmount)(configuration.fee).blockchain(this.units).value)
|
|
360
|
+
: BigInt((0, module_kit_1.newAmount)(this.feeDefaults.medium).blockchain(this.units).value);
|
|
361
|
+
// Throw if not enough balance for fee
|
|
362
|
+
if (balanceBn < feeBn) {
|
|
363
|
+
throw new errors_1.BalanceError(coinlib_core_1.Domain.ICP, 'not enough balance');
|
|
364
|
+
}
|
|
365
|
+
const amountBn = BigInt((0, module_kit_1.newAmount)(details[0].amount).blockchain(this.units).value);
|
|
366
|
+
// Transaction object
|
|
367
|
+
const transaction = {
|
|
368
|
+
from: (0, ICPImplementation_1.getAddressFromPublicKey)(publicKey.value),
|
|
369
|
+
to: details[0].to,
|
|
370
|
+
amount: amountBn,
|
|
371
|
+
fee: feeBn
|
|
372
|
+
};
|
|
373
|
+
// Create unsigned
|
|
374
|
+
const unsignedTransaction = (0, ICPImplementation_1.createUnsignedTransaction)(transaction);
|
|
375
|
+
return (0, module_kit_1.newUnsignedTransaction)({
|
|
376
|
+
transactions: [
|
|
377
|
+
{
|
|
378
|
+
actionType: transaction_1.ICPActionType.TRANSFER,
|
|
379
|
+
encoded: unsignedTransaction
|
|
380
|
+
}
|
|
381
|
+
]
|
|
375
382
|
});
|
|
376
383
|
});
|
|
377
|
-
}
|
|
378
|
-
ICPProtocolImpl.prototype.networkId = function () {
|
|
379
|
-
switch (this.options.network.type) {
|
|
380
|
-
case 'mainnet':
|
|
381
|
-
return 'icp_mainnet';
|
|
382
|
-
default:
|
|
383
|
-
throw new errors_1.ConditionViolationError(coinlib_core_1.Domain.ICP, 'Network type not supported.');
|
|
384
|
-
}
|
|
385
|
-
};
|
|
384
|
+
}
|
|
386
385
|
// TODO : discover how to get transaction hash after broadcast
|
|
387
386
|
// https://github.com/dfinity/ic-js/tree/main/packages/nns
|
|
388
387
|
// Search for : Transfer ICP from the caller to the destination accountIdentifier. Returns the index of the block containing the tx if it was successful.
|
|
389
|
-
|
|
390
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
388
|
+
broadcastTransaction(transaction) {
|
|
389
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
390
|
+
const txHashes = yield Promise.all(transaction.transactions.map(({ actionType, encoded }) => (0, ICPImplementation_1.broadcastTransaction)(encoded, this.options.network.rpcUrl, this.options.network.governanceCanisterId, transaction_1.ICP_REQUEST_TYPE_PER_ACTION[actionType])));
|
|
391
|
+
return txHashes[0];
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
// DELEGATION
|
|
395
|
+
getDefaultDelegatee() {
|
|
396
|
+
var _a, _b, _c;
|
|
397
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
398
|
+
const knownNeurons = yield (0, ICPImplementation_1.listKnownNeurons)(this.options.network.rpcUrl, this.options.network.governanceCanisterId);
|
|
399
|
+
const id = (_c = (_b = (_a = knownNeurons[0]) === null || _a === void 0 ? void 0 : _a.id) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.id;
|
|
400
|
+
return (id === null || id === void 0 ? void 0 : id.toString()) || '';
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
getCurrentDelegateesForPublicKey(publicKey) {
|
|
404
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
405
|
+
const address = yield this.getAddressFromPublicKey(publicKey);
|
|
406
|
+
return this.getCurrentDelegateesForAddress(address);
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
getCurrentDelegateesForAddress(address) {
|
|
410
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
411
|
+
// Fetch neurons of address from governance canister (listNeurons)
|
|
412
|
+
// Fetch followees from each neuron (get_full_neuron_by_id_or_subaccount)
|
|
413
|
+
// Each of the functions needs a identity (signed user)
|
|
414
|
+
return [];
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
getDelegateeDetails(address) {
|
|
418
|
+
var _a, _b;
|
|
419
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
420
|
+
const neuron = yield (0, ICPImplementation_1.getNeuronInfo)(address, this.options.network.rpcUrl, this.options.network.governanceCanisterId);
|
|
421
|
+
return {
|
|
422
|
+
name: ((_b = (_a = neuron === null || neuron === void 0 ? void 0 : neuron.known_neuron_data) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.name) || '',
|
|
423
|
+
address: address
|
|
424
|
+
};
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
isPublicKeyDelegating(publicKey) {
|
|
428
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
429
|
+
const address = yield this.getAddressFromPublicKey(publicKey);
|
|
430
|
+
return this.isAddressDelegating(address);
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
isAddressDelegating(address) {
|
|
434
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
435
|
+
// Fetch neurons of address from governance canister (listNeurons)
|
|
436
|
+
// Fetch followees from each neuron (get_full_neuron_by_id_or_subaccount)
|
|
437
|
+
// If each neuron as followes then it is delegating
|
|
438
|
+
// If no neuron has followes then it is not delegating
|
|
439
|
+
// If some, but not all, neurons have followes then it is partially delegating
|
|
440
|
+
return false;
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
getDelegatorDetailsFromPublicKey(publicKey) {
|
|
444
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
445
|
+
const address = yield this.getAddressFromPublicKey(publicKey);
|
|
446
|
+
return this.getDelegatorDetailsFromAddress(address);
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
getDelegatorDetailsFromAddress(address) {
|
|
450
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
451
|
+
const metadata = yield this.getMetadata();
|
|
452
|
+
const availableBalance = yield this.getBalanceOfAddress(address);
|
|
453
|
+
const availableActions = yield this.getAvailableDelegatorActions();
|
|
454
|
+
const totalBalance = (0, module_kit_1.newAmount)(availableBalance.total).blockchain(metadata.units);
|
|
455
|
+
return {
|
|
456
|
+
address,
|
|
457
|
+
balance: totalBalance.value,
|
|
458
|
+
delegatees: [],
|
|
459
|
+
availableActions: availableActions
|
|
460
|
+
};
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
getDelegationDetailsFromPublicKey(publicKey, delegatees) {
|
|
464
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
465
|
+
const address = yield this.getAddressFromPublicKey(publicKey);
|
|
466
|
+
return this.getDelegationDetailsFromAddress(address, delegatees);
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
getDelegationDetailsFromAddress(address, delegatees) {
|
|
470
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
471
|
+
if (delegatees.length > 1) {
|
|
472
|
+
return Promise.reject('Multiple validators for a single delegation are not supported.');
|
|
473
|
+
}
|
|
474
|
+
const validator = delegatees[0];
|
|
475
|
+
const results = yield Promise.all([this.getDelegatorDetailsFromAddress(address), this.getDelegateeDetails(validator)]);
|
|
476
|
+
const delegatorDetails = results[0];
|
|
477
|
+
const validatorDetails = results[1];
|
|
478
|
+
return {
|
|
479
|
+
delegator: delegatorDetails,
|
|
480
|
+
delegatees: [validatorDetails]
|
|
481
|
+
};
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
getAvailableDelegatorActions() {
|
|
485
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
486
|
+
const actions = [];
|
|
487
|
+
actions.push({
|
|
488
|
+
type: transaction_1.ICPActionType.GET_NEURON_INFO
|
|
489
|
+
});
|
|
490
|
+
actions.push({
|
|
491
|
+
type: transaction_1.ICPActionType.TRANSFER_TO_SUBACCOUNT,
|
|
492
|
+
args: ['amount']
|
|
396
493
|
});
|
|
494
|
+
actions.push({
|
|
495
|
+
type: transaction_1.ICPActionType.CLAIM_GOVERNANCE
|
|
496
|
+
});
|
|
497
|
+
actions.push({
|
|
498
|
+
type: transaction_1.ICPActionType.INCREASE_DELAY,
|
|
499
|
+
args: ['delaySeconds']
|
|
500
|
+
});
|
|
501
|
+
actions.push({
|
|
502
|
+
type: transaction_1.ICPActionType.START_DISSOLVING
|
|
503
|
+
});
|
|
504
|
+
actions.push({
|
|
505
|
+
type: transaction_1.ICPActionType.STOP_DISSOLVING
|
|
506
|
+
});
|
|
507
|
+
actions.push({
|
|
508
|
+
type: transaction_1.ICPActionType.FOLLOW_NEURON,
|
|
509
|
+
args: ['neuronId']
|
|
510
|
+
});
|
|
511
|
+
actions.push({
|
|
512
|
+
type: transaction_1.ICPActionType.RESTAKE_MATURITY,
|
|
513
|
+
args: ['percentageStake']
|
|
514
|
+
});
|
|
515
|
+
actions.push({
|
|
516
|
+
type: transaction_1.ICPActionType.REMOVE_STAKE
|
|
517
|
+
});
|
|
518
|
+
return actions;
|
|
397
519
|
});
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
|
|
520
|
+
}
|
|
521
|
+
prepareDelegatorActionFromPublicKey(publicKey, type, data) {
|
|
522
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
523
|
+
switch (type) {
|
|
524
|
+
case transaction_1.ICPActionType.GET_NEURON_INFO:
|
|
525
|
+
return [yield (0, ICPGovernance_1.createUnsignedTransactionGetNeuronInfo)()];
|
|
526
|
+
case transaction_1.ICPActionType.TRANSFER_TO_SUBACCOUNT:
|
|
527
|
+
// TODO : get amount from data and fee form defaults
|
|
528
|
+
const amount = BigInt(0);
|
|
529
|
+
const fee = BigInt(0);
|
|
530
|
+
return [yield (0, ICPGovernance_1.createUnsignedTransactionTransferToSubaccount)(publicKey.value, this.options.network.ledgerCanisterId, amount, fee)];
|
|
531
|
+
case transaction_1.ICPActionType.CLAIM_GOVERNANCE:
|
|
532
|
+
return [yield (0, ICPGovernance_1.createUnsignedTransactionClaimGovernance)(publicKey.value)];
|
|
533
|
+
case transaction_1.ICPActionType.INCREASE_DELAY:
|
|
534
|
+
// TODO : get delaySeconds from data
|
|
535
|
+
const delaySeconds = 0;
|
|
536
|
+
return [yield (0, ICPGovernance_1.createUnsignedTransactionIncreaseDelay)(publicKey.value, delaySeconds)];
|
|
537
|
+
case transaction_1.ICPActionType.START_DISSOLVING:
|
|
538
|
+
return [yield (0, ICPGovernance_1.createUnsignedTransactionStartDissolving)(publicKey.value)];
|
|
539
|
+
case transaction_1.ICPActionType.STOP_DISSOLVING:
|
|
540
|
+
return [yield (0, ICPGovernance_1.createUnsignedTransactionStopDissolving)(publicKey.value)];
|
|
541
|
+
case transaction_1.ICPActionType.FOLLOW_NEURON:
|
|
542
|
+
// TODO : get neuronId from data
|
|
543
|
+
const neuronId = 0;
|
|
544
|
+
return [yield (0, ICPGovernance_1.createUnsignedTransactionFollowNeuron)(publicKey.value, neuronId)];
|
|
545
|
+
case transaction_1.ICPActionType.RESTAKE_MATURITY:
|
|
546
|
+
// TODO : get percentageStake from data
|
|
547
|
+
const percentageStake = 0;
|
|
548
|
+
return [yield (0, ICPGovernance_1.createUnsignedTransactionRestakeMaturity)(publicKey.value, percentageStake)];
|
|
549
|
+
case transaction_1.ICPActionType.REMOVE_STAKE:
|
|
550
|
+
return [yield (0, ICPGovernance_1.createUnsignedTransactionRemoveStake)(publicKey.value)];
|
|
551
|
+
default:
|
|
552
|
+
return Promise.reject(`Delegator action type ${type} is not supported.`);
|
|
553
|
+
}
|
|
554
|
+
});
|
|
555
|
+
}
|
|
556
|
+
fetchValidators() {
|
|
557
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
558
|
+
return (0, ICPImplementation_1.listKnownNeurons)(this.options.network.rpcUrl, this.options.network.governanceCanisterId);
|
|
559
|
+
});
|
|
560
|
+
}
|
|
561
|
+
fetchVotingPower(neuronId) {
|
|
562
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
563
|
+
const neuron = yield (0, ICPImplementation_1.getNeuronInfo)(neuronId, this.options.network.rpcUrl, this.options.network.governanceCanisterId);
|
|
564
|
+
if (neuron) {
|
|
565
|
+
return new bignumber_1.default(neuron.voting_power.toString()).div(new bignumber_1.default(10 ** this.units.ICP.decimals));
|
|
566
|
+
}
|
|
567
|
+
else
|
|
568
|
+
return new bignumber_1.default(0);
|
|
569
|
+
});
|
|
570
|
+
}
|
|
571
|
+
fetchNeuron(publicKey) {
|
|
572
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
573
|
+
const neuron = yield (0, ICPImplementation_1.getNeuronInfoBySubAccount)(publicKey, this.options.network.rpcUrl, this.options.network.governanceCanisterId);
|
|
574
|
+
return neuron;
|
|
575
|
+
});
|
|
576
|
+
}
|
|
577
|
+
fetchSubaccountBalance(publicKey) {
|
|
578
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
579
|
+
// Create subaccount from publicKey
|
|
580
|
+
const { subAccount, nonce: _ } = (0, ICPImplementation_1.getFixedSubaccountFromPublicKey)(publicKey);
|
|
581
|
+
const accountIdentifier = account_1.AccountIdentifier.fromPrincipal({
|
|
582
|
+
principal: principal_1.Principal.from(this.options.network.ledgerCanisterId),
|
|
583
|
+
subAccount: subAccount
|
|
584
|
+
});
|
|
585
|
+
return this.getBalanceOfAddress(accountIdentifier.toHex());
|
|
586
|
+
});
|
|
587
|
+
}
|
|
588
|
+
}
|
|
401
589
|
exports.ICPProtocolImpl = ICPProtocolImpl;
|
|
402
590
|
// Factory
|
|
403
|
-
function createICPProtocol(options) {
|
|
404
|
-
if (options === void 0) { options = {}; }
|
|
591
|
+
function createICPProtocol(options = {}) {
|
|
405
592
|
return new ICPProtocolImpl(options);
|
|
406
593
|
}
|
|
407
594
|
exports.createICPProtocol = createICPProtocol;
|
|
408
595
|
exports.ICP_MAINNET_PROTOCOL_NETWORK = {
|
|
409
596
|
name: 'Mainnet',
|
|
410
597
|
type: 'mainnet',
|
|
411
|
-
rpcUrl: 'https://
|
|
598
|
+
rpcUrl: 'https://ic0.app/',
|
|
412
599
|
ledgerCanisterId: 'ryjl3-tyaaa-aaaaa-aaaba-cai',
|
|
600
|
+
governanceCanisterId: 'rrkah-fqaaa-aaaaa-aaaaq-cai',
|
|
413
601
|
explorerUrl: 'https://ledger-api.internetcomputer.org'
|
|
414
602
|
};
|
|
415
|
-
|
|
416
|
-
function createICPProtocolOptions(network) {
|
|
417
|
-
if (network === void 0) { network = {}; }
|
|
603
|
+
const DEFAULT_ICP_PROTOCOL_NETWORK = exports.ICP_MAINNET_PROTOCOL_NETWORK;
|
|
604
|
+
function createICPProtocolOptions(network = {}) {
|
|
418
605
|
return {
|
|
419
|
-
network:
|
|
606
|
+
network: Object.assign(Object.assign({}, DEFAULT_ICP_PROTOCOL_NETWORK), network)
|
|
420
607
|
};
|
|
421
608
|
}
|
|
422
609
|
exports.createICPProtocolOptions = createICPProtocolOptions;
|