Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions contracts/LiquidityBridgeContractV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,18 @@
/**
@dev Used to resign as a liquidity provider
*/
function resign() external onlyRegistered {
require(resignationBlockNum[msg.sender] == 0, "LBC023");
function resign() external {
require(
resignationBlockNum[msg.sender] == 0,
"LBC023"
);
require(
(collateral[msg.sender] > 0 || pegoutCollateral[msg.sender] > 0),
"LBC001"
);
resignationBlockNum[msg.sender] = block.number;
emit Resigned(msg.sender);
}

Check warning

Code scanning / Slither

Dangerous strict equalities Medium


/**
@dev Returns the amount of collateral of a liquidity provider
Expand Down
102 changes: 64 additions & 38 deletions test/resignation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,25 @@ import {
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", () => {
it("fail when liquidityProdvider try to withdraw collateral without resign postion as liquidity provider before", async () => {
const fixtureResult = await loadFixture(deployLbcWithProvidersFixture);
const provider = fixtureResult.liquidityProviders[1];
let lbc = fixtureResult.lbc;
lbc = lbc.connect(provider.signer);

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;
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("fail when LP resigns two times", async () => {
const fixtureResult = await loadFixture(deployLbcWithProvidersFixture);
const provider = fixtureResult.liquidityProviders[1];
let lbc = fixtureResult.lbc;
const resignBlocks = await lbc.getResignDelayBlocks();
lbc = lbc.connect(provider.signer);
await expect(lbc.resign()).not.to.be.reverted;
await expect(lbc.resign()).to.be.revertedWith("LBC001");
await hardhatHelpers.mine(resignBlocks);
await expect(lbc.withdrawCollateral()).not.to.be.reverted;
});

it("should resign", async () => {
const fixtureResult = await loadFixture(deployLbcWithProvidersFixture);
const provider = fixtureResult.liquidityProviders[0];
let lbc = fixtureResult.lbc;
lbc = lbc.connect(provider.signer);
it("resign", async () => {
const lpBalance = ethers.parseEther("0.5");
await lbc.deposit({ value: lpBalance });
const resignBlocks = await lbc.getResignDelayBlocks();
Expand All @@ -63,22 +50,22 @@ describe("LiquidityBridgeContractV2 resignation process should", () => {

const lpBalanceAfterWithdrawAssertion = await createBalanceUpdateAssertion({
source: ethers.provider,
address: provider.signer.address,
address: liquidityProvider.signer.address,
message: "Incorrect LP balance after withdraw",
});

const lpProtocolBalanceAfterWithdrawAssertion =
await createBalanceDifferenceAssertion({
source: lbc,
address: provider.signer.address,
address: liquidityProvider.signer.address,
expectedDiff: lpBalance * -1n,
message: "Incorrect LP balance in LBC after withdraw",
});

const peginCollateralAfterWithdrawAssertion =
await createCollateralUpdateAssertion({
lbc: lbc,
address: provider.signer.address,
address: liquidityProvider.signer.address,
expectedDiff: (LP_COLLATERAL / 2n) * -1n,
message: "Incorrect pegin collateral after withdraw",
type: "pegin",
Expand All @@ -87,7 +74,7 @@ describe("LiquidityBridgeContractV2 resignation process should", () => {
const pegoutCollateralAfterWithdrawAssertion =
await createCollateralUpdateAssertion({
lbc: lbc,
address: provider.signer.address,
address: liquidityProvider.signer.address,
expectedDiff: (LP_COLLATERAL / 2n) * -1n,
message: "Incorrect pegout collateral after withdraw",
type: "pegout",
Expand All @@ -103,10 +90,10 @@ describe("LiquidityBridgeContractV2 resignation process should", () => {

await expect(resignTx)
.to.emit(lbc, "Resigned")
.withArgs(provider.signer.address);
.withArgs(liquidityProvider.signer.address);
await expect(withdrawTx)
.to.emit(lbc, "Withdrawal")
.withArgs(provider.signer.address, lpBalance);
.withArgs(liquidityProvider.signer.address, lpBalance);

await lbcBalanceAfterWithdrawAssertion();
await lpBalanceAfterWithdrawAssertion(
Expand All @@ -117,7 +104,7 @@ describe("LiquidityBridgeContractV2 resignation process should", () => {
const lpBalanceAfterCollateralWithdrawAssertion =
await createBalanceUpdateAssertion({
source: ethers.provider,
address: provider.signer.address,
address: liquidityProvider.signer.address,
message: "Incorrect LP balance after withdraw",
});

Expand All @@ -136,18 +123,57 @@ describe("LiquidityBridgeContractV2 resignation process should", () => {

await expect(withdrawCollateralTx)
.to.emit(lbc, "WithdrawCollateral")
.withArgs(provider.signer.address, LP_COLLATERAL);
.withArgs(liquidityProvider.signer.address, LP_COLLATERAL);
await lpBalanceAfterCollateralWithdrawAssertion(
LP_COLLATERAL - withdrawCollateralReceipt!.fee
);
await peginCollateralAfterWithdrawAssertion();
await pegoutCollateralAfterWithdrawAssertion();
await lbcBalanceAfterCollateralWithdrawAssertion();
await expect(
lbc.getCollateral(provider.signer.address)
lbc.getCollateral(liquidityProvider.signer.address)
).to.eventually.be.eq(0);
await expect(
lbc.getPegoutCollateral(provider.signer.address)
lbc.getPegoutCollateral(liquidityProvider.signer.address)
).to.eventually.be.eq(0);
});

it("resign as pegout only, pegin only and both", async () => {
Comment thread
Luisfc68 marked this conversation as resolved.
Outdated
// 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");
});
});