@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.
- package/lib/index.js +89 -112
- 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
|
-
|
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
|
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
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
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,
|
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*/,
|
162
|
+
return [4 /*yield*/, getPools()];
|
154
163
|
case 2:
|
155
164
|
pools = _a.sent();
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
poolOut =
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
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
|
-
|
202
|
-
|
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:
|
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
|
-
|
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
|
-
|
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
|
-
|
254
|
-
|
255
|
-
throw
|
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
|
});
|