@shapeshiftoss/swapper 5.7.0 → 5.8.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/swappercli.js +2 -1
- package/dist/swappers/thorchain/types.d.ts +10 -0
- package/dist/swappers/thorchain/utils/ethereum/makeTradeTx.d.ts +23 -0
- package/dist/swappers/thorchain/utils/ethereum/makeTradeTx.js +81 -0
- package/dist/swappers/thorchain/utils/getPriceRatio/getPriceRatio.d.ts +6 -0
- package/dist/swappers/thorchain/utils/getPriceRatio/getPriceRatio.js +38 -0
- package/package.json +1 -1
package/dist/api.d.ts
CHANGED
|
@@ -153,7 +153,8 @@ export declare enum SwapErrorTypes {
|
|
|
153
153
|
USD_RATE_FAILED = "USD_RATE_FAILED",
|
|
154
154
|
UNSUPPORTED_CHAIN = "UNSUPPORTED_CHAIN",
|
|
155
155
|
VALIDATION_FAILED = "VALIDATION_FAILED",
|
|
156
|
-
MAKE_MEMO_FAILED = "MAKE_MEMO_FAILED"
|
|
156
|
+
MAKE_MEMO_FAILED = "MAKE_MEMO_FAILED",
|
|
157
|
+
PRICE_RATIO_FAILED = "PRICE_RATIO_FAILED"
|
|
157
158
|
}
|
|
158
159
|
export interface Swapper {
|
|
159
160
|
/** perform any necessary async initialization */
|
package/dist/api.js
CHANGED
|
@@ -30,4 +30,5 @@ var SwapErrorTypes;
|
|
|
30
30
|
SwapErrorTypes["UNSUPPORTED_CHAIN"] = "UNSUPPORTED_CHAIN";
|
|
31
31
|
SwapErrorTypes["VALIDATION_FAILED"] = "VALIDATION_FAILED";
|
|
32
32
|
SwapErrorTypes["MAKE_MEMO_FAILED"] = "MAKE_MEMO_FAILED";
|
|
33
|
+
SwapErrorTypes["PRICE_RATIO_FAILED"] = "PRICE_RATIO_FAILED";
|
|
33
34
|
})(SwapErrorTypes = exports.SwapErrorTypes || (exports.SwapErrorTypes = {}));
|
package/dist/swappercli.js
CHANGED
|
@@ -84,7 +84,8 @@ const main = async () => {
|
|
|
84
84
|
const web3 = new web3_1.default(web3Provider);
|
|
85
85
|
const zrxSwapperDeps = { wallet, adapterManager, web3 };
|
|
86
86
|
const thorchainSwapperDeps = {
|
|
87
|
-
midgardUrl: 'https://midgard.thorchain.info/v2'
|
|
87
|
+
midgardUrl: 'https://midgard.thorchain.info/v2',
|
|
88
|
+
adapterManager: {}
|
|
88
89
|
};
|
|
89
90
|
const manager = new SwapperManager_1.SwapperManager();
|
|
90
91
|
const zrxSwapper = new ZrxSwapper_1.ZrxSwapper(zrxSwapperDeps);
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ChainAdapterManager } from '@shapeshiftoss/chain-adapters';
|
|
1
2
|
export declare type MidgardResponse = {
|
|
2
3
|
asset: string;
|
|
3
4
|
assetDepth: string;
|
|
@@ -12,6 +13,15 @@ export declare type MidgardResponse = {
|
|
|
12
13
|
units: string;
|
|
13
14
|
volume24h: string;
|
|
14
15
|
};
|
|
16
|
+
export declare type InboundResponse = {
|
|
17
|
+
chain: string;
|
|
18
|
+
pub_key: string;
|
|
19
|
+
address: string;
|
|
20
|
+
halted: boolean;
|
|
21
|
+
gas_rate: string;
|
|
22
|
+
router?: string;
|
|
23
|
+
};
|
|
15
24
|
export declare type ThorchainSwapperDeps = {
|
|
16
25
|
midgardUrl: string;
|
|
26
|
+
adapterManager: ChainAdapterManager;
|
|
17
27
|
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { ethereum } from '@shapeshiftoss/chain-adapters';
|
|
2
|
+
import { ETHSignTx, HDWallet } from '@shapeshiftoss/hdwallet-core';
|
|
3
|
+
import { Asset, BIP44Params } from '@shapeshiftoss/types';
|
|
4
|
+
import BigNumber from 'bignumber.js';
|
|
5
|
+
import { ThorchainSwapperDeps } from '../../types';
|
|
6
|
+
export declare const fromBaseUnit: (value: BigNumber.Value, decimals: number, displayDecimals?: number) => string;
|
|
7
|
+
export declare const toBaseUnit: (amount: BigNumber.Value | undefined, precision: number) => string;
|
|
8
|
+
export declare const makeTradeTx: ({ wallet, bip44Params, sellAmount, buyAsset, sellAsset, destinationAddress, adapter, maxFeePerGas, maxPriorityFeePerGas, slippageTolerance, deps, gasLimit }: {
|
|
9
|
+
wallet: HDWallet;
|
|
10
|
+
bip44Params: BIP44Params;
|
|
11
|
+
sellAmount: string;
|
|
12
|
+
buyAsset: Asset;
|
|
13
|
+
sellAsset: Asset;
|
|
14
|
+
destinationAddress: string;
|
|
15
|
+
adapter: ethereum.ChainAdapter;
|
|
16
|
+
maxFeePerGas: string;
|
|
17
|
+
maxPriorityFeePerGas: string;
|
|
18
|
+
slippageTolerance: string;
|
|
19
|
+
deps: ThorchainSwapperDeps;
|
|
20
|
+
gasLimit: string;
|
|
21
|
+
}) => Promise<{
|
|
22
|
+
txToSign: ETHSignTx;
|
|
23
|
+
}>;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.makeTradeTx = exports.toBaseUnit = exports.fromBaseUnit = void 0;
|
|
7
|
+
const caip_1 = require("@shapeshiftoss/caip");
|
|
8
|
+
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
9
|
+
const api_1 = require("../../../../api");
|
|
10
|
+
const bignumber_1 = require("../../../zrx/utils/bignumber");
|
|
11
|
+
const getPriceRatio_1 = require("../getPriceRatio/getPriceRatio");
|
|
12
|
+
const makeSwapMemo_1 = require("../makeSwapMemo/makeSwapMemo");
|
|
13
|
+
const thorService_1 = require("../thorService");
|
|
14
|
+
const routerCalldata_1 = require("./routerCalldata");
|
|
15
|
+
const fromBaseUnit = (value, decimals, displayDecimals = 6) => {
|
|
16
|
+
return (0, bignumber_1.bnOrZero)(value)
|
|
17
|
+
.div(`1e+${decimals}`)
|
|
18
|
+
.decimalPlaces(displayDecimals, bignumber_js_1.default.ROUND_DOWN)
|
|
19
|
+
.toString();
|
|
20
|
+
};
|
|
21
|
+
exports.fromBaseUnit = fromBaseUnit;
|
|
22
|
+
const toBaseUnit = (amount, precision) => {
|
|
23
|
+
return (0, bignumber_1.bnOrZero)(amount)
|
|
24
|
+
.times(new bignumber_js_1.default(10).exponentiatedBy((0, bignumber_1.bnOrZero)(precision)))
|
|
25
|
+
.toFixed(0);
|
|
26
|
+
};
|
|
27
|
+
exports.toBaseUnit = toBaseUnit;
|
|
28
|
+
const makeTradeTx = async ({ wallet, bip44Params, sellAmount, buyAsset, sellAsset, destinationAddress, adapter, maxFeePerGas, maxPriorityFeePerGas, slippageTolerance, deps, gasLimit }) => {
|
|
29
|
+
try {
|
|
30
|
+
const { assetReference } = (0, caip_1.fromAssetId)(sellAsset.assetId);
|
|
31
|
+
const isErc20Trade = assetReference.startsWith('0x');
|
|
32
|
+
const { data: inboundAddresses } = await thorService_1.thorService.get(`${deps.midgardUrl}/thorchain/inbound_addresses`);
|
|
33
|
+
const ethInboundAddresses = inboundAddresses.find((inbound) => inbound.chain === 'ETH');
|
|
34
|
+
const vault = ethInboundAddresses === null || ethInboundAddresses === void 0 ? void 0 : ethInboundAddresses.address;
|
|
35
|
+
const router = ethInboundAddresses === null || ethInboundAddresses === void 0 ? void 0 : ethInboundAddresses.router;
|
|
36
|
+
if (!vault || !router)
|
|
37
|
+
throw new api_1.SwapError(`[getPriceRatio]: router or vault found for ETH`, {
|
|
38
|
+
code: api_1.SwapErrorTypes.RESPONSE_ERROR,
|
|
39
|
+
details: { inboundAddresses }
|
|
40
|
+
});
|
|
41
|
+
const priceRatio = await (0, getPriceRatio_1.getPriceRatio)(deps, {
|
|
42
|
+
buyAssetId: buyAsset.assetId,
|
|
43
|
+
sellAssetId: sellAsset.assetId
|
|
44
|
+
});
|
|
45
|
+
const expectedBuyAmount = (0, exports.toBaseUnit)((0, exports.fromBaseUnit)((0, bignumber_1.bnOrZero)(sellAmount).dividedBy(priceRatio), sellAsset.precision), buyAsset.precision);
|
|
46
|
+
if (!(0, bignumber_1.bnOrZero)(expectedBuyAmount).gte(0) ||
|
|
47
|
+
!((0, bignumber_1.bnOrZero)(slippageTolerance).gte(0) && (0, bignumber_1.bnOrZero)(slippageTolerance).lte(1)))
|
|
48
|
+
throw new api_1.SwapError('[makeTradeTx]: bad expected buy amount or bad slippage tolerance', {
|
|
49
|
+
code: api_1.SwapErrorTypes.BUILD_TRADE_FAILED
|
|
50
|
+
});
|
|
51
|
+
const limit = (0, bignumber_1.bnOrZero)(expectedBuyAmount)
|
|
52
|
+
.times(new bignumber_js_1.default(1).minus(slippageTolerance))
|
|
53
|
+
.decimalPlaces(0)
|
|
54
|
+
.toString();
|
|
55
|
+
const memo = (0, makeSwapMemo_1.makeSwapMemo)({
|
|
56
|
+
buyAssetId: buyAsset.assetId,
|
|
57
|
+
destinationAddress,
|
|
58
|
+
limit
|
|
59
|
+
});
|
|
60
|
+
const data = await (0, routerCalldata_1.deposit)(vault, router, isErc20Trade ? assetReference : '0x0000000000000000000000000000000000000000', sellAmount, memo);
|
|
61
|
+
return adapter.buildCustomTx({
|
|
62
|
+
wallet,
|
|
63
|
+
bip44Params,
|
|
64
|
+
to: router,
|
|
65
|
+
gasLimit,
|
|
66
|
+
maxFeePerGas,
|
|
67
|
+
maxPriorityFeePerGas,
|
|
68
|
+
value: isErc20Trade ? '0' : sellAmount,
|
|
69
|
+
data
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
catch (e) {
|
|
73
|
+
if (e instanceof api_1.SwapError)
|
|
74
|
+
throw e;
|
|
75
|
+
throw new api_1.SwapError('[makeTradeTx]: error making trade tx', {
|
|
76
|
+
code: api_1.SwapErrorTypes.BUILD_TRADE_FAILED,
|
|
77
|
+
cause: e
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
exports.makeTradeTx = makeTradeTx;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getPriceRatio = void 0;
|
|
4
|
+
const caip_1 = require("@shapeshiftoss/caip");
|
|
5
|
+
const api_1 = require("../../../../api");
|
|
6
|
+
const bignumber_1 = require("../../../zrx/utils/bignumber");
|
|
7
|
+
const thorService_1 = require("../thorService");
|
|
8
|
+
const getPriceRatio = async (deps, input) => {
|
|
9
|
+
var _a, _b;
|
|
10
|
+
const { buyAssetId, sellAssetId } = input;
|
|
11
|
+
try {
|
|
12
|
+
const buyPoolId = caip_1.adapters.assetIdToPoolAssetId({ assetId: buyAssetId });
|
|
13
|
+
const sellPoolId = caip_1.adapters.assetIdToPoolAssetId({ assetId: sellAssetId });
|
|
14
|
+
if (!buyPoolId || !sellPoolId)
|
|
15
|
+
throw new api_1.SwapError(`[getPriceRatio]: No thorchain pool found`, {
|
|
16
|
+
code: api_1.SwapErrorTypes.RESPONSE_ERROR,
|
|
17
|
+
details: { buyPoolId, sellPoolId }
|
|
18
|
+
});
|
|
19
|
+
const { data: responseData } = await thorService_1.thorService.get(`${deps.midgardUrl}/pools`);
|
|
20
|
+
const buyUsdPrice = (_a = responseData.find((response) => response.asset === buyPoolId)) === null || _a === void 0 ? void 0 : _a.assetPrice;
|
|
21
|
+
const sellUsdPrice = (_b = responseData.find((response) => response.asset === sellPoolId)) === null || _b === void 0 ? void 0 : _b.assetPrice;
|
|
22
|
+
if (!buyUsdPrice || !sellUsdPrice)
|
|
23
|
+
throw new api_1.SwapError(`[getPriceRatio]: No rate found for pools`, {
|
|
24
|
+
code: api_1.SwapErrorTypes.RESPONSE_ERROR,
|
|
25
|
+
details: { buyPoolId, sellPoolId }
|
|
26
|
+
});
|
|
27
|
+
return (0, bignumber_1.bnOrZero)(buyUsdPrice).dividedBy(sellUsdPrice).toString();
|
|
28
|
+
}
|
|
29
|
+
catch (e) {
|
|
30
|
+
if (e instanceof api_1.SwapError)
|
|
31
|
+
throw e;
|
|
32
|
+
throw new api_1.SwapError('[getUsdRate]: Thorchain getUsdRate failed', {
|
|
33
|
+
code: api_1.SwapErrorTypes.PRICE_RATIO_FAILED,
|
|
34
|
+
cause: e
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
exports.getPriceRatio = getPriceRatio;
|