@dhedge/v2-sdk 2.1.7 → 2.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/README.md +400 -53
- package/dist/config.d.ts +14 -2
- package/dist/entities/pool.d.ts +25 -86
- package/dist/entities/utils.d.ts +15 -0
- package/dist/services/cowSwap/index.d.ts +10 -0
- package/dist/services/hyperliquid/index.d.ts +22 -0
- package/dist/services/kyberSwap/index.d.ts +1 -1
- package/dist/services/oneInch/index.d.ts +1 -1
- package/dist/services/toros/easySwapper.d.ts +14 -0
- package/dist/services/toros/swapData.d.ts +5 -5
- package/dist/services/uniswap/V3Liquidity.d.ts +2 -2
- package/dist/services/velodrome/liquidity.d.ts +3 -0
- package/dist/test/constants.d.ts +48 -3
- package/dist/test/utils/testingHelper.d.ts +4 -0
- package/dist/types.d.ts +21 -5
- package/dist/utils/contract.d.ts +20 -0
- package/dist/v2-sdk.cjs.development.js +5193 -6711
- 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 +5198 -6711
- package/dist/v2-sdk.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/abi/PoolFactory.json +414 -204
- package/src/abi/PoolLogic.json +160 -134
- package/src/config.ts +19 -9
- package/src/entities/pool.ts +103 -254
- package/src/entities/utils.ts +15 -0
- package/src/services/cowSwap/index.ts +281 -0
- package/src/services/hyperliquid/index.ts +22 -0
- package/src/services/kyberSwap/index.ts +5 -3
- package/src/services/oneInch/index.ts +5 -4
- package/src/services/toros/completeWithdrawal.ts +57 -40
- package/src/services/toros/easySwapper.ts +15 -1
- package/src/services/toros/initWithdrawal.ts +39 -31
- package/src/services/toros/swapData.ts +45 -131
- package/src/services/uniswap/V3Liquidity.ts +3 -24
- package/src/services/velodrome/liquidity.ts +3 -0
- package/src/test/aave.test.ts +99 -70
- package/src/test/aerodrome.test.ts +53 -24
- package/src/test/aerodromeCL.test.ts +64 -30
- package/src/test/arrakis.test.ts +23 -35
- package/src/test/balancer.test.ts +114 -106
- package/src/test/compoundV3.test.ts +45 -29
- package/src/test/constants.ts +57 -12
- package/src/test/cowswap.test.ts +79 -0
- package/src/test/dhedge.test.ts +45 -12
- package/src/test/flatmoney.test.ts +25 -39
- package/src/test/fluid.test.ts +33 -24
- package/src/test/hyperliquid.onchain.test.ts +131 -0
- package/src/test/kyberSwap.test.ts +37 -16
- package/src/test/lyra.test.ts +159 -150
- package/src/test/odos.test.ts +2 -2
- package/src/test/oneInch.test.ts +36 -22
- package/src/test/pancakeCL.test.ts +72 -31
- package/src/test/pendle.test.ts +94 -54
- package/src/test/{pendleMint.test.ts → pendleMint.onchain.test.ts} +22 -8
- package/src/test/pool.test.ts +152 -95
- package/src/test/toros.onchain.test.ts +92 -0
- package/src/test/toros.test.ts +74 -20
- package/src/test/torosLimitOrder.test.ts +87 -42
- package/src/test/uniswap.test.ts +77 -128
- package/src/test/utils/testingHelper.ts +120 -0
- package/src/test/velodrome.test.ts +126 -92
- package/src/test/velodromeCL.test.ts +43 -31
- package/src/test/velodromeV2.test.ts +153 -95
- package/src/types.ts +22 -6
- package/src/utils/contract.ts +20 -0
- package/dist/services/futures/constants.d.ts +0 -1
- package/dist/services/futures/index.d.ts +0 -2
- package/dist/services/futures/margin.d.ts +0 -2
- package/dist/services/futures/trade.d.ts +0 -3
- package/dist/services/ramses/vesting.d.ts +0 -4
- package/dist/services/uniswap/V3Trade.d.ts +0 -3
- package/dist/test/utils/futures.d.ts +0 -2
- package/src/abi/IRamsesNonfungiblePositionManager.json +0 -486
- package/src/abi/ISynthetiXFuturesMarketV2.json +0 -531
- package/src/abi/ISynthetix.json +0 -139
- package/src/abi/IUniswapV3Quoter.json +0 -195
- package/src/abi/IUniswapV3Router.json +0 -221
- package/src/abi/IXRam.json +0 -99
- package/src/services/futures/constants.ts +0 -1
- package/src/services/futures/index.ts +0 -2
- package/src/services/futures/margin.ts +0 -10
- package/src/services/futures/trade.ts +0 -32
- package/src/services/ramses/vesting.ts +0 -24
- package/src/services/uniswap/V3Trade.ts +0 -46
- package/src/test/futures.test.ts +0 -51
- package/src/test/hyperliquid.test.ts +0 -107
- package/src/test/ramses.test.ts +0 -190
- package/src/test/ramsesCL.test.ts +0 -155
- package/src/test/synthetix.test.ts +0 -36
- package/src/test/utils/futures.ts +0 -14
package/src/test/pool.test.ts
CHANGED
|
@@ -1,12 +1,20 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import BigNumber from "bignumber.js";
|
|
2
|
+
import { Dhedge, ethers, Network, Pool } from "..";
|
|
3
|
+
import { AssetEnabled } from "../types";
|
|
4
|
+
import { CONTRACT_ADDRESS } from "./constants";
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
testingHelper,
|
|
8
|
+
TestingRunParams,
|
|
9
|
+
beforeAfterReset,
|
|
10
|
+
setUSDCAmount,
|
|
11
|
+
setWETHAmount,
|
|
12
|
+
setChainlinkTimeout,
|
|
13
|
+
fixOracleAggregatorStaleness
|
|
14
|
+
} from "./utils/testingHelper";
|
|
15
|
+
import { allowanceDelta, balanceDelta } from "./utils/token";
|
|
16
|
+
|
|
17
|
+
const testPool = ({ wallet, network, provider }: TestingRunParams) => {
|
|
10
18
|
let dhedge: Dhedge;
|
|
11
19
|
let pool: Pool;
|
|
12
20
|
|
|
@@ -14,104 +22,153 @@ const testPool = ({ wallet, network }: TestingRunParams) => {
|
|
|
14
22
|
|
|
15
23
|
describe(`pool on ${network}`, () => {
|
|
16
24
|
beforeAll(async () => {
|
|
25
|
+
await provider.send("hardhat_setBalance", [
|
|
26
|
+
wallet.address,
|
|
27
|
+
"0x10000000000000000"
|
|
28
|
+
]);
|
|
17
29
|
dhedge = new Dhedge(wallet, network);
|
|
18
|
-
|
|
30
|
+
|
|
31
|
+
// Create a fresh pool with USDC and WETH
|
|
32
|
+
pool = await dhedge.createPool("Test Manager", "Pool Test Fund", "PTF", [
|
|
33
|
+
[CONTRACT_ADDRESS[network].USDC, true],
|
|
34
|
+
[CONTRACT_ADDRESS[network].WETH, false]
|
|
35
|
+
]);
|
|
36
|
+
|
|
37
|
+
// Extend oracle timeouts so price feeds work on fork
|
|
38
|
+
await setChainlinkTimeout({ pool, provider }, 86400 * 365);
|
|
39
|
+
await fixOracleAggregatorStaleness({ pool, provider });
|
|
40
|
+
|
|
41
|
+
// Fund wallet with USDC for deposit test
|
|
42
|
+
await setUSDCAmount({
|
|
43
|
+
amount: (100 * 1e6).toString(),
|
|
44
|
+
userAddress: wallet.address,
|
|
45
|
+
network,
|
|
46
|
+
provider
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
beforeAfterReset({ beforeAll, afterAll, provider });
|
|
51
|
+
|
|
52
|
+
it("checks fund composition", async () => {
|
|
53
|
+
const result = await pool.getComposition();
|
|
54
|
+
expect(result.length).toBeGreaterThan(0);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("skips removing assets with a non-zero balance and warns", async () => {
|
|
58
|
+
// The pool currently has [USDC, WETH]. PoolManagerLogic.changeAssets
|
|
59
|
+
// reverts on-chain if you try to remove an asset with a non-zero pool
|
|
60
|
+
// balance. Pool.changeAssets() simulates each removal via callStatic
|
|
61
|
+
// and skips the ones that would revert, so the real tx still succeeds.
|
|
62
|
+
// This test proves that path: fund WETH → ask to remove it →
|
|
63
|
+
// expect WETH to stay in the composition and a warn line to be logged.
|
|
64
|
+
|
|
65
|
+
// 1. Give WETH a non-zero balance so its removal simulation will revert.
|
|
66
|
+
await setWETHAmount({
|
|
67
|
+
amount: new BigNumber(1).times(1e18).toFixed(0),
|
|
68
|
+
userAddress: pool.address,
|
|
69
|
+
network,
|
|
70
|
+
provider
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
// 2. Spy on console.warn so we can assert the skip was reported.
|
|
74
|
+
// mockImplementation silences the output during the test run.
|
|
75
|
+
const warnSpy = jest.spyOn(console, "warn").mockImplementation(() => {
|
|
76
|
+
/* swallow warnings so they don't pollute test output */
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
// 3. Ask changeAssets to end up with [USDC] only — i.e. drop WETH.
|
|
80
|
+
// Internally, the WETH removal simulation will revert, so WETH is
|
|
81
|
+
// dropped from `removedAssets` and the real tx never tries to remove it.
|
|
82
|
+
const newAssets: AssetEnabled[] = [
|
|
83
|
+
{ asset: CONTRACT_ADDRESS[network].USDC, isDeposit: true }
|
|
84
|
+
];
|
|
85
|
+
await pool.changeAssets(newAssets);
|
|
86
|
+
|
|
87
|
+
// 4. WETH should still be an enabled asset (skip worked).
|
|
88
|
+
const assetsAfter = await pool.getComposition();
|
|
89
|
+
const wethStillPresent = assetsAfter.some(
|
|
90
|
+
a =>
|
|
91
|
+
a.asset.toLowerCase() === CONTRACT_ADDRESS[network].WETH.toLowerCase()
|
|
92
|
+
);
|
|
93
|
+
expect(wethStillPresent).toBe(true);
|
|
94
|
+
|
|
95
|
+
// 5. And the skip should have been logged via console.warn.
|
|
96
|
+
expect(warnSpy).toHaveBeenCalledWith(
|
|
97
|
+
expect.stringContaining("skipping removal of")
|
|
98
|
+
);
|
|
99
|
+
warnSpy.mockRestore();
|
|
100
|
+
|
|
101
|
+
// 6. Restore pre-test state: drain WETH so the next test — which
|
|
102
|
+
// expects WETH to be removable — isn't affected by our funding.
|
|
103
|
+
await setWETHAmount({
|
|
104
|
+
amount: "0",
|
|
105
|
+
userAddress: pool.address,
|
|
106
|
+
network,
|
|
107
|
+
provider
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("changes enabled assets (removes WETH, keeps USDC only)", async () => {
|
|
112
|
+
const assetsBefore = await pool.getComposition();
|
|
113
|
+
const newAssets: AssetEnabled[] = [
|
|
114
|
+
{ asset: CONTRACT_ADDRESS[network].USDC, isDeposit: true }
|
|
115
|
+
];
|
|
116
|
+
await pool.changeAssets(newAssets);
|
|
117
|
+
const assetsAfter = await pool.getComposition();
|
|
118
|
+
expect(assetsAfter.length).toBeLessThan(assetsBefore.length);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("approves USDC for deposit", async () => {
|
|
122
|
+
await pool.approveDeposit(
|
|
123
|
+
CONTRACT_ADDRESS[network].USDC,
|
|
124
|
+
ethers.constants.MaxUint256
|
|
125
|
+
);
|
|
126
|
+
const usdcAllowanceDelta = await allowanceDelta(
|
|
127
|
+
wallet.address,
|
|
128
|
+
CONTRACT_ADDRESS[network].USDC,
|
|
129
|
+
pool.address,
|
|
130
|
+
pool.signer
|
|
131
|
+
);
|
|
132
|
+
expect(usdcAllowanceDelta.gt(0)).toBe(true);
|
|
19
133
|
});
|
|
20
134
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
// console.log(result);
|
|
24
|
-
// expect(result.length).toBeGreaterThan(0);
|
|
25
|
-
// });
|
|
26
|
-
|
|
27
|
-
// it("sets max supply cap", async () => {
|
|
28
|
-
// const totalSupply: BigNumber = await pool.poolLogic.totalSupply();
|
|
29
|
-
// let initCap = totalSupply;
|
|
30
|
-
// if (totalSupply.eq(0)) {
|
|
31
|
-
// initCap = BigNumber.from(1000).mul(BigNumber.from(10).pow(18));
|
|
32
|
-
// }
|
|
33
|
-
// await pool.setMaxCap(initCap.mul(2), null, true);
|
|
34
|
-
// const tx = await pool.setMaxCap(initCap.mul(2));
|
|
35
|
-
// await tx.wait(1);
|
|
36
|
-
// const maxCapAfter: BigNumber = await pool.managerLogic.maxSupplyCap();
|
|
37
|
-
// expect(maxCapAfter).toEqual(initCap.mul(2));
|
|
38
|
-
// });
|
|
39
|
-
|
|
40
|
-
// it("sets pool private", async () => {
|
|
41
|
-
// const result = await pool.setPrivate(true);
|
|
42
|
-
// expect(result).not.toBeNull();
|
|
43
|
-
// });
|
|
44
|
-
|
|
45
|
-
// it("adds WBTC to enabled assets", async () => {
|
|
46
|
-
// const assetsBefore = await pool.getComposition();
|
|
47
|
-
|
|
48
|
-
// const newAssets: AssetEnabled[] = [
|
|
49
|
-
// { asset: CONTRACT_ADDRESS[network].USDC, isDeposit: true },
|
|
50
|
-
// {
|
|
51
|
-
// asset: "0x3333333333333333333333333333333333333333",
|
|
52
|
-
// isDeposit: false
|
|
53
|
-
// }
|
|
54
|
-
// ];
|
|
55
|
-
// await pool.changeAssets(newAssets);
|
|
56
|
-
// const assetsAfter = await pool.getComposition();
|
|
57
|
-
// expect(assetsAfter.length).toBeLessThanOrEqual(assetsBefore.length);
|
|
58
|
-
// });
|
|
59
|
-
|
|
60
|
-
// it("approves USDT balance of User for Deposit", async () => {
|
|
61
|
-
// await pool.approveDeposit(
|
|
62
|
-
// CONTRACT_ADDRESS[network].USDC,
|
|
63
|
-
// ethers.constants.MaxUint256
|
|
64
|
-
// );
|
|
65
|
-
// const usdtAllowanceDelta = await allowanceDelta(
|
|
66
|
-
// pool.signer.address,
|
|
67
|
-
// CONTRACT_ADDRESS[network].USDC,
|
|
68
|
-
// pool.address,
|
|
69
|
-
// pool.signer
|
|
70
|
-
// );
|
|
71
|
-
// expect(usdtAllowanceDelta.gt(0));
|
|
72
|
-
// });
|
|
73
|
-
|
|
74
|
-
it("deposits 200 USDT into Pool", async () => {
|
|
75
|
-
await pool.deposit(CONTRACT_ADDRESS[network].USDC, (30000000).toString());
|
|
135
|
+
it("deposits 30 USDC into pool", async () => {
|
|
136
|
+
await pool.deposit(CONTRACT_ADDRESS[network].USDC, (30 * 1e6).toString());
|
|
76
137
|
const poolTokenDelta = await balanceDelta(
|
|
138
|
+
wallet.address,
|
|
77
139
|
pool.address,
|
|
78
|
-
CONTRACT_ADDRESS[network].USDC,
|
|
79
140
|
pool.signer
|
|
80
141
|
);
|
|
81
|
-
expect(poolTokenDelta.gt(0));
|
|
142
|
+
expect(poolTokenDelta.gt(0)).toBe(true);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it("gets available manager fee", async () => {
|
|
146
|
+
const result = await pool.getAvailableManagerFee();
|
|
147
|
+
expect(result).toBeDefined();
|
|
82
148
|
});
|
|
83
149
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
// pool.signer
|
|
102
|
-
// );
|
|
103
|
-
// expect(poolTokenDelta.lt(0));
|
|
104
|
-
// });
|
|
150
|
+
it("withdraws pool tokens", async () => {
|
|
151
|
+
// Wait for exit cooldown after deposit
|
|
152
|
+
await provider.send("evm_increaseTime", [24 * 60 * 60]);
|
|
153
|
+
await provider.send("evm_mine", []);
|
|
154
|
+
const poolTokenBalance = await dhedge.utils.getBalance(
|
|
155
|
+
pool.address,
|
|
156
|
+
wallet.address
|
|
157
|
+
);
|
|
158
|
+
expect(poolTokenBalance.gt(0)).toBe(true);
|
|
159
|
+
await pool.withdraw(poolTokenBalance.div(2).toString());
|
|
160
|
+
const poolTokenDelta = await balanceDelta(
|
|
161
|
+
wallet.address,
|
|
162
|
+
pool.address,
|
|
163
|
+
pool.signer
|
|
164
|
+
);
|
|
165
|
+
expect(poolTokenDelta.lt(0)).toBe(true);
|
|
166
|
+
});
|
|
105
167
|
});
|
|
106
168
|
};
|
|
107
169
|
|
|
108
|
-
// testingHelper({
|
|
109
|
-
// network: Network.POLYGON,
|
|
110
|
-
// testingRun: testPool
|
|
111
|
-
// });
|
|
112
|
-
|
|
113
170
|
testingHelper({
|
|
114
|
-
network: Network.
|
|
171
|
+
network: Network.ARBITRUM,
|
|
115
172
|
testingRun: testPool,
|
|
116
|
-
onFork:
|
|
173
|
+
onFork: true
|
|
117
174
|
});
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Toros on-chain tests for init and complete withdrawal.
|
|
5
|
+
* These require a live chain connection (onFork: false) because the
|
|
6
|
+
* withdrawal swap data from DEX aggregators (KyberSwap/1Inch) only
|
|
7
|
+
* works against real chain state, not a Hardhat fork.
|
|
8
|
+
*
|
|
9
|
+
* Prerequisites:
|
|
10
|
+
* - PRIVATE_KEY in .env (must be the pool manager or trader)
|
|
11
|
+
* - ARBITRUM_URL in .env
|
|
12
|
+
* - The test pool must hold Toros tokens ready for withdrawal
|
|
13
|
+
* - Cooldown period must have passed since the last deposit
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { Dhedge, Pool } from "..";
|
|
17
|
+
|
|
18
|
+
import { Dapp, Network } from "../types";
|
|
19
|
+
import { CONTRACT_ADDRESS, MAX_AMOUNT } from "./constants";
|
|
20
|
+
import { getTxOptions } from "./txOptions";
|
|
21
|
+
import { testingHelper, TestingRunParams } from "./utils/testingHelper";
|
|
22
|
+
|
|
23
|
+
const testTorosOnchain = ({ wallet, network }: TestingRunParams) => {
|
|
24
|
+
const USDC = CONTRACT_ADDRESS[network].USDC;
|
|
25
|
+
const TOROS = CONTRACT_ADDRESS[network].TOROS;
|
|
26
|
+
const TEST_POOL_ADDRESS = "0x2d4cddd2c4fa854536593bcf61d0da3b63ed80cb";
|
|
27
|
+
|
|
28
|
+
let dhedge: Dhedge;
|
|
29
|
+
let pool: Pool;
|
|
30
|
+
jest.setTimeout(200000);
|
|
31
|
+
|
|
32
|
+
describe(`[${network}] toros on-chain withdrawal tests`, () => {
|
|
33
|
+
beforeAll(async () => {
|
|
34
|
+
if (!process.env.PRIVATE_KEY || !process.env.ARBITRUM_URL) {
|
|
35
|
+
console.warn(
|
|
36
|
+
"Skipping toros on-chain tests: PRIVATE_KEY and ARBITRUM_URL env vars required"
|
|
37
|
+
);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
dhedge = new Dhedge(wallet, network);
|
|
41
|
+
pool = await dhedge.loadPool(TEST_POOL_ADDRESS);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("init Toros Token for withdrawal", async () => {
|
|
45
|
+
if (!process.env.PRIVATE_KEY || !process.env.ARBITRUM_URL) return;
|
|
46
|
+
const torosBalanceBefore = await pool.utils.getBalance(
|
|
47
|
+
TOROS,
|
|
48
|
+
pool.address
|
|
49
|
+
);
|
|
50
|
+
await pool.approve(
|
|
51
|
+
Dapp.TOROS,
|
|
52
|
+
TOROS,
|
|
53
|
+
MAX_AMOUNT,
|
|
54
|
+
await getTxOptions(network)
|
|
55
|
+
);
|
|
56
|
+
const tx = await pool.trade(
|
|
57
|
+
Dapp.TOROS,
|
|
58
|
+
TOROS,
|
|
59
|
+
USDC,
|
|
60
|
+
torosBalanceBefore,
|
|
61
|
+
1.5,
|
|
62
|
+
await getTxOptions(network)
|
|
63
|
+
);
|
|
64
|
+
await tx.wait(4);
|
|
65
|
+
const torosBalanceAfter = await pool.utils.getBalance(
|
|
66
|
+
TOROS,
|
|
67
|
+
pool.address
|
|
68
|
+
);
|
|
69
|
+
expect(torosBalanceAfter.lt(torosBalanceBefore)).toBe(true);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("complete withdrawal from Toros asset", async () => {
|
|
73
|
+
if (!process.env.PRIVATE_KEY || !process.env.ARBITRUM_URL) return;
|
|
74
|
+
const usdcBalanceBefore = await pool.utils.getBalance(USDC, pool.address);
|
|
75
|
+
const tx = await pool.completeTorosWithdrawal(
|
|
76
|
+
USDC,
|
|
77
|
+
1.5,
|
|
78
|
+
await getTxOptions(network)
|
|
79
|
+
);
|
|
80
|
+
await tx.wait(4);
|
|
81
|
+
const usdcBalanceAfter = await pool.utils.getBalance(USDC, pool.address);
|
|
82
|
+
expect(usdcBalanceAfter.gt(usdcBalanceBefore)).toBe(true);
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
// Requires live chain — cannot run on fork
|
|
88
|
+
testingHelper({
|
|
89
|
+
network: Network.ARBITRUM,
|
|
90
|
+
testingRun: testTorosOnchain,
|
|
91
|
+
onFork: false
|
|
92
|
+
});
|
package/src/test/toros.test.ts
CHANGED
|
@@ -1,23 +1,25 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
2
2
|
|
|
3
3
|
import BigNumber from "bignumber.js";
|
|
4
|
-
import { Dhedge, Pool } from "..";
|
|
5
|
-
import { routerAddress } from "../config";
|
|
4
|
+
import { Dhedge, ethers, Pool } from "..";
|
|
6
5
|
|
|
7
6
|
import { Dapp, Network } from "../types";
|
|
8
|
-
import { CONTRACT_ADDRESS, MAX_AMOUNT
|
|
7
|
+
import { CONTRACT_ADDRESS, MAX_AMOUNT } from "./constants";
|
|
9
8
|
import {
|
|
9
|
+
fixOracleAggregatorStaleness,
|
|
10
|
+
runWithImpersonateAccount,
|
|
10
11
|
setChainlinkTimeout,
|
|
11
12
|
setUSDCAmount,
|
|
12
13
|
testingHelper,
|
|
13
14
|
TestingRunParams
|
|
14
15
|
} from "./utils/testingHelper";
|
|
15
|
-
|
|
16
|
-
import {
|
|
16
|
+
import { balanceDelta } from "./utils/token";
|
|
17
|
+
import { routerAddress } from "../config";
|
|
17
18
|
|
|
18
19
|
const testToros = ({ wallet, network, provider }: TestingRunParams) => {
|
|
19
20
|
const USDC = CONTRACT_ADDRESS[network].USDC;
|
|
20
21
|
const TOROS = CONTRACT_ADDRESS[network].TOROS;
|
|
22
|
+
const TEST_POOL_ADDRESS = "0x2d4cddd2c4fa854536593bcf61d0da3b63ed80cb";
|
|
21
23
|
|
|
22
24
|
let dhedge: Dhedge;
|
|
23
25
|
let pool: Pool;
|
|
@@ -25,17 +27,37 @@ const testToros = ({ wallet, network, provider }: TestingRunParams) => {
|
|
|
25
27
|
|
|
26
28
|
describe(`pool on ${network}`, () => {
|
|
27
29
|
beforeAll(async () => {
|
|
28
|
-
dhedge = new Dhedge(wallet, network);
|
|
29
|
-
pool = await dhedge.loadPool(TEST_POOL[network]);
|
|
30
|
-
await setChainlinkTimeout({ pool, provider }, 86400 * 365);
|
|
31
|
-
// top up gas
|
|
32
30
|
await provider.send("hardhat_setBalance", [
|
|
33
31
|
wallet.address,
|
|
34
32
|
"0x10000000000000000"
|
|
35
33
|
]);
|
|
36
|
-
|
|
34
|
+
dhedge = new Dhedge(wallet, network);
|
|
35
|
+
pool = await dhedge.loadPool(TEST_POOL_ADDRESS);
|
|
36
|
+
|
|
37
|
+
await setChainlinkTimeout({ pool, provider }, 86400 * 365);
|
|
38
|
+
await fixOracleAggregatorStaleness({ pool, provider });
|
|
39
|
+
|
|
40
|
+
// Impersonate pool manager to set trader and add Toros token as asset
|
|
41
|
+
await runWithImpersonateAccount(
|
|
42
|
+
{ provider, account: await pool.managerLogic.manager() },
|
|
43
|
+
async ({ signer }) => {
|
|
44
|
+
await pool.managerLogic.connect(signer).setTrader(wallet.address);
|
|
45
|
+
const newAssets = [
|
|
46
|
+
[USDC, true],
|
|
47
|
+
[TOROS, false]
|
|
48
|
+
];
|
|
49
|
+
await pool.managerLogic.connect(signer).changeAssets(newAssets, []);
|
|
50
|
+
}
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
// Fix the Toros vault's oracle aggregators (has different assets with Pyth oracles)
|
|
54
|
+
if (TOROS) {
|
|
55
|
+
const torosPool = await dhedge.loadPool(TOROS);
|
|
56
|
+
await fixOracleAggregatorStaleness({ pool: torosPool, provider });
|
|
57
|
+
}
|
|
58
|
+
|
|
37
59
|
// top up USDC
|
|
38
|
-
const amount = new BigNumber(
|
|
60
|
+
const amount = new BigNumber(1000).times(1e6).toFixed(0);
|
|
39
61
|
await setUSDCAmount({
|
|
40
62
|
amount,
|
|
41
63
|
userAddress: pool.address,
|
|
@@ -46,13 +68,12 @@ const testToros = ({ wallet, network, provider }: TestingRunParams) => {
|
|
|
46
68
|
|
|
47
69
|
it("approves unlimited USDC on Toros", async () => {
|
|
48
70
|
await pool.approve(Dapp.TOROS, USDC, MAX_AMOUNT);
|
|
49
|
-
const
|
|
50
|
-
pool.address,
|
|
71
|
+
const usdcAllowance = await new ethers.Contract(
|
|
51
72
|
USDC,
|
|
52
|
-
|
|
73
|
+
["function allowance(address,address) view returns (uint256)"],
|
|
53
74
|
pool.signer
|
|
54
|
-
);
|
|
55
|
-
|
|
75
|
+
).allowance(pool.address, routerAddress[network].toros!);
|
|
76
|
+
expect(usdcAllowance.gt(0)).toBe(true);
|
|
56
77
|
});
|
|
57
78
|
|
|
58
79
|
it("trades USDC balance into Toros Token", async () => {
|
|
@@ -63,7 +84,37 @@ const testToros = ({ wallet, network, provider }: TestingRunParams) => {
|
|
|
63
84
|
TOROS,
|
|
64
85
|
pool.signer
|
|
65
86
|
);
|
|
66
|
-
expect(torosBalanceDelta.gt(0));
|
|
87
|
+
expect(torosBalanceDelta.gt(0)).toBe(true);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("get Tx data for init and complete withdrawal", async () => {
|
|
91
|
+
// await provider.send("evm_increaseTime", [86400]);
|
|
92
|
+
// await provider.send("evm_mine", []);
|
|
93
|
+
const torosBalance = await pool.utils.getBalance(TOROS, pool.address);
|
|
94
|
+
await pool.approve(Dapp.TOROS, TOROS, MAX_AMOUNT);
|
|
95
|
+
const tradeResult = await pool.trade(
|
|
96
|
+
Dapp.TOROS,
|
|
97
|
+
TOROS,
|
|
98
|
+
USDC,
|
|
99
|
+
torosBalance,
|
|
100
|
+
1.5,
|
|
101
|
+
null,
|
|
102
|
+
{
|
|
103
|
+
estimateGas: false,
|
|
104
|
+
onlyGetTxData: true
|
|
105
|
+
}
|
|
106
|
+
);
|
|
107
|
+
expect(tradeResult.minAmountOut).toBeDefined();
|
|
108
|
+
const completeWithdrawResult = await pool.completeTorosWithdrawal(
|
|
109
|
+
USDC,
|
|
110
|
+
5,
|
|
111
|
+
null,
|
|
112
|
+
{
|
|
113
|
+
estimateGas: false,
|
|
114
|
+
onlyGetTxData: true
|
|
115
|
+
}
|
|
116
|
+
);
|
|
117
|
+
expect(completeWithdrawResult.txData).toBeDefined();
|
|
67
118
|
});
|
|
68
119
|
|
|
69
120
|
it("init Toros Token for withdrawal", async () => {
|
|
@@ -77,23 +128,26 @@ const testToros = ({ wallet, network, provider }: TestingRunParams) => {
|
|
|
77
128
|
TOROS,
|
|
78
129
|
pool.signer
|
|
79
130
|
);
|
|
80
|
-
expect(torosBalanceDelta.lt(0));
|
|
131
|
+
expect(torosBalanceDelta.lt(0)).toBe(true);
|
|
81
132
|
});
|
|
82
133
|
|
|
83
134
|
it("complete withdrawal from Toros asset", async () => {
|
|
135
|
+
// Advance chain time past the Toros withdrawal cooldown
|
|
136
|
+
await provider.send("evm_increaseTime", [600]);
|
|
137
|
+
await provider.send("evm_mine", []);
|
|
84
138
|
await pool.completeTorosWithdrawal(USDC, 1.5);
|
|
85
139
|
const usdcBalanceDelta = await balanceDelta(
|
|
86
140
|
pool.address,
|
|
87
141
|
USDC,
|
|
88
142
|
pool.signer
|
|
89
143
|
);
|
|
90
|
-
expect(usdcBalanceDelta.gt(0));
|
|
144
|
+
expect(usdcBalanceDelta.gt(0)).toBe(true);
|
|
91
145
|
});
|
|
92
146
|
});
|
|
93
147
|
};
|
|
94
148
|
|
|
95
149
|
testingHelper({
|
|
96
|
-
network: Network.
|
|
150
|
+
network: Network.ARBITRUM,
|
|
97
151
|
testingRun: testToros,
|
|
98
152
|
onFork: true
|
|
99
153
|
});
|