const { expect } = require("chai"); const { ethers } = require("hardhat"); const { time, loadFixture, setBalance } = require("@nomicfoundation/hardhat-network-helpers"); const KHYPE = (n) => ethers.parseUnits(String(n), 18); const USDC = (n) => ethers.parseUnits(String(n), 6); const HYPE = (n) => ethers.parseEther(String(n)); const DAY = 86400n; const FEE_BPS = 100n; // 1% of the premium // Strike is expressed in USDC units per 1e18 kHYPE units: 70 USDC per kHYPE. const STRIKE_70 = USDC(70); describe("KinetiqOptions", function () { async function deploy() { const [deployer, writer, buyer, other, treasury] = await ethers.getSigners(); const Token = await ethers.getContractFactory("MockERC20"); const khype = await Token.deploy("Kinetiq Staked HYPE", "kHYPE", 18); const usdc = await Token.deploy("USD Coin", "USDC", 6); const Options = await ethers.getContractFactory("KinetiqOptions"); const options = await Options.deploy(khype.target, usdc.target, treasury.address, FEE_BPS); await khype.mint(writer.address, KHYPE(1000)); await usdc.mint(buyer.address, USDC(1_000_000)); await khype.connect(writer).approve(options.target, ethers.MaxUint256); await usdc.connect(buyer).approve(options.target, ethers.MaxUint256); const expiry = BigInt(await time.latest()) + 7n * DAY; return { options, khype, usdc, deployer, writer, buyer, other, treasury, expiry }; } async function written() { const f = await loadFixture(deploy); await f.options.connect(f.writer).write(KHYPE(100), STRIKE_70, f.expiry, HYPE(0.5)); return { ...f, id: 0n }; } async function sold() { const f = await written(); await f.options.connect(f.buyer).buy(f.id, { value: HYPE(0.5) }); return f; } describe("deployment", function () { it("stores its parameters and has no owner", async function () { const { options, khype, usdc, treasury } = await loadFixture(deploy); expect(await options.collateral()).to.equal(khype.target); expect(await options.strikeAsset()).to.equal(usdc.target); expect(await options.treasury()).to.equal(treasury.address); expect(await options.feeBps()).to.equal(FEE_BPS); expect(options.interface.getFunction("owner")).to.equal(null); }); it("refuses a fee above 3% and zero addresses", async function () { const { khype, usdc, treasury } = await loadFixture(deploy); const Options = await ethers.getContractFactory("KinetiqOptions"); await expect(Options.deploy(khype.target, usdc.target, treasury.address, 301)).to.be.revertedWithCustomError(Options, "FeeTooHigh"); await expect(Options.deploy(ethers.ZeroAddress, usdc.target, treasury.address, 0)).to.be.revertedWithCustomError(Options, "ZeroAddress"); await expect(Options.deploy(khype.target, usdc.target, ethers.ZeroAddress, 0)).to.be.revertedWithCustomError(Options, "ZeroAddress"); }); }); describe("write", function () { it("locks the collateral and records the option", async function () { const { options, khype, writer, expiry } = await loadFixture(deploy); await expect(options.connect(writer).write(KHYPE(100), STRIKE_70, expiry, HYPE(0.5))) .to.emit(options, "Written") .withArgs(0, writer.address, KHYPE(100), STRIKE_70, expiry, HYPE(0.5)); expect(await khype.balanceOf(options.target)).to.equal(KHYPE(100)); const o = await options.getOption(0); expect(o.writer).to.equal(writer.address); expect(o.holder).to.equal(ethers.ZeroAddress); expect(o.state).to.equal(1); // Open expect(await options.count()).to.equal(1); }); it("refuses zero values and expiries outside one hour to 180 days", async function () { const { options, writer, expiry } = await loadFixture(deploy); const now = BigInt(await time.latest()); const w = options.connect(writer); await expect(w.write(0, STRIKE_70, expiry, HYPE(1))).to.be.revertedWithCustomError(options, "ZeroAmount"); await expect(w.write(KHYPE(1), 0, expiry, HYPE(1))).to.be.revertedWithCustomError(options, "ZeroAmount"); await expect(w.write(KHYPE(1), STRIKE_70, expiry, 0)).to.be.revertedWithCustomError(options, "ZeroAmount"); await expect(w.write(KHYPE(1), STRIKE_70, now + 600n, HYPE(1))).to.be.revertedWithCustomError(options, "BadExpiry"); await expect(w.write(KHYPE(1), STRIKE_70, now + 181n * DAY, HYPE(1))).to.be.revertedWithCustomError(options, "BadExpiry"); }); it("refuses a collateral token that takes a fee on transfer", async function () { const { usdc, writer, treasury, expiry } = await loadFixture(deploy); const fee = await (await ethers.getContractFactory("FeeToken")).deploy(); const Options = await ethers.getContractFactory("KinetiqOptions"); const options = await Options.deploy(fee.target, usdc.target, treasury.address, 0); await fee.mint(writer.address, KHYPE(10)); await fee.connect(writer).approve(options.target, ethers.MaxUint256); await expect(options.connect(writer).write(KHYPE(10), STRIKE_70, expiry, HYPE(1))).to.be.revertedWithCustomError(options, "UnsupportedToken"); }); it("fails cleanly without an allowance", async function () { const { options, khype, other, expiry } = await loadFixture(deploy); await khype.mint(other.address, KHYPE(5)); await expect(options.connect(other).write(KHYPE(5), STRIKE_70, expiry, HYPE(1))).to.be.revertedWithCustomError(options, "TransferFailed"); }); }); describe("cancel", function () { it("returns the collateral to the writer", async function () { const { options, khype, writer, id } = await written(); await expect(options.connect(writer).cancel(id)).to.emit(options, "Cancelled").withArgs(id); expect(await khype.balanceOf(writer.address)).to.equal(KHYPE(1000)); expect((await options.getOption(id)).state).to.equal(4); // Cancelled }); it("is for the writer only, and only while unsold", async function () { const { options, writer, buyer, id } = await written(); await expect(options.connect(buyer).cancel(id)).to.be.revertedWithCustomError(options, "NotWriter"); await options.connect(buyer).buy(id, { value: HYPE(0.5) }); await expect(options.connect(writer).cancel(id)).to.be.revertedWithCustomError(options, "WrongState"); }); it("still works after expiry if nobody bought", async function () { const { options, khype, writer, expiry, id } = await written(); await time.increaseTo(expiry + 10n * DAY); await options.connect(writer).cancel(id); expect(await khype.balanceOf(writer.address)).to.equal(KHYPE(1000)); }); }); describe("buy", function () { it("pays the writer at once, less the fee to the treasury", async function () { const { options, writer, buyer, treasury, id } = await written(); const tx = options.connect(buyer).buy(id, { value: HYPE(0.5) }); await expect(tx).to.changeEtherBalances([buyer, writer, treasury, options], [-HYPE(0.5), HYPE(0.495), HYPE(0.005), 0]); await expect(tx).to.emit(options, "Bought").withArgs(id, buyer.address, HYPE(0.5), HYPE(0.005)); const o = await options.getOption(id); expect(o.holder).to.equal(buyer.address); expect(o.state).to.equal(2); // Sold }); it("refuses any amount other than the premium", async function () { const { options, buyer, id } = await written(); await expect(options.connect(buyer).buy(id, { value: HYPE(0.49) })).to.be.revertedWithCustomError(options, "WrongPremium"); await expect(options.connect(buyer).buy(id, { value: HYPE(0.51) })).to.be.revertedWithCustomError(options, "WrongPremium"); }); it("refuses an expired, sold or unknown option", async function () { const { options, buyer, other, expiry, id } = await written(); await expect(options.connect(buyer).buy(99, { value: HYPE(0.5) })).to.be.revertedWithCustomError(options, "UnknownOption"); await options.connect(buyer).buy(id, { value: HYPE(0.5) }); await expect(options.connect(other).buy(id, { value: HYPE(0.5) })).to.be.revertedWithCustomError(options, "WrongState"); const g = await written(); await time.increaseTo(g.expiry); await expect(g.options.connect(g.buyer).buy(g.id, { value: HYPE(0.5) })).to.be.revertedWithCustomError(g.options, "Expired"); expect(expiry).to.be.greaterThan(0n); }); it("blocks a writer contract that tries to re-enter when paid", async function () { const { options, khype, buyer, expiry } = await loadFixture(deploy); const evil = await (await ethers.getContractFactory("ReentrantWriter")).deploy(options.target); await khype.mint(evil.target, KHYPE(10)); await evil.approveAndWrite(khype.target, KHYPE(10), STRIKE_70, expiry, HYPE(1)); await options.connect(buyer).buy(0, { value: HYPE(1) }); expect(await evil.reentered()).to.equal(false); expect(await khype.balanceOf(options.target)).to.equal(KHYPE(10)); expect((await options.getOption(0)).state).to.equal(2); // still Sold }); it("reverts when the writer refuses the premium, leaving the option open", async function () { const { options, khype, buyer, expiry } = await loadFixture(deploy); const rej = await (await ethers.getContractFactory("RejectingWriter")).deploy(); await khype.mint(rej.target, KHYPE(10)); await rej.approveAndWrite(options.target, khype.target, KHYPE(10), STRIKE_70, expiry, HYPE(1)); await expect(options.connect(buyer).buy(0, { value: HYPE(1) })).to.be.revertedWithCustomError(options, "TransferFailed"); expect((await options.getOption(0)).state).to.equal(1); // Open }); }); describe("exercise", function () { it("swaps the strike for the collateral inside the window", async function () { const { options, khype, usdc, writer, buyer, expiry, id } = await sold(); await time.increaseTo(expiry); expect(await options.exerciseCost(id)).to.equal(USDC(7000)); await expect(options.connect(buyer).exercise(id)).to.emit(options, "Exercised").withArgs(id, buyer.address, USDC(7000)); expect(await khype.balanceOf(buyer.address)).to.equal(KHYPE(100)); expect(await usdc.balanceOf(writer.address)).to.equal(USDC(7000)); expect(await khype.balanceOf(options.target)).to.equal(0); expect((await options.getOption(id)).state).to.equal(3); // Exercised }); it("is closed before expiry and after the 24-hour window", async function () { const { options, buyer, expiry, id } = await sold(); await expect(options.connect(buyer).exercise(id)).to.be.revertedWithCustomError(options, "WindowClosed"); await time.increaseTo(expiry + DAY); await expect(options.connect(buyer).exercise(id)).to.be.revertedWithCustomError(options, "WindowClosed"); }); it("is for the holder only, and only once", async function () { const { options, writer, buyer, other, expiry, id } = await sold(); await time.increaseTo(expiry + 60n); await expect(options.connect(other).exercise(id)).to.be.revertedWithCustomError(options, "NotHolder"); await expect(options.connect(writer).exercise(id)).to.be.revertedWithCustomError(options, "NotHolder"); await options.connect(buyer).exercise(id); await expect(options.connect(buyer).exercise(id)).to.be.revertedWithCustomError(options, "WrongState"); }); it("rounds the cost up so the writer is never short a unit", async function () { const { options, usdc, writer, buyer, expiry } = await loadFixture(deploy); // 1 wei of kHYPE at 70 USDC per kHYPE is far below one USDC unit: the cost must still be 1. await options.connect(writer).write(1, STRIKE_70, expiry, 1); await options.connect(buyer).buy(0, { value: 1 }); expect(await options.exerciseCost(0)).to.equal(1); await time.increaseTo(expiry); await options.connect(buyer).exercise(0); expect(await usdc.balanceOf(writer.address)).to.equal(1); }); it("fails cleanly when the holder cannot pay the strike", async function () { const { options, usdc, buyer, other, expiry, id } = await sold(); await options.connect(buyer).transferOption(id, other.address); await usdc.connect(other).approve(options.target, ethers.MaxUint256); await time.increaseTo(expiry); await expect(options.connect(other).exercise(id)).to.be.revertedWithCustomError(options, "TransferFailed"); expect((await options.getOption(id)).state).to.equal(2); // still Sold }); }); describe("transferOption", function () { it("moves the right to exercise", async function () { const { options, khype, usdc, buyer, other, expiry, id } = await sold(); await expect(options.connect(buyer).transferOption(id, other.address)).to.emit(options, "Transferred").withArgs(id, buyer.address, other.address); await usdc.mint(other.address, USDC(7000)); await usdc.connect(other).approve(options.target, ethers.MaxUint256); await time.increaseTo(expiry); await expect(options.connect(buyer).exercise(id)).to.be.revertedWithCustomError(options, "NotHolder"); await options.connect(other).exercise(id); expect(await khype.balanceOf(other.address)).to.equal(KHYPE(100)); }); it("is for the holder only and never to the zero address", async function () { const { options, buyer, other, id } = await sold(); await expect(options.connect(other).transferOption(id, other.address)).to.be.revertedWithCustomError(options, "NotHolder"); await expect(options.connect(buyer).transferOption(id, ethers.ZeroAddress)).to.be.revertedWithCustomError(options, "ZeroAddress"); }); }); describe("reclaim", function () { it("returns the collateral to the writer once the window has passed, whoever calls", async function () { const { options, khype, writer, other, expiry, id } = await sold(); await time.increaseTo(expiry + DAY - 5n); await expect(options.connect(other).reclaim(id)).to.be.revertedWithCustomError(options, "WindowOpen"); await time.increaseTo(expiry + DAY); await expect(options.connect(other).reclaim(id)).to.emit(options, "Reclaimed").withArgs(id); expect(await khype.balanceOf(writer.address)).to.equal(KHYPE(1000)); await expect(options.connect(other).reclaim(id)).to.be.revertedWithCustomError(options, "WrongState"); }); it("cannot touch an exercised or unsold option", async function () { const { options, buyer, expiry, id } = await sold(); await time.increaseTo(expiry); await options.connect(buyer).exercise(id); await time.increaseTo(expiry + 2n * DAY); await expect(options.reclaim(id)).to.be.revertedWithCustomError(options, "WrongState"); const g = await written(); await time.increaseTo(g.expiry + 2n * DAY); await expect(g.options.reclaim(g.id)).to.be.revertedWithCustomError(g.options, "WrongState"); }); }); describe("accounting", function () { it("always holds exactly the collateral of its open and sold options, and never any HYPE", async function () { const { options, khype, usdc, writer, buyer, expiry } = await loadFixture(deploy); await setBalance(buyer.address, HYPE(1000)); const w = options.connect(writer); const b = options.connect(buyer); const sizes = [3, 7, 11, 13, 17, 19]; for (const s of sizes) await w.write(KHYPE(s), STRIKE_70, expiry, HYPE(0.1)); await w.cancel(0); // 3 out for (const id of [1, 2, 3, 4]) await b.buy(id, { value: HYPE(0.1) }); const locked = async () => { let sum = 0n; for (const o of await options.getOptions(0, 100)) if (o.state === 1n || o.state === 2n) sum += o.amount; return sum; }; expect(await khype.balanceOf(options.target)).to.equal(await locked()); await time.increaseTo(expiry); await b.exercise(1); // 7 out await b.exercise(2); // 11 out expect(await khype.balanceOf(options.target)).to.equal(await locked()); await time.increaseTo(expiry + DAY); await options.reclaim(3); // 13 back await options.reclaim(4); // 17 back await w.cancel(5); // 19 back expect(await locked()).to.equal(0n); expect(await khype.balanceOf(options.target)).to.equal(0n); expect(await ethers.provider.getBalance(options.target)).to.equal(0n); expect(await usdc.balanceOf(options.target)).to.equal(0n); expect(await khype.balanceOf(writer.address)).to.equal(KHYPE(1000 - 18)); expect(await khype.balanceOf(buyer.address)).to.equal(KHYPE(18)); }); it("pages through options", async function () { const { options, writer, expiry } = await loadFixture(deploy); for (let i = 1; i <= 5; i++) await options.connect(writer).write(KHYPE(i), STRIKE_70, expiry, HYPE(0.1)); expect((await options.getOptions(0, 2)).length).to.equal(2); expect((await options.getOptions(3, 10)).length).to.equal(2); expect((await options.getOptions(9, 10)).length).to.equal(0); expect((await options.getOptions(4, 1))[0].amount).to.equal(KHYPE(5)); }); }); });