@dhedge/v2-sdk 2.0.0 → 2.0.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/dist/entities/pool.d.ts +91 -91
- package/dist/services/flatmoney/stableLp.d.ts +4 -4
- package/dist/services/odos/index.d.ts +1 -0
- package/dist/services/toros/completeWithdrawal.d.ts +7 -0
- package/dist/services/toros/easySwapper.d.ts +2 -2
- package/dist/services/toros/initWithdrawal.d.ts +3 -0
- package/dist/services/toros/retry.d.ts +5 -0
- package/dist/services/toros/swapData.d.ts +11 -0
- package/dist/types.d.ts +5 -0
- package/dist/utils/contract.d.ts +3 -2
- package/dist/v2-sdk.cjs.development.js +3905 -1015
- package/dist/v2-sdk.cjs.development.js.map +1 -1
- package/dist/v2-sdk.cjs.production.min.js +1 -1
- package/dist/v2-sdk.cjs.production.min.js.map +1 -1
- package/dist/v2-sdk.esm.js +3905 -1015
- package/dist/v2-sdk.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/abi/IAaveLendingPoolAssetGuard.json +645 -0
- package/src/abi/IEasySwapperV2.json +1507 -0
- package/src/entities/pool.ts +231 -141
- package/src/services/flatmoney/stableLp.ts +27 -21
- package/src/services/odos/index.ts +1 -1
- package/src/services/toros/completeWithdrawal.ts +209 -0
- package/src/services/toros/easySwapper.ts +16 -84
- package/src/services/toros/initWithdrawal.ts +166 -0
- package/src/services/toros/retry.ts +28 -0
- package/src/services/toros/swapData.ts +70 -0
- package/src/test/constants.ts +1 -1
- package/src/test/toros.test.ts +10 -8
- package/src/types.ts +8 -0
- package/src/utils/contract.ts +71 -43
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import axios from "axios";
|
|
2
|
+
import BigNumber from "bignumber.js";
|
|
3
|
+
import { odosBaseUrl } from "../odos";
|
|
4
|
+
|
|
5
|
+
export const SWAPPER_ADDERSS = "0x4F754e0F0924afD74980886b0B479Fa1D7C58D0D";
|
|
6
|
+
|
|
7
|
+
export interface SwapParams {
|
|
8
|
+
srcAsset: string; // Source asset address
|
|
9
|
+
srcAmount: string; // Amount to swap (use string for big numbers)
|
|
10
|
+
dstAsset: string; // Destination asset address
|
|
11
|
+
chainId: number; // Chain ID
|
|
12
|
+
from: string; // Sender address
|
|
13
|
+
receiver: string; // Receiver address
|
|
14
|
+
slippage: number; // Slippage tolerance in basis points (100 = 1%)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const getSwapDataViaOdos = async ({
|
|
18
|
+
srcAsset,
|
|
19
|
+
srcAmount,
|
|
20
|
+
dstAsset,
|
|
21
|
+
chainId,
|
|
22
|
+
from,
|
|
23
|
+
slippage
|
|
24
|
+
}: SwapParams): Promise<string> => {
|
|
25
|
+
let referralCode = 0; // Defaults to 0 for unregistered activity.
|
|
26
|
+
if (
|
|
27
|
+
process.env.ODOS_REFERAL_CODE &&
|
|
28
|
+
Number(process.env.ODOS_REFERAL_CODE) > 0
|
|
29
|
+
) {
|
|
30
|
+
referralCode = Number(process.env.ODOS_REFERAL_CODE);
|
|
31
|
+
}
|
|
32
|
+
const quoteParams = {
|
|
33
|
+
chainId: chainId,
|
|
34
|
+
inputTokens: [
|
|
35
|
+
{
|
|
36
|
+
tokenAddress: srcAsset,
|
|
37
|
+
amount: srcAmount
|
|
38
|
+
}
|
|
39
|
+
],
|
|
40
|
+
outputTokens: [
|
|
41
|
+
{
|
|
42
|
+
tokenAddress: dstAsset,
|
|
43
|
+
proportion: 1
|
|
44
|
+
}
|
|
45
|
+
],
|
|
46
|
+
slippageLimitPercent: new BigNumber(slippage).div(100).toString(), // Convert basis points to percentage
|
|
47
|
+
userAddr: from,
|
|
48
|
+
referralCode
|
|
49
|
+
};
|
|
50
|
+
try {
|
|
51
|
+
const quoteResult = await axios.post(
|
|
52
|
+
`${odosBaseUrl}/quote/v2`,
|
|
53
|
+
quoteParams
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
const assembleParams = {
|
|
57
|
+
pathId: quoteResult.data.pathId,
|
|
58
|
+
userAddr: from
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const assembleResult = await axios.post(
|
|
62
|
+
`${odosBaseUrl}/assemble`,
|
|
63
|
+
assembleParams
|
|
64
|
+
);
|
|
65
|
+
return assembleResult.data.transaction.data;
|
|
66
|
+
} catch (e) {
|
|
67
|
+
console.error("Error in Odos API request:", e);
|
|
68
|
+
throw new Error("Swap api request of Odos failed");
|
|
69
|
+
}
|
|
70
|
+
};
|
package/src/test/constants.ts
CHANGED
|
@@ -89,7 +89,7 @@ export const CONTRACT_ADDRESS = {
|
|
|
89
89
|
VELO: "0x9560e827aF36c94D2Ac33a39bCE1Fe78631088Db",
|
|
90
90
|
COMPOUNDV3_WETH: "",
|
|
91
91
|
FLUID_WETH: "",
|
|
92
|
-
TOROS: "
|
|
92
|
+
TOROS: "0xcacb5a722a36cff6baeb359e21c098a4acbffdfa" //ETHBEAR1X
|
|
93
93
|
},
|
|
94
94
|
[Network.ARBITRUM]: {
|
|
95
95
|
USDC: "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
|
package/src/test/toros.test.ts
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
2
2
|
|
|
3
|
+
import BigNumber from "bignumber.js";
|
|
3
4
|
import { Dhedge, Pool } from "..";
|
|
4
5
|
import { routerAddress } from "../config";
|
|
5
6
|
|
|
6
7
|
import { Dapp, Network } from "../types";
|
|
7
8
|
import { CONTRACT_ADDRESS, MAX_AMOUNT, TEST_POOL } from "./constants";
|
|
8
9
|
import {
|
|
9
|
-
TestingRunParams,
|
|
10
10
|
setChainlinkTimeout,
|
|
11
11
|
setUSDCAmount,
|
|
12
|
-
testingHelper
|
|
12
|
+
testingHelper,
|
|
13
|
+
TestingRunParams
|
|
13
14
|
} from "./utils/testingHelper";
|
|
15
|
+
|
|
14
16
|
import { allowanceDelta, balanceDelta } from "./utils/token";
|
|
15
|
-
import BigNumber from "bignumber.js";
|
|
16
17
|
|
|
17
18
|
const testToros = ({ wallet, network, provider }: TestingRunParams) => {
|
|
18
19
|
const USDC = CONTRACT_ADDRESS[network].USDC;
|
|
@@ -34,7 +35,7 @@ const testToros = ({ wallet, network, provider }: TestingRunParams) => {
|
|
|
34
35
|
]);
|
|
35
36
|
await provider.send("evm_mine", []);
|
|
36
37
|
// top up USDC
|
|
37
|
-
const amount = new BigNumber(
|
|
38
|
+
const amount = new BigNumber(100).times(1e6).toFixed(0);
|
|
38
39
|
await setUSDCAmount({
|
|
39
40
|
amount,
|
|
40
41
|
userAddress: pool.address,
|
|
@@ -56,7 +57,7 @@ const testToros = ({ wallet, network, provider }: TestingRunParams) => {
|
|
|
56
57
|
|
|
57
58
|
it("trades USDC balance into Toros Token", async () => {
|
|
58
59
|
const usdcBalance = await pool.utils.getBalance(USDC, pool.address);
|
|
59
|
-
await pool.trade(Dapp.TOROS, USDC, TOROS, usdcBalance, 1);
|
|
60
|
+
await pool.trade(Dapp.TOROS, USDC, TOROS, usdcBalance, 1.5);
|
|
60
61
|
const torosBalanceDelta = await balanceDelta(
|
|
61
62
|
pool.address,
|
|
62
63
|
TOROS,
|
|
@@ -70,7 +71,7 @@ const testToros = ({ wallet, network, provider }: TestingRunParams) => {
|
|
|
70
71
|
await provider.send("evm_mine", []);
|
|
71
72
|
const torosBalance = await pool.utils.getBalance(TOROS, pool.address);
|
|
72
73
|
await pool.approve(Dapp.TOROS, TOROS, MAX_AMOUNT);
|
|
73
|
-
await pool.trade(Dapp.TOROS, TOROS, USDC, torosBalance, 1);
|
|
74
|
+
await pool.trade(Dapp.TOROS, TOROS, USDC, torosBalance, 1.5);
|
|
74
75
|
const torosBalanceDelta = await balanceDelta(
|
|
75
76
|
pool.address,
|
|
76
77
|
TOROS,
|
|
@@ -80,7 +81,7 @@ const testToros = ({ wallet, network, provider }: TestingRunParams) => {
|
|
|
80
81
|
});
|
|
81
82
|
|
|
82
83
|
it("complete withdrawal from Toros asset", async () => {
|
|
83
|
-
await pool.completeTorosWithdrawal(USDC, 1);
|
|
84
|
+
await pool.completeTorosWithdrawal(USDC, 1.5);
|
|
84
85
|
const usdcBalanceDelta = await balanceDelta(
|
|
85
86
|
pool.address,
|
|
86
87
|
USDC,
|
|
@@ -93,5 +94,6 @@ const testToros = ({ wallet, network, provider }: TestingRunParams) => {
|
|
|
93
94
|
|
|
94
95
|
testingHelper({
|
|
95
96
|
network: Network.OPTIMISM,
|
|
96
|
-
testingRun: testToros
|
|
97
|
+
testingRun: testToros,
|
|
98
|
+
onFork: true
|
|
97
99
|
});
|
package/src/types.ts
CHANGED
|
@@ -101,3 +101,11 @@ export type LyraPosition = {
|
|
|
101
101
|
collateral: BigNumber;
|
|
102
102
|
state: number;
|
|
103
103
|
};
|
|
104
|
+
|
|
105
|
+
export type SDKOptions =
|
|
106
|
+
| {
|
|
107
|
+
estimateGas: boolean;
|
|
108
|
+
onlyGetTxData?: boolean;
|
|
109
|
+
useTraderAddressAsFrom?: boolean;
|
|
110
|
+
}
|
|
111
|
+
| boolean; // shorthand for { estimateGas: true/false }; for backward compatibility
|
package/src/utils/contract.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import set from "lodash/set";
|
|
4
4
|
import { Interface } from "@ethersproject/abi";
|
|
5
5
|
import { multiCallAddress } from "../config";
|
|
6
|
-
import { ethers, Network, Pool } from "..";
|
|
6
|
+
import { ethers, Network, Pool, SDKOptions } from "..";
|
|
7
7
|
|
|
8
8
|
export async function call(
|
|
9
9
|
provider: ethers.Signer,
|
|
@@ -94,56 +94,84 @@ export class Multicaller {
|
|
|
94
94
|
}
|
|
95
95
|
}
|
|
96
96
|
|
|
97
|
+
const getGasEstimateData = async (
|
|
98
|
+
estimateFunc: ethers.ContractFunction<ethers.BigNumber>,
|
|
99
|
+
txInfoData: { to: string; txData: string; minAmountOut: any },
|
|
100
|
+
txOptions: any = {}
|
|
101
|
+
) => {
|
|
102
|
+
let gas = null;
|
|
103
|
+
let gasEstimationError = null;
|
|
104
|
+
try {
|
|
105
|
+
gas = await estimateFunc(txInfoData.to, txInfoData.txData, txOptions);
|
|
106
|
+
} catch (e) {
|
|
107
|
+
gasEstimationError = e;
|
|
108
|
+
}
|
|
109
|
+
return {
|
|
110
|
+
gas,
|
|
111
|
+
gasEstimationError,
|
|
112
|
+
...txInfoData
|
|
113
|
+
};
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
export const isSdkOptionsBoolean = (
|
|
117
|
+
sdkOptions: SDKOptions
|
|
118
|
+
): sdkOptions is boolean => {
|
|
119
|
+
return typeof sdkOptions === "boolean";
|
|
120
|
+
};
|
|
121
|
+
|
|
97
122
|
export const getPoolTxOrGasEstimate = async (
|
|
98
123
|
pool: Pool,
|
|
99
124
|
args: any[],
|
|
100
|
-
|
|
125
|
+
sdkOptions: SDKOptions
|
|
101
126
|
): Promise<any> => {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
127
|
+
const txInfoData = {
|
|
128
|
+
txData: args[1],
|
|
129
|
+
to: args[0],
|
|
130
|
+
minAmountOut: args[3] || null
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
if (!isSdkOptionsBoolean(sdkOptions) && sdkOptions.onlyGetTxData) {
|
|
134
|
+
return txInfoData;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const txOptions = args[2];
|
|
138
|
+
|
|
139
|
+
if (
|
|
140
|
+
!pool.isDhedge ||
|
|
141
|
+
(!isSdkOptionsBoolean(sdkOptions) && sdkOptions.useTraderAddressAsFrom)
|
|
142
|
+
) {
|
|
143
|
+
if (
|
|
144
|
+
sdkOptions === true ||
|
|
145
|
+
(!isSdkOptionsBoolean(sdkOptions) && sdkOptions.estimateGas)
|
|
146
|
+
) {
|
|
147
|
+
return await getGasEstimateData(
|
|
148
|
+
pool.signer.estimateGas,
|
|
149
|
+
txInfoData,
|
|
150
|
+
txOptions
|
|
151
|
+
);
|
|
122
152
|
} else {
|
|
123
|
-
return await pool.
|
|
153
|
+
return await pool.signer.sendTransaction({
|
|
154
|
+
to: txInfoData.to,
|
|
155
|
+
data: txInfoData.txData,
|
|
156
|
+
...txOptions
|
|
157
|
+
});
|
|
124
158
|
}
|
|
125
159
|
} else {
|
|
126
|
-
if (
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
gas,
|
|
136
|
-
txData: args[1],
|
|
137
|
-
to: args[0],
|
|
138
|
-
gasEstimationError,
|
|
139
|
-
minAmountOut: args[3] || null
|
|
140
|
-
};
|
|
160
|
+
if (
|
|
161
|
+
sdkOptions === true ||
|
|
162
|
+
(!isSdkOptionsBoolean(sdkOptions) && sdkOptions.estimateGas)
|
|
163
|
+
) {
|
|
164
|
+
return await getGasEstimateData(
|
|
165
|
+
pool.poolLogic.estimateGas.execTransaction,
|
|
166
|
+
txInfoData,
|
|
167
|
+
txOptions
|
|
168
|
+
);
|
|
141
169
|
} else {
|
|
142
|
-
return await pool.
|
|
143
|
-
to
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
170
|
+
return await pool.poolLogic.execTransaction(
|
|
171
|
+
txInfoData.to,
|
|
172
|
+
txInfoData.txData,
|
|
173
|
+
txOptions
|
|
174
|
+
);
|
|
147
175
|
}
|
|
148
176
|
}
|
|
149
177
|
};
|