@pioneer-platform/thorchain-client 0.0.29 → 0.0.30

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 (2) hide show
  1. package/lib/index.js +89 -112
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -41,10 +41,10 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
41
41
  };
42
42
  Object.defineProperty(exports, "__esModule", { value: true });
43
43
  var TAG = " | maya | ";
44
+ var types_1 = require("@coinmasters/types");
44
45
  var uuid = require('uuidv4').uuid;
45
46
  var log = require('@pioneer-platform/loggerdog')();
46
47
  var _a = require("@pioneer-platform/pioneer-caip"), caipToNetworkId = _a.caipToNetworkId, shortListSymbolToCaip = _a.shortListSymbolToCaip, ChainToNetworkId = _a.ChainToNetworkId;
47
- var network = require("@pioneer-platform/midgard-client");
48
48
  var _b = require('@pioneer-platform/pioneer-coins'), createMemo = _b.createMemo, parseMemo = _b.parseMemo;
49
49
  var networkSupport = [
50
50
  ChainToNetworkId["BTC"],
@@ -65,13 +65,16 @@ function nodeRequest(path) {
65
65
  return [4 /*yield*/, fetch("https://thornode.ninerealms.com".concat(path))];
66
66
  case 1:
67
67
  response = _a.sent();
68
+ if (!response.ok) {
69
+ throw new Error("Node request failed with status: ".concat(response.status, ", message: ").concat(response.statusText));
70
+ }
68
71
  return [4 /*yield*/, response.json()];
69
72
  case 2:
70
73
  data = _a.sent();
71
74
  return [2 /*return*/, data];
72
75
  case 3:
73
76
  error_1 = _a.sent();
74
- console.error('Error fetching from node:', error_1);
77
+ log.error("".concat(TAG, " Error fetching from node:"), error_1);
75
78
  throw error_1;
76
79
  case 4: return [2 /*return*/];
77
80
  }
@@ -96,40 +99,46 @@ module.exports = {
96
99
  return assets;
97
100
  },
98
101
  getQuote: function (quote) {
99
- return get_quote(quote);
102
+ return __awaiter(this, void 0, void 0, function () {
103
+ return __generator(this, function (_a) {
104
+ switch (_a.label) {
105
+ case 0: return [4 /*yield*/, get_quote(quote)];
106
+ case 1: return [2 /*return*/, _a.sent()];
107
+ }
108
+ });
109
+ });
100
110
  },
101
111
  };
102
- function quoteFromPool(sellAmount, assetPoolAmount, runePoolAmount, maxSlippage) {
103
- // Convert string inputs to numbers and scale the sell amount
104
- var swapAmount = parseFloat(sellAmount) * 1e8; // Assuming 1e6 is the scaling factor for Maya
105
- var assetDepth = parseFloat(assetPoolAmount);
106
- var runeDepth = parseFloat(runePoolAmount);
107
- // Calculate the constant product
108
- var k = assetDepth * runeDepth;
109
- // New amount of the asset in the pool after the swap
110
- var newAssetDepth = assetDepth + swapAmount;
111
- // Calculate the amount of Rune received (or the other asset in the pool)
112
- var newRuneDepth = k / newAssetDepth;
113
- var runeReceived = runeDepth - newRuneDepth;
114
- // Scale back down the amount of Rune received
115
- var scaledRuneReceived = runeReceived / 1e6; // Adjust as per Rune's scaling factor
116
- // Calculate the actual rate of the swap
117
- var actualRate = scaledRuneReceived / (swapAmount / 1e6);
118
- // Calculate the ideal rate
119
- var idealRate = runeDepth / assetDepth;
120
- // Calculate the slippage
121
- var slippage = ((idealRate - actualRate) / idealRate) * 100;
122
- // Calculate amountOutMin considering the maximum slippage
123
- var amountOutMin = scaledRuneReceived * (1 - maxSlippage / 100);
124
- return {
125
- amountOutMin: amountOutMin.toFixed(6).toString(),
126
- amountOut: scaledRuneReceived.toFixed(6),
127
- slippage: Math.max(slippage, 0).toFixed(6)
128
- };
112
+ function getPools() {
113
+ return __awaiter(this, void 0, void 0, function () {
114
+ var tag, pools, e_1;
115
+ return __generator(this, function (_a) {
116
+ switch (_a.label) {
117
+ case 0:
118
+ tag = TAG + " | getPools | ";
119
+ _a.label = 1;
120
+ case 1:
121
+ _a.trys.push([1, 3, , 4]);
122
+ return [4 /*yield*/, nodeRequest('/thorchain/pools')];
123
+ case 2:
124
+ pools = _a.sent();
125
+ if (!pools || pools.length === 0) {
126
+ throw new Error("No pools fetched from network!");
127
+ }
128
+ log.info(tag, "Pools fetched: ", pools.map(function (p) { return p.asset; }));
129
+ return [2 /*return*/, pools];
130
+ case 3:
131
+ e_1 = _a.sent();
132
+ log.error(tag, "Error fetching pools: ", e_1);
133
+ throw new Error("Unable to fetch pools");
134
+ case 4: return [2 /*return*/];
135
+ }
136
+ });
137
+ });
129
138
  }
130
139
  var get_quote = function (quote) {
131
140
  return __awaiter(this, void 0, void 0, function () {
132
- var tag, output, pools, poolIn, poolOut, BaseDecimal_1, asset, DECIMALS, BASE_UNIT, sellAmountInBaseUnits, quoteFromNode, URL_1, amountOutMin, inboundAddress, amountOutEstimated, memoInput, memo, type, chain, tx, e_1;
141
+ var tag, output, pools, poolIn, poolOut, sellAssetChain, DECIMALS, BASE_UNIT, sellAmountInBaseUnits, URL_1, quoteFromNode, amountOutMin, amountOutEstimated, memoInput, memo, txType, e_2;
133
142
  return __generator(this, function (_a) {
134
143
  switch (_a.label) {
135
144
  case 0:
@@ -150,109 +159,77 @@ var get_quote = function (quote) {
150
159
  throw new Error("missing recipientAddress");
151
160
  if (!quote.slippage)
152
161
  throw new Error("missing slippage");
153
- return [4 /*yield*/, network.getPools()];
162
+ return [4 /*yield*/, getPools()];
154
163
  case 2:
155
164
  pools = _a.sent();
156
- if (!pools)
157
- throw Error("Unable to get pools from network!");
158
- log.info(tag, "pools: ", pools);
159
- poolIn = pools.find(function (p) { return p.asset == quote.sellAsset; });
160
- log.info(tag, "poolIn: ", poolIn);
161
- poolOut = pools.find(function (p) { return p.asset == quote.buyAsset; });
162
- output.meta = {
163
- quoteMode: "TC_SUPPORTED_TO_TC_SUPPORTED"
164
- };
165
- output.steps = 1;
166
- output.complete = true;
167
- output.source = 'thorchain';
168
- output.id = uuid();
169
- BaseDecimal_1 = {
170
- ARB: 18,
171
- AVAX: 18,
172
- BCH: 8,
173
- BNB: 8,
174
- BSC: 18,
175
- BTC: 8,
176
- DASH: 8,
177
- DGB: 8,
178
- DOGE: 8,
179
- ETH: 18,
180
- BASE: 18,
181
- EOS: 6,
182
- GAIA: 6,
183
- KUJI: 6,
184
- LTC: 8,
185
- MATIC: 18,
186
- MAYA: 10,
187
- OP: 18,
188
- OSMO: 6,
189
- XRP: 6,
190
- THOR: 8,
191
- ZEC: 8
192
- };
193
- asset = quote.sellAsset.split(".")[0];
194
- if (!asset)
195
- throw Error("unable to pasre asset from quote.sellAsset");
196
- DECIMALS = BaseDecimal_1[asset];
197
- if (!DECIMALS)
198
- throw Error("unable to get DECIMALS for asset: " + asset);
165
+ poolIn = pools.find(function (p) { return p.asset === quote.sellAsset; });
166
+ if (!poolIn) {
167
+ log.error(tag, "Pool for sellAsset (".concat(quote.sellAsset, ") not found."));
168
+ throw new Error("Pool for sellAsset (".concat(quote.sellAsset, ") not found."));
169
+ }
170
+ poolOut = null;
171
+ // Skip poolOut lookup if buyAsset is RUNE/THOR
172
+ if (quote.buyAsset === 'THOR.RUNE' || quote.buyAsset === 'RUNE.RUNE') {
173
+ log.info(tag, "BuyAsset (".concat(quote.buyAsset, ") is RUNE/THOR. Skipping poolOut lookup."));
174
+ }
175
+ else {
176
+ poolOut = pools.find(function (p) { return p.asset === quote.buyAsset; });
177
+ if (!poolOut) {
178
+ log.error(tag, "Pool for buyAsset (".concat(quote.buyAsset, ") not found."));
179
+ throw new Error("Pool for buyAsset (".concat(quote.buyAsset, ") not found."));
180
+ }
181
+ }
182
+ log.info(tag, "poolIn: ", poolIn, "poolOut: ", poolOut || 'N/A (RUNE/THOR as buyAsset)');
183
+ sellAssetChain = quote.sellAsset.split(".")[0];
184
+ DECIMALS = Number(types_1.BaseDecimal[sellAssetChain]);
185
+ if (isNaN(DECIMALS))
186
+ throw new Error("Invalid DECIMALS value for asset: ".concat(sellAssetChain));
199
187
  BASE_UNIT = Math.pow(10, DECIMALS);
200
188
  sellAmountInBaseUnits = parseFloat(quote.sellAmount) * BASE_UNIT;
201
- quoteFromNode = void 0;
202
- URL_1 = "/quote/swap?from_asset=".concat(quote.sellAsset, "&to_asset=").concat(quote.buyAsset, "&amount=").concat(sellAmountInBaseUnits, "&destination=").concat(quote.recipientAddress);
203
- log.info("URL: ", URL_1);
189
+ URL_1 = "/thorchain/quote/swap?from_asset=".concat(quote.sellAsset, "&to_asset=").concat(quote.buyAsset, "&amount=").concat(sellAmountInBaseUnits, "&destination=").concat(quote.recipientAddress);
190
+ log.info(tag, "URL: ", URL_1);
204
191
  return [4 /*yield*/, nodeRequest(URL_1)];
205
192
  case 3:
206
193
  quoteFromNode = _a.sent();
207
- log.info("quoteFromNode: ", quoteFromNode);
208
194
  if (quoteFromNode.error)
209
- throw Error(quoteFromNode.error);
195
+ throw new Error(quoteFromNode.error);
210
196
  amountOutMin = quoteFromNode.amount_out_min;
211
- inboundAddress = quoteFromNode.inbound_address;
212
197
  amountOutEstimated = (parseInt(quoteFromNode.expected_amount_out) / BASE_UNIT).toFixed(DECIMALS);
213
198
  output.amountOut = amountOutEstimated;
214
199
  memoInput = {
215
200
  type: 'SWAP',
216
201
  asset: quote.buyAsset,
217
202
  destAddr: quote.recipientAddress,
218
- lim: null,
219
- interval: null,
220
- quantity: null,
221
- affiliate: null,
222
- fee: null,
223
- dexAggregatorAddr: null,
224
- finalAssetAddr: null,
225
- minAmountOut: null
203
+ lim: amountOutMin,
226
204
  };
227
205
  memo = createMemo(memoInput);
228
206
  log.info(tag, "memo: ", memo);
229
- type = void 0;
230
- chain = quote.sellAsset.split(".")[0];
231
- if (chain == "MAYA") {
232
- type = 'deposit';
233
- }
234
- else {
235
- type = 'transfer';
236
- }
237
- tx = {
238
- type: type,
239
- chain: ChainToNetworkId[quote.sellAsset.split(".")[0]],
240
- txParams: {
241
- senderAddress: quote.senderAddress,
242
- recipientAddress: quoteFromNode.inbound_address,
243
- amount: quote.sellAmount,
244
- token: quote.sellAsset.split(".")[1],
245
- memo: quoteFromNode.memo || memo
246
- }
247
- };
207
+ txType = sellAssetChain === "MAYA" ? 'deposit' : 'transfer';
248
208
  output.txs = [
249
- tx
209
+ {
210
+ type: txType,
211
+ chain: ChainToNetworkId[sellAssetChain],
212
+ txParams: {
213
+ senderAddress: quote.senderAddress,
214
+ recipientAddress: quoteFromNode.inbound_address,
215
+ amount: quote.sellAmount,
216
+ token: quote.sellAsset.split(".")[1],
217
+ memo: quoteFromNode.memo || memo,
218
+ },
219
+ },
250
220
  ];
221
+ output.meta = {
222
+ quoteMode: "TC_SUPPORTED_TO_TC_SUPPORTED",
223
+ };
224
+ output.steps = 1;
225
+ output.complete = true;
226
+ output.source = 'thorchain';
227
+ output.id = uuid();
251
228
  return [2 /*return*/, output];
252
229
  case 4:
253
- e_1 = _a.sent();
254
- console.error(tag, "e: ", e_1);
255
- throw e_1;
230
+ e_2 = _a.sent();
231
+ log.error(tag, "Error: ", e_2);
232
+ throw e_2;
256
233
  case 5: return [2 /*return*/];
257
234
  }
258
235
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pioneer-platform/thorchain-client",
3
- "version": "0.0.29",
3
+ "version": "0.0.30",
4
4
  "main": "./lib/index.js",
5
5
  "types": "./lib/index.d.ts",
6
6
  "dependencies": {