|
| 1 | +import { ethers } from 'hardhat' |
| 2 | +import { expect } from 'chai' |
| 3 | +import { Signer, Contract, BigNumber } from 'ethers' |
| 4 | + |
| 5 | +describe('CharmGeyserRouter', function () { |
| 6 | + let deployer: Signer |
| 7 | + let user: Signer |
| 8 | + let vaultOwner: Signer |
| 9 | + let token0: Contract |
| 10 | + let token1: Contract |
| 11 | + let charmLiqToken: Contract |
| 12 | + let geyser: Contract |
| 13 | + let vaultFactory: Contract |
| 14 | + let router: Contract |
| 15 | + |
| 16 | + beforeEach(async () => { |
| 17 | + ;[deployer, user, vaultOwner] = await ethers.getSigners() |
| 18 | + |
| 19 | + // Deploy mock tokens |
| 20 | + const MockERC20 = await ethers.getContractFactory('MockERC20') |
| 21 | + token0 = await MockERC20.connect(deployer).deploy(await user.getAddress(), ethers.utils.parseEther('1000000')) |
| 22 | + token1 = await MockERC20.connect(deployer).deploy(await user.getAddress(), ethers.utils.parseEther('1000000')) |
| 23 | + await token0.deployed() |
| 24 | + await token1.deployed() |
| 25 | + |
| 26 | + // Deploy mock Charm LP Token |
| 27 | + const MockCharmLiqToken = await ethers.getContractFactory('MockCharmLiqToken') |
| 28 | + charmLiqToken = await MockCharmLiqToken.deploy(token0.address, token1.address) |
| 29 | + await charmLiqToken.deployed() |
| 30 | + |
| 31 | + // Deploy mock Geyser |
| 32 | + const MockGeyser = await ethers.getContractFactory('MockGeyser') |
| 33 | + geyser = await MockGeyser.deploy(charmLiqToken.address) |
| 34 | + await geyser.deployed() |
| 35 | + |
| 36 | + // Deploy mock Vault Factory |
| 37 | + const MockVaultFactory = await ethers.getContractFactory('MockVaultFactory') |
| 38 | + vaultFactory = await MockVaultFactory.deploy() |
| 39 | + await vaultFactory.deployed() |
| 40 | + |
| 41 | + // Deploy CharmGeyserRouter |
| 42 | + const CharmGeyserRouter = await ethers.getContractFactory('CharmGeyserRouter') |
| 43 | + router = await CharmGeyserRouter.deploy() |
| 44 | + await router.deployed() |
| 45 | + }) |
| 46 | + |
| 47 | + describe('createLiqAndStake', function () { |
| 48 | + it('should create liquidity and stake correctly', async () => { |
| 49 | + const token0Amount = ethers.utils.parseEther('100') |
| 50 | + const token1Amount = ethers.utils.parseEther('200') |
| 51 | + |
| 52 | + await token0.connect(user).approve(router.address, token0Amount) |
| 53 | + await token1.connect(user).approve(router.address, token1Amount) |
| 54 | + const userInitialBal0: BigNumber = await token0.balanceOf(await user.getAddress()) |
| 55 | + const userInitialBal1: BigNumber = await token1.balanceOf(await user.getAddress()) |
| 56 | + |
| 57 | + // dummy vault address |
| 58 | + const vault = ethers.Wallet.createRandom().address |
| 59 | + |
| 60 | + const liqPayload = { |
| 61 | + token0Amt: token0Amount, |
| 62 | + token1Amt: token1Amount, |
| 63 | + token0MinAmt: ethers.utils.parseEther('90'), |
| 64 | + token1MinAmt: ethers.utils.parseEther('180'), |
| 65 | + } |
| 66 | + |
| 67 | + await expect(router.connect(user).createLiqAndStake(geyser.address, vault, '0x', liqPayload)) |
| 68 | + .to.emit(geyser, 'LogStaked') |
| 69 | + .withArgs(vault, token0Amount.add(token1Amount), '0x') |
| 70 | + |
| 71 | + // After staking, router should have transferred all tokens out |
| 72 | + expect(await token0.balanceOf(router.address)).to.equal(0) |
| 73 | + expect(await token1.balanceOf(router.address)).to.equal(0) |
| 74 | + |
| 75 | + const userFinalBal0 = await token0.balanceOf(await user.getAddress()) |
| 76 | + const userFinalBal1 = await token1.balanceOf(await user.getAddress()) |
| 77 | + expect(userInitialBal0.sub(userFinalBal0)).to.equal(token0Amount) |
| 78 | + expect(userInitialBal1.sub(userFinalBal1)).to.equal(token1Amount) |
| 79 | + }) |
| 80 | + }) |
| 81 | + |
| 82 | + describe('create2VaultCreateLiqAndStake', function () { |
| 83 | + it('should create a vault, transfer ownership, create liquidity and stake', async () => { |
| 84 | + const token0Amount = ethers.utils.parseEther('50') |
| 85 | + const token1Amount = ethers.utils.parseEther('100') |
| 86 | + |
| 87 | + await token0.connect(user).approve(router.address, token0Amount) |
| 88 | + await token1.connect(user).approve(router.address, token1Amount) |
| 89 | + |
| 90 | + const liqPayload = { |
| 91 | + token0Amt: token0Amount, |
| 92 | + token1Amt: token1Amount, |
| 93 | + token0MinAmt: ethers.utils.parseEther('40'), |
| 94 | + token1MinAmt: ethers.utils.parseEther('80'), |
| 95 | + } |
| 96 | + |
| 97 | + const salt = ethers.utils.formatBytes32String('testSalt') |
| 98 | + const args = [geyser.address, vaultFactory.address, await vaultOwner.getAddress(), salt, '0x', liqPayload] |
| 99 | + const vault = await router.connect(user).callStatic.create2VaultCreateLiqAndStake(...args) |
| 100 | + await expect(router.connect(user).create2VaultCreateLiqAndStake(...args)) |
| 101 | + .to.emit(geyser, 'LogStaked') |
| 102 | + .withArgs(vault, token0Amount.add(token1Amount), '0x') |
| 103 | + expect(await vaultFactory.ownerOf(1)).to.equal(await vaultOwner.getAddress()) |
| 104 | + }) |
| 105 | + }) |
| 106 | + |
| 107 | + describe('Approvals', function () { |
| 108 | + it('should approve max if current allowance is insufficient', async () => { |
| 109 | + const token0Amount = ethers.utils.parseEther('10') |
| 110 | + const token1Amount = ethers.utils.parseEther('20') |
| 111 | + await token0.connect(user).approve(router.address, token0Amount) |
| 112 | + await token1.connect(user).approve(router.address, token1Amount) |
| 113 | + |
| 114 | + const vault = ethers.Wallet.createRandom().address |
| 115 | + |
| 116 | + const liqPayload = { |
| 117 | + token0Amt: token0Amount, |
| 118 | + token1Amt: token1Amount, |
| 119 | + token0MinAmt: ethers.utils.parseEther('9'), |
| 120 | + token1MinAmt: ethers.utils.parseEther('18'), |
| 121 | + } |
| 122 | + |
| 123 | + expect(await token0.allowance(router.address, charmLiqToken.address)).to.equal(0) |
| 124 | + expect(await token1.allowance(router.address, charmLiqToken.address)).to.equal(0) |
| 125 | + |
| 126 | + await router.connect(user).createLiqAndStake(geyser.address, vault, '0x', liqPayload) |
| 127 | + |
| 128 | + const maxUint = ethers.constants.MaxUint256 |
| 129 | + expect(await token0.allowance(router.address, charmLiqToken.address)).to.equal(maxUint.sub(liqPayload.token0Amt)) |
| 130 | + expect(await token1.allowance(router.address, charmLiqToken.address)).to.equal(maxUint.sub(liqPayload.token1Amt)) |
| 131 | + }) |
| 132 | + }) |
| 133 | + |
| 134 | + describe('Edge cases', function () { |
| 135 | + it('should handle zero amounts gracefully', async () => { |
| 136 | + await token0.connect(user).approve(router.address, 0) |
| 137 | + await token1.connect(user).approve(router.address, 0) |
| 138 | + |
| 139 | + const vault = ethers.Wallet.createRandom().address |
| 140 | + const liqPayload = { |
| 141 | + token0Amt: 0, |
| 142 | + token1Amt: 0, |
| 143 | + token0MinAmt: 0, |
| 144 | + token1MinAmt: 0, |
| 145 | + } |
| 146 | + |
| 147 | + await expect(router.connect(user).createLiqAndStake(geyser.address, vault, '0x', liqPayload)) |
| 148 | + .to.emit(geyser, 'LogStaked') |
| 149 | + .withArgs(vault, 0, '0x') |
| 150 | + }) |
| 151 | + }) |
| 152 | +}) |
0 commit comments