carbon-js-sdk 0.4.4 → 0.4.6-dev.1

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 (102) hide show
  1. package/lib/CarbonSDK.js +1 -1
  2. package/lib/clients/CarbonQueryClient.js +2 -2
  3. package/lib/clients/ETHClient.d.ts +11 -4
  4. package/lib/clients/ETHClient.js +32 -18
  5. package/lib/clients/HydrogenClient.d.ts +16 -7
  6. package/lib/clients/HydrogenClient.js +56 -10
  7. package/lib/clients/InsightsQueryClient.d.ts +1 -0
  8. package/lib/clients/InsightsQueryClient.js +8 -0
  9. package/lib/clients/NEOClient.d.ts +1 -1
  10. package/lib/clients/NEOClient.js +15 -10
  11. package/lib/clients/TokenClient.d.ts +29 -5
  12. package/lib/clients/TokenClient.js +268 -55
  13. package/lib/clients/ZILClient.d.ts +1 -1
  14. package/lib/clients/ZILClient.js +25 -20
  15. package/lib/codec/cdp/e_mode_category.d.ts +23 -0
  16. package/lib/codec/cdp/e_mode_category.js +135 -0
  17. package/lib/codec/cosmos/gov/v1/query.js +2 -2
  18. package/lib/codec/cosmos/gov/v1/tx.js +2 -2
  19. package/lib/codec/cosmos/gov/v1beta1/query.js +2 -2
  20. package/lib/codec/cosmos/gov/v1beta1/tx.js +2 -2
  21. package/lib/codec/cosmos/group/v1/events.js +2 -2
  22. package/lib/codec/cosmos/group/v1/tx.js +4 -4
  23. package/lib/codec/cosmos/tx/v1beta1/tx.js +4 -4
  24. package/lib/codec/ethermint/crypto/v1/ethsecp256k1/export.d.ts +1 -0
  25. package/lib/codec/ethermint/crypto/v1/ethsecp256k1/export.js +6 -0
  26. package/lib/codec/ethermint/evm/v1/export.d.ts +5 -0
  27. package/lib/codec/ethermint/evm/v1/export.js +53 -0
  28. package/lib/codec/ethermint/feemarket/v1/export.d.ts +4 -0
  29. package/lib/codec/ethermint/feemarket/v1/export.js +18 -0
  30. package/lib/codec/ethermint/types/v1/export.d.ts +4 -0
  31. package/lib/codec/ethermint/types/v1/export.js +11 -0
  32. package/lib/codec/ethermint-models.d.ts +4 -0
  33. package/lib/codec/ethermint-models.js +26 -0
  34. package/lib/constant/eip712.d.ts +28 -0
  35. package/lib/constant/eip712.js +40 -0
  36. package/lib/constant/generic.js +1 -1
  37. package/lib/constant/ibc.d.ts +7 -0
  38. package/lib/constant/ibc.js +4 -1
  39. package/lib/constant/network.d.ts +2 -0
  40. package/lib/constant/network.js +5 -1
  41. package/lib/hydrogen/transfer.d.ts +5 -5
  42. package/lib/insights/common.d.ts +1 -0
  43. package/lib/insights/common.js +1 -0
  44. package/lib/insights/competition.d.ts +20 -0
  45. package/lib/modules/cdp.js +43 -43
  46. package/lib/modules/cosmwasm.js +1 -1
  47. package/lib/modules/evm.d.ts +46 -0
  48. package/lib/modules/evm.js +127 -0
  49. package/lib/modules/evmmerge.d.ts +12 -0
  50. package/lib/modules/evmmerge.js +35 -0
  51. package/lib/modules/feemarket.d.ts +20 -0
  52. package/lib/modules/feemarket.js +45 -0
  53. package/lib/modules/gov.js +12 -12
  54. package/lib/modules/ibc.d.ts +5 -1
  55. package/lib/modules/ibc.js +78 -0
  56. package/lib/modules/liquiditypool.js +1 -1
  57. package/lib/provider/account/EthLedgerAccount/EthLedgerAccount.js +1 -1
  58. package/lib/provider/account/NeoLedgerAccount/N3Ledger/ErrorCode.js +1 -1
  59. package/lib/provider/account/NeoLedgerAccount/N3Ledger/main.js +8 -8
  60. package/lib/provider/account/NeoLedgerAccount/NeoLedgerAccount.js +3 -3
  61. package/lib/provider/account/NeoLedgerAccount/NeonLedger.js +1 -1
  62. package/lib/provider/amino/types/admin.js +27 -27
  63. package/lib/provider/amino/types/bank.js +1 -1
  64. package/lib/provider/amino/types/broker.js +1 -1
  65. package/lib/provider/amino/types/cdp.js +22 -22
  66. package/lib/provider/amino/types/coin.js +4 -4
  67. package/lib/provider/amino/types/gov.js +4 -4
  68. package/lib/provider/amino/types/ibc.js +1 -1
  69. package/lib/provider/amino/types/leverage.js +1 -1
  70. package/lib/provider/amino/types/liquidityPool.js +7 -7
  71. package/lib/provider/amino/types/market.js +1 -1
  72. package/lib/provider/amino/types/oracle.js +1 -1
  73. package/lib/provider/amino/types/order.js +4 -4
  74. package/lib/provider/amino/types/position.js +1 -1
  75. package/lib/provider/amino/types/profile.js +1 -1
  76. package/lib/provider/amino/types/staking.js +4 -4
  77. package/lib/provider/amino/types/subaccount.js +3 -3
  78. package/lib/provider/amino/utils.js +11 -11
  79. package/lib/provider/keplr/KeplrStore.d.ts +1 -1
  80. package/lib/provider/keplr/KeplrStore.js +2 -5
  81. package/lib/provider/ledger/ledger.js +2 -2
  82. package/lib/provider/metamask/MetaMask.d.ts +5 -5
  83. package/lib/provider/metamask/MetaMask.js +37 -37
  84. package/lib/provider/o3/O3Wallet.js +2 -2
  85. package/lib/util/address.js +13 -13
  86. package/lib/util/api.js +4 -4
  87. package/lib/util/blockchain.d.ts +26 -0
  88. package/lib/util/blockchain.js +160 -1
  89. package/lib/util/eip712.d.ts +10 -0
  90. package/lib/util/eip712.js +46 -0
  91. package/lib/util/ethermint.d.ts +4 -0
  92. package/lib/util/ethermint.js +26 -0
  93. package/lib/util/fetch.js +1 -1
  94. package/lib/util/generic.js +3 -3
  95. package/lib/util/ibc.d.ts +5 -3
  96. package/lib/util/ibc.js +26 -1
  97. package/lib/util/number.js +5 -5
  98. package/lib/wallet/CarbonSigner.js +2 -2
  99. package/lib/wallet/CarbonSigningClient.js +8 -8
  100. package/lib/wallet/CarbonWallet.js +6 -6
  101. package/lib/websocket/connector.js +3 -3
  102. package/package.json +1 -1
@@ -12,12 +12,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
+ const pagination_1 = require("../codec/cosmos/base/query/v1beta1/pagination");
16
+ const tendermint_1 = require("../codec/ibc/lightclients/tendermint/v1/tendermint");
15
17
  const constant_1 = require("../constant");
16
18
  const ibc_1 = require("../constant/ibc");
17
19
  const network_1 = require("../constant/network");
18
- const KeplrAccount_1 = __importDefault(require("../provider/keplr/KeplrAccount"));
19
20
  const util_1 = require("../util");
21
+ const blockchain_1 = require("../util/blockchain");
20
22
  const number_1 = require("../util/number");
23
+ const query_1 = require("../codec/ibc/applications/transfer/v1/query");
24
+ const stargate_1 = require("@cosmjs/stargate");
25
+ const tendermint_rpc_1 = require("@cosmjs/tendermint-rpc");
21
26
  const long_1 = __importDefault(require("long"));
22
27
  const InsightsQueryClient_1 = __importDefault(require("./InsightsQueryClient"));
23
28
  const SYMBOL_OVERRIDE = {
@@ -43,11 +48,18 @@ class TokenClient {
43
48
  this.wrapperMap = {};
44
49
  this.poolTokens = {};
45
50
  this.cdpTokens = {};
51
+ this.bridges = { polynetwork: [], ibc: [] };
46
52
  this.symbols = {};
47
53
  this.usdValues = {};
48
54
  this.commonAssetNames = constant_1.CommonAssetName;
49
55
  this.geckoTokenNames = constant_1.CoinGeckoTokenNames;
56
+ // ibc apis
57
+ this.denomTraces = {};
50
58
  this.additionalGeckoDenoms = {};
59
+ this.getIbcChainFromBlockchainV2 = (blockchain) => {
60
+ const ibcBridge = this.bridges.ibc.find(bridge => (bridge.chainName === blockchain || bridge.chainName.toLowerCase() === blockchain));
61
+ return ibcBridge === null || ibcBridge === void 0 ? void 0 : ibcBridge.chain_id_name;
62
+ };
51
63
  }
52
64
  static instance(query, configProvider) {
53
65
  return new TokenClient(query, configProvider);
@@ -58,6 +70,8 @@ class TokenClient {
58
70
  yield this.reloadWrapperMap();
59
71
  yield this.reloadTokens();
60
72
  yield this.reloadDenomGeckoMap();
73
+ yield this.reloadDenomTraces();
74
+ yield this.getBridges();
61
75
  // non-blocking reload
62
76
  try {
63
77
  this.reloadUSDValues();
@@ -80,10 +94,11 @@ class TokenClient {
80
94
  return (_c = ((_b = (_a = this.tokens[denom]) !== null && _a !== void 0 ? _a : this.poolTokens[denom]) !== null && _b !== void 0 ? _b : this.cdpTokens[denom])) === null || _c === void 0 ? void 0 : _c.decimals.toNumber();
81
95
  }
82
96
  getBlockchain(denom) {
83
- var _a, _b, _c;
97
+ var _a, _b;
84
98
  const networkConfig = this.configProvider.getConfig();
99
+ const tokenData = this.tokens[denom];
85
100
  // chainId defaults to 3 so that blockchain will be undefined
86
- let chainId = (_c = (_b = (_a = this.tokens[denom]) === null || _a === void 0 ? void 0 : _a.chainId) === null || _b === void 0 ? void 0 : _b.toNumber()) !== null && _c !== void 0 ? _c : 3;
101
+ let chainId = (_b = (_a = tokenData === null || tokenData === void 0 ? void 0 : tokenData.chainId) === null || _a === void 0 ? void 0 : _a.toNumber()) !== null && _b !== void 0 ? _b : 3;
87
102
  if (this.isNativeToken(denom) || this.isNativeStablecoin(denom) || TokenClient.isPoolToken(denom) || TokenClient.isCdpToken(denom)) {
88
103
  // native denoms "swth" and "usc" should be native.
89
104
  // pool and cdp tokens are on the Native blockchain, hence 0
@@ -95,6 +110,18 @@ class TokenClient {
95
110
  const blockchain = util_1.BlockchainUtils.blockchainForChainId(chainId, networkConfig.network);
96
111
  return blockchain;
97
112
  }
113
+ getBlockchainV2(denom) {
114
+ if (!denom)
115
+ return undefined;
116
+ let token = this.tokens[denom];
117
+ if (this.isNativeToken(denom) || this.isNativeStablecoin(denom) || TokenClient.isPoolToken(denom) || TokenClient.isCdpToken(denom) || this.isGroupedToken(denom)) {
118
+ // native denoms "swth" and "usc" should be native.
119
+ // pool and cdp tokens are on the Native blockchain, hence 0
120
+ return 'Native';
121
+ }
122
+ const bridge = this.getBridgeFromToken(token);
123
+ return bridge === null || bridge === void 0 ? void 0 : bridge.chainName;
124
+ }
98
125
  getSymbol(denom) {
99
126
  var _a, _b;
100
127
  if (TokenClient.isCdpToken(denom)) {
@@ -117,10 +144,18 @@ class TokenClient {
117
144
  return (_a = util_1.NumberUtils.toUnitless(humanAmt, decimals)) !== null && _a !== void 0 ? _a : number_1.BN_ZERO;
118
145
  }
119
146
  tokenForId(id) {
120
- return Object.values(this.tokens).find((token) => token.id === id);
147
+ let tokensList = Object.values(this.tokens);
148
+ if (TokenClient.isPoolToken(id)) {
149
+ tokensList = Object.values(this.poolTokens);
150
+ }
151
+ else if (TokenClient.isCdpToken(id)) {
152
+ tokensList = Object.values(this.cdpTokens);
153
+ }
154
+ return tokensList.find((token) => token.id === id);
121
155
  }
122
156
  tokenForDenom(denom) {
123
- return this.tokens[denom];
157
+ var _a, _b;
158
+ return (_b = (_a = this.poolTokens[denom]) !== null && _a !== void 0 ? _a : this.cdpTokens[denom]) !== null && _b !== void 0 ? _b : this.tokens[denom];
124
159
  }
125
160
  getFeeInfo(denom) {
126
161
  return __awaiter(this, void 0, void 0, function* () {
@@ -229,7 +264,7 @@ class TokenClient {
229
264
  }
230
265
  return result;
231
266
  }
232
- getWrappedToken(denom, blockchain) {
267
+ getWrappedToken(denom, blockchain, version = 'V1') {
233
268
  const networkConfig = this.configProvider.getConfig();
234
269
  // if denom is already a wrapped denom or no blockchain was specified,
235
270
  // just return the input denom.
@@ -245,9 +280,15 @@ class TokenClient {
245
280
  }
246
281
  // check if wrapped denom is of correct blockchain
247
282
  const token = this.tokens[wrappedDenom];
248
- let tokenChain = util_1.BlockchainUtils.blockchainForChainId(token.chainId.toNumber(), networkConfig.network);
249
- if (TokenClient.isIBCDenom(wrappedDenom)) {
250
- tokenChain = util_1.IBCUtils.BlockchainMap[wrappedDenom];
283
+ let tokenChain = '';
284
+ if (version === "V1") {
285
+ tokenChain = util_1.BlockchainUtils.blockchainForChainId(token.chainId.toNumber());
286
+ if (TokenClient.isIBCDenom(token.denom)) {
287
+ tokenChain = util_1.IBCUtils.BlockchainMap[wrappedDenom];
288
+ }
289
+ }
290
+ else {
291
+ tokenChain = this.getBlockchainV2(token.denom);
251
292
  }
252
293
  if (!tokenChain) {
253
294
  continue; // unknown chain! just ignore this source token
@@ -283,21 +324,34 @@ class TokenClient {
283
324
  isNativeStablecoin(denom) {
284
325
  return denom === "usc";
285
326
  }
286
- getDepositTokenFor(tokenDenom, chain) {
327
+ isGroupedToken(denom) {
328
+ const groupedTokenRegex = new RegExp(/^cgt\/\d+$/);
329
+ return groupedTokenRegex.test(denom);
330
+ }
331
+ isCarbonToken(token) {
332
+ return Boolean(token && (this.isNativeToken(token.denom) || token.bridgeId.eq(0)));
333
+ }
334
+ getDepositTokenFor(tokenDenom, chain, version = "V1") {
287
335
  const networkConfig = this.configProvider.getConfig();
288
336
  const token = this.tokenForDenom(tokenDenom);
289
337
  if (!token) {
290
338
  console.error("getDepositTokenFor token not found for", tokenDenom);
291
339
  return;
292
340
  }
341
+ let targetChain = "";
293
342
  // check if selected token is a source token
294
- let targetChain = util_1.BlockchainUtils.blockchainForChainId(token.chainId.toNumber(), networkConfig.network);
295
- if (TokenClient.isIBCDenom(token.denom)) {
296
- targetChain = util_1.IBCUtils.BlockchainMap[token.denom];
343
+ if (version === "V1") {
344
+ targetChain = util_1.BlockchainUtils.blockchainForChainId(token.chainId.toNumber(), networkConfig.network);
345
+ if (TokenClient.isIBCDenom(token.denom)) {
346
+ targetChain = util_1.IBCUtils.BlockchainMap[token.denom];
347
+ }
348
+ }
349
+ else {
350
+ targetChain = this.getBlockchainV2(token.denom);
297
351
  }
298
352
  const isSourceToken = targetChain === chain && token.denom !== "swth";
299
353
  // if not source token find wrapped token for chain
300
- const depositToken = isSourceToken ? token : this.getWrappedToken(token.denom, chain);
354
+ const depositToken = isSourceToken ? token : this.getWrappedToken(token.denom, chain, version);
301
355
  if (!depositToken) {
302
356
  console.error(`getDepositTokenFor wrapped token not found for "${token.denom}"`);
303
357
  return;
@@ -341,52 +395,211 @@ class TokenClient {
341
395
  this.symbols[token.denom] = token.symbol;
342
396
  }
343
397
  }
344
- const networkConfig = this.configProvider.getConfig();
345
- if (networkConfig.network === network_1.Network.MainNet) {
346
- const symbolDenoms = Object.keys(this.symbols);
347
- const symbolMap = Object.values(this.symbols);
348
- const carbonIbc = this.getCarbonIbcTokens();
349
- carbonIbc.forEach((token) => {
350
- const index = symbolMap.indexOf("swth");
351
- if (!this.tokens[token.denom])
352
- this.tokens[token.denom] = token;
353
- if (!this.symbols[token.denom])
354
- this.symbols[token.denom] = token.symbol;
355
- if (index > -1) {
356
- const similarDenom = symbolDenoms[index];
357
- if (similarDenom && !this.wrapperMap[token.denom] && similarDenom !== token.denom) {
358
- this.wrapperMap[token.denom] = similarDenom;
398
+ return this.tokens;
399
+ });
400
+ }
401
+ reloadDenomTraces() {
402
+ return __awaiter(this, void 0, void 0, function* () {
403
+ const result = yield this.query.ibc.transfer.DenomTraces({
404
+ pagination: {
405
+ limit: new long_1.default(1000000),
406
+ offset: long_1.default.UZERO,
407
+ key: new Uint8Array(),
408
+ countTotal: false,
409
+ reverse: false,
410
+ },
411
+ });
412
+ result.denomTraces.forEach((denomTrace) => {
413
+ const ibcDenom = util_1.IBCUtils.makeIBCMinimalDenom(denomTrace.path, denomTrace.baseDenom);
414
+ const token = this.tokenForDenom(ibcDenom);
415
+ this.denomTraces[ibcDenom] = Object.assign(Object.assign({}, denomTrace), { token });
416
+ });
417
+ const swthTraces = yield this.getCarbonDenomTraces();
418
+ return Object.assign(Object.assign({}, this.denomTraces), swthTraces);
419
+ });
420
+ }
421
+ getDenomTraceData(denom) {
422
+ return this.denomTraces[denom];
423
+ }
424
+ getBridges() {
425
+ return __awaiter(this, void 0, void 0, function* () {
426
+ const allBridges = yield this.query.coin.BridgeAll({
427
+ pagination: {
428
+ key: new Uint8Array(),
429
+ limit: new long_1.default(10000),
430
+ offset: long_1.default.UZERO,
431
+ countTotal: true,
432
+ reverse: false,
433
+ },
434
+ });
435
+ const unmatchedIbcBridgeList = allBridges.bridges.filter(bridge => {
436
+ if (!bridge.enabled)
437
+ return;
438
+ return bridge.bridgeId.toNumber() === blockchain_1.BRIDGE_IDS.ibc;
439
+ });
440
+ const ibcBridges = yield this.matchChainsWithDifferentChainIds(unmatchedIbcBridgeList);
441
+ const polynetworkBridges = allBridges.bridges.reduce((prev, bridge) => {
442
+ if (!bridge.enabled || bridge.bridgeId.toNumber() !== blockchain_1.BRIDGE_IDS.polynetwork)
443
+ return prev;
444
+ prev.push(Object.assign(Object.assign({}, bridge), { isEvmChain: util_1.BlockchainUtils.isEvmChain(bridge.chainName) }));
445
+ return prev;
446
+ }, []);
447
+ Object.assign(this.bridges, {
448
+ polynetwork: polynetworkBridges,
449
+ ibc: ibcBridges
450
+ });
451
+ return this.bridges;
452
+ });
453
+ }
454
+ matchChainsWithDifferentChainIds(bridges) {
455
+ return __awaiter(this, void 0, void 0, function* () {
456
+ let newBridges = [];
457
+ try {
458
+ const channels_to_connection = yield this.query.ibc.channel.Channels({
459
+ pagination: pagination_1.PageRequest.fromPartial({
460
+ limit: new long_1.default(1000000)
461
+ })
462
+ });
463
+ const connection_to_clientId = yield this.query.ibc.connection.Connections({
464
+ pagination: pagination_1.PageRequest.fromPartial({
465
+ limit: new long_1.default(1000000)
466
+ })
467
+ });
468
+ const clientId_to_chainIdName = yield this.query.ibc.client.ClientStates({
469
+ pagination: pagination_1.PageRequest.fromPartial({
470
+ limit: new long_1.default(1000000)
471
+ })
472
+ });
473
+ const clientStates = clientId_to_chainIdName.clientStates.map(s => ({
474
+ clientState: Object.assign({}, tendermint_1.ClientState.decode(s.clientState.value)),
475
+ clientId: s.clientId,
476
+ }));
477
+ newBridges = bridges.map(bridge => {
478
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
479
+ const connection = channels_to_connection.channels.find(channel => channel.channelId === ("channel-" + (bridge.chainId.toNumber() - 1)));
480
+ const connectionId = connection === null || connection === void 0 ? void 0 : connection.connectionHops[0];
481
+ const src_channel = (_a = connection === null || connection === void 0 ? void 0 : connection.channelId) !== null && _a !== void 0 ? _a : "";
482
+ const dst_channel = (_c = (_b = connection === null || connection === void 0 ? void 0 : connection.counterparty) === null || _b === void 0 ? void 0 : _b.channelId) !== null && _c !== void 0 ? _c : "";
483
+ const cosmRegexArr = (_e = (_d = connection === null || connection === void 0 ? void 0 : connection.counterparty) === null || _d === void 0 ? void 0 : _d.portId) === null || _e === void 0 ? void 0 : _e.match(ibc_1.cosmBridgeRegex);
484
+ const portId = (_f = cosmRegexArr === null || cosmRegexArr === void 0 ? void 0 : cosmRegexArr[1]) !== null && _f !== void 0 ? _f : "transfer";
485
+ const clientId = (_g = connection_to_clientId.connections.find(connection => connection.id === connectionId)) === null || _g === void 0 ? void 0 : _g.clientId;
486
+ const chainIdName = (_j = ((_h = clientStates.find(client => client.clientId === clientId)) === null || _h === void 0 ? void 0 : _h.clientState)) === null || _j === void 0 ? void 0 : _j.chainId;
487
+ return Object.assign(Object.assign({}, bridge), { chain_id_name: chainIdName !== null && chainIdName !== void 0 ? chainIdName : "", channels: { src_channel, dst_channel, port_id: portId } });
488
+ });
489
+ }
490
+ finally {
491
+ const checkedBefore = new Array(newBridges.length).fill(false);
492
+ const chainMap = {};
493
+ for (let i = 0; i < newBridges.length; i++) {
494
+ if (checkedBefore[i])
495
+ continue;
496
+ const bridge = newBridges[i];
497
+ const chainId = bridge.chain_id_name;
498
+ if (chainMap[chainId]) {
499
+ const chainName = chainMap[chainId];
500
+ for (let j = i; j < newBridges.length; j++) {
501
+ const subBridge = newBridges[j];
502
+ if (subBridge.chain_id_name === chainId) {
503
+ subBridge.chainName = chainName;
504
+ checkedBefore[j] = true;
505
+ }
359
506
  }
360
507
  }
361
- });
508
+ else {
509
+ chainMap[chainId] = bridge.chainName;
510
+ }
511
+ }
512
+ return newBridges;
362
513
  }
363
- return this.tokens;
364
514
  });
365
515
  }
366
- getCarbonIbcTokens() {
367
- const swthTokens = ibc_1.swthIbcWhitelist.map((chainId) => {
368
- var _a, _b, _c;
369
- const blockchain = util_1.IBCUtils.BlockchainMap[chainId];
370
- const carbonBlockchain = util_1.IBCUtils.ChainIdBlockchainMap[blockchain !== null && blockchain !== void 0 ? blockchain : ""];
371
- const blockchainNum = (_a = util_1.BlockchainUtils.CHAIN_IDS[carbonBlockchain !== null && carbonBlockchain !== void 0 ? carbonBlockchain : ""]) !== null && _a !== void 0 ? _a : 0;
372
- const swthChannel = (_b = ibc_1.swthChannels[chainId]) === null || _b === void 0 ? void 0 : _b.ibc;
373
- const assetDenom = util_1.IBCUtils.makeIBCMinimalDenom((_c = swthChannel === null || swthChannel === void 0 ? void 0 : swthChannel.dstChannel) !== null && _c !== void 0 ? _c : "channel-0", KeplrAccount_1.default.SWTH_CURRENCY.coinMinimalDenom);
374
- return {
375
- id: assetDenom,
376
- creator: "",
377
- denom: assetDenom,
378
- name: "Carbon",
379
- symbol: KeplrAccount_1.default.SWTH_CURRENCY.coinDenom,
380
- decimals: new long_1.default(KeplrAccount_1.default.SWTH_CURRENCY.coinDecimals),
381
- bridgeId: new long_1.default(util_1.BlockchainUtils.BRIDGE_IDS.ibc),
382
- chainId: new long_1.default(blockchainNum),
383
- tokenAddress: "",
384
- bridgeAddress: "",
385
- isActive: true,
386
- isCollateral: false,
387
- };
516
+ getIbcBlockchainNames() {
517
+ return this.bridges.ibc.map(bridge => bridge.chainName);
518
+ }
519
+ getPolynetworkBlockchainNames() {
520
+ return this.bridges.polynetwork.map(bridge => bridge.chainName);
521
+ }
522
+ getAllBlockchainNames() {
523
+ return this.getIbcBlockchainNames().concat(this.getPolynetworkBlockchainNames());
524
+ }
525
+ getBridgesFromBridgeId(bridgeId) {
526
+ switch (bridgeId) {
527
+ case blockchain_1.BRIDGE_IDS.polynetwork:
528
+ return this.bridges.polynetwork;
529
+ case blockchain_1.BRIDGE_IDS.ibc:
530
+ return this.bridges.ibc;
531
+ default:
532
+ return this.bridges.polynetwork;
533
+ }
534
+ }
535
+ getIbcTokens() {
536
+ const ibcTokens = Object.values(this.tokens).reduce((prev, token) => {
537
+ const newPrev = prev;
538
+ if (token.bridgeId.toNumber() === blockchain_1.BRIDGE_IDS.ibc) {
539
+ newPrev[token.denom] = token;
540
+ }
541
+ return newPrev;
542
+ }, {});
543
+ return ibcTokens;
544
+ }
545
+ getPolyNetworkTokens() {
546
+ const polynetworkTokens = Object.values(this.tokens).reduce((prev, token) => {
547
+ const newPrev = prev;
548
+ if (token.bridgeId.toNumber() === blockchain_1.BRIDGE_IDS.polynetwork) {
549
+ newPrev[token.denom] = token;
550
+ }
551
+ return newPrev;
552
+ }, {});
553
+ return polynetworkTokens;
554
+ }
555
+ getBlockchainV2FromIDs(chainId, bridgeId) {
556
+ var _a, _b;
557
+ const chainIdNum = Number(chainId);
558
+ const bridgeIdNum = Number(bridgeId);
559
+ if ((chainIdNum === 5 && bridgeIdNum === 1) || (chainIdNum === 0 && bridgeIdNum === 2))
560
+ return "Carbon";
561
+ const bridgeList = this.getBridgesFromBridgeId(bridgeIdNum);
562
+ return (_b = (_a = bridgeList.find(bridge => bridge.chainId.toNumber() === chainIdNum)) === null || _a === void 0 ? void 0 : _a.chainName) !== null && _b !== void 0 ? _b : undefined;
563
+ }
564
+ getBridgeFromToken(token) {
565
+ if (!token || !token.bridgeId)
566
+ return undefined;
567
+ const bridgeList = this.getBridgesFromBridgeId(token.bridgeId.toNumber());
568
+ return bridgeList.find(bridge => token.chainId.equals(bridge.chainId));
569
+ }
570
+ getIbcChainIdFromToken(token) {
571
+ if (!token)
572
+ return undefined;
573
+ const bridge = this.getBridgeFromToken(token);
574
+ if (!bridge || !blockchain_1.isIbcBridge(bridge))
575
+ return undefined;
576
+ return bridge.chain_id_name;
577
+ }
578
+ getCarbonDenomTraces() {
579
+ return __awaiter(this, void 0, void 0, function* () {
580
+ // get swth on osmosis
581
+ const osmoTmClient = yield tendermint_rpc_1.Tendermint34Client.connect(network_1.publicRpcNodes.Osmosis);
582
+ const osmoClient = new stargate_1.QueryClient(osmoTmClient);
583
+ const osmosRpcClient = stargate_1.createProtobufRpcClient(osmoClient);
584
+ const osmoIbcClient = new query_1.QueryClientImpl(osmosRpcClient);
585
+ const osmoDenomTraces = yield osmoIbcClient.DenomTraces({
586
+ pagination: pagination_1.PageRequest.fromPartial({
587
+ limit: new long_1.default(1000000),
588
+ }),
589
+ });
590
+ const osmoSwthDenomTrace = osmoDenomTraces.denomTraces.filter((trace) => {
591
+ return trace.baseDenom === "swth";
592
+ });
593
+ return osmoSwthDenomTrace.reduce((prev, trace) => {
594
+ var _a;
595
+ const coinMinimalDenom = util_1.IBCUtils.makeIBCMinimalDenom(trace.path, trace.baseDenom);
596
+ const token = (_a = this.tokenForDenom(coinMinimalDenom)) !== null && _a !== void 0 ? _a : this.tokenForDenom(trace.baseDenom);
597
+ const traceExtended = Object.assign(Object.assign({}, trace), { token });
598
+ this.denomTraces[coinMinimalDenom] = traceExtended;
599
+ prev[coinMinimalDenom] = traceExtended;
600
+ return prev;
601
+ }, {});
388
602
  });
389
- return swthTokens;
390
603
  }
391
604
  getCdpUnderlyingToken(cdpDenom) {
392
605
  if (!this.cdpTokens[cdpDenom]) {
@@ -459,7 +672,7 @@ class TokenClient {
459
672
  processTokenPrices(tokenPrices) {
460
673
  return tokenPrices.reduce((prevPrices, price) => {
461
674
  const newPrev = prevPrices;
462
- newPrev[price.denom] = (0, number_1.bnOrZero)(price.twap).shiftedBy(-constant_1.decTypeDecimals);
675
+ newPrev[price.denom] = number_1.bnOrZero(price.twap).shiftedBy(-constant_1.decTypeDecimals);
463
676
  return newPrev;
464
677
  }, {});
465
678
  }
@@ -77,7 +77,7 @@ export declare class ZILClient {
77
77
  private walletProvider?;
78
78
  private constructor();
79
79
  static instance(opts: ZILClientOpts): CarbonSDK.ZILClient;
80
- getExternalBalances(sdk: CarbonSDK, address: string, whitelistDenoms?: string[]): Promise<TokensWithExternalBalance[]>;
80
+ getExternalBalances(sdk: CarbonSDK, address: string, whitelistDenoms?: string[], version?: string): Promise<TokensWithExternalBalance[]>;
81
81
  formatWithdrawalAddress(bech32Address: string): Promise<string>;
82
82
  private callContract;
83
83
  approveZRC2(params: ApproveZRC2Params): Promise<Transaction>;
@@ -69,15 +69,20 @@ class ZILClient {
69
69
  }
70
70
  return new ZILClient(configProvider, blockchain);
71
71
  }
72
- getExternalBalances(sdk, address, whitelistDenoms) {
72
+ getExternalBalances(sdk, address, whitelistDenoms, version = "V1") {
73
73
  return __awaiter(this, void 0, void 0, function* () {
74
74
  const tokenQueryResults = yield sdk.token.getAllTokens();
75
- const tokens = tokenQueryResults.filter((token) => (0, blockchain_1.blockchainForChainId)(token.chainId.toNumber(), sdk.network) == this.blockchain &&
76
- token.tokenAddress.length == 40 &&
77
- (!whitelistDenoms || whitelistDenoms.includes(token.denom)));
75
+ const tokens = tokenQueryResults.filter((token) => {
76
+ const isCorrectBlockchain = version === "V2"
77
+ ?
78
+ !!sdk.token.getBlockchainV2(token.denom) && (blockchain_1.BLOCKCHAIN_V2_TO_V1_MAPPING[sdk.token.getBlockchainV2(token.denom)] == this.blockchain)
79
+ :
80
+ blockchain_1.blockchainForChainId(token.chainId.toNumber(), sdk.network) == this.blockchain;
81
+ return isCorrectBlockchain && token.tokenAddress.length == 40 && (!whitelistDenoms || whitelistDenoms.includes(token.denom));
82
+ });
78
83
  const requests = tokens.map((token) => token.tokenAddress === zeroAddress
79
- ? (0, exports.balanceBatchRequest)(address.replace(/^0x/i, ""))
80
- : (0, exports.tokenBalanceBatchRequest)(token.tokenAddress, address));
84
+ ? exports.balanceBatchRequest(address.replace(/^0x/i, ""))
85
+ : exports.tokenBalanceBatchRequest(token.tokenAddress, address));
81
86
  const response = yield fetch(this.getProviderUrl(), {
82
87
  method: "post",
83
88
  headers: { "content-type": "application/json" },
@@ -102,7 +107,7 @@ class ZILClient {
102
107
  // if (!isValidAddress) {
103
108
  // throw new Error("invalid address")
104
109
  // }
105
- return (0, zilliqa_1.fromBech32Address)(bech32Address).toLowerCase().substr(2);
110
+ return zilliqa_1.fromBech32Address(bech32Address).toLowerCase().substr(2);
106
111
  });
107
112
  }
108
113
  // see examplesV2/zil_client.ts on how to confirm the transactions
@@ -148,7 +153,7 @@ class ZILClient {
148
153
  zilliqa = new zilliqa_1.Zilliqa(this.getProviderUrl());
149
154
  }
150
155
  const deployedContract = (this.walletProvider || zilliqa).contracts.at(contractAddress);
151
- const balanceAndNonceResp = yield zilliqa.blockchain.getBalance((0, generic_1.stripHexPrefix)(zilAddress));
156
+ const balanceAndNonceResp = yield zilliqa.blockchain.getBalance(generic_1.stripHexPrefix(zilAddress));
152
157
  if (balanceAndNonceResp.error !== undefined) {
153
158
  throw new Error(balanceAndNonceResp.error.message);
154
159
  }
@@ -166,7 +171,7 @@ class ZILClient {
166
171
  vname: "spender",
167
172
  type: "ByStr20",
168
173
  // TODO: Check if bridgeAddress corresponds to carbon token lock_proxy_hash
169
- value: spenderAddress !== null && spenderAddress !== void 0 ? spenderAddress : (0, generic_1.appendHexPrefix)(token.bridgeAddress),
174
+ value: spenderAddress !== null && spenderAddress !== void 0 ? spenderAddress : generic_1.appendHexPrefix(token.bridgeAddress),
170
175
  },
171
176
  {
172
177
  vname: "amount",
@@ -184,7 +189,7 @@ class ZILClient {
184
189
  }
185
190
  checkAllowanceZRC2(token, owner, spender) {
186
191
  return __awaiter(this, void 0, void 0, function* () {
187
- const contractAddress = (0, generic_1.appendHexPrefix)(token.tokenAddress);
192
+ const contractAddress = generic_1.appendHexPrefix(token.tokenAddress);
188
193
  const zilliqa = new zilliqa_1.Zilliqa(this.getProviderUrl());
189
194
  const resp = yield zilliqa.blockchain.getSmartContractSubState(contractAddress, "allowances", [owner, spender]);
190
195
  if (resp.error !== undefined) {
@@ -206,13 +211,13 @@ class ZILClient {
206
211
  }
207
212
  const carbonNetwork = networkConfig.network;
208
213
  const fromTokenId = fromToken.id;
209
- const fromTokenAddr = (0, generic_1.appendHexPrefix)(fromToken.tokenAddress);
214
+ const fromTokenAddr = generic_1.appendHexPrefix(fromToken.tokenAddress);
210
215
  const toTokenDenom = toToken.denom;
211
- const targetProxyHash = (0, generic_1.appendHexPrefix)(this.getTargetProxyHash(fromToken));
216
+ const targetProxyHash = generic_1.appendHexPrefix(this.getTargetProxyHash(fromToken));
212
217
  const recoveryAddressHex = ethers_1.ethers.utils.hexlify(index_1.AddressUtils.SWTHAddress.getAddressBytes(recoveryAddress, carbonNetwork));
213
218
  const fromAssetHash = ethers_1.ethers.utils.hexlify(ethers_1.ethers.utils.toUtf8Bytes(fromTokenId));
214
219
  const toAssetHash = ethers_1.ethers.utils.hexlify(ethers_1.ethers.utils.toUtf8Bytes(toTokenDenom));
215
- const feeAddress = (0, generic_1.appendHexPrefix)(networkConfig.feeAddress);
220
+ const feeAddress = generic_1.appendHexPrefix(networkConfig.feeAddress);
216
221
  const contractAddress = this.getBridgeEntranceAddr();
217
222
  let zilliqa;
218
223
  if (typeof signer === "string") {
@@ -227,7 +232,7 @@ class ZILClient {
227
232
  zilliqa = new zilliqa_1.Zilliqa(this.getProviderUrl());
228
233
  }
229
234
  const deployedContract = (this.walletProvider || zilliqa).contracts.at(contractAddress);
230
- const balanceAndNonceResp = yield zilliqa.blockchain.getBalance((0, generic_1.stripHexPrefix)(fromAddress));
235
+ const balanceAndNonceResp = yield zilliqa.blockchain.getBalance(generic_1.stripHexPrefix(fromAddress));
231
236
  if (balanceAndNonceResp.error !== undefined) {
232
237
  throw new Error(balanceAndNonceResp.error.message);
233
238
  }
@@ -304,13 +309,13 @@ class ZILClient {
304
309
  return __awaiter(this, void 0, void 0, function* () {
305
310
  const { address, amount, token, gasPrice, gasLimit, zilAddress, signer } = params;
306
311
  const networkConfig = this.getNetworkConfig();
307
- const assetId = (0, generic_1.appendHexPrefix)(token.tokenAddress);
308
- const targetProxyHash = (0, generic_1.appendHexPrefix)(this.getTargetProxyHash(token));
309
- const feeAddress = (0, generic_1.appendHexPrefix)(networkConfig.feeAddress);
312
+ const assetId = generic_1.appendHexPrefix(token.tokenAddress);
313
+ const targetProxyHash = generic_1.appendHexPrefix(this.getTargetProxyHash(token));
314
+ const feeAddress = generic_1.appendHexPrefix(networkConfig.feeAddress);
310
315
  const toAssetHash = ethers_1.ethers.utils.hexlify(ethers_1.ethers.utils.toUtf8Bytes(token.id));
311
316
  const swthAddress = ethers_1.ethers.utils.hexlify(address);
312
317
  // TODO: Check if bridgeAddress corresponds to carbon token lock_proxy_hash
313
- const contractAddress = (0, generic_1.appendHexPrefix)(token.bridgeAddress);
318
+ const contractAddress = generic_1.appendHexPrefix(token.bridgeAddress);
314
319
  let zilliqa;
315
320
  if (typeof signer === "string") {
316
321
  zilliqa = new zilliqa_1.Zilliqa(this.getProviderUrl());
@@ -324,7 +329,7 @@ class ZILClient {
324
329
  zilliqa = new zilliqa_1.Zilliqa(this.getProviderUrl());
325
330
  }
326
331
  const deployedContract = (this.walletProvider || zilliqa).contracts.at(contractAddress);
327
- const balanceAndNonceResp = yield zilliqa.blockchain.getBalance((0, generic_1.stripHexPrefix)(zilAddress));
332
+ const balanceAndNonceResp = yield zilliqa.blockchain.getBalance(generic_1.stripHexPrefix(zilAddress));
328
333
  if (balanceAndNonceResp.error !== undefined) {
329
334
  throw new Error(balanceAndNonceResp.error.message);
330
335
  }
@@ -409,7 +414,7 @@ class ZILClient {
409
414
  getTargetProxyHash(token) {
410
415
  const networkConfig = this.getNetworkConfig();
411
416
  const addressBytes = address_1.SWTHAddress.getAddressBytes(token.creator, networkConfig.network);
412
- const addressHex = (0, generic_1.stripHexPrefix)(ethers_1.ethers.utils.hexlify(addressBytes));
417
+ const addressHex = generic_1.stripHexPrefix(ethers_1.ethers.utils.hexlify(addressBytes));
413
418
  return addressHex;
414
419
  }
415
420
  getNetworkConfig() {
@@ -0,0 +1,23 @@
1
+ import Long from "long";
2
+ import _m0 from "protobufjs/minimal";
3
+ export declare const protobufPackage = "Switcheo.carbon.cdp";
4
+ export interface EModeCategory {
5
+ name: string;
6
+ denoms: string[];
7
+ loanToValue: string;
8
+ liquidationThreshold: string;
9
+ liquidationDiscount: string;
10
+ isActive: boolean;
11
+ }
12
+ export declare const EModeCategory: {
13
+ encode(message: EModeCategory, writer?: _m0.Writer): _m0.Writer;
14
+ decode(input: _m0.Reader | Uint8Array, length?: number | undefined): EModeCategory;
15
+ fromJSON(object: any): EModeCategory;
16
+ toJSON(message: EModeCategory): unknown;
17
+ fromPartial(object: DeepPartial<EModeCategory>): EModeCategory;
18
+ };
19
+ declare type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;
20
+ export declare type DeepPartial<T> = T extends Builtin ? T : T extends Long ? string | number | Long : T extends Array<infer U> ? Array<DeepPartial<U>> : T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>> : T extends {} ? {
21
+ [K in keyof T]?: DeepPartial<T[K]>;
22
+ } : Partial<T>;
23
+ export {};