@airgap/cosmos-core 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/v1/index.js +11 -11
- package/v1/index.js.map +1 -1
- package/v1/node/CosmosNodeClient.js +189 -396
- package/v1/node/CosmosNodeClient.js.map +1 -1
- package/v1/protocol/CosmosBaseProtocol.js +557 -993
- package/v1/protocol/CosmosBaseProtocol.js.map +1 -1
- package/v1/protocol/CosmosCryptoClient.js +19 -82
- package/v1/protocol/CosmosCryptoClient.js.map +1 -1
- package/v1/serializer/v3/schemas/converter/transaction-converter.js +8 -20
- package/v1/serializer/v3/schemas/converter/transaction-converter.js.map +1 -1
- package/v1/serializer/v3/validators/transaction-validators.js +12 -59
- package/v1/serializer/v3/validators/transaction-validators.js.map +1 -1
- package/v1/types/crypto.d.ts +1 -1
- package/v1/types/data/CosmosAddress.js +16 -18
- package/v1/types/data/CosmosAddress.js.map +1 -1
- package/v1/types/data/CosmosCoin.js +21 -23
- package/v1/types/data/CosmosCoin.js.map +1 -1
- package/v1/types/data/CosmosFee.js +16 -17
- package/v1/types/data/CosmosFee.js.map +1 -1
- package/v1/types/data/transaction/CosmosTransaction.js +38 -40
- package/v1/types/data/transaction/CosmosTransaction.js.map +1 -1
- package/v1/types/data/transaction/message/CosmosDelegateMessage.js +24 -27
- package/v1/types/data/transaction/message/CosmosDelegateMessage.js.map +1 -1
- package/v1/types/data/transaction/message/CosmosMessage.js +11 -12
- package/v1/types/data/transaction/message/CosmosMessage.js.map +1 -1
- package/v1/types/data/transaction/message/CosmosSendMessage.js +31 -41
- package/v1/types/data/transaction/message/CosmosSendMessage.js.map +1 -1
- package/v1/types/data/transaction/message/CosmosWithdrawDelegationRewardMessage.js +20 -21
- package/v1/types/data/transaction/message/CosmosWithdrawDelegationRewardMessage.js.map +1 -1
- package/v1/types/transaction.js +1 -1
- package/v1/types/transaction.js.map +1 -1
- package/v1/utils/key.js +4 -5
- package/v1/utils/key.js.map +1 -1
- package/v1/utils/signature.js +3 -4
- package/v1/utils/signature.js.map +1 -1
- package/v1/utils/transaction.js +1 -2
- package/v1/utils/transaction.js.map +1 -1
|
@@ -1,413 +1,206 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (_) try {
|
|
18
|
-
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;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
4
|
};
|
|
41
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
6
|
exports.CosmosNodeClient = void 0;
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
if (useCORSProxy === void 0) { useCORSProxy = false; }
|
|
7
|
+
const index_1 = __importDefault(require("@airgap/coinlib-core/dependencies/src/axios-0.19.0/index"));
|
|
8
|
+
const bignumber_1 = __importDefault(require("@airgap/coinlib-core/dependencies/src/bignumber.js-9.0.0/bignumber"));
|
|
9
|
+
const module_kit_1 = require("@airgap/module-kit");
|
|
10
|
+
const CosmosCoin_1 = require("../types/data/CosmosCoin");
|
|
11
|
+
class CosmosNodeClient {
|
|
12
|
+
constructor(baseURL, useCORSProxy = false) {
|
|
50
13
|
this.baseURL = baseURL;
|
|
51
14
|
this.useCORSProxy = useCORSProxy;
|
|
52
15
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
return [2 /*return*/, response.data];
|
|
90
|
-
}
|
|
91
|
-
});
|
|
92
|
-
});
|
|
93
|
-
};
|
|
94
|
-
CosmosNodeClient.prototype.fetchNodeInfo = function () {
|
|
95
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
96
|
-
var response, nodeInfo;
|
|
97
|
-
return __generator(this, function (_a) {
|
|
98
|
-
switch (_a.label) {
|
|
99
|
-
case 0: return [4 /*yield*/, index_1.default.get(this.url("/cosmos/base/tendermint/v1beta1/node_info"))];
|
|
100
|
-
case 1:
|
|
101
|
-
response = _a.sent();
|
|
102
|
-
nodeInfo = response.data.default_node_info;
|
|
103
|
-
return [2 /*return*/, nodeInfo];
|
|
104
|
-
}
|
|
105
|
-
});
|
|
106
|
-
});
|
|
107
|
-
};
|
|
108
|
-
CosmosNodeClient.prototype.broadcastSignedTransaction = function (tx_bytes) {
|
|
109
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
110
|
-
var response;
|
|
111
|
-
return __generator(this, function (_a) {
|
|
112
|
-
switch (_a.label) {
|
|
113
|
-
case 0: return [4 /*yield*/, index_1.default.post(this.url("/cosmos/tx/v1beta1/txs"), {
|
|
114
|
-
tx_bytes: tx_bytes,
|
|
115
|
-
mode: 'BROADCAST_MODE_ASYNC'
|
|
116
|
-
}, {
|
|
117
|
-
headers: {
|
|
118
|
-
'Content-type': 'application/json'
|
|
119
|
-
}
|
|
120
|
-
})];
|
|
121
|
-
case 1:
|
|
122
|
-
response = _a.sent();
|
|
123
|
-
return [2 /*return*/, response.data.tx_response.txhash];
|
|
124
|
-
}
|
|
125
|
-
});
|
|
126
|
-
});
|
|
127
|
-
};
|
|
128
|
-
CosmosNodeClient.prototype.fetchAccount = function (address) {
|
|
129
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
130
|
-
var response, account;
|
|
131
|
-
return __generator(this, function (_a) {
|
|
132
|
-
switch (_a.label) {
|
|
133
|
-
case 0: return [4 /*yield*/, index_1.default.get(this.url("/cosmos/auth/v1beta1/accounts/".concat(address)))];
|
|
134
|
-
case 1:
|
|
135
|
-
response = _a.sent();
|
|
136
|
-
account = response.data.account;
|
|
137
|
-
return [2 /*return*/, account];
|
|
138
|
-
}
|
|
139
|
-
});
|
|
140
|
-
});
|
|
141
|
-
};
|
|
142
|
-
CosmosNodeClient.prototype.fetchDelegations = function (address, filterEmpty) {
|
|
143
|
-
if (filterEmpty === void 0) { filterEmpty = true; }
|
|
144
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
145
|
-
var response, delegations;
|
|
146
|
-
return __generator(this, function (_a) {
|
|
147
|
-
switch (_a.label) {
|
|
148
|
-
case 0: return [4 /*yield*/, index_1.default.get(this.url("/cosmos/staking/v1beta1/delegations/".concat(address)))];
|
|
149
|
-
case 1:
|
|
150
|
-
response = _a.sent();
|
|
151
|
-
if (response.data === null) {
|
|
152
|
-
return [2 /*return*/, []];
|
|
153
|
-
}
|
|
154
|
-
delegations = response.data.delegation_responses;
|
|
155
|
-
return [2 /*return*/, filterEmpty ? delegations.filter(function (delegation) { return new bignumber_1.default(delegation.balance.amount).gt(0); }) : delegations];
|
|
156
|
-
}
|
|
157
|
-
});
|
|
158
|
-
});
|
|
159
|
-
};
|
|
160
|
-
CosmosNodeClient.prototype.fetchTotalDelegatedAmount = function (address, denom) {
|
|
161
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
162
|
-
var delegations, balances;
|
|
163
|
-
return __generator(this, function (_a) {
|
|
164
|
-
switch (_a.label) {
|
|
165
|
-
case 0: return [4 /*yield*/, this.fetchDelegations(address)];
|
|
166
|
-
case 1:
|
|
167
|
-
delegations = _a.sent();
|
|
168
|
-
balances = delegations.map(function (delegation) { return delegation.balance; });
|
|
169
|
-
return [2 /*return*/, (0, module_kit_1.newAmount)(CosmosCoin_1.CosmosCoin.sum(CosmosCoin_1.CosmosCoin.fromCoins(balances), denom).decimalPlaces(0, bignumber_1.default.ROUND_FLOOR), denom)];
|
|
170
|
-
}
|
|
171
|
-
});
|
|
172
|
-
});
|
|
173
|
-
};
|
|
174
|
-
CosmosNodeClient.prototype.fetchValidator = function (address) {
|
|
175
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
176
|
-
var response, validator;
|
|
177
|
-
return __generator(this, function (_a) {
|
|
178
|
-
switch (_a.label) {
|
|
179
|
-
case 0: return [4 /*yield*/, index_1.default.get(this.url("/cosmos/staking/v1beta1/validators/".concat(address)))];
|
|
180
|
-
case 1:
|
|
181
|
-
response = _a.sent();
|
|
182
|
-
validator = response.data.validator;
|
|
183
|
-
return [2 /*return*/, validator];
|
|
184
|
-
}
|
|
185
|
-
});
|
|
186
|
-
});
|
|
187
|
-
};
|
|
188
|
-
CosmosNodeClient.prototype.fetchValidators = function () {
|
|
189
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
190
|
-
var response, validators;
|
|
191
|
-
return __generator(this, function (_a) {
|
|
192
|
-
switch (_a.label) {
|
|
193
|
-
case 0: return [4 /*yield*/, index_1.default.get(this.url('/cosmos/staking/v1beta1/validators'))];
|
|
194
|
-
case 1:
|
|
195
|
-
response = _a.sent();
|
|
196
|
-
validators = response.data.validators;
|
|
197
|
-
return [2 /*return*/, validators];
|
|
198
|
-
}
|
|
199
|
-
});
|
|
200
|
-
});
|
|
201
|
-
};
|
|
202
|
-
CosmosNodeClient.prototype.fetchSelfDelegation = function (validatorAddress) {
|
|
203
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
204
|
-
var validatorInfo, operatorAddress, response, delegation;
|
|
205
|
-
return __generator(this, function (_a) {
|
|
206
|
-
switch (_a.label) {
|
|
207
|
-
case 0: return [4 /*yield*/, index_1.default.get(this.url("/cosmos/distribution/v1beta1/validators/".concat(validatorAddress)))];
|
|
208
|
-
case 1:
|
|
209
|
-
validatorInfo = _a.sent();
|
|
210
|
-
operatorAddress = validatorInfo.data.operator_address;
|
|
211
|
-
return [4 /*yield*/, index_1.default.get(this.url("/cosmos/staking/v1beta1/validators/".concat(validatorAddress, "/delegations/").concat(operatorAddress)))];
|
|
212
|
-
case 2:
|
|
213
|
-
response = _a.sent();
|
|
214
|
-
delegation = response.data.delegation_response;
|
|
215
|
-
return [2 /*return*/, delegation];
|
|
216
|
-
}
|
|
217
|
-
});
|
|
218
|
-
});
|
|
219
|
-
};
|
|
220
|
-
CosmosNodeClient.prototype.fetchUnbondingDelegations = function (delegatorAddress) {
|
|
221
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
222
|
-
var response, unbondingDelegations;
|
|
223
|
-
return __generator(this, function (_a) {
|
|
224
|
-
switch (_a.label) {
|
|
225
|
-
case 0: return [4 /*yield*/, index_1.default.get(this.url("/cosmos/staking/v1beta1/delegators/".concat(delegatorAddress, "/unbonding_delegations")))];
|
|
226
|
-
case 1:
|
|
227
|
-
response = _a.sent();
|
|
228
|
-
unbondingDelegations = response.data.unbonding_responses;
|
|
229
|
-
return [2 /*return*/, unbondingDelegations];
|
|
230
|
-
}
|
|
231
|
-
});
|
|
232
|
-
});
|
|
233
|
-
};
|
|
234
|
-
CosmosNodeClient.prototype.fetchTotalUnbondingAmount = function (address, denom) {
|
|
235
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
236
|
-
var unbondingDelegations, unbondings, total;
|
|
237
|
-
return __generator(this, function (_a) {
|
|
238
|
-
switch (_a.label) {
|
|
239
|
-
case 0: return [4 /*yield*/, this.fetchUnbondingDelegations(address)];
|
|
240
|
-
case 1:
|
|
241
|
-
unbondingDelegations = _a.sent();
|
|
242
|
-
if (unbondingDelegations) {
|
|
243
|
-
unbondings = unbondingDelegations.map(function (delegation) { return delegation.entries; }).reduce(function (current, next) { return current.concat(next); }, []);
|
|
244
|
-
total = unbondings
|
|
245
|
-
.reduce(function (current, next) { return current.plus(new bignumber_1.default(next.balance)); }, new bignumber_1.default(0))
|
|
246
|
-
.decimalPlaces(0, bignumber_1.default.ROUND_FLOOR);
|
|
247
|
-
return [2 /*return*/, (0, module_kit_1.newAmount)(total, denom)];
|
|
248
|
-
}
|
|
249
|
-
return [2 /*return*/, (0, module_kit_1.newAmount)(new bignumber_1.default(0), denom)];
|
|
250
|
-
}
|
|
251
|
-
});
|
|
252
|
-
});
|
|
253
|
-
};
|
|
254
|
-
CosmosNodeClient.prototype.fetchRewardDetails = function (delegatorAddress) {
|
|
255
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
256
|
-
return __generator(this, function (_a) {
|
|
257
|
-
return [2 /*return*/, index_1.default.get(this.url("/cosmos/distribution/v1beta1/delegators/".concat(delegatorAddress, "/rewards")))
|
|
258
|
-
.then(function (response) { var _a; return ((_a = response.data.rewards) !== null && _a !== void 0 ? _a : []); })
|
|
259
|
-
.catch(function () { return []; })];
|
|
260
|
-
});
|
|
261
|
-
});
|
|
262
|
-
};
|
|
263
|
-
CosmosNodeClient.prototype.fetchTotalReward = function (delegatorAddress, denom) {
|
|
264
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
265
|
-
var totalRewards;
|
|
266
|
-
return __generator(this, function (_a) {
|
|
267
|
-
switch (_a.label) {
|
|
268
|
-
case 0: return [4 /*yield*/, index_1.default.get(this.url("/cosmos/distribution/v1beta1/delegators/".concat(delegatorAddress, "/rewards")))
|
|
269
|
-
.then(function (response) { return response.data.total; })
|
|
270
|
-
.catch(function () { return []; })];
|
|
271
|
-
case 1:
|
|
272
|
-
totalRewards = _a.sent();
|
|
273
|
-
if ((totalRewards === null || totalRewards === void 0 ? void 0 : totalRewards.length) > 0) {
|
|
274
|
-
return [2 /*return*/, (0, module_kit_1.newAmount)(CosmosCoin_1.CosmosCoin.sum(CosmosCoin_1.CosmosCoin.fromCoins(totalRewards), denom).decimalPlaces(0, bignumber_1.default.ROUND_FLOOR), denom)];
|
|
275
|
-
}
|
|
276
|
-
return [2 /*return*/, (0, module_kit_1.newAmount)(new bignumber_1.default(0), denom)];
|
|
277
|
-
}
|
|
278
|
-
});
|
|
279
|
-
});
|
|
280
|
-
};
|
|
281
|
-
CosmosNodeClient.prototype.fetchRewardForDelegation = function (delegatorAddress, validatorAddress, denom) {
|
|
282
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
283
|
-
var totalRewards;
|
|
284
|
-
return __generator(this, function (_a) {
|
|
285
|
-
switch (_a.label) {
|
|
286
|
-
case 0: return [4 /*yield*/, index_1.default.get(this.url("/cosmos/distribution/v1beta1/delegators/".concat(delegatorAddress, "/rewards/").concat(validatorAddress)))
|
|
287
|
-
.then(function (response) { return response.data.rewards; })
|
|
288
|
-
.catch(function () { return []; })];
|
|
289
|
-
case 1:
|
|
290
|
-
totalRewards = _a.sent();
|
|
291
|
-
if ((totalRewards === null || totalRewards === void 0 ? void 0 : totalRewards.length) > 0) {
|
|
292
|
-
return [2 /*return*/, (0, module_kit_1.newAmount)(CosmosCoin_1.CosmosCoin.sum(CosmosCoin_1.CosmosCoin.fromCoins(totalRewards), denom).decimalPlaces(0, bignumber_1.default.ROUND_FLOOR), denom)];
|
|
293
|
-
}
|
|
294
|
-
return [2 /*return*/, (0, module_kit_1.newAmount)(new bignumber_1.default(0), denom)];
|
|
295
|
-
}
|
|
296
|
-
});
|
|
16
|
+
async fetchBalance(address, denom) {
|
|
17
|
+
const response = await index_1.default.get(this.url(`/cosmos/bank/v1beta1/balances/${address}`));
|
|
18
|
+
const data = response.data.balances;
|
|
19
|
+
if (data.length > 0) {
|
|
20
|
+
const availableBalance = CosmosCoin_1.CosmosCoin.sum(CosmosCoin_1.CosmosCoin.fromCoins(data), denom);
|
|
21
|
+
const totalBalance = (await Promise.all([
|
|
22
|
+
this.fetchTotalReward(address, denom),
|
|
23
|
+
this.fetchTotalUnbondingAmount(address, denom),
|
|
24
|
+
this.fetchTotalDelegatedAmount(address, denom)
|
|
25
|
+
])).reduce((current, next) => current.plus(next.value), new bignumber_1.default(availableBalance));
|
|
26
|
+
return {
|
|
27
|
+
total: (0, module_kit_1.newAmount)(totalBalance.decimalPlaces(0, bignumber_1.default.ROUND_FLOOR), denom),
|
|
28
|
+
available: (0, module_kit_1.newAmount)(availableBalance.decimalPlaces(0, bignumber_1.default.ROUND_FLOOR), denom)
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
return { total: (0, module_kit_1.newAmount)(new bignumber_1.default(0), denom), available: (0, module_kit_1.newAmount)(new bignumber_1.default(0), denom) };
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
async fetchSendTransactionsFor(address, limit, offset, isSender = true) {
|
|
36
|
+
const response = await index_1.default.get(this.url(`/cosmos/tx/v1beta1/txs?events=${isSender ? 'transfer.sender' : 'transfer.recipient'}='${address}'&events=tx.height=0&pagination.limit=${limit}&pagination.offset=${offset}&orderBy=2`));
|
|
37
|
+
return response.data;
|
|
38
|
+
}
|
|
39
|
+
async fetchNodeInfo() {
|
|
40
|
+
const response = await index_1.default.get(this.url(`/cosmos/base/tendermint/v1beta1/node_info`));
|
|
41
|
+
const nodeInfo = response.data.default_node_info;
|
|
42
|
+
return nodeInfo;
|
|
43
|
+
}
|
|
44
|
+
async broadcastSignedTransaction(tx_bytes) {
|
|
45
|
+
const response = await index_1.default.post(this.url(`/cosmos/tx/v1beta1/txs`), {
|
|
46
|
+
tx_bytes,
|
|
47
|
+
mode: 'BROADCAST_MODE_ASYNC'
|
|
48
|
+
}, {
|
|
49
|
+
headers: {
|
|
50
|
+
'Content-type': 'application/json'
|
|
51
|
+
}
|
|
297
52
|
});
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
53
|
+
return response.data.tx_response.txhash;
|
|
54
|
+
}
|
|
55
|
+
async fetchAccount(address) {
|
|
56
|
+
const response = await index_1.default.get(this.url(`/cosmos/auth/v1beta1/accounts/${address}`));
|
|
57
|
+
const account = response.data.account;
|
|
58
|
+
return account;
|
|
59
|
+
}
|
|
60
|
+
async fetchDelegations(address, filterEmpty = true) {
|
|
61
|
+
const response = await index_1.default.get(this.url(`/cosmos/staking/v1beta1/delegations/${address}`));
|
|
62
|
+
if (response.data === null) {
|
|
63
|
+
return [];
|
|
64
|
+
}
|
|
65
|
+
const delegations = response.data.delegation_responses;
|
|
66
|
+
return filterEmpty ? delegations.filter((delegation) => new bignumber_1.default(delegation.balance.amount).gt(0)) : delegations;
|
|
67
|
+
}
|
|
68
|
+
async fetchTotalDelegatedAmount(address, denom) {
|
|
69
|
+
const delegations = await this.fetchDelegations(address);
|
|
70
|
+
const balances = delegations.map((delegation) => delegation.balance);
|
|
71
|
+
return (0, module_kit_1.newAmount)(CosmosCoin_1.CosmosCoin.sum(CosmosCoin_1.CosmosCoin.fromCoins(balances), denom).decimalPlaces(0, bignumber_1.default.ROUND_FLOOR), denom);
|
|
72
|
+
}
|
|
73
|
+
async fetchValidator(address) {
|
|
74
|
+
const response = await index_1.default.get(this.url(`/cosmos/staking/v1beta1/validators/${address}`));
|
|
75
|
+
const validator = response.data.validator;
|
|
76
|
+
return validator;
|
|
77
|
+
}
|
|
78
|
+
async fetchValidators() {
|
|
79
|
+
const response = await index_1.default.get(this.url('/cosmos/staking/v1beta1/validators'));
|
|
80
|
+
const validators = response.data.validators;
|
|
81
|
+
return validators;
|
|
82
|
+
}
|
|
83
|
+
async fetchSelfDelegation(validatorAddress) {
|
|
84
|
+
const validatorInfo = await index_1.default.get(this.url(`/cosmos/distribution/v1beta1/validators/${validatorAddress}`));
|
|
85
|
+
const operatorAddress = validatorInfo.data.operator_address;
|
|
86
|
+
const response = await index_1.default.get(this.url(`/cosmos/staking/v1beta1/validators/${validatorAddress}/delegations/${operatorAddress}`));
|
|
87
|
+
const delegation = response.data.delegation_response;
|
|
88
|
+
return delegation;
|
|
89
|
+
}
|
|
90
|
+
async fetchUnbondingDelegations(delegatorAddress) {
|
|
91
|
+
const response = await index_1.default.get(this.url(`/cosmos/staking/v1beta1/delegators/${delegatorAddress}/unbonding_delegations`));
|
|
92
|
+
const unbondingDelegations = response.data.unbonding_responses;
|
|
93
|
+
return unbondingDelegations;
|
|
94
|
+
}
|
|
95
|
+
async fetchTotalUnbondingAmount(address, denom) {
|
|
96
|
+
const unbondingDelegations = await this.fetchUnbondingDelegations(address);
|
|
97
|
+
if (unbondingDelegations) {
|
|
98
|
+
const unbondings = unbondingDelegations.map((delegation) => delegation.entries).reduce((current, next) => current.concat(next), []);
|
|
99
|
+
const total = unbondings
|
|
100
|
+
.reduce((current, next) => current.plus(new bignumber_1.default(next.balance)), new bignumber_1.default(0))
|
|
101
|
+
.decimalPlaces(0, bignumber_1.default.ROUND_FLOOR);
|
|
102
|
+
return (0, module_kit_1.newAmount)(total, denom);
|
|
103
|
+
}
|
|
104
|
+
return (0, module_kit_1.newAmount)(new bignumber_1.default(0), denom);
|
|
105
|
+
}
|
|
106
|
+
async fetchRewardDetails(delegatorAddress) {
|
|
107
|
+
return index_1.default.get(this.url(`/cosmos/distribution/v1beta1/delegators/${delegatorAddress}/rewards`))
|
|
108
|
+
.then((response) => (response.data.rewards ?? []))
|
|
109
|
+
.catch(() => []);
|
|
110
|
+
}
|
|
111
|
+
async fetchTotalReward(delegatorAddress, denom) {
|
|
112
|
+
const totalRewards = await index_1.default.get(this.url(`/cosmos/distribution/v1beta1/delegators/${delegatorAddress}/rewards`))
|
|
113
|
+
.then((response) => response.data.total)
|
|
114
|
+
.catch(() => []);
|
|
115
|
+
if (totalRewards?.length > 0) {
|
|
116
|
+
return (0, module_kit_1.newAmount)(CosmosCoin_1.CosmosCoin.sum(CosmosCoin_1.CosmosCoin.fromCoins(totalRewards), denom).decimalPlaces(0, bignumber_1.default.ROUND_FLOOR), denom);
|
|
117
|
+
}
|
|
118
|
+
return (0, module_kit_1.newAmount)(new bignumber_1.default(0), denom);
|
|
119
|
+
}
|
|
120
|
+
async fetchRewardForDelegation(delegatorAddress, validatorAddress, denom) {
|
|
121
|
+
const totalRewards = await index_1.default.get(this.url(`/cosmos/distribution/v1beta1/delegators/${delegatorAddress}/rewards/${validatorAddress}`))
|
|
122
|
+
.then((response) => response.data.rewards)
|
|
123
|
+
.catch(() => []);
|
|
124
|
+
if (totalRewards?.length > 0) {
|
|
125
|
+
return (0, module_kit_1.newAmount)(CosmosCoin_1.CosmosCoin.sum(CosmosCoin_1.CosmosCoin.fromCoins(totalRewards), denom).decimalPlaces(0, bignumber_1.default.ROUND_FLOOR), denom);
|
|
126
|
+
}
|
|
127
|
+
return (0, module_kit_1.newAmount)(new bignumber_1.default(0), denom);
|
|
128
|
+
}
|
|
129
|
+
async withdrawAllDelegationRewards(delegatorAddress, chainID, accountNumber, sequence, gas, fee, memo, simulate = false) {
|
|
130
|
+
const body = {
|
|
131
|
+
base_req: {
|
|
132
|
+
from: delegatorAddress,
|
|
133
|
+
memo,
|
|
134
|
+
chain_id: chainID,
|
|
135
|
+
account_number: accountNumber,
|
|
136
|
+
sequence,
|
|
137
|
+
gas: gas.toFixed(),
|
|
138
|
+
gas_adjustment: '1.2',
|
|
139
|
+
fees: [
|
|
140
|
+
{
|
|
141
|
+
denom: 'uatom',
|
|
142
|
+
amount: fee.toFixed()
|
|
143
|
+
}
|
|
144
|
+
],
|
|
145
|
+
simulate
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
const response = await index_1.default.post(this.url(`/distribution/delegators/${delegatorAddress}/rewards`), JSON.stringify(body), {
|
|
149
|
+
headers: {
|
|
150
|
+
'Content-type': 'application/json'
|
|
151
|
+
}
|
|
334
152
|
});
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
};
|
|
361
|
-
return [4 /*yield*/, index_1.default.post(this.url("/distribution/delegators/".concat(delegatorAddress, "/rewards/").concat(validatorAdress)), JSON.stringify(body), {
|
|
362
|
-
headers: {
|
|
363
|
-
'Content-type': 'application/json'
|
|
364
|
-
}
|
|
365
|
-
})];
|
|
366
|
-
case 1:
|
|
367
|
-
response = _a.sent();
|
|
368
|
-
return [2 /*return*/, response.data.hash];
|
|
369
|
-
}
|
|
370
|
-
});
|
|
153
|
+
return response.data.hash;
|
|
154
|
+
}
|
|
155
|
+
async withdrawDelegationRewards(delegatorAddress, validatorAdress, chainID, accountNumber, sequence, gas, fee, memo, simulate = false) {
|
|
156
|
+
const body = {
|
|
157
|
+
base_req: {
|
|
158
|
+
from: delegatorAddress,
|
|
159
|
+
memo,
|
|
160
|
+
chain_id: chainID,
|
|
161
|
+
account_number: accountNumber,
|
|
162
|
+
sequence,
|
|
163
|
+
gas: gas.toFixed(),
|
|
164
|
+
gas_adjustment: '1.2',
|
|
165
|
+
fees: [
|
|
166
|
+
{
|
|
167
|
+
denom: 'uatom',
|
|
168
|
+
amount: fee.toFixed()
|
|
169
|
+
}
|
|
170
|
+
],
|
|
171
|
+
simulate
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
const response = await index_1.default.post(this.url(`/distribution/delegators/${delegatorAddress}/rewards/${validatorAdress}`), JSON.stringify(body), {
|
|
175
|
+
headers: {
|
|
176
|
+
'Content-type': 'application/json'
|
|
177
|
+
}
|
|
371
178
|
});
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
179
|
+
return response.data.hash;
|
|
180
|
+
}
|
|
181
|
+
url(path) {
|
|
182
|
+
let result = `${this.baseURL}${path}`;
|
|
375
183
|
if (this.useCORSProxy) {
|
|
376
|
-
result =
|
|
184
|
+
result = `https://cors-proxy.airgap.prod.gke.papers.tech/proxy?url=${encodeURI(result)}`;
|
|
377
185
|
}
|
|
378
186
|
return result;
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
case 2: throw new (_g.apply(Error, [void 0, _j.apply(_h, [_k.sent()])]))();
|
|
398
|
-
case 3: return [4 /*yield*/, res.json()];
|
|
399
|
-
case 4:
|
|
400
|
-
json = _k.sent();
|
|
401
|
-
used = Number((_f = (_b = (_a = json === null || json === void 0 ? void 0 : json.gas_info) === null || _a === void 0 ? void 0 : _a.gas_used) !== null && _b !== void 0 ? _b : (_e = (_d = (_c = json === null || json === void 0 ? void 0 : json.gas_info) === null || _c === void 0 ? void 0 : _c.gas_used) === null || _d === void 0 ? void 0 : _d.toString) === null || _e === void 0 ? void 0 : _e.call(_d)) !== null && _f !== void 0 ? _f : 0);
|
|
402
|
-
if (!Number.isFinite(used) || used <= 0) {
|
|
403
|
-
throw new Error("Invalid gas_used from simulate: ".concat(JSON.stringify(json)));
|
|
404
|
-
}
|
|
405
|
-
return [2 /*return*/, { gas_used: used }];
|
|
406
|
-
}
|
|
407
|
-
});
|
|
408
|
-
});
|
|
409
|
-
};
|
|
410
|
-
return CosmosNodeClient;
|
|
411
|
-
}());
|
|
187
|
+
}
|
|
188
|
+
async simulateTx(txBytesBase64) {
|
|
189
|
+
const res = await fetch(this.url(`/cosmos/tx/v1beta1/simulate`), {
|
|
190
|
+
method: 'POST',
|
|
191
|
+
headers: { 'Content-Type': 'application/json' },
|
|
192
|
+
body: JSON.stringify({ tx_bytes: txBytesBase64 })
|
|
193
|
+
});
|
|
194
|
+
if (!res.ok) {
|
|
195
|
+
throw new Error(`Tx simulate failed: ${res.status} ${await res.text()}`);
|
|
196
|
+
}
|
|
197
|
+
const json = await res.json();
|
|
198
|
+
const used = Number(json?.gas_info?.gas_used ?? json?.gas_info?.gas_used?.toString?.() ?? 0);
|
|
199
|
+
if (!Number.isFinite(used) || used <= 0) {
|
|
200
|
+
throw new Error(`Invalid gas_used from simulate: ${JSON.stringify(json)}`);
|
|
201
|
+
}
|
|
202
|
+
return { gas_used: used };
|
|
203
|
+
}
|
|
204
|
+
}
|
|
412
205
|
exports.CosmosNodeClient = CosmosNodeClient;
|
|
413
206
|
//# sourceMappingURL=CosmosNodeClient.js.map
|