@shapeshiftoss/swapper 6.3.0 → 7.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 +25 -27
- package/dist/api.js +1 -0
- package/dist/manager/SwapperManager.d.ts +6 -5
- package/dist/swappercli.js +34 -17
- package/dist/swappers/cow/CowApprovalNeeded/CowApprovalNeeded.d.ts +3 -0
- package/dist/swappers/cow/CowApprovalNeeded/CowApprovalNeeded.js +42 -0
- package/dist/swappers/cow/CowApproveInfinite/CowApproveInfinite.d.ts +3 -0
- package/dist/swappers/cow/CowApproveInfinite/CowApproveInfinite.js +31 -0
- package/dist/swappers/cow/CowSwapper.d.ts +11 -7
- package/dist/swappers/cow/CowSwapper.js +13 -8
- package/dist/swappers/cow/getCowSwapMinMax/getCowSwapMinMax.d.ts +4 -0
- package/dist/swappers/cow/getCowSwapMinMax/getCowSwapMinMax.js +28 -0
- package/dist/swappers/cow/utils/blacklist.d.ts +1 -0
- package/dist/swappers/cow/utils/blacklist.js +9 -0
- package/dist/swappers/cow/utils/constants.d.ts +2 -0
- package/dist/swappers/cow/utils/constants.js +4 -0
- package/dist/swappers/index.d.ts +1 -0
- package/dist/swappers/index.js +1 -0
- package/dist/swappers/test/TestSwapper.d.ts +5 -5
- package/dist/swappers/thorchain/ThorchainSwapper.d.ts +6 -6
- package/dist/swappers/thorchain/ThorchainSwapper.js +6 -3
- package/dist/swappers/thorchain/getThorTradeQuote/getTradeQuote.d.ts +2 -2
- package/dist/swappers/thorchain/getThorTradeQuote/getTradeQuote.js +9 -5
- package/dist/swappers/thorchain/utils/txFeeHelpers/ethTxFees/getEthTxFees.js +7 -1
- package/dist/swappers/{zrx/utils → utils}/abi/erc20-abi.d.ts +0 -0
- package/dist/swappers/{zrx/utils → utils}/abi/erc20-abi.js +0 -0
- package/dist/swappers/{zrx/utils → utils}/abi/erc20Allowance-abi.d.ts +0 -0
- package/dist/swappers/{zrx/utils → utils}/abi/erc20Allowance-abi.js +0 -0
- package/dist/swappers/utils/helpers/helpers.d.ts +33 -0
- package/dist/swappers/utils/helpers/helpers.js +98 -1
- package/dist/swappers/utils/test-data/assets.js +0 -13
- package/dist/swappers/utils/test-data/setupDeps.d.ts +6 -0
- package/dist/swappers/utils/test-data/setupDeps.js +48 -0
- package/dist/swappers/utils/test-data/setupSwapQuote.d.ts +8 -0
- package/dist/swappers/utils/test-data/setupSwapQuote.js +30 -0
- package/dist/swappers/zrx/ZrxApprovalNeeded/ZrxApprovalNeeded.d.ts +1 -1
- package/dist/swappers/zrx/ZrxApprovalNeeded/ZrxApprovalNeeded.js +3 -4
- package/dist/swappers/zrx/ZrxApproveInfinite/ZrxApproveInfinite.d.ts +1 -2
- package/dist/swappers/zrx/ZrxApproveInfinite/ZrxApproveInfinite.js +4 -4
- package/dist/swappers/zrx/ZrxSwapper.d.ts +7 -7
- package/dist/swappers/zrx/utils/helpers/helpers.d.ts +1 -33
- package/dist/swappers/zrx/utils/helpers/helpers.js +1 -97
- package/dist/swappers/zrx/utils/test-data/{setupSwapQuote.d.ts → setupZrxSwapQuote.d.ts} +0 -7
- package/dist/swappers/zrx/utils/test-data/{setupSwapQuote.js → setupZrxSwapQuote.js} +1 -27
- package/dist/swappers/zrx/zrxBuildTrade/zrxBuildTrade.d.ts +1 -2
- package/dist/swappers/zrx/zrxBuildTrade/zrxBuildTrade.js +10 -12
- package/dist/swappers/zrx/zrxExecuteTrade/zrxExecuteTrade.d.ts +1 -1
- package/dist/swappers/zrx/zrxExecuteTrade/zrxExecuteTrade.js +1 -2
- package/package.json +10 -10
- package/dist/swappers/zrx/utils/test-data/setupZrxDeps.d.ts +0 -32
- package/dist/swappers/zrx/utils/test-data/setupZrxDeps.js +0 -46
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getEthTxFees = void 0;
|
|
4
|
+
const types_1 = require("@shapeshiftoss/types");
|
|
4
5
|
const api_1 = require("../../../../../api");
|
|
5
6
|
const bignumber_1 = require("../../../../utils/bignumber");
|
|
6
7
|
const constants_1 = require("../../../../utils/constants");
|
|
7
8
|
const estimateTradeFee_1 = require("../../estimateTradeFee/estimateTradeFee");
|
|
8
9
|
const getEthTxFees = async ({ deps, data, router, buyAsset, sellAmount, adapterManager, receiveAddress, sellAssetReference }) => {
|
|
9
10
|
try {
|
|
10
|
-
const adapter = adapterManager.
|
|
11
|
+
const adapter = adapterManager.get(types_1.KnownChainIds.EthereumMainnet);
|
|
12
|
+
if (!adapter)
|
|
13
|
+
throw new api_1.SwapError(`[getThorTxInfo] - No chain adapter found for ${types_1.KnownChainIds.EthereumMainnet}.`, {
|
|
14
|
+
code: api_1.SwapErrorTypes.UNSUPPORTED_CHAIN,
|
|
15
|
+
details: { chainId: types_1.KnownChainIds.EthereumMainnet }
|
|
16
|
+
});
|
|
11
17
|
// TODO(ryankk): add falling back on hard coded gas Limit * new chain adapter method if it fails
|
|
12
18
|
const feeDataOptions = await adapter.getFeeData({
|
|
13
19
|
to: router,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -1,7 +1,40 @@
|
|
|
1
|
+
import { ethereum } from '@shapeshiftoss/chain-adapters';
|
|
2
|
+
import { HDWallet } from '@shapeshiftoss/hdwallet-core';
|
|
3
|
+
import { Asset } from '@shapeshiftoss/types';
|
|
4
|
+
import BigNumber from 'bignumber.js';
|
|
5
|
+
import Web3 from 'web3';
|
|
6
|
+
import { AbiItem } from 'web3-utils';
|
|
7
|
+
import { TradeQuote } from '../../../api';
|
|
1
8
|
import { BN } from '../bignumber';
|
|
9
|
+
export declare type GetAllowanceRequiredArgs = {
|
|
10
|
+
receiveAddress: string;
|
|
11
|
+
allowanceContract: string;
|
|
12
|
+
sellAsset: Asset;
|
|
13
|
+
sellAmount: string;
|
|
14
|
+
web3: Web3;
|
|
15
|
+
erc20AllowanceAbi: AbiItem[];
|
|
16
|
+
};
|
|
17
|
+
export declare type GetERC20AllowanceArgs = {
|
|
18
|
+
erc20AllowanceAbi: AbiItem[];
|
|
19
|
+
web3: Web3;
|
|
20
|
+
sellAssetErc20Address: string;
|
|
21
|
+
ownerAddress: string;
|
|
22
|
+
spenderAddress: string;
|
|
23
|
+
};
|
|
24
|
+
declare type GrantAllowanceArgs = {
|
|
25
|
+
quote: TradeQuote<'eip155:1'>;
|
|
26
|
+
wallet: HDWallet;
|
|
27
|
+
adapter: ethereum.ChainAdapter;
|
|
28
|
+
erc20Abi: AbiItem[];
|
|
29
|
+
web3: Web3;
|
|
30
|
+
};
|
|
31
|
+
export declare const getERC20Allowance: ({ erc20AllowanceAbi, web3, sellAssetErc20Address, ownerAddress, spenderAddress }: GetERC20AllowanceArgs) => Promise<any>;
|
|
32
|
+
export declare const getAllowanceRequired: ({ receiveAddress, allowanceContract, sellAsset, sellAmount, web3, erc20AllowanceAbi }: GetAllowanceRequiredArgs) => Promise<BigNumber>;
|
|
33
|
+
export declare const grantAllowance: ({ quote, wallet, adapter, erc20Abi, web3 }: GrantAllowanceArgs) => Promise<string>;
|
|
2
34
|
/**
|
|
3
35
|
* This function keeps 17 significant digits, so even if we try to trade 1 Billion of an
|
|
4
36
|
* ETH or ERC20, we still keep 7 decimal places.
|
|
5
37
|
* @param amount
|
|
6
38
|
*/
|
|
7
39
|
export declare const normalizeAmount: (amount: string | number | BN) => string;
|
|
40
|
+
export {};
|
|
@@ -1,7 +1,104 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.normalizeAmount = void 0;
|
|
3
|
+
exports.normalizeAmount = exports.grantAllowance = exports.getAllowanceRequired = exports.getERC20Allowance = void 0;
|
|
4
|
+
const caip_1 = require("@shapeshiftoss/caip");
|
|
5
|
+
const web3_utils_1 = require("web3-utils");
|
|
6
|
+
const api_1 = require("../../../api");
|
|
4
7
|
const bignumber_1 = require("../bignumber");
|
|
8
|
+
const getERC20Allowance = async ({ erc20AllowanceAbi, web3, sellAssetErc20Address, ownerAddress, spenderAddress }) => {
|
|
9
|
+
const erc20Contract = new web3.eth.Contract(erc20AllowanceAbi, sellAssetErc20Address);
|
|
10
|
+
return erc20Contract.methods.allowance(ownerAddress, spenderAddress).call();
|
|
11
|
+
};
|
|
12
|
+
exports.getERC20Allowance = getERC20Allowance;
|
|
13
|
+
const getAllowanceRequired = async ({ receiveAddress, allowanceContract, sellAsset, sellAmount, web3, erc20AllowanceAbi }) => {
|
|
14
|
+
try {
|
|
15
|
+
if (sellAsset.assetId === 'eip155:1/slip44:60') {
|
|
16
|
+
return (0, bignumber_1.bn)(0);
|
|
17
|
+
}
|
|
18
|
+
const ownerAddress = receiveAddress;
|
|
19
|
+
const spenderAddress = allowanceContract;
|
|
20
|
+
const { assetReference: sellAssetErc20Address } = (0, caip_1.fromAssetId)(sellAsset.assetId);
|
|
21
|
+
const allowanceOnChain = await (0, exports.getERC20Allowance)({
|
|
22
|
+
web3,
|
|
23
|
+
erc20AllowanceAbi,
|
|
24
|
+
ownerAddress,
|
|
25
|
+
spenderAddress,
|
|
26
|
+
sellAssetErc20Address
|
|
27
|
+
});
|
|
28
|
+
if (allowanceOnChain === '0')
|
|
29
|
+
return (0, bignumber_1.bnOrZero)(sellAmount);
|
|
30
|
+
if (!allowanceOnChain) {
|
|
31
|
+
throw new api_1.SwapError(`[getAllowanceRequired] - No allowance data`, {
|
|
32
|
+
details: { allowanceContract, receiveAddress },
|
|
33
|
+
code: api_1.SwapErrorTypes.RESPONSE_ERROR
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
const allowanceRequired = (0, bignumber_1.bnOrZero)(sellAmount).minus(allowanceOnChain);
|
|
37
|
+
return allowanceRequired.lt(0) ? (0, bignumber_1.bn)(0) : allowanceRequired;
|
|
38
|
+
}
|
|
39
|
+
catch (e) {
|
|
40
|
+
if (e instanceof api_1.SwapError)
|
|
41
|
+
throw e;
|
|
42
|
+
throw new api_1.SwapError('[getAllowanceRequired]', {
|
|
43
|
+
cause: e,
|
|
44
|
+
code: api_1.SwapErrorTypes.ALLOWANCE_REQUIRED_FAILED
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
exports.getAllowanceRequired = getAllowanceRequired;
|
|
49
|
+
const grantAllowance = async ({ quote, wallet, adapter, erc20Abi, web3 }) => {
|
|
50
|
+
var _a, _b, _c, _d, _e;
|
|
51
|
+
try {
|
|
52
|
+
const { assetReference: sellAssetErc20Address } = (0, caip_1.fromAssetId)(quote.sellAsset.assetId);
|
|
53
|
+
const erc20Contract = new web3.eth.Contract(erc20Abi, sellAssetErc20Address);
|
|
54
|
+
const approveTx = erc20Contract.methods
|
|
55
|
+
.approve(quote.allowanceContract, quote.sellAmount)
|
|
56
|
+
.encodeABI();
|
|
57
|
+
const accountNumber = quote.sellAssetAccountNumber;
|
|
58
|
+
const bip44Params = adapter.buildBIP44Params({ accountNumber });
|
|
59
|
+
const { txToSign } = await adapter.buildSendTransaction({
|
|
60
|
+
wallet,
|
|
61
|
+
to: sellAssetErc20Address,
|
|
62
|
+
bip44Params,
|
|
63
|
+
value: '0',
|
|
64
|
+
chainSpecific: {
|
|
65
|
+
erc20ContractAddress: sellAssetErc20Address,
|
|
66
|
+
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),
|
|
67
|
+
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)
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
const grantAllowanceTxToSign = {
|
|
71
|
+
...txToSign,
|
|
72
|
+
data: approveTx
|
|
73
|
+
};
|
|
74
|
+
if (wallet.supportsOfflineSigning()) {
|
|
75
|
+
const signedTx = await adapter.signTransaction({ txToSign: grantAllowanceTxToSign, wallet });
|
|
76
|
+
const broadcastedTxId = await adapter.broadcastTransaction(signedTx);
|
|
77
|
+
return broadcastedTxId;
|
|
78
|
+
}
|
|
79
|
+
else if (wallet.supportsBroadcast() && adapter.signAndBroadcastTransaction) {
|
|
80
|
+
const broadcastedTxId = await ((_e = adapter.signAndBroadcastTransaction) === null || _e === void 0 ? void 0 : _e.call(adapter, {
|
|
81
|
+
txToSign: grantAllowanceTxToSign,
|
|
82
|
+
wallet
|
|
83
|
+
}));
|
|
84
|
+
return broadcastedTxId;
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
throw new api_1.SwapError('[grantAllowance] - invalid HDWallet config', {
|
|
88
|
+
code: api_1.SwapErrorTypes.SIGN_AND_BROADCAST_FAILED
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
catch (e) {
|
|
93
|
+
if (e instanceof api_1.SwapError)
|
|
94
|
+
throw e;
|
|
95
|
+
throw new api_1.SwapError('[grantAllowance]', {
|
|
96
|
+
cause: e,
|
|
97
|
+
code: api_1.SwapErrorTypes.GRANT_ALLOWANCE_FAILED
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
exports.grantAllowance = grantAllowance;
|
|
5
102
|
/**
|
|
6
103
|
* This function keeps 17 significant digits, so even if we try to trade 1 Billion of an
|
|
7
104
|
* ETH or ERC20, we still keep 7 decimal places.
|
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.UNSUPPORTED = exports.ETH = exports.WBTC = exports.FOX = exports.WETH = exports.BTC = void 0;
|
|
4
|
-
const types_1 = require("@shapeshiftoss/types");
|
|
5
4
|
exports.BTC = {
|
|
6
5
|
assetId: 'bip122:000000000019d6689c085ae165831e93/slip44:0',
|
|
7
6
|
chainId: 'bip122:000000000019d6689c085ae165831e93',
|
|
8
7
|
name: 'bitcoin',
|
|
9
|
-
chain: types_1.ChainTypes.Bitcoin,
|
|
10
|
-
network: types_1.NetworkTypes.MAINNET,
|
|
11
8
|
precision: 8,
|
|
12
9
|
color: '#FFFFFF',
|
|
13
10
|
icon: 'https://assets.coincap.io/assets/icons/btc@2x.png',
|
|
@@ -20,8 +17,6 @@ exports.WETH = {
|
|
|
20
17
|
assetId: 'eip155:1/erc20:0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
|
|
21
18
|
chainId: 'eip155:1',
|
|
22
19
|
name: 'WETH',
|
|
23
|
-
chain: types_1.ChainTypes.Ethereum,
|
|
24
|
-
network: types_1.NetworkTypes.MAINNET,
|
|
25
20
|
precision: 18,
|
|
26
21
|
color: '#FFFFFF',
|
|
27
22
|
icon: 'https://assets.coingecko.com/coins/images/2518/thumb/weth.png?1628852295',
|
|
@@ -34,8 +29,6 @@ exports.FOX = {
|
|
|
34
29
|
assetId: 'eip155:1/erc20:0xc770eefad204b5180df6a14ee197d99d808ee52d',
|
|
35
30
|
chainId: 'eip155:1',
|
|
36
31
|
name: 'FOX',
|
|
37
|
-
chain: types_1.ChainTypes.Ethereum,
|
|
38
|
-
network: types_1.NetworkTypes.MAINNET,
|
|
39
32
|
precision: 18,
|
|
40
33
|
color: '#FFFFFF',
|
|
41
34
|
icon: 'https://assets.coincap.io/assets/icons/fox@2x.png',
|
|
@@ -48,21 +41,17 @@ exports.WBTC = {
|
|
|
48
41
|
assetId: 'eip155:1/erc20:0x2260fac5e5542a773aa44fbcfedf7c193bc2c599',
|
|
49
42
|
chainId: 'eip155:1',
|
|
50
43
|
color: '#FFFFFF',
|
|
51
|
-
chain: types_1.ChainTypes.Ethereum,
|
|
52
44
|
explorer: 'https://etherscan.io',
|
|
53
45
|
explorerAddressLink: 'https://etherscan.io/address/',
|
|
54
46
|
explorerTxLink: 'https://etherscan.io/tx/',
|
|
55
47
|
icon: 'https://rawcdn.githack.com/trustwallet/assets/master/blockchains/ethereum/assets/0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599/logo.png',
|
|
56
48
|
name: 'Wrapped Bitcoin',
|
|
57
|
-
network: types_1.NetworkTypes.MAINNET,
|
|
58
49
|
precision: 8,
|
|
59
50
|
symbol: 'WBTC'
|
|
60
51
|
};
|
|
61
52
|
exports.ETH = {
|
|
62
53
|
assetId: 'eip155:1/slip44:60',
|
|
63
54
|
chainId: 'eip155:1',
|
|
64
|
-
chain: types_1.ChainTypes.Ethereum,
|
|
65
|
-
network: types_1.NetworkTypes.MAINNET,
|
|
66
55
|
symbol: 'ETH',
|
|
67
56
|
name: 'Ethereum',
|
|
68
57
|
precision: 18,
|
|
@@ -75,8 +64,6 @@ exports.ETH = {
|
|
|
75
64
|
exports.UNSUPPORTED = {
|
|
76
65
|
assetId: 'eip155:1/slip44:420',
|
|
77
66
|
chainId: 'eip155:1',
|
|
78
|
-
chain: types_1.ChainTypes.Ethereum,
|
|
79
|
-
network: types_1.NetworkTypes.MAINNET,
|
|
80
67
|
symbol: 'ETH',
|
|
81
68
|
name: 'Ethereum',
|
|
82
69
|
precision: 18,
|
|
@@ -0,0 +1,48 @@
|
|
|
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.setupDeps = void 0;
|
|
30
|
+
const chain_adapters_1 = require("@shapeshiftoss/chain-adapters");
|
|
31
|
+
const unchained = __importStar(require("@shapeshiftoss/unchained-client"));
|
|
32
|
+
const web3_1 = __importDefault(require("web3"));
|
|
33
|
+
jest.mock('@shapeshiftoss/chain-adapters');
|
|
34
|
+
const setupDeps = () => {
|
|
35
|
+
const ethChainAdapter = new chain_adapters_1.ethereum.ChainAdapter({
|
|
36
|
+
providers: {
|
|
37
|
+
ws: new unchained.ws.Client('wss://dev-api.ethereum.shapeshift.com'),
|
|
38
|
+
http: new unchained.ethereum.V1Api(new unchained.ethereum.Configuration({
|
|
39
|
+
basePath: 'https://dev-api.ethereum.shapeshift.com'
|
|
40
|
+
}))
|
|
41
|
+
},
|
|
42
|
+
rpcUrl: 'https://mainnet.infura.io/v3/d734c7eebcdf400185d7eb67322a7e57'
|
|
43
|
+
});
|
|
44
|
+
const ethNodeUrl = 'http://localhost:1000';
|
|
45
|
+
const web3Provider = new web3_1.default.providers.HttpProvider(ethNodeUrl);
|
|
46
|
+
return { web3: new web3_1.default(web3Provider), adapter: ethChainAdapter };
|
|
47
|
+
};
|
|
48
|
+
exports.setupDeps = setupDeps;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Asset } from '@shapeshiftoss/types';
|
|
2
|
+
import { TradeQuote } from '../../../api';
|
|
3
|
+
export declare const setupQuote: () => {
|
|
4
|
+
quoteInput: import("../../../api").CommonTradeInput;
|
|
5
|
+
tradeQuote: TradeQuote<"eip155:1">;
|
|
6
|
+
buyAsset: Asset;
|
|
7
|
+
sellAsset: Asset;
|
|
8
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.setupQuote = void 0;
|
|
4
|
+
const assets_1 = require("./assets");
|
|
5
|
+
const setupQuote = () => {
|
|
6
|
+
const sellAsset = { ...assets_1.FOX };
|
|
7
|
+
const buyAsset = { ...assets_1.WETH };
|
|
8
|
+
const tradeQuote = {
|
|
9
|
+
buyAmount: '',
|
|
10
|
+
sellAmount: '1000000000000000000',
|
|
11
|
+
sellAsset,
|
|
12
|
+
buyAsset,
|
|
13
|
+
allowanceContract: 'allowanceContractAddress',
|
|
14
|
+
sellAssetAccountNumber: 0,
|
|
15
|
+
minimum: '0',
|
|
16
|
+
maximum: '999999999999',
|
|
17
|
+
feeData: { fee: '0', tradeFee: '0', chainSpecific: {} },
|
|
18
|
+
rate: '1',
|
|
19
|
+
sources: []
|
|
20
|
+
};
|
|
21
|
+
const quoteInput = {
|
|
22
|
+
sellAmount: '1000000000000000000',
|
|
23
|
+
sellAsset,
|
|
24
|
+
buyAsset,
|
|
25
|
+
sellAssetAccountNumber: 0,
|
|
26
|
+
sendMax: false
|
|
27
|
+
};
|
|
28
|
+
return { quoteInput, tradeQuote, buyAsset, sellAsset };
|
|
29
|
+
};
|
|
30
|
+
exports.setupQuote = setupQuote;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { ApprovalNeededInput, ApprovalNeededOutput } from '../../../api';
|
|
2
2
|
import { ZrxSwapperDeps } from '../ZrxSwapper';
|
|
3
|
-
export declare function ZrxApprovalNeeded({
|
|
3
|
+
export declare function ZrxApprovalNeeded({ adapter, web3 }: ZrxSwapperDeps, { quote, wallet }: ApprovalNeededInput<'eip155:1'>): Promise<ApprovalNeededOutput>;
|
|
@@ -3,10 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.ZrxApprovalNeeded = void 0;
|
|
4
4
|
const caip_1 = require("@shapeshiftoss/caip");
|
|
5
5
|
const api_1 = require("../../../api");
|
|
6
|
+
const erc20Allowance_abi_1 = require("../../utils/abi/erc20Allowance-abi");
|
|
6
7
|
const bignumber_1 = require("../../utils/bignumber");
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
async function ZrxApprovalNeeded({ adapterManager, web3 }, { quote, wallet }) {
|
|
8
|
+
const helpers_1 = require("../../utils/helpers/helpers");
|
|
9
|
+
async function ZrxApprovalNeeded({ adapter, web3 }, { quote, wallet }) {
|
|
10
10
|
var _a;
|
|
11
11
|
const { sellAsset } = quote;
|
|
12
12
|
const { assetReference: sellAssetErc20Address } = (0, caip_1.fromAssetId)(sellAsset.assetId);
|
|
@@ -22,7 +22,6 @@ async function ZrxApprovalNeeded({ adapterManager, web3 }, { quote, wallet }) {
|
|
|
22
22
|
return { approvalNeeded: false };
|
|
23
23
|
}
|
|
24
24
|
const accountNumber = quote.sellAssetAccountNumber;
|
|
25
|
-
const adapter = await adapterManager.byChainId(sellAsset.chainId);
|
|
26
25
|
const bip44Params = adapter.buildBIP44Params({ accountNumber });
|
|
27
26
|
const receiveAddress = await adapter.getAddress({ wallet, bip44Params });
|
|
28
27
|
if (!quote.allowanceContract) {
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { SupportedChainIds } from '@shapeshiftoss/types';
|
|
2
1
|
import { ApproveInfiniteInput } from '../../../api';
|
|
3
2
|
import { ZrxSwapperDeps } from '../ZrxSwapper';
|
|
4
|
-
export declare function ZrxApproveInfinite({
|
|
3
|
+
export declare function ZrxApproveInfinite({ adapter, web3 }: ZrxSwapperDeps, { quote, wallet }: ApproveInfiniteInput<'eip155:1'>): Promise<string>;
|
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ZrxApproveInfinite = void 0;
|
|
4
4
|
const api_1 = require("../../../api");
|
|
5
|
-
const erc20_abi_1 = require("
|
|
5
|
+
const erc20_abi_1 = require("../../utils/abi/erc20-abi");
|
|
6
|
+
const helpers_1 = require("../../utils/helpers/helpers");
|
|
6
7
|
const constants_1 = require("../utils/constants");
|
|
7
|
-
|
|
8
|
-
async function ZrxApproveInfinite({ adapterManager, web3 }, { quote, wallet }) {
|
|
8
|
+
async function ZrxApproveInfinite({ adapter, web3 }, { quote, wallet }) {
|
|
9
9
|
try {
|
|
10
10
|
const allowanceGrantRequired = await (0, helpers_1.grantAllowance)({
|
|
11
11
|
quote: {
|
|
@@ -13,7 +13,7 @@ async function ZrxApproveInfinite({ adapterManager, web3 }, { quote, wallet }) {
|
|
|
13
13
|
sellAmount: constants_1.MAX_ALLOWANCE
|
|
14
14
|
},
|
|
15
15
|
wallet,
|
|
16
|
-
|
|
16
|
+
adapter,
|
|
17
17
|
erc20Abi: erc20_abi_1.erc20Abi,
|
|
18
18
|
web3
|
|
19
19
|
});
|
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
import { AssetId } from '@shapeshiftoss/caip';
|
|
2
|
-
import {
|
|
3
|
-
import { Asset
|
|
2
|
+
import { ethereum } from '@shapeshiftoss/chain-adapters';
|
|
3
|
+
import { Asset } from '@shapeshiftoss/types';
|
|
4
4
|
import Web3 from 'web3';
|
|
5
5
|
import { ApprovalNeededInput, ApprovalNeededOutput, ApproveInfiniteInput, BuildTradeInput, BuyAssetBySellIdInput, ExecuteTradeInput, GetTradeQuoteInput, Swapper, SwapperType, TradeQuote, TradeResult, TradeTxs, ZrxTrade } from '../../api';
|
|
6
6
|
export declare type ZrxSwapperDeps = {
|
|
7
|
-
|
|
7
|
+
adapter: ethereum.ChainAdapter;
|
|
8
8
|
web3: Web3;
|
|
9
9
|
};
|
|
10
|
-
export declare class ZrxSwapper implements Swapper {
|
|
10
|
+
export declare class ZrxSwapper implements Swapper<'eip155:1'> {
|
|
11
11
|
static swapperName: string;
|
|
12
12
|
deps: ZrxSwapperDeps;
|
|
13
13
|
constructor(deps: ZrxSwapperDeps);
|
|
14
14
|
initialize(): Promise<void>;
|
|
15
15
|
getType(): SwapperType;
|
|
16
|
-
buildTrade(args: BuildTradeInput): Promise<ZrxTrade
|
|
17
|
-
getTradeQuote(input: GetTradeQuoteInput): Promise<TradeQuote<
|
|
16
|
+
buildTrade(args: BuildTradeInput): Promise<ZrxTrade>;
|
|
17
|
+
getTradeQuote(input: GetTradeQuoteInput): Promise<TradeQuote<'eip155:1'>>;
|
|
18
18
|
getUsdRate(input: Asset): Promise<string>;
|
|
19
19
|
executeTrade(args: ExecuteTradeInput<'eip155:1'>): Promise<TradeResult>;
|
|
20
20
|
approvalNeeded(args: ApprovalNeededInput<'eip155:1'>): Promise<ApprovalNeededOutput>;
|
|
21
|
-
approveInfinite(args: ApproveInfiniteInput<
|
|
21
|
+
approveInfinite(args: ApproveInfiniteInput<'eip155:1'>): Promise<string>;
|
|
22
22
|
filterBuyAssetsBySellAssetId(args: BuyAssetBySellIdInput): AssetId[];
|
|
23
23
|
filterAssetIdsBySellable(assetIds?: AssetId[]): AssetId[];
|
|
24
24
|
getTradeTxs(tradeResult: TradeResult): Promise<TradeTxs>;
|
|
@@ -1,34 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { HDWallet } from '@shapeshiftoss/hdwallet-core';
|
|
3
|
-
import { Asset, SupportedChainIds } from '@shapeshiftoss/types';
|
|
4
|
-
import Web3 from 'web3';
|
|
5
|
-
import { AbiItem } from 'web3-utils';
|
|
6
|
-
import { TradeQuote } from '../../../../api';
|
|
7
|
-
import { BN } from '../../../utils/bignumber';
|
|
8
|
-
export declare type GetAllowanceRequiredArgs = {
|
|
9
|
-
receiveAddress: string;
|
|
10
|
-
allowanceContract: string;
|
|
11
|
-
sellAsset: Asset;
|
|
12
|
-
sellAmount: string;
|
|
13
|
-
web3: Web3;
|
|
14
|
-
erc20AllowanceAbi: AbiItem[];
|
|
15
|
-
};
|
|
16
|
-
export declare type GetERC20AllowanceArgs = {
|
|
17
|
-
erc20AllowanceAbi: AbiItem[];
|
|
18
|
-
web3: Web3;
|
|
19
|
-
sellAssetErc20Address: string;
|
|
20
|
-
ownerAddress: string;
|
|
21
|
-
spenderAddress: string;
|
|
22
|
-
};
|
|
23
|
-
declare type GrantAllowanceArgs = {
|
|
24
|
-
quote: TradeQuote<SupportedChainIds>;
|
|
25
|
-
wallet: HDWallet;
|
|
26
|
-
adapterManager: ChainAdapterManager;
|
|
27
|
-
erc20Abi: AbiItem[];
|
|
28
|
-
web3: Web3;
|
|
29
|
-
};
|
|
30
|
-
export declare const getERC20Allowance: ({ erc20AllowanceAbi, web3, sellAssetErc20Address, ownerAddress, spenderAddress }: GetERC20AllowanceArgs) => Promise<any>;
|
|
31
|
-
export declare const getAllowanceRequired: ({ receiveAddress, allowanceContract, sellAsset, sellAmount, web3, erc20AllowanceAbi }: GetAllowanceRequiredArgs) => Promise<BN>;
|
|
1
|
+
import { Asset } from '@shapeshiftoss/types';
|
|
32
2
|
export declare const getUsdRate: (asset: Asset) => Promise<string>;
|
|
33
|
-
export declare const grantAllowance: ({ quote, wallet, adapterManager, erc20Abi, web3 }: GrantAllowanceArgs) => Promise<string>;
|
|
34
|
-
export {};
|
|
@@ -1,52 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.getUsdRate = void 0;
|
|
4
4
|
const caip_1 = require("@shapeshiftoss/caip");
|
|
5
|
-
const web3_utils_1 = require("web3-utils");
|
|
6
5
|
const api_1 = require("../../../../api");
|
|
7
6
|
const bignumber_1 = require("../../../utils/bignumber");
|
|
8
7
|
const zrxService_1 = require("../zrxService");
|
|
9
|
-
const getERC20Allowance = async ({ erc20AllowanceAbi, web3, sellAssetErc20Address, ownerAddress, spenderAddress }) => {
|
|
10
|
-
const erc20Contract = new web3.eth.Contract(erc20AllowanceAbi, sellAssetErc20Address);
|
|
11
|
-
return erc20Contract.methods.allowance(ownerAddress, spenderAddress).call();
|
|
12
|
-
};
|
|
13
|
-
exports.getERC20Allowance = getERC20Allowance;
|
|
14
|
-
const getAllowanceRequired = async ({ receiveAddress, allowanceContract, sellAsset, sellAmount, web3, erc20AllowanceAbi }) => {
|
|
15
|
-
try {
|
|
16
|
-
if (sellAsset.assetId === 'eip155:1/slip44:60') {
|
|
17
|
-
return (0, bignumber_1.bn)(0);
|
|
18
|
-
}
|
|
19
|
-
const ownerAddress = receiveAddress;
|
|
20
|
-
const spenderAddress = allowanceContract;
|
|
21
|
-
const { assetReference: sellAssetErc20Address } = (0, caip_1.fromAssetId)(sellAsset.assetId);
|
|
22
|
-
const allowanceOnChain = await (0, exports.getERC20Allowance)({
|
|
23
|
-
web3,
|
|
24
|
-
erc20AllowanceAbi,
|
|
25
|
-
ownerAddress,
|
|
26
|
-
spenderAddress,
|
|
27
|
-
sellAssetErc20Address
|
|
28
|
-
});
|
|
29
|
-
if (allowanceOnChain === '0')
|
|
30
|
-
return (0, bignumber_1.bnOrZero)(sellAmount);
|
|
31
|
-
if (!allowanceOnChain) {
|
|
32
|
-
throw new api_1.SwapError(`[getAllowanceRequired] - No allowance data`, {
|
|
33
|
-
details: { allowanceContract, receiveAddress },
|
|
34
|
-
code: api_1.SwapErrorTypes.RESPONSE_ERROR
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
const allowanceRequired = (0, bignumber_1.bnOrZero)(sellAmount).minus(allowanceOnChain);
|
|
38
|
-
return allowanceRequired.lt(0) ? (0, bignumber_1.bn)(0) : allowanceRequired;
|
|
39
|
-
}
|
|
40
|
-
catch (e) {
|
|
41
|
-
if (e instanceof api_1.SwapError)
|
|
42
|
-
throw e;
|
|
43
|
-
throw new api_1.SwapError('[getAllowanceRequired]', {
|
|
44
|
-
cause: e,
|
|
45
|
-
code: api_1.SwapErrorTypes.ALLOWANCE_REQUIRED_FAILED
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
exports.getAllowanceRequired = getAllowanceRequired;
|
|
50
8
|
const getUsdRate = async (asset) => {
|
|
51
9
|
const { assetReference: erc20Address, assetNamespace } = (0, caip_1.fromAssetId)(asset.assetId);
|
|
52
10
|
const { symbol } = asset;
|
|
@@ -78,57 +36,3 @@ const getUsdRate = async (asset) => {
|
|
|
78
36
|
}
|
|
79
37
|
};
|
|
80
38
|
exports.getUsdRate = getUsdRate;
|
|
81
|
-
const grantAllowance = async ({ quote, wallet, adapterManager, erc20Abi, web3 }) => {
|
|
82
|
-
var _a, _b, _c, _d, _e;
|
|
83
|
-
try {
|
|
84
|
-
const { assetReference: sellAssetErc20Address } = (0, caip_1.fromAssetId)(quote.sellAsset.assetId);
|
|
85
|
-
const adapter = await adapterManager.byChainId('eip155:1');
|
|
86
|
-
const erc20Contract = new web3.eth.Contract(erc20Abi, sellAssetErc20Address);
|
|
87
|
-
const approveTx = erc20Contract.methods
|
|
88
|
-
.approve(quote.allowanceContract, quote.sellAmount)
|
|
89
|
-
.encodeABI();
|
|
90
|
-
const accountNumber = quote.sellAssetAccountNumber;
|
|
91
|
-
const bip44Params = adapter.buildBIP44Params({ accountNumber });
|
|
92
|
-
const { txToSign } = await adapter.buildSendTransaction({
|
|
93
|
-
wallet,
|
|
94
|
-
to: sellAssetErc20Address,
|
|
95
|
-
bip44Params,
|
|
96
|
-
value: '0',
|
|
97
|
-
chainSpecific: {
|
|
98
|
-
erc20ContractAddress: sellAssetErc20Address,
|
|
99
|
-
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),
|
|
100
|
-
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)
|
|
101
|
-
}
|
|
102
|
-
});
|
|
103
|
-
const grantAllowanceTxToSign = {
|
|
104
|
-
...txToSign,
|
|
105
|
-
data: approveTx
|
|
106
|
-
};
|
|
107
|
-
if (wallet.supportsOfflineSigning()) {
|
|
108
|
-
const signedTx = await adapter.signTransaction({ txToSign: grantAllowanceTxToSign, wallet });
|
|
109
|
-
const broadcastedTxId = await adapter.broadcastTransaction(signedTx);
|
|
110
|
-
return broadcastedTxId;
|
|
111
|
-
}
|
|
112
|
-
else if (wallet.supportsBroadcast() && adapter.signAndBroadcastTransaction) {
|
|
113
|
-
const broadcastedTxId = await ((_e = adapter.signAndBroadcastTransaction) === null || _e === void 0 ? void 0 : _e.call(adapter, {
|
|
114
|
-
txToSign: grantAllowanceTxToSign,
|
|
115
|
-
wallet
|
|
116
|
-
}));
|
|
117
|
-
return broadcastedTxId;
|
|
118
|
-
}
|
|
119
|
-
else {
|
|
120
|
-
throw new api_1.SwapError('[grantAllowance] - invalid HDWallet config', {
|
|
121
|
-
code: api_1.SwapErrorTypes.SIGN_AND_BROADCAST_FAILED
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
catch (e) {
|
|
126
|
-
if (e instanceof api_1.SwapError)
|
|
127
|
-
throw e;
|
|
128
|
-
throw new api_1.SwapError('[grantAllowance]', {
|
|
129
|
-
cause: e,
|
|
130
|
-
code: api_1.SwapErrorTypes.GRANT_ALLOWANCE_FAILED
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
};
|
|
134
|
-
exports.grantAllowance = grantAllowance;
|
|
@@ -1,12 +1,5 @@
|
|
|
1
1
|
import { Asset } from '@shapeshiftoss/types';
|
|
2
|
-
import { TradeQuote } from '../../../../api';
|
|
3
2
|
import { ZrxQuoteResponse } from '../../types';
|
|
4
|
-
export declare const setupQuote: () => {
|
|
5
|
-
quoteInput: import("../../../../api").CommonTradeInput;
|
|
6
|
-
tradeQuote: TradeQuote<"eip155:1">;
|
|
7
|
-
buyAsset: Asset;
|
|
8
|
-
sellAsset: Asset;
|
|
9
|
-
};
|
|
10
3
|
export declare const setupZrxTradeQuoteResponse: () => {
|
|
11
4
|
quoteResponse: ZrxQuoteResponse;
|
|
12
5
|
buyAsset: Asset;
|
|
@@ -1,33 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.setupExecuteTrade = exports.setupBuildTrade = exports.setupZrxTradeQuoteResponse =
|
|
3
|
+
exports.setupExecuteTrade = exports.setupBuildTrade = exports.setupZrxTradeQuoteResponse = void 0;
|
|
4
4
|
const assets_1 = require("../../../utils/test-data/assets");
|
|
5
|
-
const setupQuote = () => {
|
|
6
|
-
const sellAsset = { ...assets_1.FOX };
|
|
7
|
-
const buyAsset = { ...assets_1.WETH };
|
|
8
|
-
const tradeQuote = {
|
|
9
|
-
buyAmount: '',
|
|
10
|
-
sellAmount: '1000000000000000000',
|
|
11
|
-
sellAsset,
|
|
12
|
-
buyAsset,
|
|
13
|
-
allowanceContract: 'allowanceContractAddress',
|
|
14
|
-
sellAssetAccountNumber: 0,
|
|
15
|
-
minimum: '0',
|
|
16
|
-
maximum: '999999999999',
|
|
17
|
-
feeData: { fee: '0', tradeFee: '0', chainSpecific: {} },
|
|
18
|
-
rate: '1',
|
|
19
|
-
sources: []
|
|
20
|
-
};
|
|
21
|
-
const quoteInput = {
|
|
22
|
-
sellAmount: '1000000000000000000',
|
|
23
|
-
sellAsset,
|
|
24
|
-
buyAsset,
|
|
25
|
-
sellAssetAccountNumber: 0,
|
|
26
|
-
sendMax: false
|
|
27
|
-
};
|
|
28
|
-
return { quoteInput, tradeQuote, buyAsset, sellAsset };
|
|
29
|
-
};
|
|
30
|
-
exports.setupQuote = setupQuote;
|
|
31
5
|
const setupZrxTradeQuoteResponse = () => {
|
|
32
6
|
const sellAsset = { ...assets_1.FOX };
|
|
33
7
|
const buyAsset = { ...assets_1.WETH };
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { SupportedChainIds } from '@shapeshiftoss/types';
|
|
2
1
|
import { BuildTradeInput, ZrxTrade } from '../../..';
|
|
3
2
|
import { ZrxSwapperDeps } from '../ZrxSwapper';
|
|
4
|
-
export declare function zrxBuildTrade({
|
|
3
|
+
export declare function zrxBuildTrade({ adapter, web3 }: ZrxSwapperDeps, input: BuildTradeInput): Promise<ZrxTrade>;
|