|
| 1 | +import { expect, use } from "chai"; |
| 2 | +import { network, ethers, upgrades } from "hardhat"; |
| 3 | +import { Contract, Transaction, Signer } from "ethers"; |
| 4 | +import { |
| 5 | + setupCollateralToken, |
| 6 | + mintCollteralToken, |
| 7 | + toFixedPtAmt, |
| 8 | + setupBondFactory, |
| 9 | + depositIntoBond, |
| 10 | + getTranches, |
| 11 | + toDiscountFixedPtAmt, |
| 12 | + toPriceFixedPtAmt, |
| 13 | + getDepositBond, |
| 14 | + advancePerpQueueToBondMaturity, |
| 15 | +} from "../helpers"; |
| 16 | +import { smock, FakeContract } from "@defi-wonderland/smock"; |
| 17 | + |
| 18 | +use(smock.matchers); |
| 19 | + |
| 20 | +let vault: Contract, perp: FakeContract, collateralToken: Contract, deployer: Signer, otherUser: Signer; |
| 21 | +describe("RolloverVault", function () { |
| 22 | + beforeEach(async function () { |
| 23 | + await network.provider.send("hardhat_reset"); |
| 24 | + |
| 25 | + const accounts = await ethers.getSigners(); |
| 26 | + deployer = accounts[0]; |
| 27 | + otherUser = accounts[1]; |
| 28 | + |
| 29 | + ({ collateralToken } = await setupCollateralToken("Bitcoin", "BTC")); |
| 30 | + await mintCollteralToken(collateralToken, toFixedPtAmt("1000"), deployer); |
| 31 | + |
| 32 | + const PerpetualTranche = await ethers.getContractFactory("PerpetualTranche"); |
| 33 | + perp = await smock.fake(PerpetualTranche); |
| 34 | + |
| 35 | + await perp.collateral.returns(collateralToken.address); |
| 36 | + await perp.feeToken.returns(perp.address); |
| 37 | + |
| 38 | + const RolloverVault = await ethers.getContractFactory("RolloverVault"); |
| 39 | + vault = await upgrades.deployProxy(RolloverVault.connect(deployer)); |
| 40 | + await collateralToken.approve(vault.address, toFixedPtAmt("1")); |
| 41 | + await vault.init("RolloverVault", "VSHARE", collateralToken.address, perp.address); |
| 42 | + }); |
| 43 | + |
| 44 | + afterEach(async function () { |
| 45 | + await network.provider.send("hardhat_reset"); |
| 46 | + }); |
| 47 | + |
| 48 | + describe("#init", function () { |
| 49 | + it("should set erc20 parameters", async function () { |
| 50 | + expect(await vault.name()).to.eq("RolloverVault"); |
| 51 | + expect(await vault.symbol()).to.eq("VSHARE"); |
| 52 | + expect(await vault.decimals()).to.eq(18); |
| 53 | + }); |
| 54 | + |
| 55 | + it("should set owner", async function () { |
| 56 | + expect(await vault.owner()).to.eq(await deployer.getAddress()); |
| 57 | + }); |
| 58 | + |
| 59 | + it("should set ext service references", async function () { |
| 60 | + expect(await vault.perp()).to.eq(perp.address); |
| 61 | + }); |
| 62 | + |
| 63 | + it("should set deposit asset reference", async function () { |
| 64 | + expect(await vault.underlying()).to.eq(collateralToken.address); |
| 65 | + }); |
| 66 | + |
| 67 | + it("should initialize lists", async function () { |
| 68 | + expect(await vault.deployedCount()).to.eq(0); |
| 69 | + expect(await vault.earnedCount()).to.eq(1); |
| 70 | + expect(await vault.earnedAt(0)).to.eq(perp.address); |
| 71 | + await expect(vault.earnedAt(1)).to.be.revertedWith("OutOfBounds"); |
| 72 | + expect(await vault.isVaultAsset(collateralToken.address)).to.eq(true); |
| 73 | + expect(await vault.isVaultAsset(perp.address)).to.eq(true); |
| 74 | + }); |
| 75 | + |
| 76 | + it("should NOT be paused", async function () { |
| 77 | + expect(await vault.paused()).to.eq(false); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + describe("#pause", function () { |
| 82 | + let tx: Transaction; |
| 83 | + beforeEach(async function () { |
| 84 | + await vault.connect(deployer).transferOwnership(await otherUser.getAddress()); |
| 85 | + }); |
| 86 | + |
| 87 | + describe("when triggered by non-owner", function () { |
| 88 | + it("should revert", async function () { |
| 89 | + await expect(vault.connect(deployer).pause()).to.be.revertedWith("Ownable: caller is not the owner"); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + describe("when already paused", function () { |
| 94 | + beforeEach(async function () { |
| 95 | + await vault.connect(otherUser).pause(); |
| 96 | + }); |
| 97 | + it("should revert", async function () { |
| 98 | + await expect(vault.connect(otherUser).pause()).to.be.revertedWith("Pausable: paused"); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + describe("when valid", function () { |
| 103 | + beforeEach(async function () { |
| 104 | + tx = await vault.connect(otherUser).pause(); |
| 105 | + await tx; |
| 106 | + }); |
| 107 | + it("should pause", async function () { |
| 108 | + expect(await vault.paused()).to.eq(true); |
| 109 | + }); |
| 110 | + it("should emit event", async function () { |
| 111 | + await expect(tx) |
| 112 | + .to.emit(vault, "Paused") |
| 113 | + .withArgs(await otherUser.getAddress()); |
| 114 | + }); |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + describe("#unpause", function () { |
| 119 | + let tx: Transaction; |
| 120 | + beforeEach(async function () { |
| 121 | + await vault.connect(deployer).transferOwnership(await otherUser.getAddress()); |
| 122 | + }); |
| 123 | + |
| 124 | + describe("when triggered by non-owner", function () { |
| 125 | + beforeEach(async function () { |
| 126 | + await vault.connect(otherUser).pause(); |
| 127 | + }); |
| 128 | + |
| 129 | + it("should revert", async function () { |
| 130 | + await expect(vault.connect(deployer).unpause()).to.be.revertedWith("Ownable: caller is not the owner"); |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + describe("when not paused", function () { |
| 135 | + it("should revert", async function () { |
| 136 | + await expect(vault.connect(otherUser).unpause()).to.be.revertedWith("Pausable: not paused"); |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + describe("when valid", function () { |
| 141 | + beforeEach(async function () { |
| 142 | + tx = await vault.connect(otherUser).pause(); |
| 143 | + await tx; |
| 144 | + tx = await vault.connect(otherUser).unpause(); |
| 145 | + await tx; |
| 146 | + }); |
| 147 | + it("should unpause", async function () { |
| 148 | + expect(await vault.paused()).to.eq(false); |
| 149 | + }); |
| 150 | + it("should emit event", async function () { |
| 151 | + await expect(tx) |
| 152 | + .to.emit(vault, "Unpaused") |
| 153 | + .withArgs(await otherUser.getAddress()); |
| 154 | + }); |
| 155 | + }); |
| 156 | + }); |
| 157 | + |
| 158 | + describe("#transferERC20", function () { |
| 159 | + let transferToken: Contract, toAddress: string; |
| 160 | + |
| 161 | + beforeEach(async function () { |
| 162 | + const Token = await ethers.getContractFactory("MockERC20"); |
| 163 | + transferToken = await Token.deploy(); |
| 164 | + await transferToken.init("Mock Token", "MOCK"); |
| 165 | + await transferToken.mint(vault.address, "100"); |
| 166 | + toAddress = await deployer.getAddress(); |
| 167 | + }); |
| 168 | + |
| 169 | + describe("when triggered by non-owner", function () { |
| 170 | + it("should revert", async function () { |
| 171 | + await expect( |
| 172 | + vault.connect(otherUser).transferERC20(transferToken.address, toAddress, "100"), |
| 173 | + ).to.be.revertedWith("Ownable: caller is not the owner"); |
| 174 | + }); |
| 175 | + }); |
| 176 | + |
| 177 | + describe("when non vault asset", function () { |
| 178 | + it("should transfer", async function () { |
| 179 | + await expect(() => vault.transferERC20(transferToken.address, toAddress, "100")).to.changeTokenBalance( |
| 180 | + transferToken, |
| 181 | + deployer, |
| 182 | + "100", |
| 183 | + ); |
| 184 | + }); |
| 185 | + }); |
| 186 | + |
| 187 | + describe("when underlying asset", function () { |
| 188 | + it("should revert", async function () { |
| 189 | + await expect(vault.transferERC20(await vault.underlying(), toAddress, toFixedPtAmt("100"))).to.be.revertedWith( |
| 190 | + "UnauthorizedTransferOut", |
| 191 | + ); |
| 192 | + }); |
| 193 | + }); |
| 194 | + |
| 195 | + describe("when earned asset", function () { |
| 196 | + it("should revert", async function () { |
| 197 | + await expect(vault.transferERC20(await vault.earnedAt(0), toAddress, toFixedPtAmt("100"))).to.be.revertedWith( |
| 198 | + "UnauthorizedTransferOut", |
| 199 | + ); |
| 200 | + }); |
| 201 | + }); |
| 202 | + |
| 203 | + describe("when deployed asset", function () { |
| 204 | + beforeEach(async function () { |
| 205 | + const bondFactory = await setupBondFactory(); |
| 206 | + ({ collateralToken } = await setupCollateralToken("Bitcoin", "BTC")); |
| 207 | + const BondIssuer = await ethers.getContractFactory("BondIssuer"); |
| 208 | + const issuer = await BondIssuer.deploy(bondFactory.address, collateralToken.address); |
| 209 | + await issuer.init(4800, [200, 300, 500], 1200, 0); |
| 210 | + |
| 211 | + const FeeStrategy = await ethers.getContractFactory("BasicFeeStrategy"); |
| 212 | + const feeStrategy = await smock.fake(FeeStrategy); |
| 213 | + await feeStrategy.computeMintFees.returns(["0", "0"]); |
| 214 | + await feeStrategy.computeBurnFees.returns(["0", "0"]); |
| 215 | + await feeStrategy.computeRolloverFees.returns(["0", "0"]); |
| 216 | + |
| 217 | + const PricingStrategy = await ethers.getContractFactory("UnitPricingStrategy"); |
| 218 | + const pricingStrategy = await smock.fake(PricingStrategy); |
| 219 | + await pricingStrategy.decimals.returns(8); |
| 220 | + await pricingStrategy.computeMatureTranchePrice.returns(toPriceFixedPtAmt("1")); |
| 221 | + await pricingStrategy.computeTranchePrice.returns(toPriceFixedPtAmt("1")); |
| 222 | + |
| 223 | + const DiscountStrategy = await ethers.getContractFactory("TrancheClassDiscountStrategy"); |
| 224 | + const discountStrategy = await smock.fake(DiscountStrategy); |
| 225 | + await discountStrategy.decimals.returns(18); |
| 226 | + await discountStrategy.computeTrancheDiscount |
| 227 | + .whenCalledWith(collateralToken.address) |
| 228 | + .returns(toDiscountFixedPtAmt("1")); |
| 229 | + |
| 230 | + const PerpetualTranche = await ethers.getContractFactory("PerpetualTranche"); |
| 231 | + perp = await upgrades.deployProxy( |
| 232 | + PerpetualTranche.connect(deployer), |
| 233 | + [ |
| 234 | + "PerpetualTranche", |
| 235 | + "PERP", |
| 236 | + collateralToken.address, |
| 237 | + issuer.address, |
| 238 | + feeStrategy.address, |
| 239 | + pricingStrategy.address, |
| 240 | + discountStrategy.address, |
| 241 | + ], |
| 242 | + { |
| 243 | + initializer: "init(string,string,address,address,address,address,address)", |
| 244 | + }, |
| 245 | + ); |
| 246 | + |
| 247 | + await feeStrategy.feeToken.returns(perp.address); |
| 248 | + await perp.updateTolerableTrancheMaturity(1200, 4800); |
| 249 | + await pricingStrategy.computeTranchePrice.returns(toPriceFixedPtAmt("1")); |
| 250 | + await discountStrategy.computeTrancheDiscount.returns(toDiscountFixedPtAmt("1")); |
| 251 | + await advancePerpQueueToBondMaturity(perp, await getDepositBond(perp)); |
| 252 | + |
| 253 | + const bond = await getDepositBond(perp); |
| 254 | + const tranches = await getTranches(bond); |
| 255 | + await depositIntoBond(bond, toFixedPtAmt("1000"), deployer); |
| 256 | + await tranches[0].approve(perp.address, toFixedPtAmt("200")); |
| 257 | + await perp.deposit(tranches[0].address, toFixedPtAmt("200")); |
| 258 | + await advancePerpQueueToBondMaturity(perp, bond); |
| 259 | + |
| 260 | + await mintCollteralToken(collateralToken, toFixedPtAmt("100000"), deployer); |
| 261 | + const RolloverVault = await ethers.getContractFactory("RolloverVault"); |
| 262 | + vault = await upgrades.deployProxy(RolloverVault.connect(deployer)); |
| 263 | + await vault.init("RolloverVault", "VSHARE", collateralToken.address, perp.address); |
| 264 | + await collateralToken.transfer(vault.address, toFixedPtAmt("1000")); |
| 265 | + await vault.deploy(); |
| 266 | + expect(await vault.deployedCount()).to.eq(2); |
| 267 | + }); |
| 268 | + it("should revert", async function () { |
| 269 | + await expect(vault.transferERC20(await vault.deployedAt(0), toAddress, toFixedPtAmt("100"))).to.be.revertedWith( |
| 270 | + "UnauthorizedTransferOut", |
| 271 | + ); |
| 272 | + await expect(vault.transferERC20(await vault.deployedAt(1), toAddress, toFixedPtAmt("100"))).to.be.revertedWith( |
| 273 | + "UnauthorizedTransferOut", |
| 274 | + ); |
| 275 | + }); |
| 276 | + }); |
| 277 | + }); |
| 278 | +}); |
0 commit comments