@shapeshiftoss/swapper 6.2.0 → 7.0.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 +28 -30
- package/dist/manager/SwapperManager.d.ts +8 -7
- package/dist/swappercli.js +38 -20
- package/dist/swappers/cow/CowSwapper.d.ts +8 -8
- package/dist/swappers/test/TestSwapper.d.ts +5 -5
- package/dist/swappers/thorchain/ThorchainSwapper.d.ts +7 -7
- package/dist/swappers/thorchain/ThorchainSwapper.js +9 -5
- package/dist/swappers/thorchain/getThorTradeQuote/getTradeQuote.d.ts +7 -0
- package/dist/swappers/thorchain/getThorTradeQuote/getTradeQuote.js +98 -0
- package/dist/swappers/thorchain/utils/constants.d.ts +1 -0
- package/dist/swappers/thorchain/utils/constants.js +4 -0
- package/dist/swappers/thorchain/utils/ethereum/makeTradeTx.d.ts +0 -3
- package/dist/swappers/thorchain/utils/ethereum/makeTradeTx.js +9 -50
- package/dist/swappers/thorchain/utils/ethereum/utils/getThorTxData.d.ts +15 -0
- package/dist/swappers/thorchain/utils/ethereum/utils/getThorTxData.js +55 -0
- package/dist/swappers/thorchain/utils/getTradeRate/getTradeRate.js +4 -4
- package/dist/swappers/thorchain/utils/txFeeHelpers/ethTxFees/getEthTxFees.d.ts +15 -0
- package/dist/swappers/thorchain/utils/txFeeHelpers/ethTxFees/getEthTxFees.js +44 -0
- package/dist/swappers/utils/bignumber.d.ts +2 -0
- package/dist/swappers/utils/bignumber.js +14 -1
- package/dist/swappers/utils/constants.d.ts +2 -0
- package/dist/swappers/utils/constants.js +5 -0
- package/dist/swappers/utils/helpers/helpers.d.ts +7 -0
- package/dist/swappers/utils/helpers/helpers.js +13 -0
- package/dist/swappers/utils/test-data/assets.js +0 -13
- package/dist/swappers/zrx/ZrxApprovalNeeded/ZrxApprovalNeeded.d.ts +1 -1
- package/dist/swappers/zrx/ZrxApprovalNeeded/ZrxApprovalNeeded.js +2 -3
- package/dist/swappers/zrx/ZrxApproveInfinite/ZrxApproveInfinite.d.ts +1 -2
- package/dist/swappers/zrx/ZrxApproveInfinite/ZrxApproveInfinite.js +2 -2
- package/dist/swappers/zrx/ZrxSwapper.d.ts +7 -7
- package/dist/swappers/zrx/getZrxTradeQuote/getZrxTradeQuote.js +6 -5
- package/dist/swappers/zrx/utils/constants.d.ts +0 -4
- package/dist/swappers/zrx/utils/constants.js +1 -5
- package/dist/swappers/zrx/utils/helpers/helpers.d.ts +7 -16
- package/dist/swappers/zrx/utils/helpers/helpers.js +3 -16
- package/dist/swappers/zrx/utils/test-data/setupSwapQuote.d.ts +3 -3
- package/dist/swappers/zrx/utils/test-data/setupSwapQuote.js +5 -5
- package/dist/swappers/zrx/utils/test-data/setupZrxDeps.d.ts +3 -29
- package/dist/swappers/zrx/utils/test-data/setupZrxDeps.js +34 -32
- package/dist/swappers/zrx/zrxBuildTrade/zrxBuildTrade.d.ts +1 -2
- package/dist/swappers/zrx/zrxBuildTrade/zrxBuildTrade.js +11 -10
- package/dist/swappers/zrx/zrxExecuteTrade/zrxExecuteTrade.d.ts +1 -1
- package/dist/swappers/zrx/zrxExecuteTrade/zrxExecuteTrade.js +2 -4
- package/package.json +10 -10
package/dist/api.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { AssetId } from '@shapeshiftoss/caip';
|
|
1
|
+
import { AssetId, ChainId } from '@shapeshiftoss/caip';
|
|
2
2
|
import { BTCSignTx, ETHSignTx, HDWallet } from '@shapeshiftoss/hdwallet-core';
|
|
3
|
-
import { Asset, ChainSpecific,
|
|
3
|
+
import { Asset, ChainSpecific, KnownChainIds } from '@shapeshiftoss/types';
|
|
4
4
|
export declare const SwapError: {
|
|
5
5
|
new <V extends {
|
|
6
6
|
cause?: unknown;
|
|
@@ -23,10 +23,7 @@ export declare const SwapError: {
|
|
|
23
23
|
prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined;
|
|
24
24
|
stackTraceLimit: number;
|
|
25
25
|
};
|
|
26
|
-
|
|
27
|
-
assetIds: AssetId[];
|
|
28
|
-
};
|
|
29
|
-
declare type ChainSpecificQuoteFeeData<T1> = ChainSpecific<T1, {
|
|
26
|
+
declare type ChainSpecificQuoteFeeData<T extends ChainId> = ChainSpecific<T, {
|
|
30
27
|
'eip155:1': {
|
|
31
28
|
estimatedGas?: string;
|
|
32
29
|
gasPrice?: string;
|
|
@@ -38,10 +35,10 @@ declare type ChainSpecificQuoteFeeData<T1> = ChainSpecific<T1, {
|
|
|
38
35
|
satsPerByte: string;
|
|
39
36
|
};
|
|
40
37
|
}>;
|
|
41
|
-
export declare type QuoteFeeData<
|
|
38
|
+
export declare type QuoteFeeData<T extends ChainId> = {
|
|
42
39
|
fee: string;
|
|
43
40
|
tradeFee: string;
|
|
44
|
-
} & ChainSpecificQuoteFeeData<
|
|
41
|
+
} & ChainSpecificQuoteFeeData<T>;
|
|
45
42
|
export declare type ByPairInput = {
|
|
46
43
|
sellAssetId: AssetId;
|
|
47
44
|
buyAssetId: AssetId;
|
|
@@ -58,15 +55,16 @@ export declare type CommonTradeInput = {
|
|
|
58
55
|
buyAsset: Asset;
|
|
59
56
|
sellAmount: string;
|
|
60
57
|
sendMax: boolean;
|
|
61
|
-
|
|
58
|
+
sellAssetAccountNumber: number;
|
|
59
|
+
wallet?: HDWallet;
|
|
62
60
|
};
|
|
63
61
|
export declare type GetTradeQuoteInput = CommonTradeInput;
|
|
64
62
|
export declare type BuildTradeInput = CommonTradeInput & {
|
|
65
|
-
|
|
63
|
+
buyAssetAccountNumber: number;
|
|
66
64
|
slippage?: string;
|
|
67
65
|
wallet: HDWallet;
|
|
68
66
|
};
|
|
69
|
-
interface TradeBase<C extends
|
|
67
|
+
interface TradeBase<C extends ChainId> {
|
|
70
68
|
buyAmount: string;
|
|
71
69
|
sellAmount: string;
|
|
72
70
|
feeData: QuoteFeeData<C>;
|
|
@@ -74,41 +72,41 @@ interface TradeBase<C extends SupportedChainIds> {
|
|
|
74
72
|
sources: Array<SwapSource>;
|
|
75
73
|
buyAsset: Asset;
|
|
76
74
|
sellAsset: Asset;
|
|
77
|
-
|
|
75
|
+
sellAssetAccountNumber: number;
|
|
78
76
|
}
|
|
79
|
-
export interface TradeQuote<C extends
|
|
77
|
+
export interface TradeQuote<C extends ChainId> extends TradeBase<C> {
|
|
80
78
|
allowanceContract: string;
|
|
81
79
|
minimum: string;
|
|
82
80
|
maximum: string;
|
|
83
81
|
}
|
|
84
|
-
export interface Trade<C extends
|
|
82
|
+
export interface Trade<C extends ChainId> extends TradeBase<C> {
|
|
85
83
|
receiveAddress: string;
|
|
86
84
|
}
|
|
87
|
-
export interface ZrxTrade
|
|
85
|
+
export interface ZrxTrade extends Trade<'eip155:1'> {
|
|
88
86
|
txData: string;
|
|
89
87
|
depositAddress: string;
|
|
90
88
|
}
|
|
91
|
-
export interface BtcThorTrade<C extends
|
|
92
|
-
chainId:
|
|
89
|
+
export interface BtcThorTrade<C extends ChainId> extends Trade<C> {
|
|
90
|
+
chainId: KnownChainIds.BitcoinMainnet;
|
|
93
91
|
txData: BTCSignTx;
|
|
94
92
|
}
|
|
95
|
-
export interface EthThorTrade<C extends
|
|
96
|
-
chainId:
|
|
93
|
+
export interface EthThorTrade<C extends ChainId> extends Trade<C> {
|
|
94
|
+
chainId: KnownChainIds.EthereumMainnet;
|
|
97
95
|
txData: ETHSignTx;
|
|
98
96
|
}
|
|
99
|
-
export declare type ThorTrade<C extends
|
|
100
|
-
export declare type ExecuteTradeInput<C extends
|
|
97
|
+
export declare type ThorTrade<C extends ChainId> = BtcThorTrade<C> | EthThorTrade<C>;
|
|
98
|
+
export declare type ExecuteTradeInput<C extends ChainId> = {
|
|
101
99
|
trade: Trade<C>;
|
|
102
100
|
wallet: HDWallet;
|
|
103
101
|
};
|
|
104
102
|
export declare type TradeResult = {
|
|
105
103
|
tradeId: string;
|
|
106
104
|
};
|
|
107
|
-
export declare type ApproveInfiniteInput<C extends
|
|
105
|
+
export declare type ApproveInfiniteInput<C extends ChainId> = {
|
|
108
106
|
quote: TradeQuote<C>;
|
|
109
107
|
wallet: HDWallet;
|
|
110
108
|
};
|
|
111
|
-
export declare type ApprovalNeededInput<C extends
|
|
109
|
+
export declare type ApprovalNeededInput<C extends ChainId> = {
|
|
112
110
|
quote: TradeQuote<C>;
|
|
113
111
|
wallet: HDWallet;
|
|
114
112
|
};
|
|
@@ -158,7 +156,7 @@ export declare enum SwapErrorTypes {
|
|
|
158
156
|
PRICE_RATIO_FAILED = "PRICE_RATIO_FAILED",
|
|
159
157
|
POOL_NOT_FOUND = "POOL_NOT_FOUND"
|
|
160
158
|
}
|
|
161
|
-
export interface Swapper {
|
|
159
|
+
export interface Swapper<T extends ChainId, TxType = unknown> {
|
|
162
160
|
/** perform any necessary async initialization */
|
|
163
161
|
initialize(): Promise<void>;
|
|
164
162
|
/** Returns the swapper type */
|
|
@@ -166,11 +164,11 @@ export interface Swapper {
|
|
|
166
164
|
/**
|
|
167
165
|
* Get builds a trade with definitive rate & txData that can be executed with executeTrade
|
|
168
166
|
**/
|
|
169
|
-
buildTrade(args: BuildTradeInput): Promise<Trade<
|
|
167
|
+
buildTrade(args: BuildTradeInput): Promise<Trade<T>>;
|
|
170
168
|
/**
|
|
171
169
|
* Get a trade quote
|
|
172
170
|
*/
|
|
173
|
-
getTradeQuote(input: GetTradeQuoteInput): Promise<TradeQuote<
|
|
171
|
+
getTradeQuote(input: GetTradeQuoteInput): Promise<TradeQuote<T>>;
|
|
174
172
|
/**
|
|
175
173
|
* Get the usd rate from either the assets symbol or tokenId
|
|
176
174
|
*/
|
|
@@ -178,15 +176,15 @@ export interface Swapper {
|
|
|
178
176
|
/**
|
|
179
177
|
* Execute a trade built with buildTrade by signing and broadcasting
|
|
180
178
|
*/
|
|
181
|
-
executeTrade(args: ExecuteTradeInput<
|
|
179
|
+
executeTrade(args: ExecuteTradeInput<T>): Promise<TradeResult>;
|
|
182
180
|
/**
|
|
183
181
|
* Get a boolean if a quote needs approval
|
|
184
182
|
*/
|
|
185
|
-
approvalNeeded(args: ApprovalNeededInput<
|
|
183
|
+
approvalNeeded(args: ApprovalNeededInput<T>): Promise<ApprovalNeededOutput>;
|
|
186
184
|
/**
|
|
187
185
|
* Get the txid of an approve infinite transaction
|
|
188
186
|
*/
|
|
189
|
-
approveInfinite(args: ApproveInfiniteInput<
|
|
187
|
+
approveInfinite(args: ApproveInfiniteInput<T>): Promise<string>;
|
|
190
188
|
/**
|
|
191
189
|
* Get supported buyAssetId's by sellAssetId
|
|
192
190
|
*/
|
|
@@ -195,6 +193,6 @@ export interface Swapper {
|
|
|
195
193
|
* Get supported sell assetIds
|
|
196
194
|
*/
|
|
197
195
|
filterAssetIdsBySellable(assetIds: AssetId[]): AssetId[];
|
|
198
|
-
getTradeTxs(tradeResult: TradeResult): Promise<
|
|
196
|
+
getTradeTxs(tradeResult: TradeResult): Promise<TxType>;
|
|
199
197
|
}
|
|
200
198
|
export {};
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { ChainId } from '@shapeshiftoss/caip';
|
|
1
2
|
import { BuyAssetBySellIdInput, ByPairInput, SupportedSellAssetsInput, Swapper } from '..';
|
|
2
3
|
import { SwapperType } from '../api';
|
|
3
4
|
export declare class SwapperManager {
|
|
4
|
-
swappers: Map<SwapperType, Swapper
|
|
5
|
+
swappers: Map<SwapperType, Swapper<ChainId>>;
|
|
5
6
|
constructor();
|
|
6
7
|
/**
|
|
7
8
|
*
|
|
@@ -9,27 +10,27 @@ export declare class SwapperManager {
|
|
|
9
10
|
* @param swapperInstance swapper instance {Swapper}
|
|
10
11
|
* @returns {SwapperManager}
|
|
11
12
|
*/
|
|
12
|
-
addSwapper(swapperType: SwapperType, swapperInstance: Swapper): this;
|
|
13
|
+
addSwapper(swapperType: SwapperType, swapperInstance: Swapper<ChainId>): this;
|
|
13
14
|
/**
|
|
14
15
|
*
|
|
15
16
|
* @param swapperType swapper type {SwapperType|string}
|
|
16
17
|
* @returns {Swapper}
|
|
17
18
|
* @deprecated this will be removed, currently used in swapper tests
|
|
18
19
|
*/
|
|
19
|
-
getSwapper(swapperType: SwapperType): Swapper
|
|
20
|
+
getSwapper(swapperType: SwapperType): Swapper<ChainId>;
|
|
20
21
|
/**
|
|
21
22
|
*
|
|
22
23
|
* @param swapperType swapper type {SwapperType|string}
|
|
23
24
|
* @returns {SwapperManager}
|
|
24
25
|
*/
|
|
25
26
|
removeSwapper(swapperType: SwapperType): this;
|
|
26
|
-
getBestSwapper(args: ByPairInput): Promise<Swapper | undefined>;
|
|
27
|
+
getBestSwapper(args: ByPairInput): Promise<Swapper<ChainId> | undefined>;
|
|
27
28
|
/**
|
|
28
29
|
*
|
|
29
30
|
* @param pair type {GetQuoteInput}
|
|
30
31
|
* @returns {SwapperType}
|
|
31
32
|
*/
|
|
32
|
-
getSwappersByPair(pair: ByPairInput): Swapper[];
|
|
33
|
-
getSupportedBuyAssetIdsFromSellId(args: BuyAssetBySellIdInput):
|
|
34
|
-
getSupportedSellableAssetIds(args: SupportedSellAssetsInput):
|
|
33
|
+
getSwappersByPair(pair: ByPairInput): Swapper<ChainId>[];
|
|
34
|
+
getSupportedBuyAssetIdsFromSellId(args: BuyAssetBySellIdInput): import("@shapeshiftoss/caip").AssetId[];
|
|
35
|
+
getSupportedSellableAssetIds(args: SupportedSellAssetsInput): import("@shapeshiftoss/caip").AssetId[];
|
|
35
36
|
}
|
package/dist/swappercli.js
CHANGED
|
@@ -1,4 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
2
25
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
26
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
27
|
};
|
|
@@ -6,14 +29,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
29
|
const asset_service_1 = require("@shapeshiftoss/asset-service");
|
|
7
30
|
const chain_adapters_1 = require("@shapeshiftoss/chain-adapters");
|
|
8
31
|
const hdwallet_native_1 = require("@shapeshiftoss/hdwallet-native");
|
|
9
|
-
const
|
|
32
|
+
const unchained = __importStar(require("@shapeshiftoss/unchained-client"));
|
|
10
33
|
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
11
34
|
const dotenv_1 = __importDefault(require("dotenv"));
|
|
12
35
|
const readline_sync_1 = __importDefault(require("readline-sync"));
|
|
13
36
|
const web3_1 = __importDefault(require("web3"));
|
|
14
37
|
const api_1 = require("./api");
|
|
15
38
|
const SwapperManager_1 = require("./manager/SwapperManager");
|
|
16
|
-
const ThorchainSwapper_1 = require("./swappers/thorchain/ThorchainSwapper");
|
|
17
39
|
const ZrxSwapper_1 = require("./swappers/zrx/ZrxSwapper");
|
|
18
40
|
dotenv_1.default.config();
|
|
19
41
|
const { UNCHAINED_HTTP_API = 'http://localhost:31300', UNCHAINED_WS_API = 'wss://localhost:31300', ETH_NODE_URL = 'http://localhost:3000', DEVICE_ID = 'device123', MNEMONIC = 'all all all all all all all all all all all all' } = process.env;
|
|
@@ -62,26 +84,21 @@ const main = async () => {
|
|
|
62
84
|
}
|
|
63
85
|
// Swapper Deps
|
|
64
86
|
const wallet = await getWallet();
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
87
|
+
const ethChainAdapter = new chain_adapters_1.ethereum.ChainAdapter({
|
|
88
|
+
providers: {
|
|
89
|
+
ws: new unchained.ws.Client(UNCHAINED_WS_API),
|
|
90
|
+
http: new unchained.ethereum.V1Api(new unchained.ethereum.Configuration({
|
|
91
|
+
basePath: UNCHAINED_HTTP_API
|
|
92
|
+
}))
|
|
93
|
+
},
|
|
94
|
+
rpcUrl: 'https://mainnet.infura.io/v3/d734c7eebcdf400185d7eb67322a7e57'
|
|
95
|
+
});
|
|
72
96
|
const web3Provider = new web3_1.default.providers.HttpProvider(ETH_NODE_URL);
|
|
73
97
|
const web3 = new web3_1.default(web3Provider);
|
|
74
|
-
const zrxSwapperDeps = { wallet,
|
|
75
|
-
const thorchainSwapperDeps = {
|
|
76
|
-
midgardUrl: 'https://midgard.thorchain.info/v2',
|
|
77
|
-
adapterManager: {}
|
|
78
|
-
};
|
|
98
|
+
const zrxSwapperDeps = { wallet, adapter: ethChainAdapter, web3 };
|
|
79
99
|
const manager = new SwapperManager_1.SwapperManager();
|
|
80
100
|
const zrxSwapper = new ZrxSwapper_1.ZrxSwapper(zrxSwapperDeps);
|
|
81
|
-
const thorchainSwapper = new ThorchainSwapper_1.ThorchainSwapper(thorchainSwapperDeps);
|
|
82
|
-
await thorchainSwapper.initialize();
|
|
83
101
|
manager.addSwapper(api_1.SwapperType.Zrx, zrxSwapper);
|
|
84
|
-
manager.addSwapper(api_1.SwapperType.Thorchain, thorchainSwapper);
|
|
85
102
|
const swapper = await manager.getBestSwapper({
|
|
86
103
|
sellAssetId: 'eip155:1/slip44:60',
|
|
87
104
|
buyAssetId: 'eip155:1/erc20:0xc770eefad204b5180df6a14ee197d99d808ee52d'
|
|
@@ -95,7 +112,8 @@ const main = async () => {
|
|
|
95
112
|
sellAsset,
|
|
96
113
|
buyAsset,
|
|
97
114
|
sellAmount: sellAmountBase,
|
|
98
|
-
|
|
115
|
+
sellAssetAccountNumber: 0,
|
|
116
|
+
wallet,
|
|
99
117
|
sendMax: false
|
|
100
118
|
});
|
|
101
119
|
}
|
|
@@ -115,8 +133,8 @@ const main = async () => {
|
|
|
115
133
|
sendMax: false,
|
|
116
134
|
sellAmount: sellAmountBase,
|
|
117
135
|
sellAsset,
|
|
118
|
-
|
|
119
|
-
|
|
136
|
+
sellAssetAccountNumber: 0,
|
|
137
|
+
buyAssetAccountNumber: 0
|
|
120
138
|
});
|
|
121
139
|
const txid = await swapper.executeTrade({ trade, wallet });
|
|
122
140
|
console.info('broadcast tx with id: ', txid);
|
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import { AssetId } from '@shapeshiftoss/caip';
|
|
2
|
-
import { Asset
|
|
1
|
+
import { AssetId, ChainId } from '@shapeshiftoss/caip';
|
|
2
|
+
import { Asset } from '@shapeshiftoss/types';
|
|
3
3
|
import { ApprovalNeededInput, ApprovalNeededOutput, ApproveInfiniteInput, BuildTradeInput, BuyAssetBySellIdInput, ExecuteTradeInput, GetTradeQuoteInput, Swapper, SwapperType, Trade, TradeQuote, TradeResult, TradeTxs } from '../../api';
|
|
4
4
|
export declare type CowSwapperDeps = {
|
|
5
5
|
apiUrl: string;
|
|
6
6
|
};
|
|
7
|
-
export declare class CowSwapper implements Swapper {
|
|
7
|
+
export declare class CowSwapper implements Swapper<ChainId> {
|
|
8
8
|
static swapperName: string;
|
|
9
9
|
deps: CowSwapperDeps;
|
|
10
10
|
constructor(deps: CowSwapperDeps);
|
|
11
11
|
initialize(): Promise<void>;
|
|
12
12
|
getType(): SwapperType;
|
|
13
|
-
buildTrade(args: BuildTradeInput): Promise<Trade<
|
|
14
|
-
getTradeQuote(input: GetTradeQuoteInput): Promise<TradeQuote<
|
|
13
|
+
buildTrade(args: BuildTradeInput): Promise<Trade<ChainId>>;
|
|
14
|
+
getTradeQuote(input: GetTradeQuoteInput): Promise<TradeQuote<ChainId>>;
|
|
15
15
|
getUsdRate(input: Asset): Promise<string>;
|
|
16
|
-
executeTrade(args: ExecuteTradeInput<
|
|
17
|
-
approvalNeeded(args: ApprovalNeededInput<
|
|
18
|
-
approveInfinite(args: ApproveInfiniteInput<
|
|
16
|
+
executeTrade(args: ExecuteTradeInput<ChainId>): Promise<TradeResult>;
|
|
17
|
+
approvalNeeded(args: ApprovalNeededInput<ChainId>): Promise<ApprovalNeededOutput>;
|
|
18
|
+
approveInfinite(args: ApproveInfiniteInput<ChainId>): Promise<string>;
|
|
19
19
|
filterBuyAssetsBySellAssetId(args: BuyAssetBySellIdInput): AssetId[];
|
|
20
20
|
filterAssetIdsBySellable(assetIds: AssetId[]): AssetId[];
|
|
21
21
|
getTradeTxs(): Promise<TradeTxs>;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { AssetId } from '@shapeshiftoss/caip';
|
|
2
|
-
import { Asset
|
|
1
|
+
import { AssetId, ChainId } from '@shapeshiftoss/caip';
|
|
2
|
+
import { Asset } from '@shapeshiftoss/types';
|
|
3
3
|
import { ApprovalNeededOutput, BuyAssetBySellIdInput, Swapper, SwapperType, Trade, TradeQuote, TradeResult, TradeTxs } from '../../api';
|
|
4
4
|
/**
|
|
5
5
|
* Playground for testing different scenarios of multiple swappers in the manager.
|
|
6
6
|
* Meant for local testing only
|
|
7
7
|
*/
|
|
8
|
-
export declare class TestSwapper implements Swapper {
|
|
8
|
+
export declare class TestSwapper implements Swapper<ChainId> {
|
|
9
9
|
supportAssets: string[];
|
|
10
10
|
initialize(): Promise<void>;
|
|
11
11
|
getType(): SwapperType;
|
|
@@ -15,8 +15,8 @@ export declare class TestSwapper implements Swapper {
|
|
|
15
15
|
approveInfinite(): Promise<string>;
|
|
16
16
|
filterBuyAssetsBySellAssetId(args: BuyAssetBySellIdInput): AssetId[];
|
|
17
17
|
filterAssetIdsBySellable(): AssetId[];
|
|
18
|
-
buildTrade(): Promise<Trade<
|
|
19
|
-
getTradeQuote(): Promise<TradeQuote<
|
|
18
|
+
buildTrade(): Promise<Trade<ChainId>>;
|
|
19
|
+
getTradeQuote(): Promise<TradeQuote<ChainId>>;
|
|
20
20
|
executeTrade(): Promise<TradeResult>;
|
|
21
21
|
getTradeTxs(): Promise<TradeTxs>;
|
|
22
22
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { AssetId } from '@shapeshiftoss/caip';
|
|
2
|
-
import { Asset
|
|
3
|
-
import { ApprovalNeededOutput, BuyAssetBySellIdInput, ExecuteTradeInput, Swapper, SwapperType, Trade, TradeQuote, TradeResult, TradeTxs } from '../../api';
|
|
1
|
+
import { AssetId, ChainId } from '@shapeshiftoss/caip';
|
|
2
|
+
import type { Asset } from '@shapeshiftoss/types';
|
|
3
|
+
import { ApprovalNeededOutput, BuyAssetBySellIdInput, ExecuteTradeInput, GetTradeQuoteInput, Swapper, SwapperType, Trade, TradeQuote, TradeResult, TradeTxs } from '../../api';
|
|
4
4
|
import { ThorchainSwapperDeps } from './types';
|
|
5
|
-
export declare class ThorchainSwapper implements Swapper {
|
|
5
|
+
export declare class ThorchainSwapper implements Swapper<ChainId> {
|
|
6
6
|
private swapSupportedChainIds;
|
|
7
7
|
private supportedAssetIds;
|
|
8
8
|
deps: ThorchainSwapperDeps;
|
|
@@ -14,8 +14,8 @@ export declare class ThorchainSwapper implements Swapper {
|
|
|
14
14
|
approveInfinite(): Promise<string>;
|
|
15
15
|
filterBuyAssetsBySellAssetId(args: BuyAssetBySellIdInput): AssetId[];
|
|
16
16
|
filterAssetIdsBySellable(): AssetId[];
|
|
17
|
-
buildTrade(): Promise<Trade<
|
|
18
|
-
getTradeQuote(): Promise<TradeQuote<
|
|
19
|
-
executeTrade(args: ExecuteTradeInput<
|
|
17
|
+
buildTrade(): Promise<Trade<ChainId>>;
|
|
18
|
+
getTradeQuote(input: GetTradeQuoteInput): Promise<TradeQuote<ChainId>>;
|
|
19
|
+
executeTrade(args: ExecuteTradeInput<ChainId>): Promise<TradeResult>;
|
|
20
20
|
getTradeTxs(): Promise<TradeTxs>;
|
|
21
21
|
}
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.ThorchainSwapper = void 0;
|
|
4
4
|
const caip_1 = require("@shapeshiftoss/caip");
|
|
5
5
|
const api_1 = require("../../api");
|
|
6
|
+
const getTradeQuote_1 = require("./getThorTradeQuote/getTradeQuote");
|
|
6
7
|
const getUsdRate_1 = require("./utils/getUsdRate/getUsdRate");
|
|
7
8
|
const thorService_1 = require("./utils/thorService");
|
|
8
9
|
class ThorchainSwapper {
|
|
@@ -55,15 +56,18 @@ class ThorchainSwapper {
|
|
|
55
56
|
async buildTrade() {
|
|
56
57
|
throw new Error('ThorchainSwapper: buildTrade unimplemented');
|
|
57
58
|
}
|
|
58
|
-
async getTradeQuote() {
|
|
59
|
-
|
|
59
|
+
async getTradeQuote(input) {
|
|
60
|
+
return (0, getTradeQuote_1.getThorTradeQuote)({ deps: this.deps, input });
|
|
60
61
|
}
|
|
61
62
|
async executeTrade(args) {
|
|
62
63
|
const { trade, wallet } = args;
|
|
63
|
-
const adapter =
|
|
64
|
-
if (trade.sellAsset.chainId === 'eip155:1') {
|
|
64
|
+
const adapter = this.deps.adapterManager.get(trade.sellAsset.chainId);
|
|
65
|
+
if (adapter && trade.sellAsset.chainId === 'eip155:1') {
|
|
65
66
|
const thorTradeEth = trade;
|
|
66
|
-
const signedTx = await adapter.signTransaction({
|
|
67
|
+
const signedTx = await adapter.signTransaction({
|
|
68
|
+
txToSign: thorTradeEth.txData,
|
|
69
|
+
wallet
|
|
70
|
+
});
|
|
67
71
|
const txid = await adapter.broadcastTransaction(signedTx);
|
|
68
72
|
return { tradeId: txid };
|
|
69
73
|
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ChainId } from '@shapeshiftoss/caip';
|
|
2
|
+
import { GetTradeQuoteInput, TradeQuote } from '../../../api';
|
|
3
|
+
import { ThorchainSwapperDeps } from '../types';
|
|
4
|
+
export declare const getThorTradeQuote: ({ deps, input }: {
|
|
5
|
+
deps: ThorchainSwapperDeps;
|
|
6
|
+
input: GetTradeQuoteInput;
|
|
7
|
+
}) => Promise<TradeQuote<ChainId>>;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getThorTradeQuote = void 0;
|
|
4
|
+
const caip_1 = require("@shapeshiftoss/caip");
|
|
5
|
+
const api_1 = require("../../../api");
|
|
6
|
+
const bignumber_1 = require("../../utils/bignumber");
|
|
7
|
+
const constants_1 = require("../../utils/constants");
|
|
8
|
+
const helpers_1 = require("../../utils/helpers/helpers");
|
|
9
|
+
const constants_2 = require("../utils/constants");
|
|
10
|
+
const getThorTxData_1 = require("../utils/ethereum/utils/getThorTxData");
|
|
11
|
+
const getPriceRatio_1 = require("../utils/getPriceRatio/getPriceRatio");
|
|
12
|
+
const getEthTxFees_1 = require("../utils/txFeeHelpers/ethTxFees/getEthTxFees");
|
|
13
|
+
const getThorTradeQuote = async ({ deps, input }) => {
|
|
14
|
+
const { sellAsset, buyAsset, sellAmount, sellAssetAccountNumber, wallet } = input;
|
|
15
|
+
if (!wallet)
|
|
16
|
+
throw new api_1.SwapError('[getTradeQuote] - wallet is required', {
|
|
17
|
+
code: api_1.SwapErrorTypes.VALIDATION_FAILED
|
|
18
|
+
});
|
|
19
|
+
try {
|
|
20
|
+
const { assetReference: sellAssetErc20Address, chainReference, chainNamespace } = (0, caip_1.fromAssetId)(sellAsset.assetId);
|
|
21
|
+
const isErc20Trade = sellAssetErc20Address.startsWith('0x');
|
|
22
|
+
const sellAssetChainId = (0, caip_1.toChainId)({ chainReference, chainNamespace });
|
|
23
|
+
const adapter = deps.adapterManager.get(sellAssetChainId);
|
|
24
|
+
if (!adapter)
|
|
25
|
+
throw new api_1.SwapError(`[getThorTxInfo] - No chain adapter found for ${sellAssetChainId}.`, {
|
|
26
|
+
code: api_1.SwapErrorTypes.UNSUPPORTED_CHAIN,
|
|
27
|
+
details: { sellAssetChainId }
|
|
28
|
+
});
|
|
29
|
+
const bip44Params = adapter.buildBIP44Params({ accountNumber: Number(sellAssetAccountNumber) });
|
|
30
|
+
const receiveAddress = await adapter.getAddress({ wallet, bip44Params });
|
|
31
|
+
const { data, router } = await (0, getThorTxData_1.getThorTxInfo)({
|
|
32
|
+
deps,
|
|
33
|
+
sellAsset,
|
|
34
|
+
buyAsset,
|
|
35
|
+
sellAmount,
|
|
36
|
+
slippageTolerance: constants_1.DEFAULT_SLIPPAGE,
|
|
37
|
+
destinationAddress: receiveAddress,
|
|
38
|
+
isErc20Trade
|
|
39
|
+
});
|
|
40
|
+
const feeData = await (async () => {
|
|
41
|
+
switch (sellAssetChainId) {
|
|
42
|
+
case 'eip155:1':
|
|
43
|
+
return await (0, getEthTxFees_1.getEthTxFees)({
|
|
44
|
+
deps,
|
|
45
|
+
data,
|
|
46
|
+
router,
|
|
47
|
+
buyAsset,
|
|
48
|
+
sellAmount,
|
|
49
|
+
adapterManager: deps.adapterManager,
|
|
50
|
+
receiveAddress,
|
|
51
|
+
sellAssetReference: sellAssetErc20Address
|
|
52
|
+
});
|
|
53
|
+
default:
|
|
54
|
+
throw new api_1.SwapError('[getThorTxInfo] - Asset chainId is not supported.', {
|
|
55
|
+
code: api_1.SwapErrorTypes.UNSUPPORTED_CHAIN,
|
|
56
|
+
details: { sellAssetChainId }
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
})();
|
|
60
|
+
const sellAssetId = sellAsset.assetId;
|
|
61
|
+
const buyAssetId = buyAsset.assetId;
|
|
62
|
+
const feeAssetId = (0, caip_1.getFeeAssetIdFromAssetId)(buyAssetId);
|
|
63
|
+
if (!feeAssetId)
|
|
64
|
+
throw new api_1.SwapError(`[getThorTxInfo] - No feeAssetId found for ${buyAssetId}.`, {
|
|
65
|
+
code: api_1.SwapErrorTypes.VALIDATION_FAILED,
|
|
66
|
+
details: { buyAssetId }
|
|
67
|
+
});
|
|
68
|
+
const feeAssetRatio = await (0, getPriceRatio_1.getPriceRatio)(deps, { sellAssetId, buyAssetId: feeAssetId });
|
|
69
|
+
const priceRatio = await (0, getPriceRatio_1.getPriceRatio)(deps, { sellAssetId, buyAssetId });
|
|
70
|
+
const rate = (0, bignumber_1.bnOrZero)(1).div(priceRatio).toString();
|
|
71
|
+
const buyAmount = (0, helpers_1.normalizeAmount)((0, bignumber_1.bnOrZero)(sellAmount).times(rate));
|
|
72
|
+
const sellAssetTradeFee = (0, bignumber_1.bnOrZero)(feeData.tradeFee).times((0, bignumber_1.bnOrZero)(feeAssetRatio));
|
|
73
|
+
// padding minimum by 1.5 the trade fee to avoid thorchain "not enough to cover fee" errors.
|
|
74
|
+
const minimum = (0, bignumber_1.fromBaseUnit)(sellAssetTradeFee.times(1.5).toString(), sellAsset.precision);
|
|
75
|
+
return {
|
|
76
|
+
rate,
|
|
77
|
+
minimum,
|
|
78
|
+
maximum: constants_2.MAX_THORCHAIN_TRADE,
|
|
79
|
+
feeData,
|
|
80
|
+
sellAmount,
|
|
81
|
+
buyAmount,
|
|
82
|
+
sources: [{ name: 'thorchain', proportion: '1' }],
|
|
83
|
+
allowanceContract: router,
|
|
84
|
+
buyAsset,
|
|
85
|
+
sellAsset,
|
|
86
|
+
sellAssetAccountNumber
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
catch (e) {
|
|
90
|
+
if (e instanceof api_1.SwapError)
|
|
91
|
+
throw e;
|
|
92
|
+
throw new api_1.SwapError('[getThorTradeQuote]', {
|
|
93
|
+
cause: e,
|
|
94
|
+
code: api_1.SwapErrorTypes.TRADE_QUOTE_FAILED
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
exports.getThorTradeQuote = getThorTradeQuote;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const MAX_THORCHAIN_TRADE = "100000000000000000000000000";
|
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
import { ethereum } from '@shapeshiftoss/chain-adapters';
|
|
2
2
|
import { ETHSignTx, HDWallet } from '@shapeshiftoss/hdwallet-core';
|
|
3
3
|
import { Asset, BIP44Params } from '@shapeshiftoss/types';
|
|
4
|
-
import BigNumber from 'bignumber.js';
|
|
5
4
|
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
5
|
export declare const makeTradeTx: ({ wallet, bip44Params, sellAmount, buyAsset, sellAsset, destinationAddress, adapter, maxFeePerGas, maxPriorityFeePerGas, slippageTolerance, deps, gasLimit }: {
|
|
9
6
|
wallet: HDWallet;
|
|
10
7
|
bip44Params: BIP44Params;
|
|
@@ -1,63 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.makeTradeTx =
|
|
3
|
+
exports.makeTradeTx = void 0;
|
|
7
4
|
const caip_1 = require("@shapeshiftoss/caip");
|
|
8
|
-
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
9
5
|
const api_1 = require("../../../../api");
|
|
10
|
-
const
|
|
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;
|
|
6
|
+
const getThorTxData_1 = require("../ethereum/utils/getThorTxData");
|
|
28
7
|
const makeTradeTx = async ({ wallet, bip44Params, sellAmount, buyAsset, sellAsset, destinationAddress, adapter, maxFeePerGas, maxPriorityFeePerGas, slippageTolerance, deps, gasLimit }) => {
|
|
29
8
|
try {
|
|
30
9
|
const { assetReference } = (0, caip_1.fromAssetId)(sellAsset.assetId);
|
|
31
10
|
const isErc20Trade = assetReference.startsWith('0x');
|
|
32
|
-
const { data
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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,
|
|
11
|
+
const { data, router } = await (0, getThorTxData_1.getThorTxInfo)({
|
|
12
|
+
deps,
|
|
13
|
+
sellAsset,
|
|
14
|
+
buyAsset,
|
|
15
|
+
sellAmount,
|
|
16
|
+
slippageTolerance,
|
|
57
17
|
destinationAddress,
|
|
58
|
-
|
|
18
|
+
isErc20Trade
|
|
59
19
|
});
|
|
60
|
-
const data = await (0, routerCalldata_1.deposit)(vault, router, isErc20Trade ? assetReference : '0x0000000000000000000000000000000000000000', sellAmount, memo);
|
|
61
20
|
return adapter.buildCustomTx({
|
|
62
21
|
wallet,
|
|
63
22
|
bip44Params,
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Asset } from '@shapeshiftoss/types';
|
|
2
|
+
import { ThorchainSwapperDeps } from '../../../types';
|
|
3
|
+
export declare const getThorTxInfo: ({ deps, sellAsset, buyAsset, sellAmount, slippageTolerance, destinationAddress, isErc20Trade }: {
|
|
4
|
+
deps: ThorchainSwapperDeps;
|
|
5
|
+
sellAsset: Asset;
|
|
6
|
+
buyAsset: Asset;
|
|
7
|
+
sellAmount: string;
|
|
8
|
+
slippageTolerance: string;
|
|
9
|
+
destinationAddress: string;
|
|
10
|
+
isErc20Trade: boolean;
|
|
11
|
+
}) => Promise<{
|
|
12
|
+
data: any;
|
|
13
|
+
memo: string;
|
|
14
|
+
router: string;
|
|
15
|
+
}>;
|