Skip to content

Commit

Permalink
add leave party tests
Browse files Browse the repository at this point in the history
  • Loading branch information
YouStillAlive committed Jul 18, 2024
1 parent 0c20d0f commit b24c864
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
9 changes: 7 additions & 2 deletions contracts/BNBPartyFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ pragma solidity ^0.8.0;
import "./token/ERC20Token.sol";
import "./BNBPartyInternal.sol";
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@bnb-party/v3-periphery/contracts/interfaces/IPeripheryPayments.sol";

contract BNBPartyFactory is BNBPartyInternal, ReentrancyGuard {
using SafeERC20 for IERC20;

constructor(
Party memory _party,
IWBNB _WBNB
Expand Down Expand Up @@ -63,13 +66,15 @@ contract BNBPartyFactory is BNBPartyInternal, ReentrancyGuard {
}

function leaveParty(
address tokenIn,
IERC20 tokenIn,
uint256 amountIn,
uint256 amountOutMinimum,
uint256 deadline
) external {
tokenIn.safeTransferFrom(msg.sender, address(this), amountIn);
tokenIn.safeIncreaseAllowance(address(swapRouter), amountIn);
_executeSwap(
tokenIn,
address(tokenIn),
address(WBNB),
address(swapRouter),
amountOutMinimum,
Expand Down
2 changes: 1 addition & 1 deletion contracts/BNBPartyInternal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,6 @@ abstract contract BNBPartyInternal is BNBPartyState {
amountOutMinimum: amountOutMinimum
});

swapRouter.exactInput{value: amountIn}(params);
swapRouter.exactInput{value: amountIn }(params);
}
}
39 changes: 30 additions & 9 deletions test/BNBPartyFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,24 +154,22 @@ describe("BNBPartyFactory", function () {
let deadline: number
let position: any
let MEME: string
let lpAddress: string

before(async () => {
tokenId = (await positionManager.totalSupply()).toString()
deadline = Math.floor(Date.now() / 1000) + 60 * 20 // 20 minutes from now
position = await positionManager.positions(tokenId)
MEME = position.token1
lpAddress = await v3Factory.getPool(await weth9.getAddress(), MEME, FeeAmount.HIGH)
})

it("should increase wbnb on party lp after join party", async () => {
const amountIn = ethers.parseUnits("5", 17)

const lpBalanceBefore = await weth9.balanceOf(
await v3Factory.getPool(await weth9.getAddress(), MEME, FeeAmount.HIGH)
)
const lpBalanceBefore = await weth9.balanceOf(lpAddress)
await bnbPartyFactory.joinParty(MEME, 0, deadline, { value: amountIn })
const lpBalanceAfter = await weth9.balanceOf(
await v3Factory.getPool(await weth9.getAddress(), MEME, FeeAmount.HIGH)
)
const lpBalanceAfter = await weth9.balanceOf(lpAddress)

expect(lpBalanceAfter).to.be.equal(lpBalanceBefore + amountIn)
})
Expand All @@ -187,6 +185,30 @@ describe("BNBPartyFactory", function () {
expect(balanceAfter).to.be.gt(balanceBefore)
})

it("user should receive bnb after leave party", async () => {
const amountIn = ethers.parseUnits("1", 16)
const tokenOutContract = await ethers.getContractAt("ERC20", MEME)
// approve token
await tokenOutContract.approve(await bnbPartyFactory.getAddress(), amountIn)
const bnbBalanceBefore = await ethers.provider.getBalance(await signers[0].getAddress())
await bnbPartyFactory.leaveParty(MEME, amountIn, 0, deadline)
const bnbBalanceAfter = await ethers.provider.getBalance(await signers[0].getAddress())
expect(bnbBalanceAfter).to.be.gt(bnbBalanceBefore)
})

it("should deacrease wbnb on party lp after leave party", async () => {
const amountIn = ethers.parseUnits("1", 16)
const tokenOutContract = await ethers.getContractAt("ERC20", MEME)
// approve token
await tokenOutContract.approve(await bnbPartyFactory.getAddress(), amountIn)

const lpBalanceBefore = await weth9.balanceOf(lpAddress)
await bnbPartyFactory.leaveParty(MEME, amountIn, 0, deadline)
const lpBalanceAfter = await weth9.balanceOf(lpAddress)

expect(lpBalanceBefore).to.be.gt(lpBalanceAfter)
})

it("BNB -> WBNB -> MEME exactInput call", async () => {
const amountIn = ethers.parseUnits("1", 18)
const path = getDataHexString(await weth9.getAddress(), MEME)
Expand Down Expand Up @@ -269,10 +291,9 @@ describe("BNBPartyFactory", function () {
bnbPartyFactory.filters["StartParty(address,address,address)"]
)
const tokenAddress = events[events.length - 1].args.tokenAddress
const lpAddress = await v3Factory.getPool(await weth9.getAddress(), tokenAddress, FeeAmount.HIGH)
// check liquidity pool balance
const liquidityPoolBalance = await weth9.balanceOf(
await v3Factory.getPool(await weth9.getAddress(), tokenAddress, FeeAmount.HIGH)
)
const liquidityPoolBalance = await weth9.balanceOf(lpAddress)
expect(liquidityPoolBalance).to.be.equal(amountIn - tokenCreationFee)
})

Expand Down

0 comments on commit b24c864

Please sign in to comment.