-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathresignation.test.ts
More file actions
179 lines (154 loc) · 6.52 KB
/
resignation.test.ts
File metadata and controls
179 lines (154 loc) · 6.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { ethers } from "hardhat";
import { LP_COLLATERAL } from "./utils/constants";
import { loadFixture } from "@nomicfoundation/hardhat-toolbox/network-helpers";
import { expect } from "chai";
import { deployLbcWithProvidersFixture } from "./utils/fixtures";
import {
createBalanceDifferenceAssertion,
createBalanceUpdateAssertion,
createCollateralUpdateAssertion,
} from "./utils/asserts";
import * as hardhatHelpers from "@nomicfoundation/hardhat-network-helpers";
import { LiquidityBridgeContractV2 } from "../typechain-types";
import { HardhatEthersSigner } from "@nomicfoundation/hardhat-ethers/signers";
describe("LiquidityBridgeContractV2 resignation process should", () => {
let lbc: LiquidityBridgeContractV2;
let liquidityProvider: {
signer: HardhatEthersSigner;
registerParams: Parameters<LiquidityBridgeContractV2["register"]>;
};
let fixtureResult: Awaited<ReturnType<typeof deployLbcWithProvidersFixture>>;
beforeEach(async () => {
fixtureResult = await loadFixture(deployLbcWithProvidersFixture);
liquidityProvider = fixtureResult.liquidityProviders[0];
lbc = fixtureResult.lbc;
lbc = lbc.connect(liquidityProvider.signer);
});
it("resign", async () => {
const lpBalance = ethers.parseEther("0.5");
await lbc.deposit({ value: lpBalance });
const resignBlocks = await lbc.getResignDelayBlocks();
const lbcBalanceAfterResignAssertion =
await createBalanceDifferenceAssertion({
source: ethers.provider,
address: await lbc.getAddress(),
expectedDiff: 0,
message: "Incorrect LBC balance after resign",
});
const lbcBalanceAfterWithdrawAssertion =
await createBalanceDifferenceAssertion({
source: ethers.provider,
address: await lbc.getAddress(),
expectedDiff: lpBalance * -1n,
message: "Incorrect LBC balance after withdraw",
});
const lpBalanceAfterWithdrawAssertion = await createBalanceUpdateAssertion({
source: ethers.provider,
address: liquidityProvider.signer.address,
message: "Incorrect LP balance after withdraw",
});
const lpProtocolBalanceAfterWithdrawAssertion =
await createBalanceDifferenceAssertion({
source: lbc,
address: liquidityProvider.signer.address,
expectedDiff: lpBalance * -1n,
message: "Incorrect LP balance in LBC after withdraw",
});
const peginCollateralAfterWithdrawAssertion =
await createCollateralUpdateAssertion({
lbc: lbc,
address: liquidityProvider.signer.address,
expectedDiff: (LP_COLLATERAL / 2n) * -1n,
message: "Incorrect pegin collateral after withdraw",
type: "pegin",
});
const pegoutCollateralAfterWithdrawAssertion =
await createCollateralUpdateAssertion({
lbc: lbc,
address: liquidityProvider.signer.address,
expectedDiff: (LP_COLLATERAL / 2n) * -1n,
message: "Incorrect pegout collateral after withdraw",
type: "pegout",
});
const resignTx = await lbc.resign();
const resignReceipt = await resignTx.wait();
await lbcBalanceAfterResignAssertion();
const withdrawTx = await lbc.withdraw(lpBalance);
const withdrawReceipt = await withdrawTx.wait();
await expect(resignTx)
.to.emit(lbc, "Resigned")
.withArgs(liquidityProvider.signer.address);
await expect(withdrawTx)
.to.emit(lbc, "Withdrawal")
.withArgs(liquidityProvider.signer.address, lpBalance);
await lbcBalanceAfterWithdrawAssertion();
await lpBalanceAfterWithdrawAssertion(
lpBalance - withdrawReceipt!.fee - resignReceipt!.fee
);
await lpProtocolBalanceAfterWithdrawAssertion();
const lpBalanceAfterCollateralWithdrawAssertion =
await createBalanceUpdateAssertion({
source: ethers.provider,
address: liquidityProvider.signer.address,
message: "Incorrect LP balance after withdraw",
});
const lbcBalanceAfterCollateralWithdrawAssertion =
await createBalanceDifferenceAssertion({
source: ethers.provider,
address: await lbc.getAddress(),
expectedDiff: LP_COLLATERAL * -1n,
message: "Incorrect LBC balance after collateral withdraw",
});
await hardhatHelpers.mine(resignBlocks);
const withdrawCollateralTx = await lbc.withdrawCollateral();
const withdrawCollateralReceipt = await withdrawCollateralTx.wait();
await expect(withdrawCollateralTx)
.to.emit(lbc, "WithdrawCollateral")
.withArgs(liquidityProvider.signer.address, LP_COLLATERAL);
await lpBalanceAfterCollateralWithdrawAssertion(
LP_COLLATERAL - withdrawCollateralReceipt!.fee
);
await peginCollateralAfterWithdrawAssertion();
await pegoutCollateralAfterWithdrawAssertion();
await lbcBalanceAfterCollateralWithdrawAssertion();
await expect(
lbc.getCollateral(liquidityProvider.signer.address)
).to.eventually.be.eq(0);
await expect(
lbc.getPegoutCollateral(liquidityProvider.signer.address)
).to.eventually.be.eq(0);
});
it("resign as pegout only, pegin only and both", async () => {
// Both LP
const peginAndPegoutLp = fixtureResult.liquidityProviders[0];
lbc = lbc.connect(peginAndPegoutLp.signer);
await expect(lbc.resign()).not.to.be.reverted;
// Pegin only LP
const peginOnlyLp = fixtureResult.liquidityProviders[1];
lbc = lbc.connect(peginOnlyLp.signer);
await expect(lbc.resign()).not.to.be.reverted;
// Pegout only LP
const pegoutOnlyLp = fixtureResult.liquidityProviders[2];
lbc = lbc.connect(pegoutOnlyLp.signer);
await expect(lbc.resign()).not.to.be.reverted;
});
it("fail when liquidityProdvider try to withdraw collateral without resign postion as liquidity provider before", async () => {
await expect(lbc.withdrawCollateral()).to.be.revertedWith("LBC021");
const resignBlocks = await lbc.getResignDelayBlocks();
await lbc.resign();
await hardhatHelpers.mine(resignBlocks);
await expect(lbc.withdrawCollateral()).not.to.be.reverted;
});
it("fail when LP resigns two times", async () => {
const resignBlocks = await lbc.getResignDelayBlocks();
await expect(lbc.resign()).not.to.be.reverted;
await expect(lbc.resign()).to.be.revertedWith("LBC023");
await hardhatHelpers.mine(resignBlocks);
await expect(lbc.withdrawCollateral()).not.to.be.reverted;
});
it("fail when LP is not registered", async () => {
const signers = await ethers.getSigners();
const lp = lbc.connect(signers[3]); // not registered LP
await expect(lp.resign()).to.be.revertedWith("LBC001");
});
});