@shapeshiftoss/swapper 12.1.0 → 12.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.
|
@@ -99,7 +99,7 @@ async function cowBuildTrade(deps, input) {
|
|
|
99
99
|
feeAmountInSellToken: quote.feeAmount,
|
|
100
100
|
sellAmountWithoutFee: quote.sellAmount,
|
|
101
101
|
};
|
|
102
|
-
const
|
|
102
|
+
const approvalRequired = await (0, helpers_1.isApprovalRequired)({
|
|
103
103
|
adapter,
|
|
104
104
|
sellAsset,
|
|
105
105
|
allowanceContract: constants_1.COW_SWAP_VAULT_RELAYER_ADDRESS,
|
|
@@ -108,7 +108,7 @@ async function cowBuildTrade(deps, input) {
|
|
|
108
108
|
web3: deps.web3,
|
|
109
109
|
erc20AllowanceAbi: erc20Allowance_abi_1.erc20AllowanceAbi,
|
|
110
110
|
});
|
|
111
|
-
if (
|
|
111
|
+
if (approvalRequired) {
|
|
112
112
|
trade.feeData.chainSpecific.approvalFee = (0, bignumber_1.bnOrZero)(feeData.chainSpecific.gasLimit)
|
|
113
113
|
.multipliedBy((0, bignumber_1.bnOrZero)(feeData.chainSpecific.gasPrice))
|
|
114
114
|
.toString();
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { Asset } from '@shapeshiftoss/asset-service';
|
|
2
2
|
import { HDWallet } from '@shapeshiftoss/hdwallet-core';
|
|
3
|
-
import BigNumber from 'bignumber.js';
|
|
4
3
|
import Web3 from 'web3';
|
|
5
4
|
import { AbiItem } from 'web3-utils';
|
|
6
5
|
import { EvmSupportedChainAdapters, EvmSupportedChainIds, TradeQuote } from '../../../api';
|
|
7
6
|
import { BN } from '../bignumber';
|
|
8
|
-
export declare type
|
|
7
|
+
export declare type IsApprovalRequiredArgs = {
|
|
9
8
|
adapter: EvmSupportedChainAdapters;
|
|
10
9
|
receiveAddress: string;
|
|
11
10
|
allowanceContract: string;
|
|
@@ -34,7 +33,7 @@ declare type GrantAllowanceArgs<T extends EvmSupportedChainIds> = {
|
|
|
34
33
|
web3: Web3;
|
|
35
34
|
};
|
|
36
35
|
export declare const getERC20Allowance: ({ erc20AllowanceAbi, web3, sellAssetErc20Address, ownerAddress, spenderAddress, }: GetERC20AllowanceArgs) => Promise<any>;
|
|
37
|
-
export declare const
|
|
36
|
+
export declare const isApprovalRequired: ({ adapter, receiveAddress, allowanceContract, sellAsset, sellAmount, web3, erc20AllowanceAbi, }: IsApprovalRequiredArgs) => Promise<boolean>;
|
|
38
37
|
export declare const grantAllowance: <T extends EvmSupportedChainIds>({ quote, wallet, adapter, erc20Abi, web3, }: GrantAllowanceArgs<T>) => Promise<string>;
|
|
39
38
|
/**
|
|
40
39
|
* This function keeps 17 significant digits, so even if we try to trade 1 Billion of an
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getApproveContractData = exports.normalizeIntegerAmount = exports.normalizeAmount = exports.grantAllowance = exports.
|
|
3
|
+
exports.getApproveContractData = exports.normalizeIntegerAmount = exports.normalizeAmount = exports.grantAllowance = exports.isApprovalRequired = 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");
|
|
@@ -12,10 +12,10 @@ const getERC20Allowance = async ({ erc20AllowanceAbi, web3, sellAssetErc20Addres
|
|
|
12
12
|
return erc20Contract.methods.allowance(ownerAddress, spenderAddress).call();
|
|
13
13
|
};
|
|
14
14
|
exports.getERC20Allowance = getERC20Allowance;
|
|
15
|
-
const
|
|
15
|
+
const isApprovalRequired = async ({ adapter, receiveAddress, allowanceContract, sellAsset, sellAmount, web3, erc20AllowanceAbi, }) => {
|
|
16
16
|
try {
|
|
17
17
|
if (sellAsset.assetId === adapter.getFeeAssetId()) {
|
|
18
|
-
return
|
|
18
|
+
return false;
|
|
19
19
|
}
|
|
20
20
|
const ownerAddress = receiveAddress;
|
|
21
21
|
const spenderAddress = allowanceContract;
|
|
@@ -27,27 +27,27 @@ const getAllowanceRequired = async ({ adapter, receiveAddress, allowanceContract
|
|
|
27
27
|
spenderAddress,
|
|
28
28
|
sellAssetErc20Address,
|
|
29
29
|
});
|
|
30
|
-
if (allowanceOnChain === '0')
|
|
31
|
-
return (0, bignumber_1.bnOrZero)(sellAmount);
|
|
32
30
|
if (!allowanceOnChain) {
|
|
33
|
-
throw new api_1.SwapError(`[
|
|
31
|
+
throw new api_1.SwapError(`[isApprovalRequired] - No allowance data`, {
|
|
34
32
|
details: { allowanceContract, receiveAddress },
|
|
35
33
|
code: api_1.SwapErrorTypes.RESPONSE_ERROR,
|
|
36
34
|
});
|
|
37
35
|
}
|
|
36
|
+
if ((0, bignumber_1.bn)(allowanceOnChain).isZero())
|
|
37
|
+
return true;
|
|
38
38
|
const allowanceRequired = (0, bignumber_1.bnOrZero)(sellAmount).minus(allowanceOnChain);
|
|
39
|
-
return allowanceRequired.
|
|
39
|
+
return allowanceRequired.gt(0);
|
|
40
40
|
}
|
|
41
41
|
catch (e) {
|
|
42
42
|
if (e instanceof api_1.SwapError)
|
|
43
43
|
throw e;
|
|
44
|
-
throw new api_1.SwapError('[
|
|
44
|
+
throw new api_1.SwapError('[isApprovalRequired]', {
|
|
45
45
|
cause: e,
|
|
46
46
|
code: api_1.SwapErrorTypes.ALLOWANCE_REQUIRED_FAILED,
|
|
47
47
|
});
|
|
48
48
|
}
|
|
49
49
|
};
|
|
50
|
-
exports.
|
|
50
|
+
exports.isApprovalRequired = isApprovalRequired;
|
|
51
51
|
const grantAllowance = async ({ quote, wallet, adapter, erc20Abi, web3, }) => {
|
|
52
52
|
var _a, _b, _c, _d, _e;
|
|
53
53
|
try {
|
|
@@ -92,7 +92,7 @@ async function zrxBuildTrade({ adapter, web3 }, input) {
|
|
|
92
92
|
const { data } = quoteResponse;
|
|
93
93
|
const estimatedGas = (0, bignumber_1.bnOrZero)(data.gas || 0);
|
|
94
94
|
const networkFee = (0, bignumber_1.bnOrZero)(estimatedGas).multipliedBy((0, bignumber_1.bnOrZero)(data.gasPrice)).toString();
|
|
95
|
-
const
|
|
95
|
+
const approvalRequired = await (0, helpers_1.isApprovalRequired)({
|
|
96
96
|
adapter,
|
|
97
97
|
sellAsset,
|
|
98
98
|
allowanceContract: data.allowanceTarget,
|
|
@@ -116,7 +116,7 @@ async function zrxBuildTrade({ adapter, web3 }, input) {
|
|
|
116
116
|
chainSpecific: {
|
|
117
117
|
estimatedGas: estimatedGas.toString(),
|
|
118
118
|
gasPrice: data.gasPrice,
|
|
119
|
-
approvalFee:
|
|
119
|
+
approvalFee: approvalRequired ? approvalFee : undefined,
|
|
120
120
|
},
|
|
121
121
|
tradeFee: '0',
|
|
122
122
|
networkFee,
|
package/package.json
CHANGED