|
| 1 | +const { expect } = require("chai") |
| 2 | +const { |
| 3 | + ZERO_ADDRESS, |
| 4 | + to1e18, |
| 5 | + lastBlockTime, |
| 6 | +} = require("../helpers/contract-test-helpers") |
| 7 | + |
| 8 | +describe("TokenGrant", () => { |
| 9 | + let grantee |
| 10 | + |
| 11 | + beforeEach(async () => { |
| 12 | + ;[grantee] = await ethers.getSigners() |
| 13 | + }) |
| 14 | + |
| 15 | + describe("unlockedAmount", () => { |
| 16 | + const assertionPrecision = to1e18(1) // +- 1 token |
| 17 | + |
| 18 | + const amount = to1e18(100000) // 100k tokens |
| 19 | + const duration = 15552000 // 180 days |
| 20 | + |
| 21 | + let now |
| 22 | + |
| 23 | + beforeEach(async () => { |
| 24 | + now = await lastBlockTime() |
| 25 | + }) |
| 26 | + |
| 27 | + context("before the schedule start", () => { |
| 28 | + it("should return no tokens unlocked", async () => { |
| 29 | + const start = now + 60 |
| 30 | + const cliff = now |
| 31 | + const grant = await createGrant(false, amount, duration, start, cliff) |
| 32 | + expect(await grant.unlockedAmount()).to.equal(0) |
| 33 | + }) |
| 34 | + }) |
| 35 | + |
| 36 | + context("before the cliff ended", () => { |
| 37 | + it("should return no tokens unlocked", async () => { |
| 38 | + const start = now - 60 |
| 39 | + const cliff = now + 60 |
| 40 | + const grant = await createGrant(false, amount, duration, start, cliff) |
| 41 | + expect(await grant.unlockedAmount()).to.equal(0) |
| 42 | + }) |
| 43 | + }) |
| 44 | + |
| 45 | + context("after the cliff ended", () => { |
| 46 | + it("should return token amount unlocked from the start", async () => { |
| 47 | + const start = now - duration / 2 |
| 48 | + const cliff = now - 1 |
| 49 | + const grant = await createGrant(false, amount, duration, start, cliff) |
| 50 | + expect(await grant.unlockedAmount()).is.closeTo( |
| 51 | + to1e18(50000), |
| 52 | + assertionPrecision |
| 53 | + ) |
| 54 | + }) |
| 55 | + }) |
| 56 | + |
| 57 | + context("with no cliff", () => { |
| 58 | + it("should return token amount unlocked so far", async () => { |
| 59 | + const start = now - duration / 4 |
| 60 | + const cliff = now - duration / 4 |
| 61 | + const grant = await createGrant(false, amount, duration, start, cliff) |
| 62 | + expect(await grant.unlockedAmount()).is.closeTo( |
| 63 | + to1e18(25000), |
| 64 | + assertionPrecision |
| 65 | + ) |
| 66 | + }) |
| 67 | + }) |
| 68 | + |
| 69 | + context("when unlocking period finished", () => { |
| 70 | + it("should return all tokens", async () => { |
| 71 | + const start = now - duration - 1 |
| 72 | + const cliff = now - duration - 1 |
| 73 | + const grant = await createGrant(false, amount, duration, start, cliff) |
| 74 | + expect(await grant.unlockedAmount()).is.closeTo( |
| 75 | + to1e18(100000), |
| 76 | + assertionPrecision |
| 77 | + ) |
| 78 | + }) |
| 79 | + }) |
| 80 | + |
| 81 | + context("when in the middle of unlocking period", () => { |
| 82 | + it("should return token amount unlocked from the start", async () => { |
| 83 | + const start = now - duration / 2 |
| 84 | + const cliff = now - duration / 2 |
| 85 | + const grant = await createGrant(false, amount, duration, start, cliff) |
| 86 | + expect(await grant.unlockedAmount()).is.closeTo( |
| 87 | + to1e18(50000), |
| 88 | + assertionPrecision |
| 89 | + ) |
| 90 | + }) |
| 91 | + }) |
| 92 | + |
| 93 | + context("when the unlocking period just started", () => { |
| 94 | + it("should return token amount unlocked so far", async () => { |
| 95 | + const start = now - 3600 // one hour earlier |
| 96 | + const cliff = now - 3600 |
| 97 | + const grant = await createGrant(false, amount, duration, start, cliff) |
| 98 | + expect(await grant.unlockedAmount()).is.closeTo( |
| 99 | + to1e18(23), // 3600 / 15552000 * 100k = ~23 tokens |
| 100 | + assertionPrecision |
| 101 | + ) |
| 102 | + }) |
| 103 | + }) |
| 104 | + }) |
| 105 | + |
| 106 | + async function createGrant(revocable, amount, duration, start, cliff) { |
| 107 | + const TokenGrant = await ethers.getContractFactory("TokenGrant") |
| 108 | + const tokenGrant = await TokenGrant.deploy() |
| 109 | + await tokenGrant.deployed() |
| 110 | + |
| 111 | + await tokenGrant.initialize( |
| 112 | + grantee.address, |
| 113 | + revocable, |
| 114 | + amount, |
| 115 | + duration, |
| 116 | + start, |
| 117 | + cliff, |
| 118 | + ZERO_ADDRESS |
| 119 | + ) |
| 120 | + |
| 121 | + return tokenGrant |
| 122 | + } |
| 123 | +}) |
0 commit comments