@shapeshiftoss/swapper 9.1.1 → 9.2.2
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/swappers/cow/types.d.ts +0 -4
- package/dist/swappers/cow/utils/constants.d.ts +2 -1
- package/dist/swappers/cow/utils/constants.js +3 -2
- package/dist/swappers/cow/utils/helpers/helpers.d.ts +8 -3
- package/dist/swappers/cow/utils/helpers/helpers.js +43 -17
- package/dist/swappers/thorchain/utils/estimateTradeFee/estimateTradeFee.js +18 -10
- package/dist/swappers/thorchain/utils/test-data/setupThorswapDeps.js +2 -0
- package/package.json +10 -10
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export declare const MAX_ALLOWANCE = "100000000000000000000000000";
|
|
2
2
|
export declare const MAX_COWSWAP_TRADE = "100000000000000000000000000";
|
|
3
|
-
export declare const MIN_COWSWAP_VALUE_USD =
|
|
3
|
+
export declare const MIN_COWSWAP_VALUE_USD = 20;
|
|
4
4
|
export declare const DEFAULT_SOURCE: {
|
|
5
5
|
name: string;
|
|
6
6
|
proportion: string;
|
|
@@ -11,6 +11,7 @@ export declare const COW_SWAP_VAULT_RELAYER_ADDRESS = "0xc92e8bdf79f0507f65a392b
|
|
|
11
11
|
export declare const COW_SWAP_SETTLEMENT_ADDRESS = "0x9008D19f58AAbD9eD0D60971565AA8510560ab41";
|
|
12
12
|
export declare const WETH_ASSET_ID = "eip155:1/erc20:0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";
|
|
13
13
|
export declare const ORDER_KIND_SELL = "sell";
|
|
14
|
+
export declare const ORDER_KIND_BUY = "buy";
|
|
14
15
|
export declare const SIGNING_SCHEME = "ethsign";
|
|
15
16
|
export declare const ERC20_TOKEN_BALANCE = "erc20";
|
|
16
17
|
export declare const ORDER_STATUS_FULFILLED = "fulfilled";
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ORDER_STATUS_FULFILLED = exports.ERC20_TOKEN_BALANCE = exports.SIGNING_SCHEME = exports.ORDER_KIND_SELL = exports.WETH_ASSET_ID = exports.COW_SWAP_SETTLEMENT_ADDRESS = 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;
|
|
3
|
+
exports.ORDER_STATUS_FULFILLED = exports.ERC20_TOKEN_BALANCE = exports.SIGNING_SCHEME = exports.ORDER_KIND_BUY = exports.ORDER_KIND_SELL = exports.WETH_ASSET_ID = exports.COW_SWAP_SETTLEMENT_ADDRESS = 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
4
|
const constants_1 = require("@ethersproject/constants");
|
|
5
5
|
exports.MAX_ALLOWANCE = '100000000000000000000000000';
|
|
6
6
|
exports.MAX_COWSWAP_TRADE = '100000000000000000000000000';
|
|
7
|
-
exports.MIN_COWSWAP_VALUE_USD =
|
|
7
|
+
exports.MIN_COWSWAP_VALUE_USD = 20;
|
|
8
8
|
exports.DEFAULT_SOURCE = [{ name: 'CowSwap', proportion: '1' }];
|
|
9
9
|
exports.DEFAULT_ADDRESS = constants_1.AddressZero;
|
|
10
10
|
exports.DEFAULT_APP_DATA = constants_1.HashZero;
|
|
@@ -12,6 +12,7 @@ exports.COW_SWAP_VAULT_RELAYER_ADDRESS = '0xc92e8bdf79f0507f65a392b0ab4667716bfe
|
|
|
12
12
|
exports.COW_SWAP_SETTLEMENT_ADDRESS = '0x9008D19f58AAbD9eD0D60971565AA8510560ab41';
|
|
13
13
|
exports.WETH_ASSET_ID = 'eip155:1/erc20:0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2';
|
|
14
14
|
exports.ORDER_KIND_SELL = 'sell';
|
|
15
|
+
exports.ORDER_KIND_BUY = 'buy';
|
|
15
16
|
exports.SIGNING_SCHEME = 'ethsign';
|
|
16
17
|
exports.ERC20_TOKEN_BALANCE = 'erc20';
|
|
17
18
|
exports.ORDER_STATUS_FULFILLED = 'fulfilled';
|
|
@@ -23,19 +23,24 @@ export declare type CowSwapOrder = {
|
|
|
23
23
|
sellTokenBalance: string;
|
|
24
24
|
buyTokenBalance: string;
|
|
25
25
|
};
|
|
26
|
-
export declare type
|
|
26
|
+
export declare type CowSwapQuoteApiInputBase = {
|
|
27
27
|
appData: string;
|
|
28
28
|
buyToken: string;
|
|
29
29
|
from: string;
|
|
30
30
|
kind: string;
|
|
31
31
|
partiallyFillable: boolean;
|
|
32
32
|
receiver: string;
|
|
33
|
-
sellAmountBeforeFee: string;
|
|
34
33
|
sellToken: string;
|
|
35
34
|
validTo: number;
|
|
36
35
|
};
|
|
37
|
-
export declare
|
|
36
|
+
export declare type CowSwapSellQuoteApiInput = CowSwapQuoteApiInputBase & {
|
|
37
|
+
sellAmountBeforeFee: string;
|
|
38
|
+
};
|
|
39
|
+
export declare type CowSwapBuyQuoteApiInput = CowSwapQuoteApiInputBase & {
|
|
40
|
+
buyAmountAfterFee: string;
|
|
41
|
+
};
|
|
38
42
|
export declare const getNowPlusThirtyMinutesTimestamp: () => number;
|
|
43
|
+
export declare const getUsdRate: ({ apiUrl }: CowSwapperDeps, input: Asset) => Promise<string>;
|
|
39
44
|
export declare const hashTypedData: (domain: TypedDataDomain, types: TypedDataTypes, data: Record<string, unknown>) => string;
|
|
40
45
|
/**
|
|
41
46
|
* Compute the 32-byte signing hash for the specified order.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.domain = exports.hashOrder = exports.hashTypedData = exports.
|
|
3
|
+
exports.domain = exports.hashOrder = exports.hashTypedData = exports.getUsdRate = exports.getNowPlusThirtyMinutesTimestamp = exports.ORDER_TYPE_FIELDS = void 0;
|
|
4
4
|
const asset_service_1 = require("@shapeshiftoss/asset-service");
|
|
5
5
|
const caip_1 = require("@shapeshiftoss/caip");
|
|
6
6
|
const ethers_1 = require("ethers");
|
|
@@ -24,6 +24,12 @@ exports.ORDER_TYPE_FIELDS = [
|
|
|
24
24
|
{ name: 'sellTokenBalance', type: 'string' },
|
|
25
25
|
{ name: 'buyTokenBalance', type: 'string' }
|
|
26
26
|
];
|
|
27
|
+
const getNowPlusThirtyMinutesTimestamp = () => {
|
|
28
|
+
const ts = new Date();
|
|
29
|
+
ts.setMinutes(ts.getMinutes() + 30);
|
|
30
|
+
return Math.round(ts.getTime() / 1000);
|
|
31
|
+
};
|
|
32
|
+
exports.getNowPlusThirtyMinutesTimestamp = getNowPlusThirtyMinutesTimestamp;
|
|
27
33
|
const getUsdRate = async ({ apiUrl }, input) => {
|
|
28
34
|
// Replacing ETH by WETH specifically for CowSwap in order to get an usd rate when called with ETH as feeAsset
|
|
29
35
|
const asset = input.assetId !== caip_1.ethAssetId ? input : new asset_service_1.AssetService().getAll()[constants_1.WETH_ASSET_ID];
|
|
@@ -37,20 +43,46 @@ const getUsdRate = async ({ apiUrl }, input) => {
|
|
|
37
43
|
if (erc20Address === USDC_CONTRACT_ADDRESS) {
|
|
38
44
|
return '1';
|
|
39
45
|
}
|
|
46
|
+
// rate is imprecise for low $ values, hence asking for $1000
|
|
40
47
|
const buyAmountInDollars = 1000;
|
|
41
|
-
const buyAmount = (0, bignumber_1.bn)(buyAmountInDollars)
|
|
48
|
+
const buyAmount = (0, bignumber_1.bn)(buyAmountInDollars)
|
|
49
|
+
.times((0, bignumber_1.bn)(10).exponentiatedBy(USDC_ASSET_PRECISION))
|
|
50
|
+
.toString();
|
|
42
51
|
try {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
52
|
+
const apiInput = {
|
|
53
|
+
sellToken: erc20Address,
|
|
54
|
+
buyToken: USDC_CONTRACT_ADDRESS,
|
|
55
|
+
receiver: constants_1.DEFAULT_ADDRESS,
|
|
56
|
+
validTo: (0, exports.getNowPlusThirtyMinutesTimestamp)(),
|
|
57
|
+
appData: constants_1.DEFAULT_APP_DATA,
|
|
58
|
+
partiallyFillable: false,
|
|
59
|
+
from: constants_1.DEFAULT_ADDRESS,
|
|
60
|
+
kind: constants_1.ORDER_KIND_BUY,
|
|
61
|
+
buyAmountAfterFee: buyAmount
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* /v1/quote
|
|
65
|
+
* params: {
|
|
66
|
+
* sellToken: contract address of token to sell
|
|
67
|
+
* buyToken: contractAddress of token to buy
|
|
68
|
+
* receiver: receiver address can be defaulted to "0x0000000000000000000000000000000000000000"
|
|
69
|
+
* validTo: time duration during which quote is valid (eg : 1654851610 as timestamp)
|
|
70
|
+
* appData: appData for the CowSwap quote that can be used later, can be defaulted to "0x0000000000000000000000000000000000000000000000000000000000000000"
|
|
71
|
+
* partiallyFillable: false
|
|
72
|
+
* from: sender address can be defaulted to "0x0000000000000000000000000000000000000000"
|
|
73
|
+
* kind: "sell" or "buy"
|
|
74
|
+
* sellAmountBeforeFee / buyAmountAfterFee: amount in base unit
|
|
75
|
+
* }
|
|
76
|
+
*/
|
|
77
|
+
const quoteResponse = await cowService_1.cowService.post(`${apiUrl}/v1/quote/`, apiInput);
|
|
78
|
+
const { data: { quote } } = quoteResponse;
|
|
79
|
+
const sellCryptoAmount = (0, bignumber_1.bn)(quote.sellAmount).div((0, bignumber_1.bn)(10).exponentiatedBy(asset.precision));
|
|
80
|
+
if (!sellCryptoAmount.gt(0))
|
|
81
|
+
throw new api_1.SwapError('[getUsdRate] - Failed to get sell token amount', {
|
|
50
82
|
code: api_1.SwapErrorTypes.RESPONSE_ERROR
|
|
51
83
|
});
|
|
52
|
-
// dividing $1000 by amount of token received
|
|
53
|
-
return (0, bignumber_1.bn)(buyAmountInDollars).
|
|
84
|
+
// dividing $1000 by amount of sell token received
|
|
85
|
+
return (0, bignumber_1.bn)(buyAmountInDollars).div(sellCryptoAmount).toString();
|
|
54
86
|
}
|
|
55
87
|
catch (e) {
|
|
56
88
|
if (e instanceof api_1.SwapError)
|
|
@@ -62,12 +94,6 @@ const getUsdRate = async ({ apiUrl }, input) => {
|
|
|
62
94
|
}
|
|
63
95
|
};
|
|
64
96
|
exports.getUsdRate = getUsdRate;
|
|
65
|
-
const getNowPlusThirtyMinutesTimestamp = () => {
|
|
66
|
-
const ts = new Date();
|
|
67
|
-
ts.setMinutes(ts.getMinutes() + 30);
|
|
68
|
-
return Math.round(ts.getTime() / 1000);
|
|
69
|
-
};
|
|
70
|
-
exports.getNowPlusThirtyMinutesTimestamp = getNowPlusThirtyMinutesTimestamp;
|
|
71
97
|
const hashTypedData = (domain, types, data) => {
|
|
72
98
|
return ethers_1.ethers.utils._TypedDataEncoder.hash(domain, types, data);
|
|
73
99
|
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.estimateTradeFee = void 0;
|
|
4
|
+
const asset_service_1 = require("@shapeshiftoss/asset-service");
|
|
4
5
|
const caip_1 = require("@shapeshiftoss/caip");
|
|
5
6
|
const api_1 = require("../../../../api");
|
|
6
7
|
const bignumber_1 = require("../../../utils/bignumber");
|
|
@@ -40,28 +41,35 @@ const estimateTradeFee = async (deps, buyAsset) => {
|
|
|
40
41
|
details: { thorPoolChainId }
|
|
41
42
|
});
|
|
42
43
|
const gasRate = inboundInfo.gas_rate;
|
|
43
|
-
const { chainId, assetNamespace } = (0, caip_1.fromAssetId)(buyAsset.assetId);
|
|
44
|
-
const
|
|
45
|
-
if (!
|
|
44
|
+
const { chainId: buyChainId, assetNamespace } = (0, caip_1.fromAssetId)(buyAsset.assetId);
|
|
45
|
+
const buyAdapter = deps.adapterManager.get(buyChainId);
|
|
46
|
+
if (!buyAdapter)
|
|
47
|
+
throw new api_1.SwapError('[estimateTradeFee] - unable to get buy asset adapter', {
|
|
48
|
+
code: api_1.SwapErrorTypes.VALIDATION_FAILED,
|
|
49
|
+
details: { buyChainId }
|
|
50
|
+
});
|
|
51
|
+
const buyFeeAssetId = buyAdapter.getFeeAssetId();
|
|
52
|
+
if (!buyFeeAssetId)
|
|
46
53
|
throw new api_1.SwapError('[estimateTradeFee] - no fee assetId', {
|
|
47
54
|
code: api_1.SwapErrorTypes.VALIDATION_FAILED,
|
|
48
55
|
details: { buyAssetId: buyAsset.assetId }
|
|
49
56
|
});
|
|
50
|
-
const feeAssetRatio = buyAsset.assetId !==
|
|
57
|
+
const feeAssetRatio = buyAsset.assetId !== buyFeeAssetId
|
|
51
58
|
? await (0, getPriceRatio_1.getPriceRatio)(deps, {
|
|
52
59
|
sellAssetId: buyAsset.assetId,
|
|
53
|
-
buyAssetId:
|
|
60
|
+
buyAssetId: buyFeeAssetId
|
|
54
61
|
})
|
|
55
62
|
: '1';
|
|
56
|
-
|
|
63
|
+
const buyFeeAsset = new asset_service_1.AssetService().getAll()[buyFeeAssetId];
|
|
64
|
+
switch (buyChainId) {
|
|
57
65
|
case 'bip122:000000000019d6689c085ae165831e93':
|
|
58
|
-
return (0, bignumber_1.fromBaseUnit)((0, bignumber_1.bnOrZero)(btcEstimate(gasRate)).times(feeAssetRatio).dp(0),
|
|
66
|
+
return (0, bignumber_1.fromBaseUnit)((0, bignumber_1.bnOrZero)(btcEstimate(gasRate)).times(feeAssetRatio).dp(0), buyFeeAsset.precision);
|
|
59
67
|
case 'eip155:1':
|
|
60
68
|
switch (assetNamespace) {
|
|
61
69
|
case 'slip44':
|
|
62
|
-
return (0, bignumber_1.fromBaseUnit)((0, bignumber_1.bnOrZero)(ethEstimate(gasRate)).times(feeAssetRatio).dp(0),
|
|
70
|
+
return (0, bignumber_1.fromBaseUnit)((0, bignumber_1.bnOrZero)(ethEstimate(gasRate)).times(feeAssetRatio).dp(0), buyFeeAsset.precision);
|
|
63
71
|
case 'erc20':
|
|
64
|
-
return (0, bignumber_1.fromBaseUnit)((0, bignumber_1.bnOrZero)(erc20Estimate(gasRate)).times(feeAssetRatio).dp(0),
|
|
72
|
+
return (0, bignumber_1.fromBaseUnit)((0, bignumber_1.bnOrZero)(erc20Estimate(gasRate)).times(feeAssetRatio).dp(0), buyFeeAsset.precision);
|
|
65
73
|
default:
|
|
66
74
|
throw new api_1.SwapError('[estimateTradeFee] - unsupported asset namespace', {
|
|
67
75
|
code: api_1.SwapErrorTypes.VALIDATION_FAILED,
|
|
@@ -71,7 +79,7 @@ const estimateTradeFee = async (deps, buyAsset) => {
|
|
|
71
79
|
default:
|
|
72
80
|
throw new api_1.SwapError('[estimateTradeFee] - unsupported chain id', {
|
|
73
81
|
code: api_1.SwapErrorTypes.VALIDATION_FAILED,
|
|
74
|
-
details: { chainId }
|
|
82
|
+
details: { chainId: buyChainId }
|
|
75
83
|
});
|
|
76
84
|
}
|
|
77
85
|
};
|
|
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.setupThorswapDeps = void 0;
|
|
7
|
+
const caip_1 = require("@shapeshiftoss/caip");
|
|
7
8
|
const chain_adapters_1 = require("@shapeshiftoss/chain-adapters");
|
|
8
9
|
const web3_1 = __importDefault(require("web3"));
|
|
9
10
|
jest.mock('web3');
|
|
@@ -22,6 +23,7 @@ const setupThorswapDeps = () => {
|
|
|
22
23
|
buildBIP44Params: jest.fn(() => ({ purpose: 44, coinType: 60, accountNumber: 0 })),
|
|
23
24
|
getAddress: jest.fn(() => Promise.resolve('0xthisIsMyAddress')),
|
|
24
25
|
getFeeData: jest.fn(() => feeData),
|
|
26
|
+
getFeeAssetId: jest.fn(() => caip_1.ethAssetId),
|
|
25
27
|
getGasFeeData: jest.fn(() => ({
|
|
26
28
|
[chain_adapters_1.FeeDataKey.Slow]: {
|
|
27
29
|
gasPrice: '1',
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shapeshiftoss/swapper",
|
|
3
|
-
"version": "9.
|
|
3
|
+
"version": "9.2.2",
|
|
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",
|
|
@@ -30,23 +30,23 @@
|
|
|
30
30
|
"web3": "1.7.4"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
|
-
"@shapeshiftoss/asset-service": "^7.
|
|
34
|
-
"@shapeshiftoss/caip": "^6.
|
|
35
|
-
"@shapeshiftoss/chain-adapters": "^7.
|
|
33
|
+
"@shapeshiftoss/asset-service": "^7.1.0",
|
|
34
|
+
"@shapeshiftoss/caip": "^6.7.0",
|
|
35
|
+
"@shapeshiftoss/chain-adapters": "^7.13.0",
|
|
36
36
|
"@shapeshiftoss/errors": "^1.1.2",
|
|
37
37
|
"@shapeshiftoss/hdwallet-core": "^1.27.0",
|
|
38
|
-
"@shapeshiftoss/types": "^8.
|
|
38
|
+
"@shapeshiftoss/types": "^8.1.0"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"@shapeshiftoss/asset-service": "^7.
|
|
42
|
-
"@shapeshiftoss/caip": "^6.
|
|
43
|
-
"@shapeshiftoss/chain-adapters": "^7.
|
|
41
|
+
"@shapeshiftoss/asset-service": "^7.1.0",
|
|
42
|
+
"@shapeshiftoss/caip": "^6.7.0",
|
|
43
|
+
"@shapeshiftoss/chain-adapters": "^7.13.0",
|
|
44
44
|
"@shapeshiftoss/errors": "^1.1.2",
|
|
45
45
|
"@shapeshiftoss/hdwallet-core": "^1.27.0",
|
|
46
|
-
"@shapeshiftoss/types": "^8.
|
|
46
|
+
"@shapeshiftoss/types": "^8.1.0",
|
|
47
47
|
"@types/readline-sync": "^1.4.4",
|
|
48
48
|
"readline-sync": "^1.4.10",
|
|
49
49
|
"web3-utils": "1.7.4",
|
|
50
|
-
"@shapeshiftoss/unchained-client": "^9.0
|
|
50
|
+
"@shapeshiftoss/unchained-client": "^9.5.0"
|
|
51
51
|
}
|
|
52
52
|
}
|