@shapeshiftoss/swapper 4.0.0 → 4.0.3
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 +5 -20
- package/dist/swappercli.js +16 -10
- package/dist/swappers/test/TestSwapper.d.ts +2 -5
- package/dist/swappers/test/TestSwapper.js +0 -9
- package/dist/swappers/thorchain/ThorchainSwapper.d.ts +2 -5
- package/dist/swappers/thorchain/ThorchainSwapper.js +0 -9
- package/dist/swappers/zrx/ZrxSwapper.d.ts +3 -6
- package/dist/swappers/zrx/ZrxSwapper.js +4 -16
- package/dist/swappers/zrx/getZrxMinMax/getZrxMinMax.d.ts +2 -2
- package/dist/swappers/zrx/getZrxMinMax/getZrxMinMax.js +2 -3
- package/dist/swappers/zrx/getZrxTradeQuote/getZrxTradeQuote.js +7 -19
- package/dist/swappers/zrx/utils/helpers/helpers.d.ts +3 -2
- package/dist/swappers/zrx/utils/test-data/setupSwapQuote.d.ts +56 -2
- package/dist/swappers/zrx/utils/test-data/setupSwapQuote.js +58 -6
- package/dist/swappers/zrx/zrxBuildTrade/zrxBuildTrade.js +1 -6
- package/package.json +1 -1
- package/dist/swappers/zrx/ZrxBuildQuoteTx/ZrxBuildQuoteTx.d.ts +0 -3
- package/dist/swappers/zrx/ZrxBuildQuoteTx/ZrxBuildQuoteTx.js +0 -161
- package/dist/swappers/zrx/ZrxExecuteQuote/ZrxExecuteQuote.d.ts +0 -3
- package/dist/swappers/zrx/ZrxExecuteQuote/ZrxExecuteQuote.js +0 -79
- package/dist/swappers/zrx/getZrxQuote/getZrxQuote.d.ts +0 -2
- package/dist/swappers/zrx/getZrxQuote/getZrxQuote.js +0 -95
package/dist/api.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AssetId } from '@shapeshiftoss/caip';
|
|
2
2
|
import { HDWallet } from '@shapeshiftoss/hdwallet-core';
|
|
3
|
-
import { ApprovalNeededOutput, Asset,
|
|
3
|
+
import { ApprovalNeededOutput, Asset, chainAdapters, ChainTypes, ExecQuoteOutput, GetMinMaxInput, MinMaxOutput, SwapperType } from '@shapeshiftoss/types';
|
|
4
4
|
export declare type SupportedAssetInput = {
|
|
5
5
|
assetIds: AssetId[];
|
|
6
6
|
};
|
|
@@ -18,8 +18,7 @@ export declare type SupportedSellAssetsInput = {
|
|
|
18
18
|
export declare type CommonTradeInput = {
|
|
19
19
|
sellAsset: Asset;
|
|
20
20
|
buyAsset: Asset;
|
|
21
|
-
sellAmount
|
|
22
|
-
buyAmount?: string;
|
|
21
|
+
sellAmount: string;
|
|
23
22
|
sendMax: boolean;
|
|
24
23
|
sellAssetAccountId: string;
|
|
25
24
|
};
|
|
@@ -63,11 +62,11 @@ export declare type SwapSource = {
|
|
|
63
62
|
proportion: string;
|
|
64
63
|
};
|
|
65
64
|
export declare type ApproveInfiniteInput<C extends ChainTypes> = {
|
|
66
|
-
quote:
|
|
65
|
+
quote: TradeQuote<C>;
|
|
67
66
|
wallet: HDWallet;
|
|
68
67
|
};
|
|
69
68
|
export declare type ApprovalNeededInput<C extends ChainTypes> = {
|
|
70
|
-
quote:
|
|
69
|
+
quote: TradeQuote<C>;
|
|
71
70
|
wallet: HDWallet;
|
|
72
71
|
};
|
|
73
72
|
export declare class SwapError extends Error {
|
|
@@ -83,16 +82,6 @@ export interface Swapper {
|
|
|
83
82
|
* Get a trade quote
|
|
84
83
|
*/
|
|
85
84
|
getTradeQuote(input: GetTradeQuoteInput): Promise<TradeQuote<ChainTypes>>;
|
|
86
|
-
/**
|
|
87
|
-
* Get a Quote along with an unsigned transaction that can be signed and broadcast to execute the swap
|
|
88
|
-
* @deprecated The method is going away soon.
|
|
89
|
-
**/
|
|
90
|
-
buildQuoteTx(args: BuildQuoteTxInput): Promise<Quote<ChainTypes>>;
|
|
91
|
-
/**
|
|
92
|
-
* Get a basic quote (rate) for a trading pair
|
|
93
|
-
* @deprecated The method is going away soon.
|
|
94
|
-
*/
|
|
95
|
-
getQuote(input: GetQuoteInput, wallet?: HDWallet): Promise<Quote<ChainTypes>>;
|
|
96
85
|
/**
|
|
97
86
|
* Get the usd rate from either the assets symbol or tokenId
|
|
98
87
|
*/
|
|
@@ -100,11 +89,7 @@ export interface Swapper {
|
|
|
100
89
|
/**
|
|
101
90
|
* Get the minimum and maximum trade value of the sellAsset and buyAsset
|
|
102
91
|
*/
|
|
103
|
-
getMinMax(input:
|
|
104
|
-
/**
|
|
105
|
-
* Execute a quote built with buildQuoteTx by signing and broadcasting
|
|
106
|
-
*/
|
|
107
|
-
executeQuote(args: ExecQuoteInput<ChainTypes>): Promise<ExecQuoteOutput>;
|
|
92
|
+
getMinMax(input: GetMinMaxInput): Promise<MinMaxOutput>;
|
|
108
93
|
/**
|
|
109
94
|
* Execute a trade built with buildTrade by signing and broadcasting
|
|
110
95
|
*/
|
package/dist/swappercli.js
CHANGED
|
@@ -86,15 +86,12 @@ const main = async () => {
|
|
|
86
86
|
manager.addSwapper(types_1.SwapperType.Zrx, zrxSwapper);
|
|
87
87
|
const swapper = manager.getSwapper(types_1.SwapperType.Zrx);
|
|
88
88
|
const sellAmountBase = toBaseUnit(sellAmount, sellAsset.precision);
|
|
89
|
-
const quote = await swapper.
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
buyAssetAccountId: '0'
|
|
96
|
-
},
|
|
97
|
-
wallet
|
|
89
|
+
const quote = await swapper.getTradeQuote({
|
|
90
|
+
sellAsset,
|
|
91
|
+
buyAsset,
|
|
92
|
+
sellAmount: sellAmountBase,
|
|
93
|
+
sellAssetAccountId: '0',
|
|
94
|
+
sendMax: false
|
|
98
95
|
});
|
|
99
96
|
console.info('quote = ', JSON.stringify(quote));
|
|
100
97
|
if (!quote.success) {
|
|
@@ -104,7 +101,16 @@ const main = async () => {
|
|
|
104
101
|
const buyAmount = fromBaseUnit(quote.buyAmount || '0', buyAsset.precision);
|
|
105
102
|
const answer = readline_sync_1.default.question(`Swap ${sellAmount} ${sellAsset.symbol} for ${buyAmount} ${buyAsset.symbol} on ${swapper.getType()}? (y/n): `);
|
|
106
103
|
if (answer === 'y') {
|
|
107
|
-
const
|
|
104
|
+
const trade = await swapper.buildTrade({
|
|
105
|
+
wallet,
|
|
106
|
+
buyAsset,
|
|
107
|
+
sendMax: false,
|
|
108
|
+
sellAmount: sellAmountBase,
|
|
109
|
+
sellAsset,
|
|
110
|
+
sellAssetAccountId: '0',
|
|
111
|
+
buyAssetAccountId: '0'
|
|
112
|
+
});
|
|
113
|
+
const txid = await swapper.executeTrade({ trade, wallet });
|
|
108
114
|
console.info('broadcast tx with id: ', txid);
|
|
109
115
|
}
|
|
110
116
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AssetId } from '@shapeshiftoss/caip';
|
|
2
|
-
import { ApprovalNeededOutput, Asset, ChainTypes, ExecQuoteOutput,
|
|
2
|
+
import { ApprovalNeededOutput, Asset, ChainTypes, ExecQuoteOutput, GetMinMaxInput, MinMaxOutput, 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.
|
|
@@ -9,11 +9,8 @@ export declare class TestSwapper implements Swapper {
|
|
|
9
9
|
supportAssets: string[];
|
|
10
10
|
getType(): SwapperType;
|
|
11
11
|
constructor();
|
|
12
|
-
getQuote(): Promise<Quote<ChainTypes>>;
|
|
13
|
-
buildQuoteTx(): Promise<Quote<ChainTypes>>;
|
|
14
12
|
getUsdRate(input: Pick<Asset, 'symbol' | 'tokenId'>): Promise<string>;
|
|
15
|
-
getMinMax(input:
|
|
16
|
-
executeQuote(): Promise<ExecQuoteOutput>;
|
|
13
|
+
getMinMax(input: GetMinMaxInput): Promise<MinMaxOutput>;
|
|
17
14
|
approvalNeeded(): Promise<ApprovalNeededOutput>;
|
|
18
15
|
approveInfinite(): Promise<string>;
|
|
19
16
|
filterBuyAssetsBySellAssetId(args: BuyAssetBySellIdInput): AssetId[];
|
|
@@ -17,12 +17,6 @@ class TestSwapper {
|
|
|
17
17
|
getType() {
|
|
18
18
|
return types_1.SwapperType.Test;
|
|
19
19
|
}
|
|
20
|
-
async getQuote() {
|
|
21
|
-
throw new Error('TestSwapper: getQuote unimplemented');
|
|
22
|
-
}
|
|
23
|
-
async buildQuoteTx() {
|
|
24
|
-
throw new Error('TestSwapper: getQuote unimplemented');
|
|
25
|
-
}
|
|
26
20
|
getUsdRate(input) {
|
|
27
21
|
console.info(input);
|
|
28
22
|
throw new Error('TestSwapper: getUsdRate unimplemented');
|
|
@@ -31,9 +25,6 @@ class TestSwapper {
|
|
|
31
25
|
console.info(input);
|
|
32
26
|
throw new Error('TestSwapper: getMinMax unimplemented');
|
|
33
27
|
}
|
|
34
|
-
async executeQuote() {
|
|
35
|
-
throw new Error('TestSwapper: executeQuote unimplemented');
|
|
36
|
-
}
|
|
37
28
|
async approvalNeeded() {
|
|
38
29
|
throw new Error('TestSwapper: approvalNeeded unimplemented');
|
|
39
30
|
}
|
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
import { AssetId } from '@shapeshiftoss/caip';
|
|
2
|
-
import { ApprovalNeededOutput, Asset, ChainTypes, ExecQuoteOutput,
|
|
2
|
+
import { ApprovalNeededOutput, Asset, ChainTypes, ExecQuoteOutput, GetMinMaxInput, MinMaxOutput, 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
|
-
getQuote(): Promise<Quote<ChainTypes>>;
|
|
7
|
-
buildQuoteTx(): Promise<Quote<ChainTypes>>;
|
|
8
6
|
getUsdRate(input: Pick<Asset, 'symbol' | 'tokenId'>): Promise<string>;
|
|
9
|
-
getMinMax(input:
|
|
10
|
-
executeQuote(): Promise<ExecQuoteOutput>;
|
|
7
|
+
getMinMax(input: GetMinMaxInput): Promise<MinMaxOutput>;
|
|
11
8
|
approvalNeeded(): Promise<ApprovalNeededOutput>;
|
|
12
9
|
approveInfinite(): Promise<string>;
|
|
13
10
|
filterBuyAssetsBySellAssetId(): AssetId[];
|
|
@@ -6,12 +6,6 @@ class ThorchainSwapper {
|
|
|
6
6
|
getType() {
|
|
7
7
|
return types_1.SwapperType.Thorchain;
|
|
8
8
|
}
|
|
9
|
-
async getQuote() {
|
|
10
|
-
throw new Error('ThorchainSwapper: getQuote unimplemented');
|
|
11
|
-
}
|
|
12
|
-
async buildQuoteTx() {
|
|
13
|
-
throw new Error('ThorchainSwapper: getQuote unimplemented');
|
|
14
|
-
}
|
|
15
9
|
getUsdRate(input) {
|
|
16
10
|
console.info(input);
|
|
17
11
|
throw new Error('ThorchainSwapper: getUsdRate unimplemented');
|
|
@@ -20,9 +14,6 @@ class ThorchainSwapper {
|
|
|
20
14
|
console.info(input);
|
|
21
15
|
throw new Error('ThorchainSwapper: getMinMax unimplemented');
|
|
22
16
|
}
|
|
23
|
-
async executeQuote() {
|
|
24
|
-
throw new Error('ThorchainSwapper: executeQuote unimplemented');
|
|
25
|
-
}
|
|
26
17
|
async approvalNeeded() {
|
|
27
18
|
throw new Error('ThorchainSwapper: approvalNeeded unimplemented');
|
|
28
19
|
}
|
|
@@ -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, ChainTypes, ExecQuoteOutput, GetMinMaxInput, MinMaxOutput, 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,16 +15,13 @@ export declare class ZrxSwapper implements Swapper {
|
|
|
15
15
|
deps: ZrxSwapperDeps;
|
|
16
16
|
constructor(deps: ZrxSwapperDeps);
|
|
17
17
|
getType(): SwapperType;
|
|
18
|
-
buildQuoteTx(args: BuildQuoteTxInput): Promise<Quote<ChainTypes>>;
|
|
19
|
-
getQuote(input: GetQuoteInput): Promise<Quote<ChainTypes>>;
|
|
20
18
|
buildTrade(args: BuildTradeInput): Promise<Trade<ChainTypes>>;
|
|
21
19
|
getTradeQuote(input: GetTradeQuoteInput): Promise<TradeQuote<ChainTypes>>;
|
|
22
20
|
getUsdRate(input: Pick<Asset, 'symbol' | 'tokenId'>): Promise<string>;
|
|
23
|
-
getMinMax(input:
|
|
24
|
-
executeQuote(args: ExecQuoteInput<ChainTypes>): Promise<ExecQuoteOutput>;
|
|
21
|
+
getMinMax(input: GetMinMaxInput): Promise<MinMaxOutput>;
|
|
25
22
|
executeTrade(args: ExecuteTradeInput<ChainTypes>): Promise<ExecQuoteOutput>;
|
|
26
23
|
approvalNeeded(args: ApprovalNeededInput<ChainTypes>): Promise<ApprovalNeededOutput>;
|
|
27
24
|
approveInfinite(args: ApproveInfiniteInput<ChainTypes>): Promise<string>;
|
|
28
25
|
filterBuyAssetsBySellAssetId(args: BuyAssetBySellIdInput): AssetId[];
|
|
29
|
-
filterAssetIdsBySellable(assetIds
|
|
26
|
+
filterAssetIdsBySellable(assetIds?: AssetId[]): AssetId[];
|
|
30
27
|
}
|
|
@@ -3,14 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.ZrxSwapper = exports.ZrxError = void 0;
|
|
4
4
|
const types_1 = require("@shapeshiftoss/types");
|
|
5
5
|
const getZrxMinMax_1 = require("./getZrxMinMax/getZrxMinMax");
|
|
6
|
-
const getZrxQuote_1 = require("./getZrxQuote/getZrxQuote");
|
|
7
6
|
const getZrxTradeQuote_1 = require("./getZrxTradeQuote/getZrxTradeQuote");
|
|
8
7
|
const helpers_1 = require("./utils/helpers/helpers");
|
|
9
8
|
const ZrxApprovalNeeded_1 = require("./ZrxApprovalNeeded/ZrxApprovalNeeded");
|
|
10
9
|
const ZrxApproveInfinite_1 = require("./ZrxApproveInfinite/ZrxApproveInfinite");
|
|
11
|
-
const ZrxBuildQuoteTx_1 = require("./ZrxBuildQuoteTx/ZrxBuildQuoteTx");
|
|
12
10
|
const zrxBuildTrade_1 = require("./zrxBuildTrade/zrxBuildTrade");
|
|
13
|
-
const ZrxExecuteQuote_1 = require("./ZrxExecuteQuote/ZrxExecuteQuote");
|
|
14
11
|
const zrxExecuteTrade_1 = require("./zrxExecuteTrade/zrxExecuteTrade");
|
|
15
12
|
class ZrxError extends Error {
|
|
16
13
|
constructor(message) {
|
|
@@ -26,12 +23,6 @@ class ZrxSwapper {
|
|
|
26
23
|
getType() {
|
|
27
24
|
return types_1.SwapperType.Zrx;
|
|
28
25
|
}
|
|
29
|
-
async buildQuoteTx(args) {
|
|
30
|
-
return (0, ZrxBuildQuoteTx_1.ZrxBuildQuoteTx)(this.deps, args);
|
|
31
|
-
}
|
|
32
|
-
async getQuote(input) {
|
|
33
|
-
return (0, getZrxQuote_1.getZrxQuote)(input);
|
|
34
|
-
}
|
|
35
26
|
async buildTrade(args) {
|
|
36
27
|
return (0, zrxBuildTrade_1.zrxBuildTrade)(this.deps, args);
|
|
37
28
|
}
|
|
@@ -42,10 +33,7 @@ class ZrxSwapper {
|
|
|
42
33
|
return (0, helpers_1.getUsdRate)(input);
|
|
43
34
|
}
|
|
44
35
|
async getMinMax(input) {
|
|
45
|
-
return (0, getZrxMinMax_1.getZrxMinMax)(input);
|
|
46
|
-
}
|
|
47
|
-
async executeQuote(args) {
|
|
48
|
-
return (0, ZrxExecuteQuote_1.ZrxExecuteQuote)(this.deps, args);
|
|
36
|
+
return (0, getZrxMinMax_1.getZrxMinMax)(input.sellAsset, input.buyAsset);
|
|
49
37
|
}
|
|
50
38
|
async executeTrade(args) {
|
|
51
39
|
return (0, zrxExecuteTrade_1.zrxExecuteTrade)(this.deps, args);
|
|
@@ -57,10 +45,10 @@ class ZrxSwapper {
|
|
|
57
45
|
return (0, ZrxApproveInfinite_1.ZrxApproveInfinite)(this.deps, args);
|
|
58
46
|
}
|
|
59
47
|
filterBuyAssetsBySellAssetId(args) {
|
|
60
|
-
const { assetIds, sellAssetId } = args;
|
|
61
|
-
return assetIds.filter((id) => id.startsWith('eip155:1') && sellAssetId.startsWith('eip155:1'));
|
|
48
|
+
const { assetIds = [], sellAssetId } = args;
|
|
49
|
+
return assetIds.filter((id) => id.startsWith('eip155:1') && (sellAssetId === null || sellAssetId === void 0 ? void 0 : sellAssetId.startsWith('eip155:1')));
|
|
62
50
|
}
|
|
63
|
-
filterAssetIdsBySellable(assetIds) {
|
|
51
|
+
filterAssetIdsBySellable(assetIds = []) {
|
|
64
52
|
return assetIds.filter((id) => id.startsWith('eip155:1'));
|
|
65
53
|
}
|
|
66
54
|
}
|
|
@@ -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>;
|
|
@@ -6,8 +6,7 @@ const bignumber_1 = require("../utils/bignumber");
|
|
|
6
6
|
const constants_1 = require("../utils/constants");
|
|
7
7
|
const helpers_1 = require("../utils/helpers/helpers");
|
|
8
8
|
const ZrxSwapper_1 = require("../ZrxSwapper");
|
|
9
|
-
const getZrxMinMax = async (
|
|
10
|
-
const { sellAsset, buyAsset } = input;
|
|
9
|
+
const getZrxMinMax = async (sellAsset, buyAsset) => {
|
|
11
10
|
if (sellAsset.chain !== types_1.ChainTypes.Ethereum || buyAsset.chain !== types_1.ChainTypes.Ethereum) {
|
|
12
11
|
throw new ZrxSwapper_1.ZrxError('getZrxMinMax - must be eth assets');
|
|
13
12
|
}
|
|
@@ -15,7 +14,7 @@ const getZrxMinMax = async (input) => {
|
|
|
15
14
|
symbol: sellAsset.symbol,
|
|
16
15
|
tokenId: sellAsset.tokenId
|
|
17
16
|
});
|
|
18
|
-
const minimum = (0, bignumber_1.
|
|
17
|
+
const minimum = (0, bignumber_1.bn)(1).dividedBy((0, bignumber_1.bnOrZero)(usdRate)).toString();
|
|
19
18
|
return {
|
|
20
19
|
minimum,
|
|
21
20
|
maximum: constants_1.MAX_ZRX_TRADE // Arbitrarily large value. 10e+28 here.
|
|
@@ -10,7 +10,7 @@ const zrxService_1 = require("../utils/zrxService");
|
|
|
10
10
|
const ZrxSwapper_1 = require("../ZrxSwapper");
|
|
11
11
|
async function getZrxTradeQuote(input) {
|
|
12
12
|
var _a, _b, _c, _d, _e, _f, _g;
|
|
13
|
-
const { sellAsset, buyAsset, sellAmount,
|
|
13
|
+
const { sellAsset, buyAsset, sellAmount, sellAssetAccountId } = input;
|
|
14
14
|
if (!buyAsset) {
|
|
15
15
|
throw new ZrxSwapper_1.ZrxError('getQuote - Missing buyAsset');
|
|
16
16
|
}
|
|
@@ -20,24 +20,12 @@ async function getZrxTradeQuote(input) {
|
|
|
20
20
|
if (buyAsset.chain !== types_1.ChainTypes.Ethereum || sellAsset.chain !== types_1.ChainTypes.Ethereum) {
|
|
21
21
|
throw new ZrxSwapper_1.ZrxError('getQuote - Both assets need to be on the Ethereum chain to use Zrx');
|
|
22
22
|
}
|
|
23
|
-
if (!sellAmount && !buyAmount) {
|
|
24
|
-
throw new ZrxSwapper_1.ZrxError('getQuote - sellAmount or buyAmount amount is required');
|
|
25
|
-
}
|
|
26
23
|
const useSellAmount = !!sellAmount;
|
|
27
24
|
const buyToken = buyAsset.tokenId || buyAsset.symbol;
|
|
28
25
|
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
|
-
}
|
|
26
|
+
const { minimum, maximum } = await (0, getZrxMinMax_1.getZrxMinMax)(buyAsset, sellAsset);
|
|
27
|
+
const minQuoteSellAmount = (0, bignumber_1.bnOrZero)(minimum).times((0, bignumber_1.bn)(10).exponentiatedBy(sellAsset.precision));
|
|
28
|
+
const normalizedSellAmount = (0, helpers_1.normalizeAmount)((0, bignumber_1.bnOrZero)(minimum).lt(minQuoteSellAmount) ? minQuoteSellAmount.toString() : sellAmount);
|
|
41
29
|
try {
|
|
42
30
|
/**
|
|
43
31
|
* /swap/v1/price
|
|
@@ -52,7 +40,7 @@ async function getZrxTradeQuote(input) {
|
|
|
52
40
|
params: {
|
|
53
41
|
sellToken,
|
|
54
42
|
buyToken,
|
|
55
|
-
|
|
43
|
+
sellAmount: normalizedSellAmount
|
|
56
44
|
}
|
|
57
45
|
});
|
|
58
46
|
const { data } = quoteResponse;
|
|
@@ -62,8 +50,8 @@ async function getZrxTradeQuote(input) {
|
|
|
62
50
|
success: true,
|
|
63
51
|
statusReason: '',
|
|
64
52
|
rate,
|
|
65
|
-
minimum
|
|
66
|
-
maximum
|
|
53
|
+
minimum,
|
|
54
|
+
maximum,
|
|
67
55
|
feeData: {
|
|
68
56
|
fee: (0, bignumber_1.bnOrZero)(estimatedGas).multipliedBy((0, bignumber_1.bnOrZero)(data.gasPrice)).toString(),
|
|
69
57
|
chainSpecific: {
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { ChainAdapter } from '@shapeshiftoss/chain-adapters';
|
|
2
2
|
import { HDWallet } from '@shapeshiftoss/hdwallet-core';
|
|
3
|
-
import { Asset, ChainTypes
|
|
3
|
+
import { Asset, ChainTypes } 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,7 +21,7 @@ export declare type GetERC20AllowanceArgs = {
|
|
|
20
21
|
spenderAddress: string;
|
|
21
22
|
};
|
|
22
23
|
declare type GrantAllowanceArgs = {
|
|
23
|
-
quote:
|
|
24
|
+
quote: TradeQuote<ChainTypes>;
|
|
24
25
|
wallet: HDWallet;
|
|
25
26
|
adapter: ChainAdapter<ChainTypes.Ethereum>;
|
|
26
27
|
erc20Abi: AbiItem[];
|
|
@@ -6,18 +6,72 @@ export declare const setupQuote: () => {
|
|
|
6
6
|
buyAsset: Asset;
|
|
7
7
|
success: boolean;
|
|
8
8
|
sellAmount: string;
|
|
9
|
-
slippage: string;
|
|
10
9
|
allowanceContract: string;
|
|
11
10
|
allowanceTarget: string;
|
|
12
11
|
receiveAddress: string;
|
|
13
12
|
sellAssetAccountId: string;
|
|
14
13
|
buyAssetAccountId: string;
|
|
14
|
+
sendMax: boolean;
|
|
15
|
+
minimum: string;
|
|
16
|
+
maximum: string;
|
|
17
|
+
statusReason: string;
|
|
18
|
+
buyAmount: string;
|
|
19
|
+
feeData: {
|
|
20
|
+
fee: string;
|
|
21
|
+
};
|
|
22
|
+
rate: string;
|
|
23
|
+
sources: never[];
|
|
15
24
|
};
|
|
16
25
|
buyAsset: Asset;
|
|
17
26
|
sellAsset: Asset;
|
|
18
27
|
};
|
|
19
|
-
export declare const
|
|
28
|
+
export declare const setupZrxTradeQuoteResponse: () => {
|
|
20
29
|
quoteResponse: ZrxQuoteResponse;
|
|
21
30
|
buyAsset: Asset;
|
|
22
31
|
sellAsset: Asset;
|
|
23
32
|
};
|
|
33
|
+
export declare const setupBuildTrade: () => {
|
|
34
|
+
buildTradeInput: {
|
|
35
|
+
sellAmount: string;
|
|
36
|
+
allowanceTarget: string;
|
|
37
|
+
price: string;
|
|
38
|
+
to: string;
|
|
39
|
+
buyAmount: string;
|
|
40
|
+
buyAsset: Asset;
|
|
41
|
+
sendMax: boolean;
|
|
42
|
+
sellAssetAccountId: string;
|
|
43
|
+
buyAssetAccountId: string;
|
|
44
|
+
sellAsset: Asset;
|
|
45
|
+
};
|
|
46
|
+
buyAsset: Asset;
|
|
47
|
+
sellAsset: Asset;
|
|
48
|
+
};
|
|
49
|
+
export declare const setupExecuteTrade: () => {
|
|
50
|
+
executeTradeInput: {
|
|
51
|
+
sellAmount: string;
|
|
52
|
+
allowanceTarget: string;
|
|
53
|
+
price: string;
|
|
54
|
+
to: string;
|
|
55
|
+
gas: string;
|
|
56
|
+
gasPrice: string;
|
|
57
|
+
buyAmount: string;
|
|
58
|
+
buyAsset: Asset;
|
|
59
|
+
sellAsset: Asset;
|
|
60
|
+
sendMax: boolean;
|
|
61
|
+
sellAssetAccountId: string;
|
|
62
|
+
txData: string;
|
|
63
|
+
depositAddress: string;
|
|
64
|
+
receiveAddress: string;
|
|
65
|
+
success: boolean;
|
|
66
|
+
statusReason: string;
|
|
67
|
+
feeData: {
|
|
68
|
+
fee: string;
|
|
69
|
+
chainSpecific: {};
|
|
70
|
+
};
|
|
71
|
+
rate: string;
|
|
72
|
+
allowanceContract: string;
|
|
73
|
+
sources: never[];
|
|
74
|
+
};
|
|
75
|
+
buyAsset: Asset;
|
|
76
|
+
sellAsset: Asset;
|
|
77
|
+
};
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
const constants_1 = require("../constants");
|
|
3
|
+
exports.setupExecuteTrade = exports.setupBuildTrade = exports.setupZrxTradeQuoteResponse = exports.setupQuote = void 0;
|
|
5
4
|
const assets_1 = require("./assets");
|
|
6
5
|
const setupQuote = () => {
|
|
7
6
|
const sellAsset = { ...assets_1.FOX };
|
|
@@ -11,17 +10,24 @@ const setupQuote = () => {
|
|
|
11
10
|
buyAsset,
|
|
12
11
|
success: true,
|
|
13
12
|
sellAmount: '1000000000000000000',
|
|
14
|
-
slippage: constants_1.DEFAULT_SLIPPAGE,
|
|
15
13
|
allowanceContract: 'allowanceContractAddress',
|
|
16
14
|
allowanceTarget: 'allowanceTargetAddress',
|
|
17
15
|
receiveAddress: 'receiveAddress',
|
|
18
16
|
sellAssetAccountId: '0',
|
|
19
|
-
buyAssetAccountId: '0'
|
|
17
|
+
buyAssetAccountId: '0',
|
|
18
|
+
sendMax: false,
|
|
19
|
+
minimum: '0',
|
|
20
|
+
maximum: '999999999999',
|
|
21
|
+
statusReason: '',
|
|
22
|
+
buyAmount: '',
|
|
23
|
+
feeData: { fee: '0' },
|
|
24
|
+
rate: '1',
|
|
25
|
+
sources: []
|
|
20
26
|
};
|
|
21
27
|
return { quoteInput, buyAsset, sellAsset };
|
|
22
28
|
};
|
|
23
29
|
exports.setupQuote = setupQuote;
|
|
24
|
-
const
|
|
30
|
+
const setupZrxTradeQuoteResponse = () => {
|
|
25
31
|
const sellAsset = { ...assets_1.FOX };
|
|
26
32
|
const buyAsset = { ...assets_1.WETH };
|
|
27
33
|
const quoteResponse = {
|
|
@@ -37,4 +43,50 @@ const setupZrxQuoteResponse = () => {
|
|
|
37
43
|
};
|
|
38
44
|
return { quoteResponse, buyAsset, sellAsset };
|
|
39
45
|
};
|
|
40
|
-
exports.
|
|
46
|
+
exports.setupZrxTradeQuoteResponse = setupZrxTradeQuoteResponse;
|
|
47
|
+
const setupBuildTrade = () => {
|
|
48
|
+
const sellAsset = { ...assets_1.FOX };
|
|
49
|
+
const buyAsset = { ...assets_1.WETH };
|
|
50
|
+
const buildTradeInput = {
|
|
51
|
+
sellAmount: '1000000000000000000',
|
|
52
|
+
allowanceTarget: 'allowanceTargetAddress',
|
|
53
|
+
price: '1',
|
|
54
|
+
to: '0x123',
|
|
55
|
+
buyAmount: '',
|
|
56
|
+
buyAsset,
|
|
57
|
+
sendMax: false,
|
|
58
|
+
sellAssetAccountId: '0',
|
|
59
|
+
buyAssetAccountId: '0',
|
|
60
|
+
sellAsset
|
|
61
|
+
};
|
|
62
|
+
return { buildTradeInput, buyAsset, sellAsset };
|
|
63
|
+
};
|
|
64
|
+
exports.setupBuildTrade = setupBuildTrade;
|
|
65
|
+
const setupExecuteTrade = () => {
|
|
66
|
+
const sellAsset = { ...assets_1.FOX };
|
|
67
|
+
const buyAsset = { ...assets_1.WETH };
|
|
68
|
+
const executeTradeInput = {
|
|
69
|
+
sellAmount: '1000000000000000000',
|
|
70
|
+
allowanceTarget: 'allowanceTargetAddress',
|
|
71
|
+
price: '1',
|
|
72
|
+
to: '0x123',
|
|
73
|
+
gas: '1235',
|
|
74
|
+
gasPrice: '1236',
|
|
75
|
+
buyAmount: '',
|
|
76
|
+
buyAsset,
|
|
77
|
+
sellAsset,
|
|
78
|
+
sendMax: false,
|
|
79
|
+
sellAssetAccountId: '0',
|
|
80
|
+
txData: '0x0',
|
|
81
|
+
depositAddress: '0x0',
|
|
82
|
+
receiveAddress: '0x0',
|
|
83
|
+
success: true,
|
|
84
|
+
statusReason: '',
|
|
85
|
+
feeData: { fee: '0', chainSpecific: {} },
|
|
86
|
+
rate: '0',
|
|
87
|
+
allowanceContract: '0x0',
|
|
88
|
+
sources: []
|
|
89
|
+
};
|
|
90
|
+
return { executeTradeInput, buyAsset, sellAsset };
|
|
91
|
+
};
|
|
92
|
+
exports.setupExecuteTrade = setupExecuteTrade;
|
|
@@ -35,10 +35,7 @@ const helpers_1 = require("../utils/helpers/helpers");
|
|
|
35
35
|
const zrxService_1 = require("../utils/zrxService");
|
|
36
36
|
async function zrxBuildTrade({ adapterManager, web3 }, input) {
|
|
37
37
|
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
|
-
}
|
|
38
|
+
const { sellAsset, buyAsset, sellAmount, slippage, sellAssetAccountId, buyAssetAccountId, wallet } = input;
|
|
42
39
|
if (!sellAssetAccountId || !buyAssetAccountId) {
|
|
43
40
|
throw new __1.SwapError('ZrxSwapper:ZrxBuildTrade Both sellAssetAccountId and buyAssetAccountId are required');
|
|
44
41
|
}
|
|
@@ -67,7 +64,6 @@ async function zrxBuildTrade({ adapterManager, web3 }, input) {
|
|
|
67
64
|
* sellToken: contract address (or symbol) of token to sell
|
|
68
65
|
* buyToken: contractAddress (or symbol) of token to buy
|
|
69
66
|
* 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
67
|
* }
|
|
72
68
|
*/
|
|
73
69
|
const zrxRetry = (0, applyAxiosRetry_1.applyAxiosRetry)(zrxService_1.zrxService, {
|
|
@@ -92,7 +88,6 @@ async function zrxBuildTrade({ adapterManager, web3 }, input) {
|
|
|
92
88
|
buyToken,
|
|
93
89
|
sellToken,
|
|
94
90
|
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
91
|
takerAddress: receiveAddress,
|
|
97
92
|
slippagePercentage,
|
|
98
93
|
skipValidation: false,
|
package/package.json
CHANGED
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import { BuildQuoteTxInput, ChainTypes, Quote } from '@shapeshiftoss/types';
|
|
2
|
-
import { ZrxSwapperDeps } from '../ZrxSwapper';
|
|
3
|
-
export declare function ZrxBuildQuoteTx({ adapterManager, web3 }: ZrxSwapperDeps, { input, wallet }: BuildQuoteTxInput): Promise<Quote<ChainTypes>>;
|
|
@@ -1,161 +0,0 @@
|
|
|
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
|
-
};
|
|
25
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.ZrxBuildQuoteTx = void 0;
|
|
27
|
-
const types_1 = require("@shapeshiftoss/types");
|
|
28
|
-
const rax = __importStar(require("retry-axios"));
|
|
29
|
-
const __1 = require("../../..");
|
|
30
|
-
const erc20Allowance_abi_1 = require("../utils/abi/erc20Allowance-abi");
|
|
31
|
-
const applyAxiosRetry_1 = require("../utils/applyAxiosRetry");
|
|
32
|
-
const bignumber_1 = require("../utils/bignumber");
|
|
33
|
-
const constants_1 = require("../utils/constants");
|
|
34
|
-
const helpers_1 = require("../utils/helpers/helpers");
|
|
35
|
-
const zrxService_1 = require("../utils/zrxService");
|
|
36
|
-
async function ZrxBuildQuoteTx({ adapterManager, web3 }, { input, wallet }) {
|
|
37
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
38
|
-
const { sellAsset, buyAsset, sellAmount, buyAmount, slippage, sellAssetAccountId, buyAssetAccountId } = input;
|
|
39
|
-
if ((buyAmount && sellAmount) || (!buyAmount && !sellAmount)) {
|
|
40
|
-
throw new __1.SwapError('ZrxSwapper:ZrxBuildQuoteTx Exactly one of buyAmount or sellAmount is required');
|
|
41
|
-
}
|
|
42
|
-
if (!sellAssetAccountId || !buyAssetAccountId) {
|
|
43
|
-
throw new __1.SwapError('ZrxSwapper:ZrxBuildQuoteTx Both sellAssetAccountId and buyAssetAccountId are required');
|
|
44
|
-
}
|
|
45
|
-
const buyToken = buyAsset.tokenId || buyAsset.symbol || buyAsset.network;
|
|
46
|
-
const sellToken = sellAsset.tokenId || sellAsset.symbol || sellAsset.network;
|
|
47
|
-
if (!buyToken) {
|
|
48
|
-
throw new __1.SwapError('ZrxSwapper:ZrxBuildQuoteTx One of buyAssetContract or buyAssetSymbol or buyAssetNetwork are required');
|
|
49
|
-
}
|
|
50
|
-
if (!sellToken) {
|
|
51
|
-
throw new __1.SwapError('ZrxSwapper:ZrxBuildQuoteTx One of sellAssetContract or sellAssetSymbol or sellAssetNetwork are required');
|
|
52
|
-
}
|
|
53
|
-
if (buyAsset.chain !== types_1.ChainTypes.Ethereum) {
|
|
54
|
-
throw new __1.SwapError(`ZrxSwapper:ZrxBuildQuoteTx buyAsset must be on chain [${types_1.ChainTypes.Ethereum}]`);
|
|
55
|
-
}
|
|
56
|
-
const adapter = adapterManager.byChain(buyAsset.chain);
|
|
57
|
-
const bip44Params = adapter.buildBIP44Params({ accountNumber: Number(buyAssetAccountId) });
|
|
58
|
-
const receiveAddress = await adapter.getAddress({ wallet, bip44Params });
|
|
59
|
-
if ((0, bignumber_1.bnOrZero)(slippage).gt(constants_1.MAX_SLIPPAGE)) {
|
|
60
|
-
throw new __1.SwapError(`ZrxSwapper:ZrxBuildQuoteTx slippage value of ${slippage} is greater than max slippage value of ${constants_1.MAX_SLIPPAGE}`);
|
|
61
|
-
}
|
|
62
|
-
const slippagePercentage = slippage ? (0, bignumber_1.bnOrZero)(slippage).div(100).toString() : constants_1.DEFAULT_SLIPPAGE;
|
|
63
|
-
try {
|
|
64
|
-
/**
|
|
65
|
-
* /swap/v1/quote
|
|
66
|
-
* params: {
|
|
67
|
-
* sellToken: contract address (or symbol) of token to sell
|
|
68
|
-
* buyToken: contractAddress (or symbol) of token to buy
|
|
69
|
-
* 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
|
-
* }
|
|
72
|
-
*/
|
|
73
|
-
const zrxRetry = (0, applyAxiosRetry_1.applyAxiosRetry)(zrxService_1.zrxService, {
|
|
74
|
-
statusCodesToRetry: [[400, 400]],
|
|
75
|
-
shouldRetry: (err) => {
|
|
76
|
-
var _a, _b, _c, _d;
|
|
77
|
-
const cfg = rax.getConfig(err);
|
|
78
|
-
const retryAttempt = (_a = cfg === null || cfg === void 0 ? void 0 : cfg.currentRetryAttempt) !== null && _a !== void 0 ? _a : 0;
|
|
79
|
-
const retry = (_b = cfg === null || cfg === void 0 ? void 0 : cfg.retry) !== null && _b !== void 0 ? _b : 3;
|
|
80
|
-
// ensure max retries is always respected
|
|
81
|
-
if (retryAttempt >= retry)
|
|
82
|
-
return false;
|
|
83
|
-
// retry if 0x returns error code 111 Gas estimation failed
|
|
84
|
-
if (((_d = (_c = err === null || err === void 0 ? void 0 : err.response) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d.code) === 111)
|
|
85
|
-
return true;
|
|
86
|
-
// Handle the request based on your other config options, e.g. `statusCodesToRetry`
|
|
87
|
-
return rax.shouldRetryRequest(err);
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
const quoteResponse = await zrxRetry.get('/swap/v1/quote', {
|
|
91
|
-
params: {
|
|
92
|
-
buyToken,
|
|
93
|
-
sellToken,
|
|
94
|
-
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
|
-
takerAddress: receiveAddress,
|
|
97
|
-
slippagePercentage,
|
|
98
|
-
skipValidation: false,
|
|
99
|
-
affiliateAddress: constants_1.AFFILIATE_ADDRESS
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
const { data } = quoteResponse;
|
|
103
|
-
const estimatedGas = (0, bignumber_1.bnOrZero)(data.gas);
|
|
104
|
-
const quote = {
|
|
105
|
-
sellAsset,
|
|
106
|
-
buyAsset,
|
|
107
|
-
sellAssetAccountId,
|
|
108
|
-
buyAssetAccountId,
|
|
109
|
-
receiveAddress,
|
|
110
|
-
success: true,
|
|
111
|
-
rate: data.price,
|
|
112
|
-
depositAddress: data.to,
|
|
113
|
-
feeData: {
|
|
114
|
-
fee: (0, bignumber_1.bnOrZero)(estimatedGas).multipliedBy((0, bignumber_1.bnOrZero)(data.gasPrice)).toString(),
|
|
115
|
-
chainSpecific: {
|
|
116
|
-
estimatedGas: estimatedGas.toString(),
|
|
117
|
-
gasPrice: data.gasPrice
|
|
118
|
-
}
|
|
119
|
-
},
|
|
120
|
-
txData: data.data,
|
|
121
|
-
sellAmount: data.sellAmount,
|
|
122
|
-
buyAmount: data.buyAmount,
|
|
123
|
-
allowanceContract: data.allowanceTarget,
|
|
124
|
-
sources: ((_a = data.sources) === null || _a === void 0 ? void 0 : _a.filter((s) => parseFloat(s.proportion) > 0)) || constants_1.DEFAULT_SOURCE
|
|
125
|
-
};
|
|
126
|
-
if (!data.allowanceTarget)
|
|
127
|
-
throw new __1.SwapError('ZrxSwapper:ZrxBuildQuoteTx no allowance target');
|
|
128
|
-
if (!data.sellAmount)
|
|
129
|
-
throw new __1.SwapError('ZrxSwapper:ZrxBuildQuoteTx no sell amount');
|
|
130
|
-
const allowanceRequired = await (0, helpers_1.getAllowanceRequired)({
|
|
131
|
-
sellAsset,
|
|
132
|
-
allowanceContract: data.allowanceTarget,
|
|
133
|
-
receiveAddress,
|
|
134
|
-
sellAmount: data.sellAmount,
|
|
135
|
-
web3,
|
|
136
|
-
erc20AllowanceAbi: erc20Allowance_abi_1.erc20AllowanceAbi
|
|
137
|
-
});
|
|
138
|
-
if (allowanceRequired) {
|
|
139
|
-
quote.feeData = {
|
|
140
|
-
fee: ((_b = quote.feeData) === null || _b === void 0 ? void 0 : _b.fee) || '0',
|
|
141
|
-
chainSpecific: {
|
|
142
|
-
...(_c = quote.feeData) === null || _c === void 0 ? void 0 : _c.chainSpecific,
|
|
143
|
-
approvalFee: (0, bignumber_1.bnOrZero)(constants_1.APPROVAL_GAS_LIMIT).multipliedBy((0, bignumber_1.bnOrZero)(data.gasPrice)).toString()
|
|
144
|
-
}
|
|
145
|
-
};
|
|
146
|
-
}
|
|
147
|
-
return quote;
|
|
148
|
-
}
|
|
149
|
-
catch (e) {
|
|
150
|
-
const statusReason = ((_g = (_f = (_e = (_d = e === null || e === void 0 ? void 0 : e.response) === null || _d === void 0 ? void 0 : _d.data) === null || _e === void 0 ? void 0 : _e.validationErrors) === null || _f === void 0 ? void 0 : _f[0]) === null || _g === void 0 ? void 0 : _g.reason) ||
|
|
151
|
-
((_j = (_h = e === null || e === void 0 ? void 0 : e.response) === null || _h === void 0 ? void 0 : _h.data) === null || _j === void 0 ? void 0 : _j.reason) ||
|
|
152
|
-
'Unknown Error';
|
|
153
|
-
return {
|
|
154
|
-
sellAsset,
|
|
155
|
-
buyAsset,
|
|
156
|
-
success: false,
|
|
157
|
-
statusReason
|
|
158
|
-
};
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
exports.ZrxBuildQuoteTx = ZrxBuildQuoteTx;
|
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import { ChainTypes, ExecQuoteInput, ExecQuoteOutput } from '@shapeshiftoss/types';
|
|
2
|
-
import { ZrxSwapperDeps } from '../ZrxSwapper';
|
|
3
|
-
export declare function ZrxExecuteQuote({ adapterManager }: ZrxSwapperDeps, { quote, wallet }: ExecQuoteInput<ChainTypes.Ethereum>): Promise<ExecQuoteOutput>;
|
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ZrxExecuteQuote = void 0;
|
|
4
|
-
const web3_utils_1 = require("web3-utils");
|
|
5
|
-
const api_1 = require("../../../api");
|
|
6
|
-
async function ZrxExecuteQuote({ adapterManager }, { quote, wallet }) {
|
|
7
|
-
var _a, _b, _c, _d, _e, _f;
|
|
8
|
-
const { sellAsset } = quote;
|
|
9
|
-
if (!quote.success) {
|
|
10
|
-
throw new api_1.SwapError('ZrxSwapper:ZrxExecuteQuote Cannot execute a failed quote');
|
|
11
|
-
}
|
|
12
|
-
if (!sellAsset.network || !sellAsset.symbol) {
|
|
13
|
-
throw new api_1.SwapError('ZrxSwapper:ZrxExecuteQuote sellAssetNetwork and sellAssetSymbol are required');
|
|
14
|
-
}
|
|
15
|
-
if (!quote.sellAssetAccountId) {
|
|
16
|
-
throw new api_1.SwapError('ZrxSwapper:ZrxExecuteQuote sellAssetAccountId is required');
|
|
17
|
-
}
|
|
18
|
-
if (!quote.sellAmount) {
|
|
19
|
-
throw new api_1.SwapError('ZrxSwapper:ZrxExecuteQuote sellAmount is required');
|
|
20
|
-
}
|
|
21
|
-
if (!quote.depositAddress) {
|
|
22
|
-
throw new api_1.SwapError('ZrxSwapper:ZrxExecuteQuote depositAddress is required');
|
|
23
|
-
}
|
|
24
|
-
// value is 0 for erc20s
|
|
25
|
-
const value = sellAsset.symbol === 'ETH' ? quote.sellAmount : '0';
|
|
26
|
-
const adapter = adapterManager.byChain(sellAsset.chain);
|
|
27
|
-
const bip44Params = adapter.buildBIP44Params({
|
|
28
|
-
accountNumber: Number(quote.sellAssetAccountId)
|
|
29
|
-
});
|
|
30
|
-
let buildTxResponse, signedTx, txid;
|
|
31
|
-
try {
|
|
32
|
-
buildTxResponse = await adapter.buildSendTransaction({
|
|
33
|
-
value,
|
|
34
|
-
wallet,
|
|
35
|
-
to: quote.depositAddress,
|
|
36
|
-
chainSpecific: {
|
|
37
|
-
gasPrice: (0, web3_utils_1.numberToHex)(((_b = (_a = quote.feeData) === null || _a === void 0 ? void 0 : _a.chainSpecific) === null || _b === void 0 ? void 0 : _b.gasPrice) || 0),
|
|
38
|
-
gasLimit: (0, web3_utils_1.numberToHex)(((_d = (_c = quote.feeData) === null || _c === void 0 ? void 0 : _c.chainSpecific) === null || _d === void 0 ? void 0 : _d.estimatedGas) || 0)
|
|
39
|
-
},
|
|
40
|
-
bip44Params
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
catch (error) {
|
|
44
|
-
throw new api_1.SwapError(`ZrxExecuteQuote - buildSendTransaction error: ${error}`);
|
|
45
|
-
}
|
|
46
|
-
const { txToSign } = buildTxResponse;
|
|
47
|
-
const txWithQuoteData = { ...txToSign, data: (_e = quote.txData) !== null && _e !== void 0 ? _e : '' };
|
|
48
|
-
if (wallet.supportsOfflineSigning()) {
|
|
49
|
-
try {
|
|
50
|
-
signedTx = await adapter.signTransaction({ txToSign: txWithQuoteData, wallet });
|
|
51
|
-
}
|
|
52
|
-
catch (error) {
|
|
53
|
-
throw new api_1.SwapError(`ZrxExecuteQuote - signTransaction error: ${error}`);
|
|
54
|
-
}
|
|
55
|
-
if (!signedTx) {
|
|
56
|
-
throw new api_1.SwapError(`ZrxExecuteQuote - Signed transaction is required: ${signedTx}`);
|
|
57
|
-
}
|
|
58
|
-
try {
|
|
59
|
-
txid = await adapter.broadcastTransaction(signedTx);
|
|
60
|
-
}
|
|
61
|
-
catch (error) {
|
|
62
|
-
throw new api_1.SwapError(`ZrxExecuteQuote - broadcastTransaction error: ${error}`);
|
|
63
|
-
}
|
|
64
|
-
return { txid };
|
|
65
|
-
}
|
|
66
|
-
else if (wallet.supportsBroadcast() && adapter.signAndBroadcastTransaction) {
|
|
67
|
-
try {
|
|
68
|
-
txid = await ((_f = adapter.signAndBroadcastTransaction) === null || _f === void 0 ? void 0 : _f.call(adapter, { txToSign: txWithQuoteData, wallet }));
|
|
69
|
-
}
|
|
70
|
-
catch (error) {
|
|
71
|
-
throw new api_1.SwapError(`ZrxExecuteQuote - signAndBroadcastTransaction error: ${error}`);
|
|
72
|
-
}
|
|
73
|
-
return { txid };
|
|
74
|
-
}
|
|
75
|
-
else {
|
|
76
|
-
throw new api_1.SwapError('ZrxExecuteQuote - invalid HDWallet config');
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
exports.ZrxExecuteQuote = ZrxExecuteQuote;
|
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getZrxQuote = void 0;
|
|
4
|
-
const types_1 = require("@shapeshiftoss/types");
|
|
5
|
-
const getZrxMinMax_1 = require("../getZrxMinMax/getZrxMinMax");
|
|
6
|
-
const bignumber_1 = require("../utils/bignumber");
|
|
7
|
-
const constants_1 = require("../utils/constants");
|
|
8
|
-
const helpers_1 = require("../utils/helpers/helpers");
|
|
9
|
-
const zrxService_1 = require("../utils/zrxService");
|
|
10
|
-
const ZrxSwapper_1 = require("../ZrxSwapper");
|
|
11
|
-
async function getZrxQuote(input) {
|
|
12
|
-
var _a, _b, _c, _d, _e, _f, _g;
|
|
13
|
-
const { sellAsset, buyAsset, sellAmount, buyAmount } = input;
|
|
14
|
-
if (!buyAsset) {
|
|
15
|
-
throw new ZrxSwapper_1.ZrxError('getQuote - Missing buyAsset');
|
|
16
|
-
}
|
|
17
|
-
if (!sellAsset) {
|
|
18
|
-
throw new ZrxSwapper_1.ZrxError('getQuote - Missing sellAsset');
|
|
19
|
-
}
|
|
20
|
-
if (buyAsset.chain !== types_1.ChainTypes.Ethereum || sellAsset.chain !== types_1.ChainTypes.Ethereum) {
|
|
21
|
-
throw new ZrxSwapper_1.ZrxError('getQuote - Both assets need to be on the Ethereum chain to use Zrx');
|
|
22
|
-
}
|
|
23
|
-
if (!sellAmount && !buyAmount) {
|
|
24
|
-
throw new ZrxSwapper_1.ZrxError('getQuote - sellAmount or buyAmount amount is required');
|
|
25
|
-
}
|
|
26
|
-
const useSellAmount = !!sellAmount;
|
|
27
|
-
const buyToken = buyAsset.tokenId || buyAsset.symbol;
|
|
28
|
-
const sellToken = sellAsset.tokenId || sellAsset.symbol;
|
|
29
|
-
const { minimum: minQuoteSellAmount, maximum: maxSellAmount } = await (0, getZrxMinMax_1.getZrxMinMax)(input);
|
|
30
|
-
const minQuoteSellAmountWei = (0, bignumber_1.bnOrZero)(minQuoteSellAmount).times((0, bignumber_1.bnOrZero)(10).exponentiatedBy(sellAsset.precision));
|
|
31
|
-
const amount = useSellAmount ? { sellAmount } : { buyAmount };
|
|
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
|
-
}
|
|
41
|
-
try {
|
|
42
|
-
/**
|
|
43
|
-
* /swap/v1/price
|
|
44
|
-
* params: {
|
|
45
|
-
* sellToken: contract address (or symbol) of token to sell
|
|
46
|
-
* buyToken: contractAddress (or symbol) of token to buy
|
|
47
|
-
* sellAmount?: integer string value of the smallest increment of the sell token
|
|
48
|
-
* buyAmount?: integer string value of the smallest incremtent of the buy token
|
|
49
|
-
* }
|
|
50
|
-
*/
|
|
51
|
-
const quoteResponse = await zrxService_1.zrxService.get('/swap/v1/price', {
|
|
52
|
-
params: {
|
|
53
|
-
sellToken,
|
|
54
|
-
buyToken,
|
|
55
|
-
[amountKey]: normalizedAmount
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
const { data } = quoteResponse;
|
|
59
|
-
const estimatedGas = (0, bignumber_1.bnOrZero)(data.estimatedGas).times(1.5);
|
|
60
|
-
const rate = useSellAmount ? data.price : (0, bignumber_1.bnOrZero)(1).div(data.price).toString();
|
|
61
|
-
return {
|
|
62
|
-
sellAsset,
|
|
63
|
-
buyAsset,
|
|
64
|
-
success: true,
|
|
65
|
-
rate,
|
|
66
|
-
minimum: minQuoteSellAmount,
|
|
67
|
-
maximum: maxSellAmount,
|
|
68
|
-
feeData: {
|
|
69
|
-
fee: (0, bignumber_1.bnOrZero)(estimatedGas).multipliedBy((0, bignumber_1.bnOrZero)(data.gasPrice)).toString(),
|
|
70
|
-
chainSpecific: {
|
|
71
|
-
estimatedGas: estimatedGas.toString(),
|
|
72
|
-
gasPrice: data.gasPrice,
|
|
73
|
-
approvalFee: sellAsset.tokenId &&
|
|
74
|
-
(0, bignumber_1.bnOrZero)(constants_1.APPROVAL_GAS_LIMIT).multipliedBy((0, bignumber_1.bnOrZero)(data.gasPrice)).toString()
|
|
75
|
-
}
|
|
76
|
-
},
|
|
77
|
-
sellAmount: data.sellAmount,
|
|
78
|
-
buyAmount: data.buyAmount,
|
|
79
|
-
sources: ((_a = data.sources) === null || _a === void 0 ? void 0 : _a.filter((s) => parseFloat(s.proportion) > 0)) || constants_1.DEFAULT_SOURCE,
|
|
80
|
-
allowanceContract: data.allowanceTarget
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
catch (e) {
|
|
84
|
-
const statusReason = ((_e = (_d = (_c = (_b = e === null || e === void 0 ? void 0 : e.response) === null || _b === void 0 ? void 0 : _b.data) === null || _c === void 0 ? void 0 : _c.validationErrors) === null || _d === void 0 ? void 0 : _d[0]) === null || _e === void 0 ? void 0 : _e.reason) ||
|
|
85
|
-
((_g = (_f = e === null || e === void 0 ? void 0 : e.response) === null || _f === void 0 ? void 0 : _f.data) === null || _g === void 0 ? void 0 : _g.reason) ||
|
|
86
|
-
'Unknown Error';
|
|
87
|
-
return {
|
|
88
|
-
sellAsset,
|
|
89
|
-
buyAsset,
|
|
90
|
-
success: false,
|
|
91
|
-
statusReason
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
exports.getZrxQuote = getZrxQuote;
|