@paraswap/dex-lib 2.16.4 → 2.16.5-direct-swaps.1
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/build/abi/DirectSwap.json +970 -0
- package/build/abi/woo-fi/WooFeeManager.abi.json +318 -0
- package/build/abi/woo-fi/WooPP.abi.json +548 -0
- package/build/abi/woo-fi/Wooracle.abi.json +260 -0
- package/build/config.js +14 -8
- package/build/config.js.map +1 -1
- package/build/dex/aave-v3/tokens/arbitrum.json +50 -0
- package/build/dex/aave-v3/tokens/avalanche.json +74 -0
- package/build/dex/aave-v3/tokens/fantom.json +62 -0
- package/build/dex/aave-v3/tokens/optimism.json +50 -0
- package/build/dex/aave-v3/tokens/polygon.json +116 -0
- package/build/dex/balancer-v2/balancer-v2.d.ts +9 -3
- package/build/dex/balancer-v2/balancer-v2.js +81 -0
- package/build/dex/balancer-v2/balancer-v2.js.map +1 -1
- package/build/dex/balancer-v2/constants.d.ts +4 -0
- package/build/dex/balancer-v2/constants.js +6 -1
- package/build/dex/balancer-v2/constants.js.map +1 -1
- package/build/dex/balancer-v2/types.d.ts +18 -1
- package/build/dex/balancer-v2/types.js.map +1 -1
- package/build/dex/curve-v1/constants.d.ts +1 -0
- package/build/dex/curve-v1/constants.js +2 -1
- package/build/dex/curve-v1/constants.js.map +1 -1
- package/build/dex/curve-v1/curve-v1.d.ts +9 -3
- package/build/dex/curve-v1/curve-v1.js +90 -4
- package/build/dex/curve-v1/curve-v1.js.map +1 -1
- package/build/dex/curve-v1/types.d.ts +23 -0
- package/build/dex/curve-v1/types.js +6 -1
- package/build/dex/curve-v1/types.js.map +1 -1
- package/build/dex/curve-v1-factory/constants.d.ts +1 -0
- package/build/dex/curve-v1-factory/constants.js +2 -1
- package/build/dex/curve-v1-factory/constants.js.map +1 -1
- package/build/dex/curve-v1-factory/curve-v1-factory.d.ts +11 -4
- package/build/dex/curve-v1-factory/curve-v1-factory.js +85 -0
- package/build/dex/curve-v1-factory/curve-v1-factory.js.map +1 -1
- package/build/dex/curve-v1-factory/types.d.ts +23 -0
- package/build/dex/curve-v1-factory/types.js +6 -1
- package/build/dex/curve-v1-factory/types.js.map +1 -1
- package/build/dex/curve-v2.d.ts +28 -7
- package/build/dex/curve-v2.js +89 -1
- package/build/dex/curve-v2.js.map +1 -1
- package/build/dex/generic-rfq/generic-rfq.js +0 -1
- package/build/dex/generic-rfq/generic-rfq.js.map +1 -1
- package/build/dex/idex.d.ts +1 -1
- package/build/dex/quickswap-v3.d.ts +21 -0
- package/build/dex/quickswap-v3.js +40 -0
- package/build/dex/quickswap-v3.js.map +1 -0
- package/build/dex/simple-exchange.d.ts +2 -0
- package/build/dex/simple-exchange.js +1 -0
- package/build/dex/simple-exchange.js.map +1 -1
- package/build/dex/test-dex/config.d.ts +4 -0
- package/build/dex/test-dex/config.js +15 -0
- package/build/dex/test-dex/config.js.map +1 -0
- package/build/dex/test-dex/test-dex-pool.d.ts +39 -0
- package/build/dex/test-dex/test-dex-pool.js +63 -0
- package/build/dex/test-dex/test-dex-pool.js.map +1 -0
- package/build/dex/test-dex/test-dex.d.ts +37 -0
- package/build/dex/test-dex/test-dex.js +126 -0
- package/build/dex/test-dex/test-dex.js.map +1 -0
- package/build/dex/test-dex/types.d.ts +6 -0
- package/build/dex/test-dex/types.js +3 -0
- package/build/dex/test-dex/types.js.map +1 -0
- package/build/dex/uniswap-v2/config.js +1 -1
- package/build/dex/uniswap-v2/config.js.map +1 -1
- package/build/dex/uniswap-v2/uniswap-v2.d.ts +1 -1
- package/build/dex/uniswap-v2/uniswap-v2.js +1 -1
- package/build/dex/uniswap-v2/uniswap-v2.js.map +1 -1
- package/build/dex/uniswap-v3/constants.d.ts +4 -0
- package/build/dex/uniswap-v3/constants.js +6 -1
- package/build/dex/uniswap-v3/constants.js.map +1 -1
- package/build/dex/uniswap-v3/types.d.ts +20 -3
- package/build/dex/uniswap-v3/types.js.map +1 -1
- package/build/dex/uniswap-v3/uniswap-v3.d.ts +7 -1
- package/build/dex/uniswap-v3/uniswap-v3.js +85 -1
- package/build/dex/uniswap-v3/uniswap-v3.js.map +1 -1
- package/build/dex/woo-fi/config.d.ts +4 -0
- package/build/dex/woo-fi/config.js +141 -0
- package/build/dex/woo-fi/config.js.map +1 -0
- package/build/dex/woo-fi/constants.d.ts +5 -0
- package/build/dex/woo-fi/constants.js +20 -0
- package/build/dex/woo-fi/constants.js.map +1 -0
- package/build/dex/woo-fi/types.d.ts +48 -0
- package/build/dex/woo-fi/types.js +3 -0
- package/build/dex/woo-fi/types.js.map +1 -0
- package/build/dex/woo-fi/woo-fi-decimal-math.d.ts +14 -0
- package/build/dex/woo-fi/woo-fi-decimal-math.js +42 -0
- package/build/dex/woo-fi/woo-fi-decimal-math.js.map +1 -0
- package/build/dex/woo-fi/woo-fi-math.d.ts +31 -0
- package/build/dex/woo-fi/woo-fi-math.js +300 -0
- package/build/dex/woo-fi/woo-fi-math.js.map +1 -0
- package/build/dex/woo-fi/woo-fi.d.ts +65 -0
- package/build/dex/woo-fi/woo-fi.js +480 -0
- package/build/dex/woo-fi/woo-fi.js.map +1 -0
- package/build/dex/woo-fi-v2/woo-fi-math.d.ts +16 -0
- package/build/dex/woo-fi-v2/woo-fi-math.js +137 -0
- package/build/dex/woo-fi-v2/woo-fi-math.js.map +1 -0
- package/build/dex/zerox/index.d.ts +1 -1
- package/build/dex/zerox/index.js +1 -1
- package/build/dex/zerox/index.js.map +1 -1
- package/build/implementations/local-paraswap-sdk.js +5 -1
- package/build/implementations/local-paraswap-sdk.js.map +1 -1
- package/build/router/directswap.js +3 -1
- package/build/router/directswap.js.map +1 -1
- package/build/router/index.js.map +1 -1
- package/build/types.d.ts +1 -0
- package/package.json +5 -3
- package/src/abi/DirectSwap.json +970 -0
- package/src/config.ts +14 -8
- package/src/dex/balancer-v2/balancer-v2-e2e.test.ts +71 -0
- package/src/dex/balancer-v2/balancer-v2.ts +150 -3
- package/src/dex/balancer-v2/constants.ts +5 -0
- package/src/dex/balancer-v2/types.ts +19 -1
- package/src/dex/curve-v1/constants.ts +2 -0
- package/src/dex/curve-v1/curve-v1-e2e.test.ts +57 -0
- package/src/dex/curve-v1/curve-v1.ts +142 -5
- package/src/dex/curve-v1/types.ts +25 -0
- package/src/dex/curve-v1-factory/constants.ts +2 -0
- package/src/dex/curve-v1-factory/curve-v1-factory-e2e.test.ts +1 -0
- package/src/dex/curve-v1-factory/curve-v1-factory.ts +138 -4
- package/src/dex/curve-v1-factory/types.ts +25 -0
- package/src/dex/curve-v2.ts +166 -8
- package/src/dex/generic-rfq/generic-rfq-e2e.test.ts +2 -2
- package/src/dex/generic-rfq/generic-rfq.ts +0 -1
- package/src/dex/idex.ts +6 -0
- package/src/dex/simple-exchange.ts +6 -0
- package/src/dex/uniswap-v2/config.ts +2 -1
- package/src/dex/uniswap-v2/uniswap-v2.ts +6 -0
- package/src/dex/uniswap-v3/constants.ts +5 -0
- package/src/dex/uniswap-v3/types.ts +23 -3
- package/src/dex/uniswap-v3/uniswap-v3-e2e.test.ts +100 -2
- package/src/dex/uniswap-v3/uniswap-v3.ts +138 -3
- package/src/dex/zerox/index.ts +7 -0
- package/src/implementations/local-paraswap-sdk.ts +8 -1
- package/src/router/directswap.ts +9 -0
- package/src/router/index.ts +0 -2
- package/src/types.ts +1 -0
- package/tests/constants-e2e.ts +3 -3
- package/tests/utils-e2e.ts +135 -20
- package/tests/utils.ts +52 -0
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { NumberAsString } from '@paraswap/core';
|
|
1
2
|
import { Address, Token } from '../../types';
|
|
2
3
|
|
|
3
4
|
export type CurveV1Data = {
|
|
@@ -6,6 +7,7 @@ export type CurveV1Data = {
|
|
|
6
7
|
j: number;
|
|
7
8
|
underlyingSwap: boolean;
|
|
8
9
|
deadline: number;
|
|
10
|
+
isApproved?: boolean;
|
|
9
11
|
};
|
|
10
12
|
|
|
11
13
|
export type PoolConfig = {
|
|
@@ -42,3 +44,26 @@ export enum CurveSwapFunctions {
|
|
|
42
44
|
exchange = 'exchange',
|
|
43
45
|
exchange_underlying = 'exchange_underlying',
|
|
44
46
|
}
|
|
47
|
+
|
|
48
|
+
export enum CurveV1SwapType {
|
|
49
|
+
EXCHANGE,
|
|
50
|
+
EXCHANGE_UNDERLYING,
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export type DirectCurveV1Param = [
|
|
54
|
+
fromToken: Address,
|
|
55
|
+
toToken: Address,
|
|
56
|
+
exchange: Address,
|
|
57
|
+
fromAmount: NumberAsString,
|
|
58
|
+
toAmount: NumberAsString,
|
|
59
|
+
expectedAmount: NumberAsString,
|
|
60
|
+
feePercent: NumberAsString,
|
|
61
|
+
i: NumberAsString,
|
|
62
|
+
j: NumberAsString,
|
|
63
|
+
partner: Address,
|
|
64
|
+
isApproved: boolean,
|
|
65
|
+
swapType: CurveV1SwapType,
|
|
66
|
+
beneficiary: Address,
|
|
67
|
+
permit: string,
|
|
68
|
+
uuid: string,
|
|
69
|
+
];
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import _ from 'lodash';
|
|
2
|
-
import {
|
|
2
|
+
import { AbiItem } from 'web3-utils';
|
|
3
|
+
import { NumberAsString, OptimalSwapExchange } from '@paraswap/core';
|
|
4
|
+
import { assert, AsyncOrSync } from 'ts-essentials';
|
|
3
5
|
import { Interface, JsonFragment } from '@ethersproject/abi';
|
|
4
6
|
import {
|
|
5
7
|
Token,
|
|
@@ -11,6 +13,9 @@ import {
|
|
|
11
13
|
PoolLiquidity,
|
|
12
14
|
Logger,
|
|
13
15
|
TransferFeeParams,
|
|
16
|
+
TxInfo,
|
|
17
|
+
PreprocessTransactionOptions,
|
|
18
|
+
ExchangeTxInfo,
|
|
14
19
|
} from '../../types';
|
|
15
20
|
import {
|
|
16
21
|
SwapSide,
|
|
@@ -23,6 +28,7 @@ import {
|
|
|
23
28
|
getBigIntPow,
|
|
24
29
|
getDexKeysWithNetwork,
|
|
25
30
|
isSrcTokenTransferFeeToBeExchanged,
|
|
31
|
+
uuidToBytes16,
|
|
26
32
|
} from '../../utils';
|
|
27
33
|
import { IDex } from '../idex';
|
|
28
34
|
import { IDexHelper } from '../../dex-helper/idex-helper';
|
|
@@ -30,13 +36,19 @@ import {
|
|
|
30
36
|
CurveSwapFunctions,
|
|
31
37
|
CurveV1FactoryData,
|
|
32
38
|
CurveV1FactoryIfaces,
|
|
39
|
+
CurveV1SwapType,
|
|
33
40
|
CustomImplementationNames,
|
|
34
41
|
ImplementationNames,
|
|
35
42
|
PoolConstants,
|
|
43
|
+
DirectCurveV1Param,
|
|
36
44
|
} from './types';
|
|
37
|
-
import {
|
|
45
|
+
import {
|
|
46
|
+
getLocalDeadlineAsFriendlyPlaceholder,
|
|
47
|
+
SimpleExchange,
|
|
48
|
+
} from '../simple-exchange';
|
|
38
49
|
import { CurveV1FactoryConfig, Adapters } from './config';
|
|
39
50
|
import {
|
|
51
|
+
DIRECT_METHOD_NAME,
|
|
40
52
|
FACTORY_MAX_PLAIN_COINS,
|
|
41
53
|
FACTORY_MAX_PLAIN_IMPLEMENTATIONS_FOR_COIN,
|
|
42
54
|
MIN_AMOUNT_TO_RECEIVE,
|
|
@@ -44,6 +56,7 @@ import {
|
|
|
44
56
|
} from './constants';
|
|
45
57
|
import { CurveV1FactoryPoolManager } from './curve-v1-pool-manager';
|
|
46
58
|
import CurveABI from '../../abi/Curve.json';
|
|
59
|
+
import DirectSwapABI from '../../abi/DirectSwap.json';
|
|
47
60
|
import FactoryCurveV1ABI from '../../abi/curve-v1-factory/FactoryCurveV1.json';
|
|
48
61
|
import ThreePoolABI from '../../abi/curve-v1-factory/ThreePool.json';
|
|
49
62
|
import ERC20ABI from '../../abi/erc20.json';
|
|
@@ -61,7 +74,6 @@ import { CustomBasePoolForFactory } from './state-polling-pools/custom-pool-poll
|
|
|
61
74
|
import ImplementationConstants from './price-handlers/functions/constants';
|
|
62
75
|
import { applyTransferFee } from '../../lib/token-transfer-fee';
|
|
63
76
|
import { PriceHandler } from './price-handlers/price-handler';
|
|
64
|
-
import { AbiItem } from 'web3-utils';
|
|
65
77
|
|
|
66
78
|
const DefaultCoinsABI: AbiItem = {
|
|
67
79
|
type: 'function',
|
|
@@ -82,7 +94,7 @@ const DefaultCoinsABI: AbiItem = {
|
|
|
82
94
|
|
|
83
95
|
export class CurveV1Factory
|
|
84
96
|
extends SimpleExchange
|
|
85
|
-
implements IDex<CurveV1FactoryData>
|
|
97
|
+
implements IDex<CurveV1FactoryData, DirectCurveV1Param>
|
|
86
98
|
{
|
|
87
99
|
readonly hasConstantPriceLargeAmounts = false;
|
|
88
100
|
readonly needWrapNative = false;
|
|
@@ -98,6 +110,8 @@ export class CurveV1Factory
|
|
|
98
110
|
readonly SRC_TOKEN_DEX_TRANSFERS = 1;
|
|
99
111
|
readonly DEST_TOKEN_DEX_TRANSFERS = 1;
|
|
100
112
|
|
|
113
|
+
readonly directSwapIface = new Interface(DirectSwapABI);
|
|
114
|
+
|
|
101
115
|
public static dexKeysWithNetwork: { key: string; networks: Network[] }[] =
|
|
102
116
|
getDexKeysWithNetwork(CurveV1FactoryConfig);
|
|
103
117
|
|
|
@@ -814,6 +828,126 @@ export class CurveV1Factory
|
|
|
814
828
|
};
|
|
815
829
|
}
|
|
816
830
|
|
|
831
|
+
getTokenFromAddress(address: Address): Token {
|
|
832
|
+
// In this Dex decimals are not used
|
|
833
|
+
return { address, decimals: 0 };
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
async preProcessTransaction(
|
|
837
|
+
optimalSwapExchange: OptimalSwapExchange<CurveV1FactoryData>,
|
|
838
|
+
srcToken: Token,
|
|
839
|
+
_0: Token,
|
|
840
|
+
_1: SwapSide,
|
|
841
|
+
options: PreprocessTransactionOptions,
|
|
842
|
+
): Promise<[OptimalSwapExchange<CurveV1FactoryData>, ExchangeTxInfo]> {
|
|
843
|
+
if (!options.isDirectMethod) {
|
|
844
|
+
return [
|
|
845
|
+
optimalSwapExchange,
|
|
846
|
+
{
|
|
847
|
+
deadline: BigInt(getLocalDeadlineAsFriendlyPlaceholder()),
|
|
848
|
+
},
|
|
849
|
+
];
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
assert(
|
|
853
|
+
optimalSwapExchange.data !== undefined,
|
|
854
|
+
`preProcessTransaction: data field is missing`,
|
|
855
|
+
);
|
|
856
|
+
|
|
857
|
+
let isApproved: boolean | undefined;
|
|
858
|
+
|
|
859
|
+
try {
|
|
860
|
+
this.erc20Contract.options.address =
|
|
861
|
+
this.dexHelper.config.wrapETH(srcToken).address;
|
|
862
|
+
const allowance = await this.erc20Contract.methods
|
|
863
|
+
.allowance(this.augustusAddress, optimalSwapExchange.data.exchange)
|
|
864
|
+
.call(undefined, 'latest');
|
|
865
|
+
isApproved =
|
|
866
|
+
BigInt(allowance.toString()) >= BigInt(optimalSwapExchange.srcAmount);
|
|
867
|
+
} catch (e) {
|
|
868
|
+
this.logger.error(
|
|
869
|
+
`preProcessTransaction failed to retrieve allowance info: `,
|
|
870
|
+
e,
|
|
871
|
+
);
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
return [
|
|
875
|
+
{
|
|
876
|
+
...optimalSwapExchange,
|
|
877
|
+
data: {
|
|
878
|
+
...optimalSwapExchange.data,
|
|
879
|
+
isApproved,
|
|
880
|
+
},
|
|
881
|
+
},
|
|
882
|
+
{
|
|
883
|
+
deadline: BigInt(getLocalDeadlineAsFriendlyPlaceholder()),
|
|
884
|
+
},
|
|
885
|
+
];
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
getDirectParam(
|
|
889
|
+
srcToken: Address,
|
|
890
|
+
destToken: Address,
|
|
891
|
+
srcAmount: NumberAsString,
|
|
892
|
+
destAmount: NumberAsString,
|
|
893
|
+
expectedAmount: NumberAsString,
|
|
894
|
+
data: CurveV1FactoryData,
|
|
895
|
+
side: SwapSide,
|
|
896
|
+
permit: string,
|
|
897
|
+
uuid: string,
|
|
898
|
+
feePercent: NumberAsString,
|
|
899
|
+
deadline: NumberAsString,
|
|
900
|
+
partner: string,
|
|
901
|
+
beneficiary: string,
|
|
902
|
+
contractMethod?: string,
|
|
903
|
+
): TxInfo<DirectCurveV1Param> {
|
|
904
|
+
if (contractMethod !== DIRECT_METHOD_NAME) {
|
|
905
|
+
throw new Error(`Invalid contract method ${contractMethod}`);
|
|
906
|
+
}
|
|
907
|
+
assert(side === SwapSide.SELL, 'Buy not supported');
|
|
908
|
+
|
|
909
|
+
let isApproved: boolean = !!data.isApproved;
|
|
910
|
+
if (data.isApproved === undefined) {
|
|
911
|
+
this.logger.warn(`isApproved is undefined, defaulting to false`);
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
const swapParams: DirectCurveV1Param = [
|
|
915
|
+
srcToken,
|
|
916
|
+
destToken,
|
|
917
|
+
data.exchange,
|
|
918
|
+
srcAmount,
|
|
919
|
+
destAmount,
|
|
920
|
+
expectedAmount,
|
|
921
|
+
feePercent,
|
|
922
|
+
data.i.toString(),
|
|
923
|
+
data.j.toString(),
|
|
924
|
+
partner,
|
|
925
|
+
isApproved,
|
|
926
|
+
data.underlyingSwap
|
|
927
|
+
? CurveV1SwapType.EXCHANGE_UNDERLYING
|
|
928
|
+
: CurveV1SwapType.EXCHANGE,
|
|
929
|
+
beneficiary,
|
|
930
|
+
permit,
|
|
931
|
+
uuidToBytes16(uuid),
|
|
932
|
+
];
|
|
933
|
+
|
|
934
|
+
const encoder = (...params: DirectCurveV1Param) => {
|
|
935
|
+
return this.directSwapIface.encodeFunctionData(DIRECT_METHOD_NAME, [
|
|
936
|
+
params,
|
|
937
|
+
]);
|
|
938
|
+
};
|
|
939
|
+
|
|
940
|
+
return {
|
|
941
|
+
params: swapParams,
|
|
942
|
+
encoder,
|
|
943
|
+
networkFee: '0',
|
|
944
|
+
};
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
static getDirectFunctionName(): string[] {
|
|
948
|
+
return [DIRECT_METHOD_NAME];
|
|
949
|
+
}
|
|
950
|
+
|
|
817
951
|
async getSimpleParam(
|
|
818
952
|
srcToken: string,
|
|
819
953
|
destToken: string,
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Interface } from '@ethersproject/abi';
|
|
2
|
+
import { NumberAsString } from '@paraswap/core';
|
|
2
3
|
import { Address } from '../../types';
|
|
3
4
|
|
|
4
5
|
// The difference between PoolContextConstants and PoolConstants lies in the fact
|
|
@@ -69,6 +70,7 @@ export type CurveV1FactoryData = {
|
|
|
69
70
|
i: number;
|
|
70
71
|
j: number;
|
|
71
72
|
underlyingSwap: boolean;
|
|
73
|
+
isApproved?: boolean;
|
|
72
74
|
};
|
|
73
75
|
|
|
74
76
|
export enum FactoryImplementationNames {
|
|
@@ -181,3 +183,26 @@ export type CurveV1FactoryIfaces = {
|
|
|
181
183
|
};
|
|
182
184
|
|
|
183
185
|
export type LiquidityInCache = Record<string, number>;
|
|
186
|
+
|
|
187
|
+
export enum CurveV1SwapType {
|
|
188
|
+
EXCHANGE,
|
|
189
|
+
EXCHANGE_UNDERLYING,
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export type DirectCurveV1Param = [
|
|
193
|
+
fromToken: Address,
|
|
194
|
+
toToken: Address,
|
|
195
|
+
exchange: Address,
|
|
196
|
+
fromAmount: NumberAsString,
|
|
197
|
+
toAmount: NumberAsString,
|
|
198
|
+
expectedAmount: NumberAsString,
|
|
199
|
+
feePercent: NumberAsString,
|
|
200
|
+
i: NumberAsString,
|
|
201
|
+
j: NumberAsString,
|
|
202
|
+
partner: Address,
|
|
203
|
+
isApproved: boolean,
|
|
204
|
+
swapType: CurveV1SwapType,
|
|
205
|
+
beneficiary: Address,
|
|
206
|
+
permit: string,
|
|
207
|
+
uuid: string,
|
|
208
|
+
];
|
package/src/dex/curve-v2.ts
CHANGED
|
@@ -3,15 +3,26 @@ import { SwapSide } from '../constants';
|
|
|
3
3
|
import {
|
|
4
4
|
AdapterExchangeParam,
|
|
5
5
|
Address,
|
|
6
|
+
ExchangeTxInfo,
|
|
6
7
|
NumberAsString,
|
|
8
|
+
PreprocessTransactionOptions,
|
|
7
9
|
SimpleExchangeParam,
|
|
10
|
+
Token,
|
|
11
|
+
TxInfo,
|
|
8
12
|
} from '../types';
|
|
9
13
|
import { IDexTxBuilder } from './idex';
|
|
10
|
-
import {
|
|
14
|
+
import {
|
|
15
|
+
getLocalDeadlineAsFriendlyPlaceholder,
|
|
16
|
+
SimpleExchange,
|
|
17
|
+
} from './simple-exchange';
|
|
11
18
|
import GenericFactoryZapABI from '../abi/curve-v2/GenericFactoryZap.json';
|
|
19
|
+
import DirectSwapABI from '../abi/DirectSwap.json';
|
|
12
20
|
import CurveV2ABI from '../abi/CurveV2.json';
|
|
13
|
-
import Web3 from 'web3';
|
|
14
21
|
import { IDexHelper } from '../dex-helper';
|
|
22
|
+
import { assert } from 'ts-essentials';
|
|
23
|
+
import { Logger } from 'log4js';
|
|
24
|
+
import { OptimalSwapExchange } from '@paraswap/core';
|
|
25
|
+
import { uuidToBytes16 } from '../utils';
|
|
15
26
|
|
|
16
27
|
export enum CurveV2SwapType {
|
|
17
28
|
EXCHANGE,
|
|
@@ -19,12 +30,15 @@ export enum CurveV2SwapType {
|
|
|
19
30
|
EXCHANGE_GENERIC_FACTORY_ZAP,
|
|
20
31
|
}
|
|
21
32
|
|
|
33
|
+
const DIRECT_METHOD_NAME = 'directCurveV2Swap';
|
|
34
|
+
|
|
22
35
|
type CurveV2Data = {
|
|
23
36
|
i: number;
|
|
24
37
|
j: number;
|
|
25
38
|
exchange: string;
|
|
26
39
|
originalPoolAddress: Address;
|
|
27
40
|
swapType: CurveV2SwapType;
|
|
41
|
+
isApproved?: boolean;
|
|
28
42
|
};
|
|
29
43
|
|
|
30
44
|
type CurveV2Param = [
|
|
@@ -43,6 +57,25 @@ type CurveV2ParamsForGenericFactoryZap = [
|
|
|
43
57
|
min_dy: NumberAsString,
|
|
44
58
|
];
|
|
45
59
|
|
|
60
|
+
export type DirectCurveV2Param = [
|
|
61
|
+
fromToken: Address,
|
|
62
|
+
toToken: Address,
|
|
63
|
+
exchange: Address,
|
|
64
|
+
poolAddress: Address,
|
|
65
|
+
fromAmount: NumberAsString,
|
|
66
|
+
toAmount: NumberAsString,
|
|
67
|
+
expectedAmount: NumberAsString,
|
|
68
|
+
feePercent: NumberAsString,
|
|
69
|
+
i: NumberAsString,
|
|
70
|
+
j: NumberAsString,
|
|
71
|
+
partner: Address,
|
|
72
|
+
isApproved: boolean,
|
|
73
|
+
swapType: CurveV2SwapType,
|
|
74
|
+
beneficiary: Address,
|
|
75
|
+
permit: string,
|
|
76
|
+
uuid: string,
|
|
77
|
+
];
|
|
78
|
+
|
|
46
79
|
enum CurveV2SwapFunctions {
|
|
47
80
|
exchange = 'exchange(uint256 i, uint256 j, uint256 dx, uint256 minDy)',
|
|
48
81
|
exchange_underlying = 'exchange_underlying(uint256 i, uint256 j, uint256 dx, uint256 minDy)',
|
|
@@ -51,25 +84,31 @@ enum CurveV2SwapFunctions {
|
|
|
51
84
|
|
|
52
85
|
export class CurveV2
|
|
53
86
|
extends SimpleExchange
|
|
54
|
-
implements IDexTxBuilder<CurveV2Data,
|
|
87
|
+
implements IDexTxBuilder<CurveV2Data, DirectCurveV2Param>
|
|
55
88
|
{
|
|
56
89
|
static dexKeys = ['curvev2'];
|
|
57
90
|
exchangeRouterInterface: Interface;
|
|
58
91
|
genericFactoryZapIface: Interface;
|
|
59
92
|
minConversionRate = '1';
|
|
60
93
|
needWrapNative = true;
|
|
94
|
+
logger: Logger;
|
|
95
|
+
|
|
96
|
+
readonly directSwapIface = new Interface(DirectSwapABI);
|
|
61
97
|
|
|
62
|
-
constructor(dexHelper: IDexHelper) {
|
|
98
|
+
constructor(readonly dexHelper: IDexHelper) {
|
|
63
99
|
super(dexHelper, 'curvev2');
|
|
64
100
|
this.exchangeRouterInterface = new Interface(CurveV2ABI as JsonFragment[]);
|
|
65
101
|
this.genericFactoryZapIface = new Interface(GenericFactoryZapABI);
|
|
102
|
+
this.logger = dexHelper.getLogger(
|
|
103
|
+
`CurveV2_${dexHelper.config.data.network}`,
|
|
104
|
+
);
|
|
66
105
|
}
|
|
67
106
|
|
|
68
107
|
getAdapterParam(
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
108
|
+
_0: string,
|
|
109
|
+
_1: string,
|
|
110
|
+
_2: string,
|
|
111
|
+
_3: string,
|
|
73
112
|
data: CurveV2Data,
|
|
74
113
|
side: SwapSide,
|
|
75
114
|
): AdapterExchangeParam {
|
|
@@ -100,6 +139,125 @@ export class CurveV2
|
|
|
100
139
|
};
|
|
101
140
|
}
|
|
102
141
|
|
|
142
|
+
getTokenFromAddress(address: Address): Token {
|
|
143
|
+
// In this Dex decimals are not used
|
|
144
|
+
return { address, decimals: 0 };
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async preProcessTransaction(
|
|
148
|
+
optimalSwapExchange: OptimalSwapExchange<CurveV2Data>,
|
|
149
|
+
srcToken: Token,
|
|
150
|
+
_0: Token,
|
|
151
|
+
_1: SwapSide,
|
|
152
|
+
options: PreprocessTransactionOptions,
|
|
153
|
+
): Promise<[OptimalSwapExchange<CurveV2Data>, ExchangeTxInfo]> {
|
|
154
|
+
if (!options.isDirectMethod) {
|
|
155
|
+
return [
|
|
156
|
+
optimalSwapExchange,
|
|
157
|
+
{
|
|
158
|
+
deadline: BigInt(getLocalDeadlineAsFriendlyPlaceholder()),
|
|
159
|
+
},
|
|
160
|
+
];
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
assert(
|
|
164
|
+
optimalSwapExchange.data !== undefined,
|
|
165
|
+
`preProcessTransaction: data field is missing`,
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
let isApproved: boolean | undefined;
|
|
169
|
+
|
|
170
|
+
try {
|
|
171
|
+
this.erc20Contract.options.address =
|
|
172
|
+
this.dexHelper.config.wrapETH(srcToken).address;
|
|
173
|
+
const allowance = await this.erc20Contract.methods
|
|
174
|
+
.allowance(this.augustusAddress, optimalSwapExchange.data.exchange)
|
|
175
|
+
.call(undefined, 'latest');
|
|
176
|
+
isApproved =
|
|
177
|
+
BigInt(allowance.toString()) >= BigInt(optimalSwapExchange.srcAmount);
|
|
178
|
+
} catch (e) {
|
|
179
|
+
this.logger.error(
|
|
180
|
+
`preProcessTransaction failed to retrieve allowance info: `,
|
|
181
|
+
e,
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return [
|
|
186
|
+
{
|
|
187
|
+
...optimalSwapExchange,
|
|
188
|
+
data: {
|
|
189
|
+
...optimalSwapExchange.data,
|
|
190
|
+
isApproved,
|
|
191
|
+
},
|
|
192
|
+
},
|
|
193
|
+
{
|
|
194
|
+
deadline: BigInt(getLocalDeadlineAsFriendlyPlaceholder()),
|
|
195
|
+
},
|
|
196
|
+
];
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
getDirectParam(
|
|
200
|
+
srcToken: Address,
|
|
201
|
+
destToken: Address,
|
|
202
|
+
srcAmount: NumberAsString,
|
|
203
|
+
destAmount: NumberAsString,
|
|
204
|
+
expectedAmount: NumberAsString,
|
|
205
|
+
data: CurveV2Data,
|
|
206
|
+
side: SwapSide,
|
|
207
|
+
permit: string,
|
|
208
|
+
uuid: string,
|
|
209
|
+
feePercent: NumberAsString,
|
|
210
|
+
_0: NumberAsString,
|
|
211
|
+
partner: string,
|
|
212
|
+
beneficiary: string,
|
|
213
|
+
contractMethod?: string,
|
|
214
|
+
): TxInfo<DirectCurveV2Param> {
|
|
215
|
+
if (contractMethod !== DIRECT_METHOD_NAME) {
|
|
216
|
+
throw new Error(`Invalid contract method ${contractMethod}`);
|
|
217
|
+
}
|
|
218
|
+
assert(side === SwapSide.SELL, 'Buy not supported');
|
|
219
|
+
|
|
220
|
+
let isApproved: boolean = !!data.isApproved;
|
|
221
|
+
if (data.isApproved === undefined) {
|
|
222
|
+
this.logger.warn(`isApproved is undefined, defaulting to false`);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const swapParams: DirectCurveV2Param = [
|
|
226
|
+
srcToken,
|
|
227
|
+
destToken,
|
|
228
|
+
data.exchange,
|
|
229
|
+
data.originalPoolAddress,
|
|
230
|
+
srcAmount,
|
|
231
|
+
destAmount,
|
|
232
|
+
expectedAmount,
|
|
233
|
+
feePercent,
|
|
234
|
+
data.i.toString(),
|
|
235
|
+
data.j.toString(),
|
|
236
|
+
partner,
|
|
237
|
+
isApproved,
|
|
238
|
+
data.swapType,
|
|
239
|
+
beneficiary,
|
|
240
|
+
permit,
|
|
241
|
+
uuidToBytes16(uuid),
|
|
242
|
+
];
|
|
243
|
+
|
|
244
|
+
const encoder = (...params: DirectCurveV2Param) => {
|
|
245
|
+
return this.directSwapIface.encodeFunctionData(DIRECT_METHOD_NAME, [
|
|
246
|
+
params,
|
|
247
|
+
]);
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
return {
|
|
251
|
+
params: swapParams,
|
|
252
|
+
encoder,
|
|
253
|
+
networkFee: '0',
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
static getDirectFunctionName(): string[] {
|
|
258
|
+
return [DIRECT_METHOD_NAME];
|
|
259
|
+
}
|
|
260
|
+
|
|
103
261
|
async getSimpleParam(
|
|
104
262
|
srcToken: string,
|
|
105
263
|
destToken: string,
|
|
@@ -64,8 +64,8 @@ const buildConfigForGenericRFQ = (): RFQConfig => {
|
|
|
64
64
|
method: 'GET',
|
|
65
65
|
},
|
|
66
66
|
secret,
|
|
67
|
-
intervalMs: 1000 *
|
|
68
|
-
dataTTLS: 1000 *
|
|
67
|
+
intervalMs: 1000 * 2 * 1, // every 2 seconds
|
|
68
|
+
dataTTLS: 1000 * 5 * 1, // ttl 5 seconds
|
|
69
69
|
},
|
|
70
70
|
firmRateConfig: {
|
|
71
71
|
url: `${url}/firm`,
|
package/src/dex/idex.ts
CHANGED
|
@@ -76,9 +76,15 @@ export interface IDexTxBuilder<ExchangeData, DirectParam = null> {
|
|
|
76
76
|
destToken: Address,
|
|
77
77
|
srcAmount: NumberAsString,
|
|
78
78
|
destAmount: NumberAsString,
|
|
79
|
+
expectedAmount: NumberAsString,
|
|
79
80
|
data: ExchangeData,
|
|
80
81
|
side: SwapSide,
|
|
81
82
|
permit: string,
|
|
83
|
+
uuid: string,
|
|
84
|
+
feePercent: NumberAsString,
|
|
85
|
+
deadline: NumberAsString,
|
|
86
|
+
partner: string,
|
|
87
|
+
beneficiary: string,
|
|
82
88
|
contractMethod?: string,
|
|
83
89
|
): TxInfo<DirectParam>;
|
|
84
90
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Interface } from '@ethersproject/abi';
|
|
2
2
|
import Web3Abi, { AbiCoder } from 'web3-eth-abi';
|
|
3
|
+
import { Contract } from 'web3-eth-contract';
|
|
3
4
|
import { Address, SimpleExchangeParam, NumberAsString } from '../types';
|
|
4
5
|
import { CACHE_PREFIX, ETHER_ADDRESS } from '../constants';
|
|
5
6
|
import SimpleSwapHelperABI from '../abi/SimpleSwapHelperRouter.json';
|
|
@@ -9,6 +10,7 @@ import { isETHAddress } from '../utils';
|
|
|
9
10
|
import { MAX_UINT } from '../constants';
|
|
10
11
|
import Web3 from 'web3';
|
|
11
12
|
import { IDexHelper } from '../dex-helper';
|
|
13
|
+
import { AbiItem } from 'web3-utils';
|
|
12
14
|
|
|
13
15
|
/*
|
|
14
16
|
* Context: Augustus routers have all a deadline protection logic implemented globally.
|
|
@@ -26,6 +28,7 @@ export class SimpleExchange {
|
|
|
26
28
|
simpleSwapHelper: Interface;
|
|
27
29
|
protected abiCoder: AbiCoder;
|
|
28
30
|
erc20Interface: Interface;
|
|
31
|
+
erc20Contract: Contract;
|
|
29
32
|
|
|
30
33
|
needWrapNative = false;
|
|
31
34
|
isFeeOnTransferSupported = false;
|
|
@@ -42,6 +45,9 @@ export class SimpleExchange {
|
|
|
42
45
|
constructor(dexHelper: IDexHelper, public dexKey: string) {
|
|
43
46
|
this.simpleSwapHelper = new Interface(SimpleSwapHelperABI);
|
|
44
47
|
this.erc20Interface = new Interface(ERC20ABI);
|
|
48
|
+
this.erc20Contract = new dexHelper.web3Provider.eth.Contract(
|
|
49
|
+
ERC20ABI as AbiItem[],
|
|
50
|
+
);
|
|
45
51
|
this.abiCoder = Web3Abi as unknown as AbiCoder;
|
|
46
52
|
|
|
47
53
|
this.network = dexHelper.config.data.network;
|
|
@@ -518,7 +518,8 @@ export const UniswapV2Config: DexConfigMap<DexParams> = {
|
|
|
518
518
|
},
|
|
519
519
|
QuickSwap: {
|
|
520
520
|
[Network.POLYGON]: {
|
|
521
|
-
subgraphURL:
|
|
521
|
+
subgraphURL:
|
|
522
|
+
'https://api.thegraph.com/subgraphs/name/sameepsi/quickswap06',
|
|
522
523
|
factoryAddress: '0x5757371414417b8C6CAad45bAeF941aBc7d3Ab32',
|
|
523
524
|
initCode:
|
|
524
525
|
'0x96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f',
|
|
@@ -788,9 +788,15 @@ export class UniswapV2
|
|
|
788
788
|
destToken: Address,
|
|
789
789
|
srcAmount: NumberAsString,
|
|
790
790
|
destAmount: NumberAsString,
|
|
791
|
+
expectedAmount: NumberAsString,
|
|
791
792
|
_data: UniswapData,
|
|
792
793
|
side: SwapSide,
|
|
793
794
|
permit: string,
|
|
795
|
+
uuid: string,
|
|
796
|
+
feePercent: NumberAsString,
|
|
797
|
+
deadline: NumberAsString,
|
|
798
|
+
partner: string,
|
|
799
|
+
beneficiary: string,
|
|
794
800
|
contractMethod?: string,
|
|
795
801
|
): TxInfo<UniswapParam> {
|
|
796
802
|
if (!contractMethod) throw new Error(`contractMethod need to be passed`);
|
|
@@ -33,3 +33,8 @@ export const ZERO_ORACLE_OBSERVATION = {
|
|
|
33
33
|
export const OUT_OF_RANGE_ERROR_POSTFIX = `INVALID_TICK_BIT_MAP_RANGES`;
|
|
34
34
|
|
|
35
35
|
export const DEFAULT_POOL_INIT_CODE_HASH = `0xe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54`;
|
|
36
|
+
|
|
37
|
+
export enum DirectMethods {
|
|
38
|
+
directSell = 'directUniV3Swap',
|
|
39
|
+
directBuy = 'directUniV3Buy',
|
|
40
|
+
}
|
|
@@ -57,6 +57,7 @@ export type UniswapV3Data = {
|
|
|
57
57
|
tokenOut: Address;
|
|
58
58
|
fee: NumberAsString;
|
|
59
59
|
}[];
|
|
60
|
+
isApproved?: boolean;
|
|
60
61
|
};
|
|
61
62
|
|
|
62
63
|
export type DexParams = {
|
|
@@ -72,7 +73,7 @@ export type DexParams = {
|
|
|
72
73
|
initHash: string;
|
|
73
74
|
};
|
|
74
75
|
|
|
75
|
-
export type
|
|
76
|
+
export type UniswapV3SimpleSwapSellParam = {
|
|
76
77
|
path: string;
|
|
77
78
|
recipient: Address;
|
|
78
79
|
deadline: string;
|
|
@@ -80,7 +81,7 @@ export type UniswapV3SellParam = {
|
|
|
80
81
|
amountOutMinimum: NumberAsString;
|
|
81
82
|
};
|
|
82
83
|
|
|
83
|
-
export type
|
|
84
|
+
export type UniswapV3SimpleSwapBuyParam = {
|
|
84
85
|
path: string;
|
|
85
86
|
recipient: Address;
|
|
86
87
|
deadline: string;
|
|
@@ -88,7 +89,26 @@ export type UniswapV3BuyParam = {
|
|
|
88
89
|
amountInMaximum: NumberAsString;
|
|
89
90
|
};
|
|
90
91
|
|
|
91
|
-
export type
|
|
92
|
+
export type UniswapV3SimpleSwapParams =
|
|
93
|
+
| UniswapV3SimpleSwapSellParam
|
|
94
|
+
| UniswapV3SimpleSwapBuyParam;
|
|
95
|
+
|
|
96
|
+
export type UniswapV3Param = [
|
|
97
|
+
fromToken: Address,
|
|
98
|
+
toToken: Address,
|
|
99
|
+
exchange: Address,
|
|
100
|
+
fromAmount: NumberAsString,
|
|
101
|
+
toAmount: NumberAsString,
|
|
102
|
+
expectedAmount: NumberAsString,
|
|
103
|
+
feePercent: NumberAsString,
|
|
104
|
+
deadline: NumberAsString,
|
|
105
|
+
partner: Address,
|
|
106
|
+
isApproved: boolean,
|
|
107
|
+
beneficiary: Address,
|
|
108
|
+
path: string,
|
|
109
|
+
permit: string,
|
|
110
|
+
uuid: string,
|
|
111
|
+
];
|
|
92
112
|
|
|
93
113
|
export enum UniswapV3Functions {
|
|
94
114
|
exactInput = 'exactInput',
|