@shapeshiftoss/swapper 8.0.0 → 8.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api.d.ts +2 -0
- package/dist/swappers/cow/CowSwapper.d.ts +1 -1
- package/dist/swappers/cow/CowSwapper.js +3 -3
- package/dist/swappers/cow/cowApprovalNeeded/cowApprovalNeeded.js +2 -2
- package/dist/swappers/cow/cowBuildTrade/cowBuildTrade.d.ts +4 -0
- package/dist/swappers/cow/cowBuildTrade/cowBuildTrade.js +124 -0
- package/dist/swappers/cow/getCowSwapTradeQuote/getCowSwapTradeQuote.d.ts +0 -11
- package/dist/swappers/cow/getCowSwapTradeQuote/getCowSwapTradeQuote.js +34 -25
- package/dist/swappers/cow/utils/constants.d.ts +0 -1
- package/dist/swappers/cow/utils/constants.js +5 -6
- package/dist/swappers/cow/utils/helpers/helpers.d.ts +12 -0
- package/dist/swappers/cow/utils/helpers/helpers.js +10 -1
- package/dist/swappers/test/TestSwapper.d.ts +1 -0
- package/dist/swappers/test/TestSwapper.js +1 -0
- package/dist/swappers/thorchain/ThorchainSwapper.d.ts +1 -0
- package/dist/swappers/thorchain/ThorchainSwapper.js +1 -0
- package/dist/swappers/utils/helpers/helpers.d.ts +6 -0
- package/dist/swappers/utils/helpers/helpers.js +8 -1
- package/dist/swappers/utils/test-data/assets.d.ts +1 -0
- package/dist/swappers/utils/test-data/assets.js +13 -1
- package/dist/swappers/utils/test-data/setupSwapQuote.d.ts +19 -0
- package/dist/swappers/utils/test-data/setupSwapQuote.js +17 -1
- package/dist/swappers/zrx/ZrxSwapper.d.ts +1 -1
- package/dist/swappers/zrx/ZrxSwapper.js +1 -1
- package/dist/swappers/zrx/utils/test-data/setupZrxSwapQuote.d.ts +0 -19
- package/dist/swappers/zrx/utils/test-data/setupZrxSwapQuote.js +1 -18
- package/package.json +3 -3
package/dist/api.d.ts
CHANGED
|
@@ -160,6 +160,8 @@ export declare enum SwapErrorTypes {
|
|
|
160
160
|
POOL_NOT_FOUND = "POOL_NOT_FOUND"
|
|
161
161
|
}
|
|
162
162
|
export interface Swapper<T extends ChainId> {
|
|
163
|
+
/** Human readable swapper name */
|
|
164
|
+
readonly name: string;
|
|
163
165
|
/** perform any necessary async initialization */
|
|
164
166
|
initialize?(): Promise<void>;
|
|
165
167
|
/** Returns the swapper type */
|
|
@@ -14,7 +14,7 @@ export declare type CowSwapperDeps = {
|
|
|
14
14
|
feeAsset: Asset;
|
|
15
15
|
};
|
|
16
16
|
export declare class CowSwapper implements Swapper<KnownChainIds.EthereumMainnet> {
|
|
17
|
-
|
|
17
|
+
readonly name = "CowSwap";
|
|
18
18
|
deps: CowSwapperDeps;
|
|
19
19
|
constructor(deps: CowSwapperDeps);
|
|
20
20
|
getType(): SwapperType;
|
|
@@ -4,19 +4,20 @@ exports.CowSwapper = void 0;
|
|
|
4
4
|
const api_1 = require("../../api");
|
|
5
5
|
const cowApprovalNeeded_1 = require("./cowApprovalNeeded/cowApprovalNeeded");
|
|
6
6
|
const cowApproveInfinite_1 = require("./cowApproveInfinite/cowApproveInfinite");
|
|
7
|
+
const cowBuildTrade_1 = require("./cowBuildTrade/cowBuildTrade");
|
|
7
8
|
const getCowSwapTradeQuote_1 = require("./getCowSwapTradeQuote/getCowSwapTradeQuote");
|
|
8
9
|
const blacklist_1 = require("./utils/blacklist");
|
|
9
10
|
const helpers_1 = require("./utils/helpers/helpers");
|
|
10
11
|
class CowSwapper {
|
|
11
12
|
constructor(deps) {
|
|
13
|
+
this.name = 'CowSwap';
|
|
12
14
|
this.deps = deps;
|
|
13
15
|
}
|
|
14
16
|
getType() {
|
|
15
17
|
return api_1.SwapperType.CowSwap;
|
|
16
18
|
}
|
|
17
19
|
async buildTrade(args) {
|
|
18
|
-
|
|
19
|
-
throw new Error('CowSwapper: buildTrade unimplemented');
|
|
20
|
+
return (0, cowBuildTrade_1.cowBuildTrade)(this.deps, args);
|
|
20
21
|
}
|
|
21
22
|
async getTradeQuote(input) {
|
|
22
23
|
return (0, getCowSwapTradeQuote_1.getCowSwapTradeQuote)(this.deps, input);
|
|
@@ -51,4 +52,3 @@ class CowSwapper {
|
|
|
51
52
|
}
|
|
52
53
|
}
|
|
53
54
|
exports.CowSwapper = CowSwapper;
|
|
54
|
-
CowSwapper.swapperName = 'CowSwapper';
|
|
@@ -6,7 +6,7 @@ const api_1 = require("../../../api");
|
|
|
6
6
|
const erc20Allowance_abi_1 = require("../../utils/abi/erc20Allowance-abi");
|
|
7
7
|
const bignumber_1 = require("../../utils/bignumber");
|
|
8
8
|
const helpers_1 = require("../../utils/helpers/helpers");
|
|
9
|
-
const
|
|
9
|
+
const constants_1 = require("../utils/constants");
|
|
10
10
|
async function cowApprovalNeeded({ adapter, web3 }, { quote, wallet }) {
|
|
11
11
|
const { sellAsset } = quote;
|
|
12
12
|
const { assetReference: sellAssetErc20Address, assetNamespace } = (0, caip_1.fromAssetId)(sellAsset.assetId);
|
|
@@ -22,7 +22,7 @@ async function cowApprovalNeeded({ adapter, web3 }, { quote, wallet }) {
|
|
|
22
22
|
web3,
|
|
23
23
|
erc20AllowanceAbi: erc20Allowance_abi_1.erc20AllowanceAbi,
|
|
24
24
|
sellAssetErc20Address,
|
|
25
|
-
spenderAddress: COW_SWAP_VAULT_RELAYER_ADDRESS,
|
|
25
|
+
spenderAddress: constants_1.COW_SWAP_VAULT_RELAYER_ADDRESS,
|
|
26
26
|
ownerAddress: receiveAddress
|
|
27
27
|
});
|
|
28
28
|
const allowanceOnChain = (0, bignumber_1.bnOrZero)(allowanceResult);
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { KnownChainIds } from '@shapeshiftoss/types';
|
|
2
|
+
import { BuildTradeInput, Trade } from '../../../api';
|
|
3
|
+
import { CowSwapperDeps } from '../CowSwapper';
|
|
4
|
+
export declare function cowBuildTrade(deps: CowSwapperDeps, input: BuildTradeInput): Promise<Trade<KnownChainIds.EthereumMainnet>>;
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.cowBuildTrade = void 0;
|
|
4
|
+
const caip_1 = require("@shapeshiftoss/caip");
|
|
5
|
+
const api_1 = require("../../../api");
|
|
6
|
+
const erc20Allowance_abi_1 = require("../../utils/abi/erc20Allowance-abi");
|
|
7
|
+
const bignumber_1 = require("../../utils/bignumber");
|
|
8
|
+
const helpers_1 = require("../../utils/helpers/helpers");
|
|
9
|
+
const constants_1 = require("../utils/constants");
|
|
10
|
+
const cowService_1 = require("../utils/cowService");
|
|
11
|
+
const helpers_2 = require("../utils/helpers/helpers");
|
|
12
|
+
async function cowBuildTrade(deps, input) {
|
|
13
|
+
try {
|
|
14
|
+
const { sellAsset, buyAsset, sellAmount, sellAssetAccountNumber, wallet } = input;
|
|
15
|
+
const { adapter, feeAsset, web3 } = deps;
|
|
16
|
+
const { assetReference: sellAssetErc20Address, assetNamespace: sellAssetNamespace } = (0, caip_1.fromAssetId)(sellAsset.assetId);
|
|
17
|
+
const { assetReference: buyAssetErc20Address, assetNamespace: buyAssetNamespace } = (0, caip_1.fromAssetId)(buyAsset.assetId);
|
|
18
|
+
if (buyAssetNamespace !== 'erc20' || sellAssetNamespace !== 'erc20') {
|
|
19
|
+
throw new api_1.SwapError('[cowBuildTrade] - Both assets need to be ERC-20 to use CowSwap', {
|
|
20
|
+
code: api_1.SwapErrorTypes.UNSUPPORTED_PAIR,
|
|
21
|
+
details: { buyAssetNamespace, sellAssetNamespace }
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
const receiveAddress = await adapter.getAddress({ wallet });
|
|
25
|
+
const normalizedSellAmount = (0, helpers_1.normalizeAmount)(sellAmount);
|
|
26
|
+
/**
|
|
27
|
+
* /v1/quote
|
|
28
|
+
* params: {
|
|
29
|
+
* sellToken: contract address of token to sell
|
|
30
|
+
* buyToken: contractAddress of token to buy
|
|
31
|
+
* receiver: receiver address can be defaulted to "0x0000000000000000000000000000000000000000"
|
|
32
|
+
* validTo: time duration during which quote is valid (eg : 1654851610 as timestamp)
|
|
33
|
+
* appData: appData for the CowSwap quote that can be used later, can be defaulted to "0x0000000000000000000000000000000000000000000000000000000000000000"
|
|
34
|
+
* partiallyFillable: false
|
|
35
|
+
* from: sender address can be defaulted to "0x0000000000000000000000000000000000000000"
|
|
36
|
+
* kind: "sell" or "buy"
|
|
37
|
+
* sellAmountBeforeFee / buyAmountAfterFee: amount in base unit
|
|
38
|
+
* }
|
|
39
|
+
*/
|
|
40
|
+
const quoteResponse = await cowService_1.cowService.post(`${deps.apiUrl}/v1/quote/`, {
|
|
41
|
+
sellToken: sellAssetErc20Address,
|
|
42
|
+
buyToken: buyAssetErc20Address,
|
|
43
|
+
receiver: receiveAddress,
|
|
44
|
+
validTo: (0, helpers_2.getNowPlusThirtyMinutesTimestamp)(),
|
|
45
|
+
appData: constants_1.DEFAULT_APP_DATA,
|
|
46
|
+
partiallyFillable: false,
|
|
47
|
+
from: receiveAddress,
|
|
48
|
+
kind: constants_1.ORDER_KIND_SELL,
|
|
49
|
+
sellAmountBeforeFee: normalizedSellAmount
|
|
50
|
+
});
|
|
51
|
+
const { data: { quote } } = quoteResponse;
|
|
52
|
+
const buyCryptoAmount = (0, bignumber_1.bn)(quote.buyAmount).div((0, bignumber_1.bn)(10).exponentiatedBy(buyAsset.precision));
|
|
53
|
+
const sellCryptoAmount = (0, bignumber_1.bn)(quote.sellAmount).div((0, bignumber_1.bn)(10).exponentiatedBy(sellAsset.precision));
|
|
54
|
+
const rate = buyCryptoAmount.div(sellCryptoAmount).toString();
|
|
55
|
+
const data = (0, helpers_1.getApproveContractData)({
|
|
56
|
+
web3,
|
|
57
|
+
spenderAddress: constants_1.COW_SWAP_VAULT_RELAYER_ADDRESS,
|
|
58
|
+
contractAddress: sellAssetErc20Address
|
|
59
|
+
});
|
|
60
|
+
const [feeDataOptions, feeAssetUsdRate, sellAssetUsdRate] = await Promise.all([
|
|
61
|
+
adapter.getFeeData({
|
|
62
|
+
to: sellAssetErc20Address,
|
|
63
|
+
value: '0',
|
|
64
|
+
chainSpecific: { from: receiveAddress, contractData: data }
|
|
65
|
+
}),
|
|
66
|
+
(0, helpers_2.getUsdRate)(deps, feeAsset),
|
|
67
|
+
(0, helpers_2.getUsdRate)(deps, sellAsset)
|
|
68
|
+
]);
|
|
69
|
+
const feeData = feeDataOptions['fast'];
|
|
70
|
+
// quote.feeAmount is expressed in sellAsset token
|
|
71
|
+
// We need fee to be expressed in feeAsset token
|
|
72
|
+
// feeInFeeAsset * feeAssetUsdRate = feeInSellAsset * sellAssetUsdRate
|
|
73
|
+
// hence feeInFeeAsset = feeInSellAsset * sellAssetUsdRate / feeAssetUsdRate
|
|
74
|
+
const feeInSellAsset = (0, bignumber_1.bnOrZero)(quote.feeAmount).div((0, bignumber_1.bn)(10).exponentiatedBy(sellAsset.precision));
|
|
75
|
+
// feeInFeeAsset = feeInSellAsset * sellAssetUsdRate / feeAssetUsdRate
|
|
76
|
+
const feeInFeeAsset = feeInSellAsset
|
|
77
|
+
.multipliedBy((0, bignumber_1.bnOrZero)(sellAssetUsdRate))
|
|
78
|
+
.div((0, bignumber_1.bnOrZero)(feeAssetUsdRate));
|
|
79
|
+
// taking precision into account
|
|
80
|
+
const fee = feeInFeeAsset.multipliedBy((0, bignumber_1.bn)(10).exponentiatedBy(feeAsset.precision)).toString();
|
|
81
|
+
const trade = {
|
|
82
|
+
rate,
|
|
83
|
+
feeData: {
|
|
84
|
+
fee,
|
|
85
|
+
chainSpecific: {
|
|
86
|
+
estimatedGas: feeData.chainSpecific.gasLimit,
|
|
87
|
+
gasPrice: feeData.chainSpecific.gasPrice
|
|
88
|
+
},
|
|
89
|
+
tradeFee: '0'
|
|
90
|
+
},
|
|
91
|
+
sellAmount: normalizedSellAmount,
|
|
92
|
+
buyAmount: quote.buyAmount,
|
|
93
|
+
sources: constants_1.DEFAULT_SOURCE,
|
|
94
|
+
buyAsset,
|
|
95
|
+
sellAsset,
|
|
96
|
+
sellAssetAccountNumber,
|
|
97
|
+
receiveAddress
|
|
98
|
+
};
|
|
99
|
+
const allowanceRequired = await (0, helpers_1.getAllowanceRequired)({
|
|
100
|
+
adapter,
|
|
101
|
+
sellAsset,
|
|
102
|
+
allowanceContract: constants_1.COW_SWAP_VAULT_RELAYER_ADDRESS,
|
|
103
|
+
receiveAddress,
|
|
104
|
+
sellAmount: quote.sellAmount,
|
|
105
|
+
web3: deps.web3,
|
|
106
|
+
erc20AllowanceAbi: erc20Allowance_abi_1.erc20AllowanceAbi
|
|
107
|
+
});
|
|
108
|
+
if (!allowanceRequired.isZero()) {
|
|
109
|
+
trade.feeData.chainSpecific.approvalFee = (0, bignumber_1.bnOrZero)(feeData.chainSpecific.gasLimit)
|
|
110
|
+
.multipliedBy((0, bignumber_1.bnOrZero)(feeData.chainSpecific.gasPrice))
|
|
111
|
+
.toString();
|
|
112
|
+
}
|
|
113
|
+
return trade;
|
|
114
|
+
}
|
|
115
|
+
catch (e) {
|
|
116
|
+
if (e instanceof api_1.SwapError)
|
|
117
|
+
throw e;
|
|
118
|
+
throw new api_1.SwapError('[cowBuildTrade]', {
|
|
119
|
+
cause: e,
|
|
120
|
+
code: api_1.SwapErrorTypes.TRADE_QUOTE_FAILED
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
exports.cowBuildTrade = cowBuildTrade;
|
|
@@ -1,15 +1,4 @@
|
|
|
1
1
|
import { KnownChainIds } from '@shapeshiftoss/types';
|
|
2
2
|
import { GetTradeQuoteInput, TradeQuote } from '../../../api';
|
|
3
3
|
import { CowSwapperDeps } from '../CowSwapper';
|
|
4
|
-
export declare type CowSwapQuoteApiInput = {
|
|
5
|
-
appData: string;
|
|
6
|
-
buyToken: string;
|
|
7
|
-
from: string;
|
|
8
|
-
kind: string;
|
|
9
|
-
partiallyFillable: boolean;
|
|
10
|
-
receiver: string;
|
|
11
|
-
sellAmountBeforeFee: string;
|
|
12
|
-
sellToken: string;
|
|
13
|
-
validTo: number;
|
|
14
|
-
};
|
|
15
4
|
export declare function getCowSwapTradeQuote(deps: CowSwapperDeps, input: GetTradeQuoteInput): Promise<TradeQuote<KnownChainIds.EthereumMainnet>>;
|
|
@@ -4,16 +4,15 @@ exports.getCowSwapTradeQuote = void 0;
|
|
|
4
4
|
const caip_1 = require("@shapeshiftoss/caip");
|
|
5
5
|
const api_1 = require("../../../api");
|
|
6
6
|
const bignumber_1 = require("../../utils/bignumber");
|
|
7
|
-
const constants_1 = require("../../utils/constants");
|
|
8
7
|
const helpers_1 = require("../../utils/helpers/helpers");
|
|
9
8
|
const getCowSwapMinMax_1 = require("../getCowSwapMinMax/getCowSwapMinMax");
|
|
10
|
-
const
|
|
9
|
+
const constants_1 = require("../utils/constants");
|
|
11
10
|
const cowService_1 = require("../utils/cowService");
|
|
12
11
|
const helpers_2 = require("../utils/helpers/helpers");
|
|
13
12
|
async function getCowSwapTradeQuote(deps, input) {
|
|
14
13
|
try {
|
|
15
14
|
const { sellAsset, buyAsset, sellAmount, sellAssetAccountNumber, wallet } = input;
|
|
16
|
-
const { adapter, feeAsset } = deps;
|
|
15
|
+
const { adapter, feeAsset, web3 } = deps;
|
|
17
16
|
const { assetReference: sellAssetErc20Address, assetNamespace: sellAssetNamespace } = (0, caip_1.fromAssetId)(sellAsset.assetId);
|
|
18
17
|
const { assetReference: buyAssetErc20Address, assetNamespace: buyAssetNamespace } = (0, caip_1.fromAssetId)(buyAsset.assetId);
|
|
19
18
|
if (buyAssetNamespace !== 'erc20' || sellAssetNamespace !== 'erc20') {
|
|
@@ -29,12 +28,12 @@ async function getCowSwapTradeQuote(deps, input) {
|
|
|
29
28
|
const apiInput = {
|
|
30
29
|
sellToken: sellAssetErc20Address,
|
|
31
30
|
buyToken: buyAssetErc20Address,
|
|
32
|
-
receiver:
|
|
33
|
-
validTo:
|
|
34
|
-
appData:
|
|
31
|
+
receiver: constants_1.DEFAULT_ADDRESS,
|
|
32
|
+
validTo: (0, helpers_2.getNowPlusThirtyMinutesTimestamp)(),
|
|
33
|
+
appData: constants_1.DEFAULT_APP_DATA,
|
|
35
34
|
partiallyFillable: false,
|
|
36
|
-
from:
|
|
37
|
-
kind:
|
|
35
|
+
from: constants_1.DEFAULT_ADDRESS,
|
|
36
|
+
kind: constants_1.ORDER_KIND_SELL,
|
|
38
37
|
sellAmountBeforeFee: normalizedSellAmount
|
|
39
38
|
};
|
|
40
39
|
/**
|
|
@@ -53,26 +52,36 @@ async function getCowSwapTradeQuote(deps, input) {
|
|
|
53
52
|
*/
|
|
54
53
|
const quoteResponse = await cowService_1.cowService.post(`${deps.apiUrl}/v1/quote/`, apiInput);
|
|
55
54
|
const { data: { quote } } = quoteResponse;
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
const
|
|
61
|
-
|
|
55
|
+
const buyCryptoAmount = (0, bignumber_1.bn)(quote.buyAmount).div((0, bignumber_1.bn)(10).exponentiatedBy(buyAsset.precision));
|
|
56
|
+
const sellCryptoAmount = (0, bignumber_1.bn)(quote.sellAmount).div((0, bignumber_1.bn)(10).exponentiatedBy(sellAsset.precision));
|
|
57
|
+
const rate = buyCryptoAmount.div(sellCryptoAmount).toString();
|
|
58
|
+
const receiveAddress = wallet ? await adapter.getAddress({ wallet }) : constants_1.DEFAULT_ADDRESS;
|
|
59
|
+
const data = (0, helpers_1.getApproveContractData)({
|
|
60
|
+
web3,
|
|
61
|
+
spenderAddress: constants_1.COW_SWAP_VAULT_RELAYER_ADDRESS,
|
|
62
|
+
contractAddress: sellAssetErc20Address
|
|
63
|
+
});
|
|
64
|
+
const [feeDataOptions, feeAssetUsdRate, sellAssetUsdRate] = await Promise.all([
|
|
62
65
|
adapter.getFeeData({
|
|
63
|
-
to:
|
|
66
|
+
to: sellAssetErc20Address,
|
|
64
67
|
value: '0',
|
|
65
|
-
chainSpecific: { from: receiveAddress,
|
|
68
|
+
chainSpecific: { from: receiveAddress, contractData: data }
|
|
66
69
|
}),
|
|
67
70
|
(0, helpers_2.getUsdRate)(deps, feeAsset),
|
|
68
71
|
(0, helpers_2.getUsdRate)(deps, sellAsset)
|
|
69
72
|
]);
|
|
70
73
|
const feeData = feeDataOptions['fast'];
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
74
|
+
// quote.feeAmount is expressed in sellAsset token
|
|
75
|
+
// We need fee to be expressed in feeAsset token
|
|
76
|
+
// feeInFeeAsset * feeAssetUsdRate = feeInSellAsset * sellAssetUsdRate
|
|
77
|
+
// hence feeInFeeAsset = feeInSellAsset * sellAssetUsdRate / feeAssetUsdRate
|
|
78
|
+
const feeInSellAsset = (0, bignumber_1.bnOrZero)(quote.feeAmount).div((0, bignumber_1.bn)(10).exponentiatedBy(sellAsset.precision));
|
|
79
|
+
// feeInFeeAsset = feeInSellAsset * sellAssetUsdRate / feeAssetUsdRate
|
|
80
|
+
const feeInFeeAsset = feeInSellAsset
|
|
81
|
+
.multipliedBy((0, bignumber_1.bnOrZero)(sellAssetUsdRate))
|
|
82
|
+
.div((0, bignumber_1.bnOrZero)(feeAssetUsdRate));
|
|
83
|
+
// taking precision into account
|
|
84
|
+
const fee = feeInFeeAsset.multipliedBy((0, bignumber_1.bn)(10).exponentiatedBy(feeAsset.precision)).toString();
|
|
76
85
|
return {
|
|
77
86
|
rate,
|
|
78
87
|
minimum,
|
|
@@ -82,16 +91,16 @@ async function getCowSwapTradeQuote(deps, input) {
|
|
|
82
91
|
chainSpecific: {
|
|
83
92
|
estimatedGas: feeData.chainSpecific.gasLimit,
|
|
84
93
|
gasPrice: feeData.chainSpecific.gasPrice,
|
|
85
|
-
approvalFee: (0, bignumber_1.bnOrZero)(
|
|
94
|
+
approvalFee: (0, bignumber_1.bnOrZero)(feeData.chainSpecific.gasLimit)
|
|
86
95
|
.multipliedBy((0, bignumber_1.bnOrZero)(feeData.chainSpecific.gasPrice))
|
|
87
96
|
.toString()
|
|
88
97
|
},
|
|
89
98
|
tradeFee: '0'
|
|
90
99
|
},
|
|
91
|
-
sellAmount:
|
|
100
|
+
sellAmount: normalizedSellAmount,
|
|
92
101
|
buyAmount: quote.buyAmount,
|
|
93
|
-
sources:
|
|
94
|
-
allowanceContract:
|
|
102
|
+
sources: constants_1.DEFAULT_SOURCE,
|
|
103
|
+
allowanceContract: constants_1.COW_SWAP_VAULT_RELAYER_ADDRESS,
|
|
95
104
|
buyAsset,
|
|
96
105
|
sellAsset,
|
|
97
106
|
sellAssetAccountNumber
|
|
@@ -8,5 +8,4 @@ export declare const DEFAULT_SOURCE: {
|
|
|
8
8
|
export declare const DEFAULT_ADDRESS = "0x0000000000000000000000000000000000000000";
|
|
9
9
|
export declare const DEFAULT_APP_DATA = "0x0000000000000000000000000000000000000000000000000000000000000000";
|
|
10
10
|
export declare const COW_SWAP_VAULT_RELAYER_ADDRESS = "0xc92e8bdf79f0507f65a392b0ab4667716bfe0110";
|
|
11
|
-
export declare const DEFAULT_VALIDTO_TIMESTAMP = 4294967295;
|
|
12
11
|
export declare const ORDER_KIND_SELL = "sell";
|
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ORDER_KIND_SELL = exports.
|
|
3
|
+
exports.ORDER_KIND_SELL = exports.COW_SWAP_VAULT_RELAYER_ADDRESS = exports.DEFAULT_APP_DATA = exports.DEFAULT_ADDRESS = exports.DEFAULT_SOURCE = exports.MIN_COWSWAP_VALUE_USD = exports.MAX_COWSWAP_TRADE = exports.MAX_ALLOWANCE = void 0;
|
|
4
|
+
const constants_1 = require("@ethersproject/constants");
|
|
5
|
+
exports.MAX_ALLOWANCE = '100000000000000000000000000';
|
|
4
6
|
exports.MAX_COWSWAP_TRADE = '100000000000000000000000000';
|
|
5
7
|
exports.MIN_COWSWAP_VALUE_USD = 10;
|
|
6
8
|
exports.DEFAULT_SOURCE = [{ name: 'CowSwap', proportion: '1' }];
|
|
7
|
-
exports.DEFAULT_ADDRESS =
|
|
8
|
-
exports.DEFAULT_APP_DATA =
|
|
9
|
+
exports.DEFAULT_ADDRESS = constants_1.AddressZero;
|
|
10
|
+
exports.DEFAULT_APP_DATA = constants_1.HashZero;
|
|
9
11
|
exports.COW_SWAP_VAULT_RELAYER_ADDRESS = '0xc92e8bdf79f0507f65a392b0ab4667716bfe0110';
|
|
10
|
-
// CowSwap API requires validTo field which specifies a limit date for the order to be valid
|
|
11
|
-
// This is the default value being used by CowSwap exchange, corresponding to a date in year 2106
|
|
12
|
-
exports.DEFAULT_VALIDTO_TIMESTAMP = 4294967295;
|
|
13
12
|
exports.ORDER_KIND_SELL = 'sell';
|
|
@@ -1,3 +1,15 @@
|
|
|
1
1
|
import { Asset } from '@shapeshiftoss/types';
|
|
2
2
|
import { CowSwapperDeps } from '../../CowSwapper';
|
|
3
|
+
export declare type CowSwapQuoteApiInput = {
|
|
4
|
+
appData: string;
|
|
5
|
+
buyToken: string;
|
|
6
|
+
from: string;
|
|
7
|
+
kind: string;
|
|
8
|
+
partiallyFillable: boolean;
|
|
9
|
+
receiver: string;
|
|
10
|
+
sellAmountBeforeFee: string;
|
|
11
|
+
sellToken: string;
|
|
12
|
+
validTo: number;
|
|
13
|
+
};
|
|
3
14
|
export declare const getUsdRate: ({ apiUrl, feeAsset }: CowSwapperDeps, input: Asset) => Promise<string>;
|
|
15
|
+
export declare const getNowPlusThirtyMinutesTimestamp: () => number;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getUsdRate = void 0;
|
|
3
|
+
exports.getNowPlusThirtyMinutesTimestamp = exports.getUsdRate = void 0;
|
|
4
4
|
const caip_1 = require("@shapeshiftoss/caip");
|
|
5
5
|
const api_1 = require("../../../../api");
|
|
6
6
|
const bignumber_1 = require("../../../utils/bignumber");
|
|
@@ -17,6 +17,9 @@ const getUsdRate = async ({ apiUrl, feeAsset }, input) => {
|
|
|
17
17
|
details: { assetNamespace }
|
|
18
18
|
});
|
|
19
19
|
}
|
|
20
|
+
if (erc20Address === USDC_CONTRACT_ADDRESS) {
|
|
21
|
+
return '1';
|
|
22
|
+
}
|
|
20
23
|
const buyAmountInDollars = 1000;
|
|
21
24
|
const buyAmount = (0, bignumber_1.bn)(buyAmountInDollars).times((0, bignumber_1.bn)(10).exponentiatedBy(USDC_ASSET_PRECISION));
|
|
22
25
|
try {
|
|
@@ -42,3 +45,9 @@ const getUsdRate = async ({ apiUrl, feeAsset }, input) => {
|
|
|
42
45
|
}
|
|
43
46
|
};
|
|
44
47
|
exports.getUsdRate = getUsdRate;
|
|
48
|
+
const getNowPlusThirtyMinutesTimestamp = () => {
|
|
49
|
+
const ts = new Date();
|
|
50
|
+
ts.setMinutes(ts.getMinutes() + 30);
|
|
51
|
+
return Math.round(ts.getTime() / 1000);
|
|
52
|
+
};
|
|
53
|
+
exports.getNowPlusThirtyMinutesTimestamp = getNowPlusThirtyMinutesTimestamp;
|
|
@@ -6,6 +6,7 @@ import { ApprovalNeededOutput, BuyAssetBySellIdInput, Swapper, SwapperType, Trad
|
|
|
6
6
|
* Meant for local testing only
|
|
7
7
|
*/
|
|
8
8
|
export declare class TestSwapper implements Swapper<ChainId> {
|
|
9
|
+
readonly name = "Test";
|
|
9
10
|
supportAssets: string[];
|
|
10
11
|
initialize(): Promise<void>;
|
|
11
12
|
getType(): SwapperType;
|
|
@@ -4,6 +4,7 @@ import { KnownChainIds } from '@shapeshiftoss/types';
|
|
|
4
4
|
import { ApprovalNeededInput, ApprovalNeededOutput, ApproveInfiniteInput, BuyAssetBySellIdInput, ExecuteTradeInput, GetTradeQuoteInput, Swapper, SwapperType, Trade, TradeQuote, TradeResult, TradeTxs } from '../../api';
|
|
5
5
|
import { ThorchainSwapperDeps } from './types';
|
|
6
6
|
export declare class ThorchainSwapper implements Swapper<ChainId> {
|
|
7
|
+
readonly name = "Thorchain";
|
|
7
8
|
private swapSupportedChainIds;
|
|
8
9
|
private supportedAssetIds;
|
|
9
10
|
deps: ThorchainSwapperDeps;
|
|
@@ -11,6 +11,7 @@ const getUsdRate_1 = require("./utils/getUsdRate/getUsdRate");
|
|
|
11
11
|
const thorService_1 = require("./utils/thorService");
|
|
12
12
|
class ThorchainSwapper {
|
|
13
13
|
constructor(deps) {
|
|
14
|
+
this.name = 'Thorchain';
|
|
14
15
|
this.swapSupportedChainIds = {
|
|
15
16
|
'eip155:1': true,
|
|
16
17
|
'bip122:000000000019d6689c085ae165831e93': true
|
|
@@ -21,6 +21,11 @@ export declare type GetERC20AllowanceArgs = {
|
|
|
21
21
|
ownerAddress: string;
|
|
22
22
|
spenderAddress: string;
|
|
23
23
|
};
|
|
24
|
+
export declare type GetApproveContractDataArgs = {
|
|
25
|
+
web3: Web3;
|
|
26
|
+
spenderAddress: string;
|
|
27
|
+
contractAddress: string;
|
|
28
|
+
};
|
|
24
29
|
declare type GrantAllowanceArgs<T extends EvmSupportedChainIds> = {
|
|
25
30
|
quote: TradeQuote<T>;
|
|
26
31
|
wallet: HDWallet;
|
|
@@ -38,4 +43,5 @@ export declare const grantAllowance: <T extends EvmSupportedChainIds>({ quote, w
|
|
|
38
43
|
*/
|
|
39
44
|
export declare const normalizeAmount: (amount: string | number | BN) => string;
|
|
40
45
|
export declare const normalizeIntegerAmount: (amount: string | number | BN) => string;
|
|
46
|
+
export declare const getApproveContractData: ({ web3, spenderAddress, contractAddress }: GetApproveContractDataArgs) => string;
|
|
41
47
|
export {};
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.normalizeIntegerAmount = exports.normalizeAmount = exports.grantAllowance = exports.getAllowanceRequired = exports.getERC20Allowance = void 0;
|
|
3
|
+
exports.getApproveContractData = exports.normalizeIntegerAmount = exports.normalizeAmount = exports.grantAllowance = exports.getAllowanceRequired = exports.getERC20Allowance = void 0;
|
|
4
4
|
const caip_1 = require("@shapeshiftoss/caip");
|
|
5
5
|
const web3_utils_1 = require("web3-utils");
|
|
6
6
|
const api_1 = require("../../../api");
|
|
7
|
+
const constants_1 = require("../../cow/utils/constants");
|
|
8
|
+
const erc20_abi_1 = require("../abi/erc20-abi");
|
|
7
9
|
const bignumber_1 = require("../bignumber");
|
|
8
10
|
const getERC20Allowance = async ({ erc20AllowanceAbi, web3, sellAssetErc20Address, ownerAddress, spenderAddress }) => {
|
|
9
11
|
const erc20Contract = new web3.eth.Contract(erc20AllowanceAbi, sellAssetErc20Address);
|
|
@@ -115,3 +117,8 @@ const normalizeIntegerAmount = (amount) => {
|
|
|
115
117
|
.toLocaleString('fullwide', { useGrouping: false });
|
|
116
118
|
};
|
|
117
119
|
exports.normalizeIntegerAmount = normalizeIntegerAmount;
|
|
120
|
+
const getApproveContractData = ({ web3, spenderAddress, contractAddress }) => {
|
|
121
|
+
const contract = new web3.eth.Contract(erc20_abi_1.erc20Abi, contractAddress);
|
|
122
|
+
return contract.methods.approve(spenderAddress, constants_1.MAX_ALLOWANCE).encodeABI();
|
|
123
|
+
};
|
|
124
|
+
exports.getApproveContractData = getApproveContractData;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.UNSUPPORTED = exports.ETH = exports.WBTC = exports.FOX = exports.WETH = exports.BTC = void 0;
|
|
3
|
+
exports.USDC = exports.UNSUPPORTED = exports.ETH = exports.WBTC = exports.FOX = exports.WETH = exports.BTC = void 0;
|
|
4
4
|
exports.BTC = {
|
|
5
5
|
assetId: 'bip122:000000000019d6689c085ae165831e93/slip44:0',
|
|
6
6
|
chainId: 'bip122:000000000019d6689c085ae165831e93',
|
|
@@ -73,3 +73,15 @@ exports.UNSUPPORTED = {
|
|
|
73
73
|
explorerTxLink: 'https://etherscan.io/tx/',
|
|
74
74
|
explorerAddressLink: 'https://etherscan.io/address/'
|
|
75
75
|
};
|
|
76
|
+
exports.USDC = {
|
|
77
|
+
assetId: 'eip155:1/erc20:0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
|
|
78
|
+
chainId: 'eip155:1',
|
|
79
|
+
symbol: 'USDC',
|
|
80
|
+
name: 'USD Coin',
|
|
81
|
+
precision: 6,
|
|
82
|
+
color: '#2373CB',
|
|
83
|
+
icon: 'https://rawcdn.githack.com/trustwallet/assets/master/blockchains/ethereum/assets/0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48/logo.png',
|
|
84
|
+
explorer: 'https://etherscan.io',
|
|
85
|
+
explorerAddressLink: 'https://etherscan.io/address/',
|
|
86
|
+
explorerTxLink: 'https://etherscan.io/tx/'
|
|
87
|
+
};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { HDWallet } from '@shapeshiftoss/hdwallet-core';
|
|
1
2
|
import { Asset, KnownChainIds } from '@shapeshiftoss/types';
|
|
2
3
|
import { TradeQuote } from '../../../api';
|
|
3
4
|
export declare const setupQuote: () => {
|
|
@@ -6,3 +7,21 @@ export declare const setupQuote: () => {
|
|
|
6
7
|
buyAsset: Asset;
|
|
7
8
|
sellAsset: Asset;
|
|
8
9
|
};
|
|
10
|
+
export declare const setupBuildTrade: () => {
|
|
11
|
+
buildTradeInput: {
|
|
12
|
+
sellAsset: Asset;
|
|
13
|
+
buyAsset: Asset;
|
|
14
|
+
sellAmount: string;
|
|
15
|
+
sendMax: boolean;
|
|
16
|
+
sellAssetAccountNumber: number;
|
|
17
|
+
wallet?: HDWallet | undefined;
|
|
18
|
+
} & {
|
|
19
|
+
chainId: import("../../../api").EvmSupportedChainIds;
|
|
20
|
+
} & {
|
|
21
|
+
buyAssetAccountNumber: number;
|
|
22
|
+
slippage?: string | undefined;
|
|
23
|
+
wallet: HDWallet;
|
|
24
|
+
};
|
|
25
|
+
buyAsset: Asset;
|
|
26
|
+
sellAsset: Asset;
|
|
27
|
+
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.setupQuote = void 0;
|
|
3
|
+
exports.setupBuildTrade = exports.setupQuote = void 0;
|
|
4
4
|
const types_1 = require("@shapeshiftoss/types");
|
|
5
5
|
const assets_1 = require("./assets");
|
|
6
6
|
const setupQuote = () => {
|
|
@@ -30,3 +30,19 @@ const setupQuote = () => {
|
|
|
30
30
|
return { quoteInput, tradeQuote, buyAsset, sellAsset };
|
|
31
31
|
};
|
|
32
32
|
exports.setupQuote = setupQuote;
|
|
33
|
+
const setupBuildTrade = () => {
|
|
34
|
+
const sellAsset = { ...assets_1.FOX };
|
|
35
|
+
const buyAsset = { ...assets_1.WETH };
|
|
36
|
+
const buildTradeInput = {
|
|
37
|
+
chainId: types_1.KnownChainIds.EthereumMainnet,
|
|
38
|
+
sellAmount: '1000000000000000000',
|
|
39
|
+
buyAsset,
|
|
40
|
+
sendMax: false,
|
|
41
|
+
sellAssetAccountNumber: 0,
|
|
42
|
+
buyAssetAccountNumber: 0,
|
|
43
|
+
sellAsset,
|
|
44
|
+
wallet: {}
|
|
45
|
+
};
|
|
46
|
+
return { buildTradeInput, buyAsset, sellAsset };
|
|
47
|
+
};
|
|
48
|
+
exports.setupBuildTrade = setupBuildTrade;
|
|
@@ -3,7 +3,7 @@ import { Asset } from '@shapeshiftoss/types';
|
|
|
3
3
|
import { ApprovalNeededInput, ApprovalNeededOutput, ApproveInfiniteInput, BuildTradeInput, BuyAssetBySellIdInput, EvmSupportedChainIds, GetEvmTradeQuoteInput, Swapper, SwapperType, TradeQuote, TradeResult, TradeTxs } from '../../api';
|
|
4
4
|
import { ZrxExecuteTradeInput, ZrxSwapperDeps, ZrxTrade } from './types';
|
|
5
5
|
export declare class ZrxSwapper<T extends EvmSupportedChainIds> implements Swapper<T> {
|
|
6
|
-
|
|
6
|
+
readonly name = "0x";
|
|
7
7
|
deps: ZrxSwapperDeps;
|
|
8
8
|
chainId: ChainId;
|
|
9
9
|
constructor(deps: ZrxSwapperDeps);
|
|
@@ -12,6 +12,7 @@ const zrxBuildTrade_1 = require("./zrxBuildTrade/zrxBuildTrade");
|
|
|
12
12
|
const zrxExecuteTrade_1 = require("./zrxExecuteTrade/zrxExecuteTrade");
|
|
13
13
|
class ZrxSwapper {
|
|
14
14
|
constructor(deps) {
|
|
15
|
+
this.name = '0x';
|
|
15
16
|
this.deps = deps;
|
|
16
17
|
this.chainId = deps.adapter.getChainId();
|
|
17
18
|
}
|
|
@@ -62,4 +63,3 @@ class ZrxSwapper {
|
|
|
62
63
|
}
|
|
63
64
|
}
|
|
64
65
|
exports.ZrxSwapper = ZrxSwapper;
|
|
65
|
-
ZrxSwapper.swapperName = 'ZrxSwapper';
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { HDWallet } from '@shapeshiftoss/hdwallet-core';
|
|
2
1
|
import { Asset } from '@shapeshiftoss/types';
|
|
3
2
|
import { ZrxQuoteResponse } from '../../types';
|
|
4
3
|
export declare const setupZrxTradeQuoteResponse: () => {
|
|
@@ -6,24 +5,6 @@ export declare const setupZrxTradeQuoteResponse: () => {
|
|
|
6
5
|
buyAsset: Asset;
|
|
7
6
|
sellAsset: Asset;
|
|
8
7
|
};
|
|
9
|
-
export declare const setupBuildTrade: () => {
|
|
10
|
-
buildTradeInput: {
|
|
11
|
-
sellAsset: Asset;
|
|
12
|
-
buyAsset: Asset;
|
|
13
|
-
sellAmount: string;
|
|
14
|
-
sendMax: boolean;
|
|
15
|
-
sellAssetAccountNumber: number;
|
|
16
|
-
wallet?: HDWallet | undefined;
|
|
17
|
-
} & {
|
|
18
|
-
chainId: import("../../../../api").EvmSupportedChainIds;
|
|
19
|
-
} & {
|
|
20
|
-
buyAssetAccountNumber: number;
|
|
21
|
-
slippage?: string | undefined;
|
|
22
|
-
wallet: HDWallet;
|
|
23
|
-
};
|
|
24
|
-
buyAsset: Asset;
|
|
25
|
-
sellAsset: Asset;
|
|
26
|
-
};
|
|
27
8
|
export declare const setupExecuteTrade: () => {
|
|
28
9
|
executeTradeInput: {
|
|
29
10
|
sellAmount: string;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.setupExecuteTrade = exports.
|
|
4
|
-
const types_1 = require("@shapeshiftoss/types");
|
|
3
|
+
exports.setupExecuteTrade = exports.setupZrxTradeQuoteResponse = void 0;
|
|
5
4
|
const assets_1 = require("../../../utils/test-data/assets");
|
|
6
5
|
const setupZrxTradeQuoteResponse = () => {
|
|
7
6
|
const sellAsset = { ...assets_1.FOX };
|
|
@@ -20,22 +19,6 @@ const setupZrxTradeQuoteResponse = () => {
|
|
|
20
19
|
return { quoteResponse, buyAsset, sellAsset };
|
|
21
20
|
};
|
|
22
21
|
exports.setupZrxTradeQuoteResponse = setupZrxTradeQuoteResponse;
|
|
23
|
-
const setupBuildTrade = () => {
|
|
24
|
-
const sellAsset = { ...assets_1.FOX };
|
|
25
|
-
const buyAsset = { ...assets_1.WETH };
|
|
26
|
-
const buildTradeInput = {
|
|
27
|
-
chainId: types_1.KnownChainIds.EthereumMainnet,
|
|
28
|
-
sellAmount: '1000000000000000000',
|
|
29
|
-
buyAsset,
|
|
30
|
-
sendMax: false,
|
|
31
|
-
sellAssetAccountNumber: 0,
|
|
32
|
-
buyAssetAccountNumber: 0,
|
|
33
|
-
sellAsset,
|
|
34
|
-
wallet: {}
|
|
35
|
-
};
|
|
36
|
-
return { buildTradeInput, buyAsset, sellAsset };
|
|
37
|
-
};
|
|
38
|
-
exports.setupBuildTrade = setupBuildTrade;
|
|
39
22
|
const setupExecuteTrade = () => {
|
|
40
23
|
const sellAsset = { ...assets_1.FOX };
|
|
41
24
|
const buyAsset = { ...assets_1.WETH };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shapeshiftoss/swapper",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.2.0",
|
|
4
4
|
"description": "An interface and implementations to trade coins via various protocols",
|
|
5
5
|
"repository": "https://github.com/shapeshift/lib",
|
|
6
6
|
"license": "MIT",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"@shapeshiftoss/chain-adapters": "^7.2.0",
|
|
36
36
|
"@shapeshiftoss/errors": "^1.1.2",
|
|
37
37
|
"@shapeshiftoss/hdwallet-core": "^1.23.0",
|
|
38
|
-
"@shapeshiftoss/types": "^
|
|
38
|
+
"@shapeshiftoss/types": "^7.0.0"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@shapeshiftoss/asset-service": "^6.2.1",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"@shapeshiftoss/chain-adapters": "^7.2.0",
|
|
44
44
|
"@shapeshiftoss/errors": "^1.1.2",
|
|
45
45
|
"@shapeshiftoss/hdwallet-core": "^1.23.0",
|
|
46
|
-
"@shapeshiftoss/types": "^
|
|
46
|
+
"@shapeshiftoss/types": "^7.0.0",
|
|
47
47
|
"@types/readline-sync": "^1.4.4",
|
|
48
48
|
"readline-sync": "^1.4.10",
|
|
49
49
|
"web3-utils": "1.7.4",
|