@airgap/cosmos-core 0.13.45-beta.3 → 0.13.45-beta.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/package.json +5 -5
  2. package/v1/index.js +11 -11
  3. package/v1/index.js.map +1 -1
  4. package/v1/node/CosmosNodeClient.js +396 -189
  5. package/v1/node/CosmosNodeClient.js.map +1 -1
  6. package/v1/protocol/CosmosBaseProtocol.js +993 -557
  7. package/v1/protocol/CosmosBaseProtocol.js.map +1 -1
  8. package/v1/protocol/CosmosCryptoClient.js +82 -19
  9. package/v1/protocol/CosmosCryptoClient.js.map +1 -1
  10. package/v1/serializer/v3/schemas/converter/transaction-converter.js +20 -8
  11. package/v1/serializer/v3/schemas/converter/transaction-converter.js.map +1 -1
  12. package/v1/serializer/v3/validators/transaction-validators.js +59 -12
  13. package/v1/serializer/v3/validators/transaction-validators.js.map +1 -1
  14. package/v1/types/crypto.d.ts +1 -1
  15. package/v1/types/data/CosmosAddress.js +18 -16
  16. package/v1/types/data/CosmosAddress.js.map +1 -1
  17. package/v1/types/data/CosmosCoin.js +23 -21
  18. package/v1/types/data/CosmosCoin.js.map +1 -1
  19. package/v1/types/data/CosmosFee.js +17 -16
  20. package/v1/types/data/CosmosFee.js.map +1 -1
  21. package/v1/types/data/transaction/CosmosTransaction.js +40 -38
  22. package/v1/types/data/transaction/CosmosTransaction.js.map +1 -1
  23. package/v1/types/data/transaction/message/CosmosDelegateMessage.js +27 -24
  24. package/v1/types/data/transaction/message/CosmosDelegateMessage.js.map +1 -1
  25. package/v1/types/data/transaction/message/CosmosMessage.js +12 -11
  26. package/v1/types/data/transaction/message/CosmosMessage.js.map +1 -1
  27. package/v1/types/data/transaction/message/CosmosSendMessage.js +41 -31
  28. package/v1/types/data/transaction/message/CosmosSendMessage.js.map +1 -1
  29. package/v1/types/data/transaction/message/CosmosWithdrawDelegationRewardMessage.js +21 -20
  30. package/v1/types/data/transaction/message/CosmosWithdrawDelegationRewardMessage.js.map +1 -1
  31. package/v1/types/transaction.js +1 -1
  32. package/v1/types/transaction.js.map +1 -1
  33. package/v1/utils/key.js +5 -4
  34. package/v1/utils/key.js.map +1 -1
  35. package/v1/utils/signature.js +4 -3
  36. package/v1/utils/signature.js.map +1 -1
  37. package/v1/utils/transaction.js +2 -1
  38. package/v1/utils/transaction.js.map +1 -1
@@ -1,206 +1,413 @@
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
+ };
2
38
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
39
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
40
  };
5
41
  Object.defineProperty(exports, "__esModule", { value: true });
6
42
  exports.CosmosNodeClient = void 0;
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) {
43
+ var index_1 = __importDefault(require("@airgap/coinlib-core/dependencies/src/axios-0.19.0/index"));
44
+ var bignumber_1 = __importDefault(require("@airgap/coinlib-core/dependencies/src/bignumber.js-9.0.0/bignumber"));
45
+ var module_kit_1 = require("@airgap/module-kit");
46
+ var CosmosCoin_1 = require("../types/data/CosmosCoin");
47
+ var CosmosNodeClient = /** @class */ (function () {
48
+ function CosmosNodeClient(baseURL, useCORSProxy) {
49
+ if (useCORSProxy === void 0) { useCORSProxy = false; }
13
50
  this.baseURL = baseURL;
14
51
  this.useCORSProxy = useCORSProxy;
15
52
  }
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
- }
53
+ CosmosNodeClient.prototype.fetchBalance = function (address, denom) {
54
+ return __awaiter(this, void 0, void 0, function () {
55
+ var response, data, availableBalance, totalBalance;
56
+ return __generator(this, function (_a) {
57
+ switch (_a.label) {
58
+ case 0: return [4 /*yield*/, index_1.default.get(this.url("/cosmos/bank/v1beta1/balances/".concat(address)))];
59
+ case 1:
60
+ response = _a.sent();
61
+ data = response.data.balances;
62
+ if (!(data.length > 0)) return [3 /*break*/, 3];
63
+ availableBalance = CosmosCoin_1.CosmosCoin.sum(CosmosCoin_1.CosmosCoin.fromCoins(data), denom);
64
+ return [4 /*yield*/, Promise.all([
65
+ this.fetchTotalReward(address, denom),
66
+ this.fetchTotalUnbondingAmount(address, denom),
67
+ this.fetchTotalDelegatedAmount(address, denom)
68
+ ])];
69
+ case 2:
70
+ totalBalance = (_a.sent()).reduce(function (current, next) { return current.plus(next.value); }, new bignumber_1.default(availableBalance));
71
+ return [2 /*return*/, {
72
+ total: (0, module_kit_1.newAmount)(totalBalance.decimalPlaces(0, bignumber_1.default.ROUND_FLOOR), denom),
73
+ available: (0, module_kit_1.newAmount)(availableBalance.decimalPlaces(0, bignumber_1.default.ROUND_FLOOR), denom)
74
+ }];
75
+ case 3: return [2 /*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) }];
76
+ }
77
+ });
52
78
  });
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
- }
79
+ };
80
+ CosmosNodeClient.prototype.fetchSendTransactionsFor = function (address, limit, offset, isSender) {
81
+ if (isSender === void 0) { isSender = true; }
82
+ return __awaiter(this, void 0, void 0, function () {
83
+ var response;
84
+ return __generator(this, function (_a) {
85
+ switch (_a.label) {
86
+ case 0: return [4 /*yield*/, index_1.default.get(this.url("/cosmos/tx/v1beta1/txs?events=".concat(isSender ? 'transfer.sender' : 'transfer.recipient', "='").concat(address, "'&events=tx.height=0&pagination.limit=").concat(limit, "&pagination.offset=").concat(offset, "&orderBy=2")))];
87
+ case 1:
88
+ response = _a.sent();
89
+ return [2 /*return*/, response.data];
90
+ }
91
+ });
152
92
  });
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
- }
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
+ });
178
106
  });
179
- return response.data.hash;
180
- }
181
- url(path) {
182
- let result = `${this.baseURL}${path}`;
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
+ });
297
+ });
298
+ };
299
+ CosmosNodeClient.prototype.withdrawAllDelegationRewards = function (delegatorAddress, chainID, accountNumber, sequence, gas, fee, memo, simulate) {
300
+ if (simulate === void 0) { simulate = false; }
301
+ return __awaiter(this, void 0, void 0, function () {
302
+ var body, response;
303
+ return __generator(this, function (_a) {
304
+ switch (_a.label) {
305
+ case 0:
306
+ body = {
307
+ base_req: {
308
+ from: delegatorAddress,
309
+ memo: memo,
310
+ chain_id: chainID,
311
+ account_number: accountNumber,
312
+ sequence: sequence,
313
+ gas: gas.toFixed(),
314
+ gas_adjustment: '1.2',
315
+ fees: [
316
+ {
317
+ denom: 'uatom',
318
+ amount: fee.toFixed()
319
+ }
320
+ ],
321
+ simulate: simulate
322
+ }
323
+ };
324
+ return [4 /*yield*/, index_1.default.post(this.url("/distribution/delegators/".concat(delegatorAddress, "/rewards")), JSON.stringify(body), {
325
+ headers: {
326
+ 'Content-type': 'application/json'
327
+ }
328
+ })];
329
+ case 1:
330
+ response = _a.sent();
331
+ return [2 /*return*/, response.data.hash];
332
+ }
333
+ });
334
+ });
335
+ };
336
+ CosmosNodeClient.prototype.withdrawDelegationRewards = function (delegatorAddress, validatorAdress, chainID, accountNumber, sequence, gas, fee, memo, simulate) {
337
+ if (simulate === void 0) { simulate = false; }
338
+ return __awaiter(this, void 0, void 0, function () {
339
+ var body, response;
340
+ return __generator(this, function (_a) {
341
+ switch (_a.label) {
342
+ case 0:
343
+ body = {
344
+ base_req: {
345
+ from: delegatorAddress,
346
+ memo: memo,
347
+ chain_id: chainID,
348
+ account_number: accountNumber,
349
+ sequence: sequence,
350
+ gas: gas.toFixed(),
351
+ gas_adjustment: '1.2',
352
+ fees: [
353
+ {
354
+ denom: 'uatom',
355
+ amount: fee.toFixed()
356
+ }
357
+ ],
358
+ simulate: simulate
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
+ });
371
+ });
372
+ };
373
+ CosmosNodeClient.prototype.url = function (path) {
374
+ var result = "".concat(this.baseURL).concat(path);
183
375
  if (this.useCORSProxy) {
184
- result = `https://cors-proxy.airgap.prod.gke.papers.tech/proxy?url=${encodeURI(result)}`;
376
+ result = "https://cors-proxy.airgap.prod.gke.papers.tech/proxy?url=".concat(encodeURI(result));
185
377
  }
186
378
  return result;
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
- }
379
+ };
380
+ CosmosNodeClient.prototype.simulateTx = function (txBytesBase64) {
381
+ var _a, _b, _c, _d, _e, _f;
382
+ return __awaiter(this, void 0, void 0, function () {
383
+ var res, _g, _h, _j, json, used;
384
+ return __generator(this, function (_k) {
385
+ switch (_k.label) {
386
+ case 0: return [4 /*yield*/, fetch(this.url("/cosmos/tx/v1beta1/simulate"), {
387
+ method: 'POST',
388
+ headers: { 'Content-Type': 'application/json' },
389
+ body: JSON.stringify({ tx_bytes: txBytesBase64 })
390
+ })];
391
+ case 1:
392
+ res = _k.sent();
393
+ if (!!res.ok) return [3 /*break*/, 3];
394
+ _g = Error.bind;
395
+ _j = (_h = "Tx simulate failed: ".concat(res.status, " ")).concat;
396
+ return [4 /*yield*/, res.text()];
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
+ }());
205
412
  exports.CosmosNodeClient = CosmosNodeClient;
206
413
  //# sourceMappingURL=CosmosNodeClient.js.map