@shapeshiftoss/swapper 6.0.0 → 6.2.0
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/dist/api.d.ts +2 -1
- package/dist/api.js +1 -0
- package/dist/swappers/thorchain/ThorchainSwapper.d.ts +2 -2
- package/dist/swappers/thorchain/ThorchainSwapper.js +15 -2
- package/dist/swappers/thorchain/utils/getTradeRate/getTradeRate.d.ts +4 -0
- package/dist/swappers/thorchain/utils/getTradeRate/getTradeRate.js +57 -0
- package/dist/swappers/utils/test-data/assets.d.ts +1 -0
- package/dist/swappers/utils/test-data/assets.js +15 -1
- package/package.json +3 -3
package/dist/api.d.ts
CHANGED
|
@@ -155,7 +155,8 @@ export declare enum SwapErrorTypes {
|
|
|
155
155
|
UNSUPPORTED_CHAIN = "UNSUPPORTED_CHAIN",
|
|
156
156
|
VALIDATION_FAILED = "VALIDATION_FAILED",
|
|
157
157
|
MAKE_MEMO_FAILED = "MAKE_MEMO_FAILED",
|
|
158
|
-
PRICE_RATIO_FAILED = "PRICE_RATIO_FAILED"
|
|
158
|
+
PRICE_RATIO_FAILED = "PRICE_RATIO_FAILED",
|
|
159
|
+
POOL_NOT_FOUND = "POOL_NOT_FOUND"
|
|
159
160
|
}
|
|
160
161
|
export interface Swapper {
|
|
161
162
|
/** perform any necessary async initialization */
|
package/dist/api.js
CHANGED
|
@@ -31,4 +31,5 @@ var SwapErrorTypes;
|
|
|
31
31
|
SwapErrorTypes["VALIDATION_FAILED"] = "VALIDATION_FAILED";
|
|
32
32
|
SwapErrorTypes["MAKE_MEMO_FAILED"] = "MAKE_MEMO_FAILED";
|
|
33
33
|
SwapErrorTypes["PRICE_RATIO_FAILED"] = "PRICE_RATIO_FAILED";
|
|
34
|
+
SwapErrorTypes["POOL_NOT_FOUND"] = "POOL_NOT_FOUND";
|
|
34
35
|
})(SwapErrorTypes = exports.SwapErrorTypes || (exports.SwapErrorTypes = {}));
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AssetId } from '@shapeshiftoss/caip';
|
|
2
2
|
import { Asset, SupportedChainIds } from '@shapeshiftoss/types';
|
|
3
|
-
import { ApprovalNeededOutput, BuyAssetBySellIdInput, Swapper, SwapperType, Trade, TradeQuote, TradeResult, TradeTxs } from '../../api';
|
|
3
|
+
import { ApprovalNeededOutput, BuyAssetBySellIdInput, ExecuteTradeInput, Swapper, SwapperType, Trade, TradeQuote, TradeResult, TradeTxs } from '../../api';
|
|
4
4
|
import { ThorchainSwapperDeps } from './types';
|
|
5
5
|
export declare class ThorchainSwapper implements Swapper {
|
|
6
6
|
private swapSupportedChainIds;
|
|
@@ -16,6 +16,6 @@ export declare class ThorchainSwapper implements Swapper {
|
|
|
16
16
|
filterAssetIdsBySellable(): AssetId[];
|
|
17
17
|
buildTrade(): Promise<Trade<SupportedChainIds>>;
|
|
18
18
|
getTradeQuote(): Promise<TradeQuote<SupportedChainIds>>;
|
|
19
|
-
executeTrade(): Promise<TradeResult>;
|
|
19
|
+
executeTrade(args: ExecuteTradeInput<SupportedChainIds>): Promise<TradeResult>;
|
|
20
20
|
getTradeTxs(): Promise<TradeTxs>;
|
|
21
21
|
}
|
|
@@ -58,8 +58,21 @@ class ThorchainSwapper {
|
|
|
58
58
|
async getTradeQuote() {
|
|
59
59
|
throw new Error('ThorchainSwapper: getTradeQuote unimplemented');
|
|
60
60
|
}
|
|
61
|
-
async executeTrade() {
|
|
62
|
-
|
|
61
|
+
async executeTrade(args) {
|
|
62
|
+
const { trade, wallet } = args;
|
|
63
|
+
const adapter = await this.deps.adapterManager.byChainId(trade.sellAsset.chainId);
|
|
64
|
+
if (trade.sellAsset.chainId === 'eip155:1') {
|
|
65
|
+
const thorTradeEth = trade;
|
|
66
|
+
const signedTx = await adapter.signTransaction({ txToSign: thorTradeEth.txData, wallet });
|
|
67
|
+
const txid = await adapter.broadcastTransaction(signedTx);
|
|
68
|
+
return { tradeId: txid };
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
throw new api_1.SwapError('[executeTrade]: unsupported trade', {
|
|
72
|
+
code: api_1.SwapErrorTypes.SIGN_AND_BROADCAST_FAILED,
|
|
73
|
+
fn: 'executeTrade'
|
|
74
|
+
});
|
|
75
|
+
}
|
|
63
76
|
}
|
|
64
77
|
async getTradeTxs() {
|
|
65
78
|
throw new Error('ThorchainSwapper: executeTrade unimplemented');
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { AssetId } from '@shapeshiftoss/caip';
|
|
2
|
+
import { Asset } from '@shapeshiftoss/types';
|
|
3
|
+
import { ThorchainSwapperDeps } from '../../types';
|
|
4
|
+
export declare const getTradeRate: (sellAsset: Asset, buyAssetId: AssetId, sellAmount: string, deps: ThorchainSwapperDeps) => Promise<string>;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getTradeRate = void 0;
|
|
4
|
+
const caip_1 = require("@shapeshiftoss/caip");
|
|
5
|
+
const api_1 = require("../../../../api");
|
|
6
|
+
const bignumber_1 = require("../../../utils/bignumber");
|
|
7
|
+
const makeTradeTx_1 = require("../ethereum/makeTradeTx");
|
|
8
|
+
const thorService_1 = require("../thorService");
|
|
9
|
+
const THOR_PRECISION = 8;
|
|
10
|
+
const getSwapOutput = (inputAmount, poolData, toRune) => {
|
|
11
|
+
const x = inputAmount;
|
|
12
|
+
const X = toRune ? poolData.assetBalance : poolData.runeBalance;
|
|
13
|
+
const Y = toRune ? poolData.runeBalance : poolData.assetBalance;
|
|
14
|
+
const numerator = x.times(X).times(Y);
|
|
15
|
+
const denominator = x.plus(X).pow(2);
|
|
16
|
+
return numerator.div(denominator);
|
|
17
|
+
};
|
|
18
|
+
const getDoubleSwapOutput = (input, inputPool, outputPool) => {
|
|
19
|
+
const runeToOutput = getSwapOutput(input, inputPool, true);
|
|
20
|
+
return getSwapOutput(runeToOutput, outputPool, false);
|
|
21
|
+
};
|
|
22
|
+
// https://docs.thorchain.org/how-it-works/prices
|
|
23
|
+
// TODO this does not support swaps between native "RUNE"
|
|
24
|
+
// Rune swaps use a different calculation because its 1 hop between pools instead of 2
|
|
25
|
+
const getTradeRate = async (sellAsset, buyAssetId, sellAmount, deps) => {
|
|
26
|
+
const buyPoolId = caip_1.adapters.assetIdToPoolAssetId({ assetId: buyAssetId });
|
|
27
|
+
const sellPoolId = caip_1.adapters.assetIdToPoolAssetId({ assetId: sellAsset.assetId });
|
|
28
|
+
if (!buyPoolId || !sellPoolId)
|
|
29
|
+
throw new api_1.SwapError(`[getPriceRatio]: No thorchain pool found`, {
|
|
30
|
+
code: api_1.SwapErrorTypes.POOL_NOT_FOUND,
|
|
31
|
+
fn: 'getPriceRatio',
|
|
32
|
+
details: { buyPoolId, sellPoolId }
|
|
33
|
+
});
|
|
34
|
+
const { data: responseData } = await thorService_1.thorService.get(`${deps.midgardUrl}/pools`);
|
|
35
|
+
const buyPool = responseData.find((response) => response.asset === buyPoolId);
|
|
36
|
+
const sellPool = responseData.find((response) => response.asset === sellPoolId);
|
|
37
|
+
if (!buyPool || !sellPool)
|
|
38
|
+
throw new api_1.SwapError(`[getPriceRatio]: no pools found for`, {
|
|
39
|
+
code: api_1.SwapErrorTypes.POOL_NOT_FOUND,
|
|
40
|
+
fn: 'getPriceRatio',
|
|
41
|
+
details: { buyPoolId, sellPoolId }
|
|
42
|
+
});
|
|
43
|
+
// All thorchain pool amounts are base 8 regardless of token precision
|
|
44
|
+
const sellBaseAmount = (0, bignumber_1.bn)((0, makeTradeTx_1.toBaseUnit)((0, makeTradeTx_1.fromBaseUnit)(sellAmount, sellAsset.precision), THOR_PRECISION));
|
|
45
|
+
const sellAssetPoolData = {
|
|
46
|
+
assetBalance: (0, bignumber_1.bn)(sellPool.assetDepth),
|
|
47
|
+
runeBalance: (0, bignumber_1.bn)(sellPool.runeDepth)
|
|
48
|
+
};
|
|
49
|
+
const buyAssetPoolData = {
|
|
50
|
+
assetBalance: (0, bignumber_1.bn)(buyPool.assetDepth),
|
|
51
|
+
runeBalance: (0, bignumber_1.bn)(buyPool.runeDepth)
|
|
52
|
+
};
|
|
53
|
+
const outputAmountBase8 = getDoubleSwapOutput(sellBaseAmount, sellAssetPoolData, buyAssetPoolData);
|
|
54
|
+
const outputAmount = (0, makeTradeTx_1.fromBaseUnit)(outputAmountBase8, THOR_PRECISION);
|
|
55
|
+
return (0, bignumber_1.bn)(outputAmount).div((0, makeTradeTx_1.fromBaseUnit)(sellAmount, sellAsset.precision)).toString();
|
|
56
|
+
};
|
|
57
|
+
exports.getTradeRate = getTradeRate;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ETH = exports.WBTC = exports.FOX = exports.WETH = exports.BTC = void 0;
|
|
3
|
+
exports.UNSUPPORTED = exports.ETH = exports.WBTC = exports.FOX = exports.WETH = exports.BTC = void 0;
|
|
4
4
|
const types_1 = require("@shapeshiftoss/types");
|
|
5
5
|
exports.BTC = {
|
|
6
6
|
assetId: 'bip122:000000000019d6689c085ae165831e93/slip44:0',
|
|
@@ -72,3 +72,17 @@ exports.ETH = {
|
|
|
72
72
|
explorerTxLink: 'https://etherscan.io/tx/',
|
|
73
73
|
explorerAddressLink: 'https://etherscan.io/address/'
|
|
74
74
|
};
|
|
75
|
+
exports.UNSUPPORTED = {
|
|
76
|
+
assetId: 'eip155:1/slip44:420',
|
|
77
|
+
chainId: 'eip155:1',
|
|
78
|
+
chain: types_1.ChainTypes.Ethereum,
|
|
79
|
+
network: types_1.NetworkTypes.MAINNET,
|
|
80
|
+
symbol: 'ETH',
|
|
81
|
+
name: 'Ethereum',
|
|
82
|
+
precision: 18,
|
|
83
|
+
color: '#FFFFFF',
|
|
84
|
+
icon: 'https://assets.coincap.io/assets/icons/eth@2x.png',
|
|
85
|
+
explorer: 'https://etherscan.io',
|
|
86
|
+
explorerTxLink: 'https://etherscan.io/tx/',
|
|
87
|
+
explorerAddressLink: 'https://etherscan.io/address/'
|
|
88
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shapeshiftoss/swapper",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.2.0",
|
|
4
4
|
"description": "TODO: description",
|
|
5
5
|
"repository": "https://github.com/shapeshift/lib",
|
|
6
6
|
"license": "MIT",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"peerDependencies": {
|
|
33
33
|
"@shapeshiftoss/asset-service": "^4.6.0",
|
|
34
34
|
"@shapeshiftoss/caip": "^5.2.2",
|
|
35
|
-
"@shapeshiftoss/chain-adapters": "
|
|
35
|
+
"@shapeshiftoss/chain-adapters": "^5.0.0",
|
|
36
36
|
"@shapeshiftoss/errors": "^1.1.1",
|
|
37
37
|
"@shapeshiftoss/hdwallet-core": "^1.23.0",
|
|
38
38
|
"@shapeshiftoss/types": "^4.4.0"
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@shapeshiftoss/asset-service": "^4.6.0",
|
|
42
42
|
"@shapeshiftoss/caip": "^5.2.2",
|
|
43
|
-
"@shapeshiftoss/chain-adapters": "
|
|
43
|
+
"@shapeshiftoss/chain-adapters": "^5.0.0",
|
|
44
44
|
"@shapeshiftoss/errors": "^1.1.1",
|
|
45
45
|
"@shapeshiftoss/hdwallet-core": "^1.23.0",
|
|
46
46
|
"@shapeshiftoss/types": "^4.4.0",
|