|
| 1 | +import { ethers, network } from 'hardhat' |
| 2 | +import { Contract } from 'ethers' |
| 3 | +import { SignerWithAddress as Account } from '@nomiclabs/hardhat-ethers/signers' |
| 4 | +import { toWei, fromWei } from 'web3-utils' |
| 5 | +import { expect } from 'chai' |
| 6 | + |
| 7 | +const getTimestamp = (dates: string): number => { |
| 8 | + const date = new Date(dates) |
| 9 | + return date.getTime() / 1000 |
| 10 | +} |
| 11 | + |
| 12 | +const setBlockTimestamp = async (dates: string) => { |
| 13 | + await network.provider.send('evm_setNextBlockTimestamp', [getTimestamp(dates)]) |
| 14 | +} |
| 15 | + |
| 16 | +describe('LOAS', () => { |
| 17 | + let loas: Contract |
| 18 | + let genesis: Account |
| 19 | + let originalClaimer: Account |
| 20 | + let allowedClaimer: Account |
| 21 | + let invalidClaimer: Account |
| 22 | + |
| 23 | + before(async () => { |
| 24 | + const accounts = await ethers.getSigners() |
| 25 | + genesis = accounts[1] |
| 26 | + originalClaimer = accounts[2] |
| 27 | + allowedClaimer = accounts[3] |
| 28 | + invalidClaimer = accounts[4] |
| 29 | + }) |
| 30 | + |
| 31 | + beforeEach(async () => { |
| 32 | + await network.provider.send('hardhat_reset') |
| 33 | + loas = await (await ethers.getContractFactory('LOAS')).deploy() |
| 34 | + }) |
| 35 | + |
| 36 | + describe('revoke()', async () => { |
| 37 | + const expectBalance = async (user: Account, expOAS: number, expLOAS: number) => { |
| 38 | + const actualOAS = fromWei((await user.getBalance()).toString()) |
| 39 | + const actualLOAS = fromWei((await loas.balanceOf(user.address)).toString()) |
| 40 | + expect(actualOAS).to.match(new RegExp(`^${expOAS + 10000}`)) |
| 41 | + expect(actualLOAS).to.match(new RegExp(`^${expLOAS}`)) |
| 42 | + } |
| 43 | + const expectTotalSupply = async (exp: number) => { |
| 44 | + const actual = fromWei((await loas.totalSupply()).toString()) |
| 45 | + expect(actual).to.match(new RegExp(`^${exp}`)) |
| 46 | + } |
| 47 | + |
| 48 | + it('revoke all', async () => { |
| 49 | + // initial balance. |
| 50 | + await expectBalance(genesis, 0, 0) |
| 51 | + await expectBalance(originalClaimer, 0, 0) |
| 52 | + await expectTotalSupply(0) |
| 53 | + |
| 54 | + // minting. |
| 55 | + await setBlockTimestamp('2100/01/01') |
| 56 | + await loas |
| 57 | + .connect(genesis) |
| 58 | + .mint(originalClaimer.address, getTimestamp('2100/07/01'), getTimestamp('2100/12/31'), { |
| 59 | + value: toWei('100'), |
| 60 | + }) |
| 61 | + |
| 62 | + // after minted. |
| 63 | + await expectBalance(originalClaimer, 0, 100) |
| 64 | + await expectTotalSupply(100) |
| 65 | + |
| 66 | + // 1 month elapsed. |
| 67 | + await setBlockTimestamp('2100/07/31') |
| 68 | + await loas.connect(originalClaimer).claim(toWei('16')) |
| 69 | + await expectBalance(originalClaimer, 16, 84) |
| 70 | + await expectTotalSupply(84) |
| 71 | + |
| 72 | + // Fail by non-existing holder |
| 73 | + let tx = loas.connect(originalClaimer).revoke(invalidClaimer.address, toWei('84')) |
| 74 | + await expect(tx).to.revertedWith('NotFound()') |
| 75 | + |
| 76 | + // Fail by invalid revoker. |
| 77 | + tx = loas.connect(originalClaimer).revoke(originalClaimer.address, toWei('84')) |
| 78 | + await expect(tx).to.revertedWith('InvalidRevoker()') |
| 79 | + |
| 80 | + // Revoke all. |
| 81 | + await expect(await loas.connect(genesis).revoke(originalClaimer.address, toWei('84'))) |
| 82 | + .to.emit(loas, 'Revoke') |
| 83 | + .withArgs(originalClaimer.address, originalClaimer.address, toWei('84')) |
| 84 | + await expectBalance(genesis, -16, 0) |
| 85 | + await expectTotalSupply(0) |
| 86 | + }) |
| 87 | + |
| 88 | + it('revoke locked only', async () => { |
| 89 | + // minting. |
| 90 | + await setBlockTimestamp('2100/01/01') |
| 91 | + await loas |
| 92 | + .connect(genesis) |
| 93 | + .mint(originalClaimer.address, getTimestamp('2100/07/01'), getTimestamp('2100/12/31'), { |
| 94 | + value: toWei('100'), |
| 95 | + }) |
| 96 | + |
| 97 | + // after minted. |
| 98 | + await expectBalance(originalClaimer, 0, 100) |
| 99 | + await expectTotalSupply(100) |
| 100 | + |
| 101 | + // 1 month elapsed. |
| 102 | + await setBlockTimestamp('2100/07/31') |
| 103 | + |
| 104 | + // Revoke locked only. |
| 105 | + await loas.connect(genesis).revoke(originalClaimer.address, 0) |
| 106 | + let actualOAS = fromWei((await genesis.getBalance()).toString()) |
| 107 | + expect(Math.ceil(Number(actualOAS))).to.equal(10000-100+84) |
| 108 | + let supply = fromWei((await loas.totalSupply()).toString()) |
| 109 | + expect(Math.ceil(Number(supply))).to.equal(17) |
| 110 | + |
| 111 | + // Claim. |
| 112 | + await loas.connect(originalClaimer).claim(toWei('16')) |
| 113 | + await expectBalance(originalClaimer, 16, 0) |
| 114 | + supply = fromWei((await loas.totalSupply()).toString()) |
| 115 | + expect(Math.ceil(Number(supply))).to.equal(1) // left less than 1 OAS. |
| 116 | + |
| 117 | + // 2 month elapsed. |
| 118 | + await setBlockTimestamp('2100/08/31') |
| 119 | + |
| 120 | + // Claim left. |
| 121 | + const left = await loas.balanceOf(originalClaimer.address) |
| 122 | + await loas.connect(originalClaimer).claim(left) |
| 123 | + supply = fromWei((await loas.totalSupply()).toString()) |
| 124 | + expect(Math.ceil(Number(supply))).to.equal(0) |
| 125 | + }) |
| 126 | + |
| 127 | + it('revoke splited LOAS', async () => { |
| 128 | + await setBlockTimestamp('2100/01/01') |
| 129 | + await loas |
| 130 | + .connect(genesis) |
| 131 | + .mint(originalClaimer.address, getTimestamp('2100/07/01'), getTimestamp('2100/12/31'), { |
| 132 | + value: toWei('100', 'ether'), |
| 133 | + }) |
| 134 | + |
| 135 | + await expectBalance(allowedClaimer, 0, 0) |
| 136 | + |
| 137 | + // 2 month elapsed. |
| 138 | + await setBlockTimestamp('2100/08/31') |
| 139 | + |
| 140 | + // transfer. |
| 141 | + await loas.connect(genesis)['allow(address,address)'](originalClaimer.address, allowedClaimer.address) |
| 142 | + await loas.connect(originalClaimer)['transfer(address,uint256)'](allowedClaimer.address, toWei('50')) |
| 143 | + await expectBalance(originalClaimer, 0, 50) |
| 144 | + await expectBalance(allowedClaimer, 0, 50) |
| 145 | + await expectTotalSupply(100) |
| 146 | + |
| 147 | + // try to revoke original claimer, but it should fail as the locked LOAS is over than original claimer's balance. |
| 148 | + const tx = loas.connect(genesis).revoke(originalClaimer.address, 0) |
| 149 | + await expect(tx).to.revertedWith('OverAmount()') |
| 150 | + |
| 151 | + // revoke all the original claimer's balance. |
| 152 | + await loas.connect(genesis).revoke(originalClaimer.address, toWei('50')) |
| 153 | + await expectBalance(originalClaimer, 0, 0) |
| 154 | + await expectBalance(genesis, -50, 0) |
| 155 | + await expectTotalSupply(50) |
| 156 | + |
| 157 | + // revoke only locked LOAS from allowed claimer. |
| 158 | + await loas.connect(genesis).revoke(allowedClaimer.address, 0) |
| 159 | + let actualOAS = fromWei((await genesis.getBalance()).toString()) |
| 160 | + expect(Math.ceil(Number(actualOAS))).to.equal(10000-100+50+17) |
| 161 | + let supply = fromWei((await loas.totalSupply()).toString()) |
| 162 | + expect(Math.ceil(Number(supply))).to.equal(34) |
| 163 | + const clainInfo = await loas.claimInfo(originalClaimer.address) |
| 164 | + expect(Math.ceil(Number(fromWei((clainInfo.revoked).toString())))).to.equal(50+17) |
| 165 | + |
| 166 | + // claim all the left amount |
| 167 | + const left = await loas.balanceOf(allowedClaimer.address) |
| 168 | + await loas.connect(allowedClaimer).claim(left) |
| 169 | + supply = fromWei((await loas.totalSupply()).toString()) |
| 170 | + expect(Math.ceil(Number(supply))).to.equal(0) |
| 171 | + }) |
| 172 | + }) |
| 173 | +}) |
0 commit comments