@pioneer-platform/chainflip-client 0.0.4 → 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.
Files changed (2) hide show
  1. package/lib/index.js +38 -24
  2. 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 = [
@@ -86,19 +86,45 @@ const get_quote = function (quote) {
86
86
  if (!networkSupport.includes(caipToNetworkId(quote.sellAsset))) {
87
87
  throw new Error("unsupported sellAsset");
88
88
  }
89
- //https://chainflip-swap.chainflip.io/quote?amount=100000000&srcChain=Bitcoin&srcAsset=BTC&destChain=Ethereum&destAsset=ETH
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);
90
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);
91
117
  const responseQuote = yield axios.get('https://chainflip-swap.chainflip.io/quote', {
92
- params: {
93
- amount: 100000000,
94
- srcChain: 'Bitcoin',
95
- srcAsset: 'BTC',
96
- destChain: 'Ethereum',
97
- destAsset: 'ETH'
98
- }
118
+ params
99
119
  });
100
- // console.log(responseQuote.data); // Handle the response data as needed
101
- //https://chainflip-swap.chainflip.io/trpc/openSwapDepositChannel?batch=1
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
102
128
  const url = 'https://chainflip-swap.chainflip.io/trpc/openSwapDepositChannel?batch=1';
103
129
  const data = {
104
130
  "0": {
@@ -133,22 +159,10 @@ const get_quote = function (quote) {
133
159
  // console.log(response.data); // Log the response data
134
160
  // console.log(JSON.stringify(response.data)); // Log the response data
135
161
  let result = response.data[0].result.data.json;
136
- // log.info(tag,"result: ",result)
162
+ log.info(tag, "result: ", result);
137
163
  output.id = result.id;
138
164
  output.source = 'chainflip';
139
165
  output.estimatedExpiryTime = result.estimatedExpiryTime;
140
- output.amountOut = responseQuote.data.egressAmount;
141
- if (caipToNetworkId(quote.buyAsset) == caipToNetworkId(ChainToNetworkId["BTC"])) {
142
- // Convert from satoshis to BTC
143
- output.amountOut /= 1e8;
144
- }
145
- else if (caipToNetworkId(quote.buyAsset) == caipToNetworkId(ChainToNetworkId["ETH"])) {
146
- // Convert from wei to ETH
147
- output.amountOut /= 1e18;
148
- }
149
- else {
150
- throw Error("Chain not supported!");
151
- }
152
166
  let tx = {
153
167
  type: "transfer",
154
168
  chain: caipToNetworkId(quote.sellAsset),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pioneer-platform/chainflip-client",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "main": "./lib/index.js",
5
5
  "types": "./lib/index.d.ts",
6
6
  "dependencies": {