@shapeshiftoss/swapper 4.0.2 → 4.1.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 -18
- package/dist/swappers/test/TestSwapper.d.ts +4 -4
- package/dist/swappers/thorchain/ThorchainSwapper.d.ts +4 -4
- package/dist/swappers/zrx/ZrxApprovalNeeded/ZrxApprovalNeeded.d.ts +2 -2
- package/dist/swappers/zrx/ZrxApprovalNeeded/ZrxApprovalNeeded.js +4 -5
- package/dist/swappers/zrx/ZrxApproveInfinite/ZrxApproveInfinite.d.ts +2 -2
- package/dist/swappers/zrx/ZrxApproveInfinite/ZrxApproveInfinite.js +1 -3
- package/dist/swappers/zrx/ZrxSwapper.d.ts +7 -7
- package/dist/swappers/zrx/ZrxSwapper.js +1 -1
- package/dist/swappers/zrx/getZrxMinMax/getZrxMinMax.d.ts +2 -2
- package/dist/swappers/zrx/getZrxMinMax/getZrxMinMax.js +3 -5
- package/dist/swappers/zrx/getZrxTradeQuote/getZrxTradeQuote.d.ts +1 -2
- package/dist/swappers/zrx/getZrxTradeQuote/getZrxTradeQuote.js +9 -22
- package/dist/swappers/zrx/utils/helpers/helpers.d.ts +6 -5
- package/dist/swappers/zrx/utils/helpers/helpers.js +5 -4
- package/dist/swappers/zrx/utils/test-data/setupSwapQuote.d.ts +3 -12
- package/dist/swappers/zrx/utils/test-data/setupSwapQuote.js +17 -7
- package/dist/swappers/zrx/utils/test-data/setupZrxDeps.js +2 -1
- package/dist/swappers/zrx/zrxBuildTrade/zrxBuildTrade.d.ts +2 -2
- package/dist/swappers/zrx/zrxBuildTrade/zrxBuildTrade.js +3 -9
- package/dist/swappers/zrx/zrxExecuteTrade/zrxExecuteTrade.d.ts +1 -2
- package/package.json +1 -1
package/dist/api.d.ts
CHANGED
|
@@ -1,9 +1,20 @@
|
|
|
1
1
|
import { AssetId } from '@shapeshiftoss/caip';
|
|
2
2
|
import { HDWallet } from '@shapeshiftoss/hdwallet-core';
|
|
3
|
-
import { ApprovalNeededOutput, Asset,
|
|
3
|
+
import { ApprovalNeededOutput, Asset, ChainSpecific, ExecQuoteOutput, GetMinMaxInput, MinMaxOutput, SupportedChainIds, SwapperType } from '@shapeshiftoss/types';
|
|
4
4
|
export declare type SupportedAssetInput = {
|
|
5
5
|
assetIds: AssetId[];
|
|
6
6
|
};
|
|
7
|
+
declare type ChainSpecificQuoteFeeData<T1> = ChainSpecific<T1, {
|
|
8
|
+
'eip155:1': {
|
|
9
|
+
estimatedGas?: string;
|
|
10
|
+
gasPrice?: string;
|
|
11
|
+
approvalFee?: string;
|
|
12
|
+
totalFee?: string;
|
|
13
|
+
};
|
|
14
|
+
}>;
|
|
15
|
+
export declare type QuoteFeeData<T1 extends SupportedChainIds> = {
|
|
16
|
+
fee: string;
|
|
17
|
+
} & ChainSpecificQuoteFeeData<T1>;
|
|
7
18
|
export declare type ByPairInput = {
|
|
8
19
|
sellAssetId: AssetId;
|
|
9
20
|
buyAssetId: AssetId;
|
|
@@ -18,8 +29,7 @@ export declare type SupportedSellAssetsInput = {
|
|
|
18
29
|
export declare type CommonTradeInput = {
|
|
19
30
|
sellAsset: Asset;
|
|
20
31
|
buyAsset: Asset;
|
|
21
|
-
sellAmount
|
|
22
|
-
buyAmount?: string;
|
|
32
|
+
sellAmount: string;
|
|
23
33
|
sendMax: boolean;
|
|
24
34
|
sellAssetAccountId: string;
|
|
25
35
|
};
|
|
@@ -29,12 +39,12 @@ export declare type BuildTradeInput = CommonTradeInput & {
|
|
|
29
39
|
slippage?: string;
|
|
30
40
|
wallet: HDWallet;
|
|
31
41
|
};
|
|
32
|
-
interface TradeBase<C extends
|
|
42
|
+
interface TradeBase<C extends SupportedChainIds> {
|
|
33
43
|
success: boolean;
|
|
34
44
|
statusReason: string;
|
|
35
45
|
buyAmount: string;
|
|
36
46
|
sellAmount: string;
|
|
37
|
-
feeData:
|
|
47
|
+
feeData: QuoteFeeData<C>;
|
|
38
48
|
rate: string;
|
|
39
49
|
allowanceContract: string;
|
|
40
50
|
sources: Array<SwapSource>;
|
|
@@ -42,16 +52,16 @@ interface TradeBase<C extends ChainTypes> {
|
|
|
42
52
|
sellAsset: Asset;
|
|
43
53
|
sellAssetAccountId: string;
|
|
44
54
|
}
|
|
45
|
-
export interface TradeQuote<C extends
|
|
55
|
+
export interface TradeQuote<C extends SupportedChainIds> extends TradeBase<C> {
|
|
46
56
|
minimum: string;
|
|
47
57
|
maximum: string;
|
|
48
58
|
}
|
|
49
|
-
export interface Trade<C extends
|
|
59
|
+
export interface Trade<C extends SupportedChainIds> extends TradeBase<C> {
|
|
50
60
|
txData: string;
|
|
51
61
|
depositAddress: string;
|
|
52
62
|
receiveAddress: string;
|
|
53
63
|
}
|
|
54
|
-
export declare type ExecuteTradeInput<C extends
|
|
64
|
+
export declare type ExecuteTradeInput<C extends SupportedChainIds> = {
|
|
55
65
|
trade: Trade<C>;
|
|
56
66
|
wallet: HDWallet;
|
|
57
67
|
};
|
|
@@ -62,12 +72,12 @@ export declare type SwapSource = {
|
|
|
62
72
|
name: string;
|
|
63
73
|
proportion: string;
|
|
64
74
|
};
|
|
65
|
-
export declare type ApproveInfiniteInput<C extends
|
|
66
|
-
quote:
|
|
75
|
+
export declare type ApproveInfiniteInput<C extends SupportedChainIds> = {
|
|
76
|
+
quote: TradeQuote<C>;
|
|
67
77
|
wallet: HDWallet;
|
|
68
78
|
};
|
|
69
|
-
export declare type ApprovalNeededInput<C extends
|
|
70
|
-
quote:
|
|
79
|
+
export declare type ApprovalNeededInput<C extends SupportedChainIds> = {
|
|
80
|
+
quote: TradeQuote<C>;
|
|
71
81
|
wallet: HDWallet;
|
|
72
82
|
};
|
|
73
83
|
export declare class SwapError extends Error {
|
|
@@ -78,11 +88,11 @@ export interface Swapper {
|
|
|
78
88
|
/**
|
|
79
89
|
* Get builds a trade with definitive rate & txData that can be executed with executeTrade
|
|
80
90
|
**/
|
|
81
|
-
buildTrade(args: BuildTradeInput): Promise<Trade<
|
|
91
|
+
buildTrade(args: BuildTradeInput): Promise<Trade<SupportedChainIds>>;
|
|
82
92
|
/**
|
|
83
93
|
* Get a trade quote
|
|
84
94
|
*/
|
|
85
|
-
getTradeQuote(input: GetTradeQuoteInput): Promise<TradeQuote<
|
|
95
|
+
getTradeQuote(input: GetTradeQuoteInput): Promise<TradeQuote<SupportedChainIds>>;
|
|
86
96
|
/**
|
|
87
97
|
* Get the usd rate from either the assets symbol or tokenId
|
|
88
98
|
*/
|
|
@@ -90,19 +100,19 @@ export interface Swapper {
|
|
|
90
100
|
/**
|
|
91
101
|
* Get the minimum and maximum trade value of the sellAsset and buyAsset
|
|
92
102
|
*/
|
|
93
|
-
getMinMax(input:
|
|
103
|
+
getMinMax(input: GetMinMaxInput): Promise<MinMaxOutput>;
|
|
94
104
|
/**
|
|
95
105
|
* Execute a trade built with buildTrade by signing and broadcasting
|
|
96
106
|
*/
|
|
97
|
-
executeTrade(args: ExecuteTradeInput<
|
|
107
|
+
executeTrade(args: ExecuteTradeInput<SupportedChainIds>): Promise<ExecQuoteOutput>;
|
|
98
108
|
/**
|
|
99
109
|
* Get a boolean if a quote needs approval
|
|
100
110
|
*/
|
|
101
|
-
approvalNeeded(args: ApprovalNeededInput<
|
|
111
|
+
approvalNeeded(args: ApprovalNeededInput<SupportedChainIds>): Promise<ApprovalNeededOutput>;
|
|
102
112
|
/**
|
|
103
113
|
* Get the txid of an approve infinite transaction
|
|
104
114
|
*/
|
|
105
|
-
approveInfinite(args: ApproveInfiniteInput<
|
|
115
|
+
approveInfinite(args: ApproveInfiniteInput<SupportedChainIds>): Promise<string>;
|
|
106
116
|
/**
|
|
107
117
|
* Get supported buyAssetId's by sellAssetId
|
|
108
118
|
*/
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AssetId } from '@shapeshiftoss/caip';
|
|
2
|
-
import { ApprovalNeededOutput, Asset,
|
|
2
|
+
import { ApprovalNeededOutput, Asset, ExecQuoteOutput, GetMinMaxInput, MinMaxOutput, SupportedChainIds, SwapperType } from '@shapeshiftoss/types';
|
|
3
3
|
import { BuyAssetBySellIdInput, Swapper, Trade, TradeQuote } from '../../api';
|
|
4
4
|
/**
|
|
5
5
|
* Playground for testing different scenarios of multiple swappers in the manager.
|
|
@@ -10,12 +10,12 @@ export declare class TestSwapper implements Swapper {
|
|
|
10
10
|
getType(): SwapperType;
|
|
11
11
|
constructor();
|
|
12
12
|
getUsdRate(input: Pick<Asset, 'symbol' | 'tokenId'>): Promise<string>;
|
|
13
|
-
getMinMax(input:
|
|
13
|
+
getMinMax(input: GetMinMaxInput): Promise<MinMaxOutput>;
|
|
14
14
|
approvalNeeded(): Promise<ApprovalNeededOutput>;
|
|
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<SupportedChainIds>>;
|
|
19
|
+
getTradeQuote(): Promise<TradeQuote<SupportedChainIds>>;
|
|
20
20
|
executeTrade(): Promise<ExecQuoteOutput>;
|
|
21
21
|
}
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import { AssetId } from '@shapeshiftoss/caip';
|
|
2
|
-
import { ApprovalNeededOutput, Asset,
|
|
2
|
+
import { ApprovalNeededOutput, Asset, ExecQuoteOutput, GetMinMaxInput, MinMaxOutput, SupportedChainIds, SwapperType } from '@shapeshiftoss/types';
|
|
3
3
|
import { Swapper, Trade, TradeQuote } from '../../api';
|
|
4
4
|
export declare class ThorchainSwapper implements Swapper {
|
|
5
5
|
getType(): SwapperType;
|
|
6
6
|
getUsdRate(input: Pick<Asset, 'symbol' | 'tokenId'>): Promise<string>;
|
|
7
|
-
getMinMax(input:
|
|
7
|
+
getMinMax(input: GetMinMaxInput): Promise<MinMaxOutput>;
|
|
8
8
|
approvalNeeded(): Promise<ApprovalNeededOutput>;
|
|
9
9
|
approveInfinite(): Promise<string>;
|
|
10
10
|
filterBuyAssetsBySellAssetId(): AssetId[];
|
|
11
11
|
filterAssetIdsBySellable(): AssetId[];
|
|
12
|
-
buildTrade(): Promise<Trade<
|
|
13
|
-
getTradeQuote(): Promise<TradeQuote<
|
|
12
|
+
buildTrade(): Promise<Trade<SupportedChainIds>>;
|
|
13
|
+
getTradeQuote(): Promise<TradeQuote<SupportedChainIds>>;
|
|
14
14
|
executeTrade(): Promise<ExecQuoteOutput>;
|
|
15
15
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ApprovalNeededOutput,
|
|
1
|
+
import { ApprovalNeededOutput, SupportedChainIds } from '@shapeshiftoss/types';
|
|
2
2
|
import { ApprovalNeededInput } from '../../../api';
|
|
3
3
|
import { ZrxSwapperDeps } from '../ZrxSwapper';
|
|
4
|
-
export declare function ZrxApprovalNeeded({ adapterManager, web3 }: ZrxSwapperDeps, { quote, wallet }: ApprovalNeededInput<
|
|
4
|
+
export declare function ZrxApprovalNeeded({ adapterManager, web3 }: ZrxSwapperDeps, { quote, wallet }: ApprovalNeededInput<SupportedChainIds>): Promise<ApprovalNeededOutput>;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ZrxApprovalNeeded = void 0;
|
|
4
|
-
const types_1 = require("@shapeshiftoss/types");
|
|
5
4
|
const api_1 = require("../../../api");
|
|
6
5
|
const erc20Allowance_abi_1 = require("../utils/abi/erc20Allowance-abi");
|
|
7
6
|
const bignumber_1 = require("../utils/bignumber");
|
|
@@ -10,14 +9,14 @@ const helpers_1 = require("../utils/helpers/helpers");
|
|
|
10
9
|
async function ZrxApprovalNeeded({ adapterManager, web3 }, { quote, wallet }) {
|
|
11
10
|
var _a, _b;
|
|
12
11
|
const { sellAsset } = quote;
|
|
12
|
+
if (sellAsset.chainId !== 'eip155:1') {
|
|
13
|
+
throw new api_1.SwapError('ZrxSwapper:ZrxApprovalNeeded only Ethereum chain type is supported');
|
|
14
|
+
}
|
|
13
15
|
if (sellAsset.symbol === 'ETH') {
|
|
14
16
|
return { approvalNeeded: false };
|
|
15
17
|
}
|
|
16
|
-
if (sellAsset.chain !== types_1.ChainTypes.Ethereum) {
|
|
17
|
-
throw new api_1.SwapError('ZrxSwapper:ZrxApprovalNeeded only Ethereum chain type is supported');
|
|
18
|
-
}
|
|
19
18
|
const accountNumber = quote.sellAssetAccountId ? Number(quote.sellAssetAccountId) : 0;
|
|
20
|
-
const adapter = adapterManager.
|
|
19
|
+
const adapter = await adapterManager.byChainId(sellAsset.chainId);
|
|
21
20
|
const bip44Params = adapter.buildBIP44Params({ accountNumber });
|
|
22
21
|
const receiveAddress = await adapter.getAddress({ wallet, bip44Params });
|
|
23
22
|
if (!quote.sellAsset.tokenId || !quote.allowanceContract) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { SupportedChainIds } from '@shapeshiftoss/types';
|
|
2
2
|
import { ApproveInfiniteInput } from '../../../api';
|
|
3
3
|
import { ZrxSwapperDeps } from '../ZrxSwapper';
|
|
4
|
-
export declare function ZrxApproveInfinite({ adapterManager, web3 }: ZrxSwapperDeps, { quote, wallet }: ApproveInfiniteInput<
|
|
4
|
+
export declare function ZrxApproveInfinite({ adapterManager, web3 }: ZrxSwapperDeps, { quote, wallet }: ApproveInfiniteInput<SupportedChainIds>): Promise<string>;
|
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ZrxApproveInfinite = void 0;
|
|
4
|
-
const types_1 = require("@shapeshiftoss/types");
|
|
5
4
|
const erc20_abi_1 = require("../utils/abi/erc20-abi");
|
|
6
5
|
const constants_1 = require("../utils/constants");
|
|
7
6
|
const helpers_1 = require("../utils/helpers/helpers");
|
|
8
7
|
async function ZrxApproveInfinite({ adapterManager, web3 }, { quote, wallet }) {
|
|
9
|
-
const adapter = adapterManager.byChain(types_1.ChainTypes.Ethereum);
|
|
10
8
|
const allowanceGrantRequired = await (0, helpers_1.grantAllowance)({
|
|
11
9
|
quote: {
|
|
12
10
|
...quote,
|
|
@@ -14,7 +12,7 @@ async function ZrxApproveInfinite({ adapterManager, web3 }, { quote, wallet }) {
|
|
|
14
12
|
sellAmount: constants_1.MAX_ALLOWANCE
|
|
15
13
|
},
|
|
16
14
|
wallet,
|
|
17
|
-
|
|
15
|
+
adapterManager,
|
|
18
16
|
erc20Abi: erc20_abi_1.erc20Abi,
|
|
19
17
|
web3
|
|
20
18
|
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AssetId } from '@shapeshiftoss/caip';
|
|
2
2
|
import { ChainAdapterManager } from '@shapeshiftoss/chain-adapters';
|
|
3
|
-
import { ApprovalNeededOutput, Asset,
|
|
3
|
+
import { ApprovalNeededOutput, Asset, ExecQuoteOutput, GetMinMaxInput, MinMaxOutput, SupportedChainIds, SwapperType } from '@shapeshiftoss/types';
|
|
4
4
|
import Web3 from 'web3';
|
|
5
5
|
import { ApprovalNeededInput, ApproveInfiniteInput, BuildTradeInput, BuyAssetBySellIdInput, ExecuteTradeInput, GetTradeQuoteInput, Swapper, Trade, TradeQuote } from '../../api';
|
|
6
6
|
export declare type ZrxSwapperDeps = {
|
|
@@ -15,13 +15,13 @@ export declare class ZrxSwapper implements Swapper {
|
|
|
15
15
|
deps: ZrxSwapperDeps;
|
|
16
16
|
constructor(deps: ZrxSwapperDeps);
|
|
17
17
|
getType(): SwapperType;
|
|
18
|
-
buildTrade(args: BuildTradeInput): Promise<Trade<
|
|
19
|
-
getTradeQuote(input: GetTradeQuoteInput): Promise<TradeQuote<
|
|
18
|
+
buildTrade(args: BuildTradeInput): Promise<Trade<SupportedChainIds>>;
|
|
19
|
+
getTradeQuote(input: GetTradeQuoteInput): Promise<TradeQuote<SupportedChainIds>>;
|
|
20
20
|
getUsdRate(input: Pick<Asset, 'symbol' | 'tokenId'>): Promise<string>;
|
|
21
|
-
getMinMax(input:
|
|
22
|
-
executeTrade(args: ExecuteTradeInput<
|
|
23
|
-
approvalNeeded(args: ApprovalNeededInput<
|
|
24
|
-
approveInfinite(args: ApproveInfiniteInput<
|
|
21
|
+
getMinMax(input: GetMinMaxInput): Promise<MinMaxOutput>;
|
|
22
|
+
executeTrade(args: ExecuteTradeInput<SupportedChainIds>): Promise<ExecQuoteOutput>;
|
|
23
|
+
approvalNeeded(args: ApprovalNeededInput<SupportedChainIds>): Promise<ApprovalNeededOutput>;
|
|
24
|
+
approveInfinite(args: ApproveInfiniteInput<SupportedChainIds>): Promise<string>;
|
|
25
25
|
filterBuyAssetsBySellAssetId(args: BuyAssetBySellIdInput): AssetId[];
|
|
26
26
|
filterAssetIdsBySellable(assetIds?: AssetId[]): AssetId[];
|
|
27
27
|
}
|
|
@@ -33,7 +33,7 @@ class ZrxSwapper {
|
|
|
33
33
|
return (0, helpers_1.getUsdRate)(input);
|
|
34
34
|
}
|
|
35
35
|
async getMinMax(input) {
|
|
36
|
-
return (0, getZrxMinMax_1.getZrxMinMax)(input);
|
|
36
|
+
return (0, getZrxMinMax_1.getZrxMinMax)(input.sellAsset, input.buyAsset);
|
|
37
37
|
}
|
|
38
38
|
async executeTrade(args) {
|
|
39
39
|
return (0, zrxExecuteTrade_1.zrxExecuteTrade)(this.deps, args);
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare const getZrxMinMax: (
|
|
1
|
+
import { Asset, MinMaxOutput } from '@shapeshiftoss/types';
|
|
2
|
+
export declare const getZrxMinMax: (sellAsset: Asset, buyAsset: Asset) => Promise<MinMaxOutput>;
|
|
@@ -1,21 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getZrxMinMax = void 0;
|
|
4
|
-
const types_1 = require("@shapeshiftoss/types");
|
|
5
4
|
const bignumber_1 = require("../utils/bignumber");
|
|
6
5
|
const constants_1 = require("../utils/constants");
|
|
7
6
|
const helpers_1 = require("../utils/helpers/helpers");
|
|
8
7
|
const ZrxSwapper_1 = require("../ZrxSwapper");
|
|
9
|
-
const getZrxMinMax = async (
|
|
10
|
-
|
|
11
|
-
if (sellAsset.chain !== types_1.ChainTypes.Ethereum || buyAsset.chain !== types_1.ChainTypes.Ethereum) {
|
|
8
|
+
const getZrxMinMax = async (sellAsset, buyAsset) => {
|
|
9
|
+
if (sellAsset.chainId !== 'eip155:1' || buyAsset.chainId !== 'eip155:1') {
|
|
12
10
|
throw new ZrxSwapper_1.ZrxError('getZrxMinMax - must be eth assets');
|
|
13
11
|
}
|
|
14
12
|
const usdRate = await (0, helpers_1.getUsdRate)({
|
|
15
13
|
symbol: sellAsset.symbol,
|
|
16
14
|
tokenId: sellAsset.tokenId
|
|
17
15
|
});
|
|
18
|
-
const minimum = (0, bignumber_1.
|
|
16
|
+
const minimum = (0, bignumber_1.bn)(1).dividedBy((0, bignumber_1.bnOrZero)(usdRate)).toString();
|
|
19
17
|
return {
|
|
20
18
|
minimum,
|
|
21
19
|
maximum: constants_1.MAX_ZRX_TRADE // Arbitrarily large value. 10e+28 here.
|
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
import { ChainTypes } from '@shapeshiftoss/types';
|
|
2
1
|
import { GetTradeQuoteInput, TradeQuote } from '../../../api';
|
|
3
|
-
export declare function getZrxTradeQuote(input: GetTradeQuoteInput): Promise<TradeQuote<
|
|
2
|
+
export declare function getZrxTradeQuote(input: GetTradeQuoteInput): Promise<TradeQuote<'eip155:1'>>;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getZrxTradeQuote = void 0;
|
|
4
|
-
const types_1 = require("@shapeshiftoss/types");
|
|
5
4
|
const getZrxMinMax_1 = require("../getZrxMinMax/getZrxMinMax");
|
|
6
5
|
const bignumber_1 = require("../utils/bignumber");
|
|
7
6
|
const constants_1 = require("../utils/constants");
|
|
@@ -10,34 +9,22 @@ const zrxService_1 = require("../utils/zrxService");
|
|
|
10
9
|
const ZrxSwapper_1 = require("../ZrxSwapper");
|
|
11
10
|
async function getZrxTradeQuote(input) {
|
|
12
11
|
var _a, _b, _c, _d, _e, _f, _g;
|
|
13
|
-
const { sellAsset, buyAsset, sellAmount,
|
|
12
|
+
const { sellAsset, buyAsset, sellAmount, sellAssetAccountId } = input;
|
|
14
13
|
if (!buyAsset) {
|
|
15
14
|
throw new ZrxSwapper_1.ZrxError('getQuote - Missing buyAsset');
|
|
16
15
|
}
|
|
17
16
|
if (!sellAsset) {
|
|
18
17
|
throw new ZrxSwapper_1.ZrxError('getQuote - Missing sellAsset');
|
|
19
18
|
}
|
|
20
|
-
if (buyAsset.
|
|
19
|
+
if (buyAsset.chainId !== 'eip155:1' || sellAsset.chainId !== 'eip155:1') {
|
|
21
20
|
throw new ZrxSwapper_1.ZrxError('getQuote - Both assets need to be on the Ethereum chain to use Zrx');
|
|
22
21
|
}
|
|
23
|
-
if (!sellAmount && !buyAmount) {
|
|
24
|
-
throw new ZrxSwapper_1.ZrxError('getQuote - sellAmount or buyAmount amount is required');
|
|
25
|
-
}
|
|
26
22
|
const useSellAmount = !!sellAmount;
|
|
27
23
|
const buyToken = buyAsset.tokenId || buyAsset.symbol;
|
|
28
24
|
const sellToken = sellAsset.tokenId || sellAsset.symbol;
|
|
29
|
-
const { minimum
|
|
30
|
-
const
|
|
31
|
-
const
|
|
32
|
-
const amountKey = Object.keys(amount)[0];
|
|
33
|
-
const amountValue = Object.values(amount)[0];
|
|
34
|
-
let normalizedAmount = (0, helpers_1.normalizeAmount)(amountValue);
|
|
35
|
-
if (!normalizedAmount || normalizedAmount === '0') {
|
|
36
|
-
normalizedAmount = (0, helpers_1.normalizeAmount)(minQuoteSellAmountWei === null || minQuoteSellAmountWei === void 0 ? void 0 : minQuoteSellAmountWei.toString());
|
|
37
|
-
}
|
|
38
|
-
if (!normalizedAmount || normalizedAmount === '0') {
|
|
39
|
-
throw new ZrxSwapper_1.ZrxError('getQuote - Must have valid sellAmount, buyAmount or minimum amount');
|
|
40
|
-
}
|
|
25
|
+
const { minimum, maximum } = await (0, getZrxMinMax_1.getZrxMinMax)(buyAsset, sellAsset);
|
|
26
|
+
const minQuoteSellAmount = (0, bignumber_1.bnOrZero)(minimum).times((0, bignumber_1.bn)(10).exponentiatedBy(sellAsset.precision));
|
|
27
|
+
const normalizedSellAmount = (0, helpers_1.normalizeAmount)((0, bignumber_1.bnOrZero)(minimum).lt(minQuoteSellAmount) ? minQuoteSellAmount.toString() : sellAmount);
|
|
41
28
|
try {
|
|
42
29
|
/**
|
|
43
30
|
* /swap/v1/price
|
|
@@ -52,18 +39,18 @@ async function getZrxTradeQuote(input) {
|
|
|
52
39
|
params: {
|
|
53
40
|
sellToken,
|
|
54
41
|
buyToken,
|
|
55
|
-
|
|
42
|
+
sellAmount: normalizedSellAmount
|
|
56
43
|
}
|
|
57
44
|
});
|
|
58
45
|
const { data } = quoteResponse;
|
|
59
46
|
const estimatedGas = (0, bignumber_1.bnOrZero)(data.estimatedGas).times(1.5);
|
|
60
|
-
const rate = useSellAmount ? data.price : (0, bignumber_1.
|
|
47
|
+
const rate = useSellAmount ? data.price : (0, bignumber_1.bn)(1).div(data.price).toString();
|
|
61
48
|
return {
|
|
62
49
|
success: true,
|
|
63
50
|
statusReason: '',
|
|
64
51
|
rate,
|
|
65
|
-
minimum
|
|
66
|
-
maximum
|
|
52
|
+
minimum,
|
|
53
|
+
maximum,
|
|
67
54
|
feeData: {
|
|
68
55
|
fee: (0, bignumber_1.bnOrZero)(estimatedGas).multipliedBy((0, bignumber_1.bnOrZero)(data.gasPrice)).toString(),
|
|
69
56
|
chainSpecific: {
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ChainAdapterManager } from '@shapeshiftoss/chain-adapters';
|
|
2
2
|
import { HDWallet } from '@shapeshiftoss/hdwallet-core';
|
|
3
|
-
import { Asset,
|
|
3
|
+
import { Asset, SupportedChainIds } from '@shapeshiftoss/types';
|
|
4
4
|
import BigNumber from 'bignumber.js';
|
|
5
5
|
import Web3 from 'web3';
|
|
6
6
|
import { AbiItem } from 'web3-utils';
|
|
7
|
+
import { TradeQuote } from '../../../../api';
|
|
7
8
|
export declare type GetAllowanceRequiredArgs = {
|
|
8
9
|
receiveAddress: string;
|
|
9
10
|
allowanceContract: string;
|
|
@@ -20,9 +21,9 @@ export declare type GetERC20AllowanceArgs = {
|
|
|
20
21
|
spenderAddress: string;
|
|
21
22
|
};
|
|
22
23
|
declare type GrantAllowanceArgs = {
|
|
23
|
-
quote:
|
|
24
|
+
quote: TradeQuote<SupportedChainIds>;
|
|
24
25
|
wallet: HDWallet;
|
|
25
|
-
|
|
26
|
+
adapterManager: ChainAdapterManager;
|
|
26
27
|
erc20Abi: AbiItem[];
|
|
27
28
|
web3: Web3;
|
|
28
29
|
};
|
|
@@ -38,5 +39,5 @@ export declare const normalizeAmount: (amount: string | undefined) => string | u
|
|
|
38
39
|
export declare const getERC20Allowance: ({ erc20AllowanceAbi, web3, tokenId, ownerAddress, spenderAddress }: GetERC20AllowanceArgs) => Promise<any>;
|
|
39
40
|
export declare const getAllowanceRequired: ({ receiveAddress, allowanceContract, sellAsset, sellAmount, web3, erc20AllowanceAbi }: GetAllowanceRequiredArgs) => Promise<BigNumber>;
|
|
40
41
|
export declare const getUsdRate: (input: Pick<Asset, 'symbol' | 'tokenId'>) => Promise<string>;
|
|
41
|
-
export declare const grantAllowance: ({ quote, wallet,
|
|
42
|
+
export declare const grantAllowance: ({ quote, wallet, adapterManager, erc20Abi, web3 }: GrantAllowanceArgs) => Promise<string>;
|
|
42
43
|
export {};
|
|
@@ -27,7 +27,7 @@ const getERC20Allowance = async ({ erc20AllowanceAbi, web3, tokenId, ownerAddres
|
|
|
27
27
|
exports.getERC20Allowance = getERC20Allowance;
|
|
28
28
|
const getAllowanceRequired = async ({ receiveAddress, allowanceContract, sellAsset, sellAmount, web3, erc20AllowanceAbi }) => {
|
|
29
29
|
if (sellAsset.symbol === 'ETH') {
|
|
30
|
-
return (0, bignumber_1.
|
|
30
|
+
return (0, bignumber_1.bn)(0);
|
|
31
31
|
}
|
|
32
32
|
const ownerAddress = receiveAddress;
|
|
33
33
|
const spenderAddress = allowanceContract;
|
|
@@ -49,7 +49,7 @@ const getAllowanceRequired = async ({ receiveAddress, allowanceContract, sellAss
|
|
|
49
49
|
throw new api_1.SwapError(`No allowance data for ${allowanceContract} to ${receiveAddress}`);
|
|
50
50
|
}
|
|
51
51
|
const allowanceRequired = (0, bignumber_1.bnOrZero)(sellAmount).minus(allowanceOnChain);
|
|
52
|
-
return allowanceRequired.lt(0) ? (0, bignumber_1.
|
|
52
|
+
return allowanceRequired.lt(0) ? (0, bignumber_1.bn)(0) : allowanceRequired;
|
|
53
53
|
};
|
|
54
54
|
exports.getAllowanceRequired = getAllowanceRequired;
|
|
55
55
|
const getUsdRate = async (input) => {
|
|
@@ -66,14 +66,15 @@ const getUsdRate = async (input) => {
|
|
|
66
66
|
const price = (0, bignumber_1.bnOrZero)(rateResponse.data.price);
|
|
67
67
|
if (!price.gt(0))
|
|
68
68
|
throw new ZrxSwapper_1.ZrxError('getUsdRate - Failed to get price data');
|
|
69
|
-
return (0, bignumber_1.
|
|
69
|
+
return (0, bignumber_1.bn)(1).dividedBy(price).toString();
|
|
70
70
|
};
|
|
71
71
|
exports.getUsdRate = getUsdRate;
|
|
72
|
-
const grantAllowance = async ({ quote, wallet,
|
|
72
|
+
const grantAllowance = async ({ quote, wallet, adapterManager, erc20Abi, web3 }) => {
|
|
73
73
|
var _a, _b, _c, _d, _e;
|
|
74
74
|
if (!quote.sellAsset.tokenId) {
|
|
75
75
|
throw new Error('sellAsset.tokenId is required');
|
|
76
76
|
}
|
|
77
|
+
const adapter = await adapterManager.byChainId('eip155:1');
|
|
77
78
|
const erc20Contract = new web3.eth.Contract(erc20Abi, quote.sellAsset.tokenId);
|
|
78
79
|
const approveTx = erc20Contract.methods
|
|
79
80
|
.approve(quote.allowanceContract, quote.sellAmount)
|
|
@@ -1,18 +1,9 @@
|
|
|
1
1
|
import { Asset } from '@shapeshiftoss/types';
|
|
2
|
+
import { TradeQuote } from '../../../../api';
|
|
2
3
|
import { ZrxQuoteResponse } from '../../types';
|
|
3
4
|
export declare const setupQuote: () => {
|
|
4
|
-
quoteInput:
|
|
5
|
-
|
|
6
|
-
buyAsset: Asset;
|
|
7
|
-
success: boolean;
|
|
8
|
-
sellAmount: string;
|
|
9
|
-
allowanceContract: string;
|
|
10
|
-
allowanceTarget: string;
|
|
11
|
-
receiveAddress: string;
|
|
12
|
-
sellAssetAccountId: string;
|
|
13
|
-
buyAssetAccountId: string;
|
|
14
|
-
sendMax: boolean;
|
|
15
|
-
};
|
|
5
|
+
quoteInput: import("../../../../api").CommonTradeInput;
|
|
6
|
+
tradeQuote: TradeQuote<"eip155:1" | "bip122:000000000019d6689c085ae165831e93" | "cosmos:cosmoshub-4" | "cosmos:osmosis-1">;
|
|
16
7
|
buyAsset: Asset;
|
|
17
8
|
sellAsset: Asset;
|
|
18
9
|
};
|
|
@@ -5,19 +5,29 @@ const assets_1 = require("./assets");
|
|
|
5
5
|
const setupQuote = () => {
|
|
6
6
|
const sellAsset = { ...assets_1.FOX };
|
|
7
7
|
const buyAsset = { ...assets_1.WETH };
|
|
8
|
-
const
|
|
9
|
-
sellAsset,
|
|
10
|
-
buyAsset,
|
|
8
|
+
const tradeQuote = {
|
|
11
9
|
success: true,
|
|
10
|
+
statusReason: '',
|
|
11
|
+
buyAmount: '',
|
|
12
12
|
sellAmount: '1000000000000000000',
|
|
13
|
+
sellAsset,
|
|
14
|
+
buyAsset,
|
|
13
15
|
allowanceContract: 'allowanceContractAddress',
|
|
14
|
-
allowanceTarget: 'allowanceTargetAddress',
|
|
15
|
-
receiveAddress: 'receiveAddress',
|
|
16
16
|
sellAssetAccountId: '0',
|
|
17
|
-
|
|
17
|
+
minimum: '0',
|
|
18
|
+
maximum: '999999999999',
|
|
19
|
+
feeData: { fee: '0' },
|
|
20
|
+
rate: '1',
|
|
21
|
+
sources: []
|
|
22
|
+
};
|
|
23
|
+
const quoteInput = {
|
|
24
|
+
sellAmount: '1000000000000000000',
|
|
25
|
+
sellAsset,
|
|
26
|
+
buyAsset,
|
|
27
|
+
sellAssetAccountId: '0',
|
|
18
28
|
sendMax: false
|
|
19
29
|
};
|
|
20
|
-
return { quoteInput, buyAsset, sellAsset };
|
|
30
|
+
return { quoteInput, tradeQuote, buyAsset, sellAsset };
|
|
21
31
|
};
|
|
22
32
|
exports.setupQuote = setupQuote;
|
|
23
33
|
const setupZrxTradeQuoteResponse = () => {
|
|
@@ -24,7 +24,8 @@ exports.chainAdapterMockFuncs = {
|
|
|
24
24
|
};
|
|
25
25
|
// @ts-ignore
|
|
26
26
|
chain_adapters_1.ChainAdapterManager.mockImplementation(() => ({
|
|
27
|
-
byChain: jest.fn(() => exports.chainAdapterMockFuncs)
|
|
27
|
+
byChain: jest.fn(() => exports.chainAdapterMockFuncs),
|
|
28
|
+
byChainId: jest.fn(() => exports.chainAdapterMockFuncs)
|
|
28
29
|
}));
|
|
29
30
|
const setupZrxDeps = () => {
|
|
30
31
|
const unchainedUrls = {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { SupportedChainIds } from '@shapeshiftoss/types';
|
|
2
2
|
import { BuildTradeInput, Trade } from '../../..';
|
|
3
3
|
import { ZrxSwapperDeps } from '../ZrxSwapper';
|
|
4
|
-
export declare function zrxBuildTrade({ adapterManager, web3 }: ZrxSwapperDeps, input: BuildTradeInput): Promise<Trade<
|
|
4
|
+
export declare function zrxBuildTrade({ adapterManager, web3 }: ZrxSwapperDeps, input: BuildTradeInput): Promise<Trade<SupportedChainIds>>;
|
|
@@ -24,7 +24,6 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
26
|
exports.zrxBuildTrade = void 0;
|
|
27
|
-
const types_1 = require("@shapeshiftoss/types");
|
|
28
27
|
const rax = __importStar(require("retry-axios"));
|
|
29
28
|
const __1 = require("../../..");
|
|
30
29
|
const erc20Allowance_abi_1 = require("../utils/abi/erc20Allowance-abi");
|
|
@@ -35,10 +34,7 @@ const helpers_1 = require("../utils/helpers/helpers");
|
|
|
35
34
|
const zrxService_1 = require("../utils/zrxService");
|
|
36
35
|
async function zrxBuildTrade({ adapterManager, web3 }, input) {
|
|
37
36
|
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
38
|
-
const { sellAsset, buyAsset, sellAmount,
|
|
39
|
-
if ((buyAmount && sellAmount) || (!buyAmount && !sellAmount)) {
|
|
40
|
-
throw new __1.SwapError('ZrxSwapper:ZrxBuildTrade Exactly one of buyAmount or sellAmount is required');
|
|
41
|
-
}
|
|
37
|
+
const { sellAsset, buyAsset, sellAmount, slippage, sellAssetAccountId, buyAssetAccountId, wallet } = input;
|
|
42
38
|
if (!sellAssetAccountId || !buyAssetAccountId) {
|
|
43
39
|
throw new __1.SwapError('ZrxSwapper:ZrxBuildTrade Both sellAssetAccountId and buyAssetAccountId are required');
|
|
44
40
|
}
|
|
@@ -50,8 +46,8 @@ async function zrxBuildTrade({ adapterManager, web3 }, input) {
|
|
|
50
46
|
if (!sellToken) {
|
|
51
47
|
throw new __1.SwapError('ZrxSwapper:ZrxBuildTrade One of sellAssetContract or sellAssetSymbol or sellAssetNetwork are required');
|
|
52
48
|
}
|
|
53
|
-
if (buyAsset.
|
|
54
|
-
throw new __1.SwapError(
|
|
49
|
+
if (buyAsset.chainId !== 'eip155:1') {
|
|
50
|
+
throw new __1.SwapError('ZrxSwapper:ZrxBuildTrade buyAsset must be on chainId eip155:1');
|
|
55
51
|
}
|
|
56
52
|
const adapter = adapterManager.byChain(buyAsset.chain);
|
|
57
53
|
const bip44Params = adapter.buildBIP44Params({ accountNumber: Number(buyAssetAccountId) });
|
|
@@ -67,7 +63,6 @@ async function zrxBuildTrade({ adapterManager, web3 }, input) {
|
|
|
67
63
|
* sellToken: contract address (or symbol) of token to sell
|
|
68
64
|
* buyToken: contractAddress (or symbol) of token to buy
|
|
69
65
|
* sellAmount?: integer string value of the smallest increment of the sell token
|
|
70
|
-
* buyAmount?: integer string value of the smallest incremtent of the buy token
|
|
71
66
|
* }
|
|
72
67
|
*/
|
|
73
68
|
const zrxRetry = (0, applyAxiosRetry_1.applyAxiosRetry)(zrxService_1.zrxService, {
|
|
@@ -92,7 +87,6 @@ async function zrxBuildTrade({ adapterManager, web3 }, input) {
|
|
|
92
87
|
buyToken,
|
|
93
88
|
sellToken,
|
|
94
89
|
sellAmount: (0, helpers_1.normalizeAmount)(sellAmount === null || sellAmount === void 0 ? void 0 : sellAmount.toString()),
|
|
95
|
-
buyAmount: (0, helpers_1.normalizeAmount)(buyAmount === null || buyAmount === void 0 ? void 0 : buyAmount.toString()),
|
|
96
90
|
takerAddress: receiveAddress,
|
|
97
91
|
slippagePercentage,
|
|
98
92
|
skipValidation: false,
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { ChainTypes } from '@shapeshiftoss/types';
|
|
2
1
|
import { ExecuteTradeInput, TradeResult } from '../../../api';
|
|
3
2
|
import { ZrxSwapperDeps } from '../ZrxSwapper';
|
|
4
|
-
export declare function zrxExecuteTrade({ adapterManager }: ZrxSwapperDeps, { trade, wallet }: ExecuteTradeInput<
|
|
3
|
+
export declare function zrxExecuteTrade({ adapterManager }: ZrxSwapperDeps, { trade, wallet }: ExecuteTradeInput<'eip155:1'>): Promise<TradeResult>;
|