@pioneer-platform/thorchain-client 0.0.28 → 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 +99 -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,19 +65,29 @@ 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
|
}
|
78
81
|
});
|
79
82
|
});
|
80
83
|
}
|
84
|
+
var assets = [
|
85
|
+
'bip122:000000000019d6689c085ae165831e93/slip44:0', // BTC
|
86
|
+
'eip155:1/slip44:60', // ETH
|
87
|
+
'cosmos:thorchain-mainnet-v1/slip44:931', // RUNE
|
88
|
+
'bip122:00000000001a91e3dace36e2be3bf030/slip44:3', // Doge
|
89
|
+
'bip122:000000000000000000651ef99cb9fcbe/slip44:145', // BCH
|
90
|
+
];
|
81
91
|
module.exports = {
|
82
92
|
init: function (settings) {
|
83
93
|
return true;
|
@@ -85,41 +95,50 @@ module.exports = {
|
|
85
95
|
networkSupport: function () {
|
86
96
|
return networkSupport;
|
87
97
|
},
|
98
|
+
assetSupport: function () {
|
99
|
+
return assets;
|
100
|
+
},
|
88
101
|
getQuote: function (quote) {
|
89
|
-
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
|
+
});
|
90
110
|
},
|
91
111
|
};
|
92
|
-
function
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
};
|
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
|
+
});
|
119
138
|
}
|
120
139
|
var get_quote = function (quote) {
|
121
140
|
return __awaiter(this, void 0, void 0, function () {
|
122
|
-
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;
|
123
142
|
return __generator(this, function (_a) {
|
124
143
|
switch (_a.label) {
|
125
144
|
case 0:
|
@@ -140,109 +159,77 @@ var get_quote = function (quote) {
|
|
140
159
|
throw new Error("missing recipientAddress");
|
141
160
|
if (!quote.slippage)
|
142
161
|
throw new Error("missing slippage");
|
143
|
-
return [4 /*yield*/,
|
162
|
+
return [4 /*yield*/, getPools()];
|
144
163
|
case 2:
|
145
164
|
pools = _a.sent();
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
poolOut =
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
DOGE: 8,
|
169
|
-
ETH: 18,
|
170
|
-
BASE: 18,
|
171
|
-
EOS: 6,
|
172
|
-
GAIA: 6,
|
173
|
-
KUJI: 6,
|
174
|
-
LTC: 8,
|
175
|
-
MATIC: 18,
|
176
|
-
MAYA: 10,
|
177
|
-
OP: 18,
|
178
|
-
OSMO: 6,
|
179
|
-
XRP: 6,
|
180
|
-
THOR: 8,
|
181
|
-
ZEC: 8
|
182
|
-
};
|
183
|
-
asset = quote.sellAsset.split(".")[0];
|
184
|
-
if (!asset)
|
185
|
-
throw Error("unable to pasre asset from quote.sellAsset");
|
186
|
-
DECIMALS = BaseDecimal_1[asset];
|
187
|
-
if (!DECIMALS)
|
188
|
-
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));
|
189
187
|
BASE_UNIT = Math.pow(10, DECIMALS);
|
190
188
|
sellAmountInBaseUnits = parseFloat(quote.sellAmount) * BASE_UNIT;
|
191
|
-
|
192
|
-
|
193
|
-
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);
|
194
191
|
return [4 /*yield*/, nodeRequest(URL_1)];
|
195
192
|
case 3:
|
196
193
|
quoteFromNode = _a.sent();
|
197
|
-
log.info("quoteFromNode: ", quoteFromNode);
|
198
194
|
if (quoteFromNode.error)
|
199
|
-
throw Error(quoteFromNode.error);
|
195
|
+
throw new Error(quoteFromNode.error);
|
200
196
|
amountOutMin = quoteFromNode.amount_out_min;
|
201
|
-
inboundAddress = quoteFromNode.inbound_address;
|
202
197
|
amountOutEstimated = (parseInt(quoteFromNode.expected_amount_out) / BASE_UNIT).toFixed(DECIMALS);
|
203
198
|
output.amountOut = amountOutEstimated;
|
204
199
|
memoInput = {
|
205
200
|
type: 'SWAP',
|
206
201
|
asset: quote.buyAsset,
|
207
202
|
destAddr: quote.recipientAddress,
|
208
|
-
lim:
|
209
|
-
interval: null,
|
210
|
-
quantity: null,
|
211
|
-
affiliate: null,
|
212
|
-
fee: null,
|
213
|
-
dexAggregatorAddr: null,
|
214
|
-
finalAssetAddr: null,
|
215
|
-
minAmountOut: null
|
203
|
+
lim: amountOutMin,
|
216
204
|
};
|
217
205
|
memo = createMemo(memoInput);
|
218
206
|
log.info(tag, "memo: ", memo);
|
219
|
-
|
220
|
-
chain = quote.sellAsset.split(".")[0];
|
221
|
-
if (chain == "MAYA") {
|
222
|
-
type = 'deposit';
|
223
|
-
}
|
224
|
-
else {
|
225
|
-
type = 'transfer';
|
226
|
-
}
|
227
|
-
tx = {
|
228
|
-
type: type,
|
229
|
-
chain: ChainToNetworkId[quote.sellAsset.split(".")[0]],
|
230
|
-
txParams: {
|
231
|
-
senderAddress: quote.senderAddress,
|
232
|
-
recipientAddress: quoteFromNode.inbound_address,
|
233
|
-
amount: quote.sellAmount,
|
234
|
-
token: quote.sellAsset.split(".")[1],
|
235
|
-
memo: quoteFromNode.memo || memo
|
236
|
-
}
|
237
|
-
};
|
207
|
+
txType = sellAssetChain === "MAYA" ? 'deposit' : 'transfer';
|
238
208
|
output.txs = [
|
239
|
-
|
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
|
+
},
|
240
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();
|
241
228
|
return [2 /*return*/, output];
|
242
229
|
case 4:
|
243
|
-
|
244
|
-
|
245
|
-
throw
|
230
|
+
e_2 = _a.sent();
|
231
|
+
log.error(tag, "Error: ", e_2);
|
232
|
+
throw e_2;
|
246
233
|
case 5: return [2 /*return*/];
|
247
234
|
}
|
248
235
|
});
|