@pioneer-platform/chainflip-client 0.0.3 → 0.0.5
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 +58 -24
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -20,7 +20,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
20
20
|
const TAG = " | chainflip | ";
|
|
21
21
|
const { uuid } = require('uuidv4');
|
|
22
22
|
const log = require('@pioneer-platform/loggerdog')();
|
|
23
|
-
let { caipToNetworkId, shortListSymbolToCaip, ChainToNetworkId } = require("@pioneer-platform/pioneer-caip");
|
|
23
|
+
let { Chain, caipToNetworkId, shortListSymbolToCaip, ChainToNetworkId, NetworkIdToChain, getChainEnumValue } = require("@pioneer-platform/pioneer-caip");
|
|
24
24
|
const { createMemo, parseMemo } = require('@pioneer-platform/pioneer-coins');
|
|
25
25
|
const axios = require('axios');
|
|
26
26
|
let networkSupport = [
|
|
@@ -37,6 +37,26 @@ module.exports = {
|
|
|
37
37
|
getQuote: function (quote) {
|
|
38
38
|
return get_quote(quote);
|
|
39
39
|
},
|
|
40
|
+
lookupTx: function (quoteId) {
|
|
41
|
+
return get_quote_info(quoteId);
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
const get_quote_info = function (quoteId) {
|
|
45
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
46
|
+
let tag = TAG + " | get_quote_info | ";
|
|
47
|
+
try {
|
|
48
|
+
let output = {};
|
|
49
|
+
// Make a GET request using Axios
|
|
50
|
+
const response = yield axios.get(`https://chainflip-swap.chainflip.io/swaps/${quoteId}`);
|
|
51
|
+
// Assuming the response data is what you want to use
|
|
52
|
+
output.data = response.data;
|
|
53
|
+
return output;
|
|
54
|
+
}
|
|
55
|
+
catch (e) {
|
|
56
|
+
console.error(tag, "e: ", e);
|
|
57
|
+
throw e;
|
|
58
|
+
}
|
|
59
|
+
});
|
|
40
60
|
};
|
|
41
61
|
const get_quote = function (quote) {
|
|
42
62
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -66,19 +86,45 @@ const get_quote = function (quote) {
|
|
|
66
86
|
if (!networkSupport.includes(caipToNetworkId(quote.sellAsset))) {
|
|
67
87
|
throw new Error("unsupported sellAsset");
|
|
68
88
|
}
|
|
69
|
-
|
|
89
|
+
let chainSell = NetworkIdToChain[caipToNetworkId(quote.sellAsset)];
|
|
90
|
+
let chainBuy = NetworkIdToChain[caipToNetworkId(quote.buyAsset)];
|
|
91
|
+
log.info(tag, "chainSell: ", chainSell);
|
|
92
|
+
log.info(tag, "chainBuy: ", chainBuy);
|
|
93
|
+
let longNameSell = Object.keys(Chain).find(key => Chain[key] === chainSell);
|
|
94
|
+
let longNameBuy = Object.keys(Chain).find(key => Chain[key] === chainBuy);
|
|
95
|
+
log.info(tag, "chainSell: ", longNameSell);
|
|
96
|
+
log.info(tag, "chainBuy: ", longNameBuy);
|
|
97
|
+
const decimals = {
|
|
98
|
+
'BTC': 8, // Bitcoin uses 8 decimal places
|
|
99
|
+
'ETH': 18, // Ethereum uses 18 decimal places
|
|
100
|
+
};
|
|
101
|
+
// Helper function to scale the amount by the number of decimals
|
|
102
|
+
function scaleAmount(amount, decimals) {
|
|
103
|
+
return amount * Math.pow(10, decimals);
|
|
104
|
+
}
|
|
105
|
+
// Adjusting the amount according to the asset's decimals
|
|
106
|
+
const amountToQuote = scaleAmount(quote.sellAmount, decimals[chainSell]); // Assumes 1 unit of the selling asset
|
|
107
|
+
log.info(tag, "amountToQuote: ", amountToQuote);
|
|
70
108
|
//get quote
|
|
109
|
+
let params = {
|
|
110
|
+
amount: amountToQuote,
|
|
111
|
+
srcChain: longNameSell,
|
|
112
|
+
srcAsset: chainSell,
|
|
113
|
+
destChain: longNameBuy,
|
|
114
|
+
destAsset: chainBuy
|
|
115
|
+
};
|
|
116
|
+
log.info(tag, "params: ", params);
|
|
71
117
|
const responseQuote = yield axios.get('https://chainflip-swap.chainflip.io/quote', {
|
|
72
|
-
params
|
|
73
|
-
amount: 100000000,
|
|
74
|
-
srcChain: 'Bitcoin',
|
|
75
|
-
srcAsset: 'BTC',
|
|
76
|
-
destChain: 'Ethereum',
|
|
77
|
-
destAsset: 'ETH'
|
|
78
|
-
}
|
|
118
|
+
params
|
|
79
119
|
});
|
|
80
|
-
|
|
81
|
-
|
|
120
|
+
function convertToReadableAmount(amount, decimals) {
|
|
121
|
+
return amount / Math.pow(10, decimals);
|
|
122
|
+
}
|
|
123
|
+
console.log("responseQuote: ", responseQuote.data); // Handle the response data as needed
|
|
124
|
+
const egressAmountReadable = convertToReadableAmount(responseQuote.data.egressAmount, decimals[chainBuy]);
|
|
125
|
+
log.info(tag, "egressAmountReadable: ", egressAmountReadable);
|
|
126
|
+
output.amountOut = egressAmountReadable;
|
|
127
|
+
// //https://chainflip-swap.chainflip.io/trpc/openSwapDepositChannel?batch=1
|
|
82
128
|
const url = 'https://chainflip-swap.chainflip.io/trpc/openSwapDepositChannel?batch=1';
|
|
83
129
|
const data = {
|
|
84
130
|
"0": {
|
|
@@ -113,22 +159,10 @@ const get_quote = function (quote) {
|
|
|
113
159
|
// console.log(response.data); // Log the response data
|
|
114
160
|
// console.log(JSON.stringify(response.data)); // Log the response data
|
|
115
161
|
let result = response.data[0].result.data.json;
|
|
116
|
-
|
|
162
|
+
log.info(tag, "result: ", result);
|
|
117
163
|
output.id = result.id;
|
|
118
164
|
output.source = 'chainflip';
|
|
119
165
|
output.estimatedExpiryTime = result.estimatedExpiryTime;
|
|
120
|
-
output.amountOut = responseQuote.data.egressAmount;
|
|
121
|
-
if (caipToNetworkId(quote.buyAsset) == caipToNetworkId(ChainToNetworkId["BTC"])) {
|
|
122
|
-
// Convert from satoshis to BTC
|
|
123
|
-
output.amountOut /= 1e8;
|
|
124
|
-
}
|
|
125
|
-
else if (caipToNetworkId(quote.buyAsset) == caipToNetworkId(ChainToNetworkId["ETH"])) {
|
|
126
|
-
// Convert from wei to ETH
|
|
127
|
-
output.amountOut /= 1e18;
|
|
128
|
-
}
|
|
129
|
-
else {
|
|
130
|
-
throw Error("Chain not supported!");
|
|
131
|
-
}
|
|
132
166
|
let tx = {
|
|
133
167
|
type: "transfer",
|
|
134
168
|
chain: caipToNetworkId(quote.sellAsset),
|