|
| 1 | +import chai, { expect } from 'chai'; |
| 2 | +import { deployments, ethers } from 'hardhat'; |
| 3 | +import type { PayoutsController } from '../../typechain/contracts/payouts/PayoutsController'; |
| 4 | +import { BigNumber } from 'ethers'; |
| 5 | +import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers'; |
| 6 | +import getRandomBytesHexString from '../utils/getRandomBytesHexString'; |
| 7 | +import BalanceTree from '../../trees/balance-tree'; |
| 8 | +import { GoldenToken } from '../../typechain'; |
| 9 | +chai.config.includeStack = true; |
| 10 | +chai.Assertion.includeStack = true; |
| 11 | + |
| 12 | +const address1 = '0x9ED5724f7dc9eCDd6b48185F875A8779c2059533'; |
| 13 | +const address2 = '0x61bfCd8d7fcbd61508027Ba5935176A3298E941e'; |
| 14 | + |
| 15 | +const ownableError = 'Ownable: caller is not the owner'; |
| 16 | +const overrides = { |
| 17 | + gasLimit: 9999999, |
| 18 | +}; |
| 19 | + |
| 20 | +describe('PayoutsController', function () { |
| 21 | + let PayoutsController: PayoutsController; |
| 22 | + let GoldenToken: GoldenToken; |
| 23 | + let owner: SignerWithAddress; |
| 24 | + |
| 25 | + beforeEach(async function () { |
| 26 | + await deployments.fixture(['GoldenToken']); |
| 27 | + await deployments.fixture(['PayoutsController']); |
| 28 | + PayoutsController = await ethers.getContract('PayoutsController'); |
| 29 | + GoldenToken = await ethers.getContract('GoldenToken'); |
| 30 | + expect(await PayoutsController.getToken()).to.equal(GoldenToken.address); |
| 31 | + const [deployer] = await ethers.getSigners(); |
| 32 | + owner = deployer; |
| 33 | + GoldenToken.connect(owner); |
| 34 | + await GoldenToken.transfer( |
| 35 | + PayoutsController.address, |
| 36 | + '1000000000000000000000' |
| 37 | + ); |
| 38 | + expect(await GoldenToken.balanceOf(PayoutsController.address)).to.equal( |
| 39 | + '1000000000000000000000' |
| 40 | + ); |
| 41 | + }); |
| 42 | + |
| 43 | + describe('Test functions', function () { |
| 44 | + it('Should test contract functions', async () => { |
| 45 | + expect(await PayoutsController.getLastEpoch()).to.equal(0); |
| 46 | + const randomMerkleTree = getRandomBytesHexString(32); |
| 47 | + await PayoutsController.addMerkleRoot(randomMerkleTree); |
| 48 | + expect(await PayoutsController.getLastEpoch()).to.equal(1); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + describe('Access control', function () { |
| 53 | + it('Should test onlyOwner functions', async () => { |
| 54 | + await PayoutsController.setToken(address1); |
| 55 | + expect(await PayoutsController.getToken()).to.equal(address1); |
| 56 | + const randomMerkleTree = getRandomBytesHexString(32); |
| 57 | + await PayoutsController.addMerkleRoot(randomMerkleTree); |
| 58 | + expect( |
| 59 | + await PayoutsController.getMerkleRoot( |
| 60 | + await PayoutsController.getLastEpoch() |
| 61 | + ) |
| 62 | + ).to.equal(randomMerkleTree); |
| 63 | + const randomMerkleTree2 = getRandomBytesHexString(32); |
| 64 | + await PayoutsController.addMerkleRoot(randomMerkleTree2); |
| 65 | + expect( |
| 66 | + await PayoutsController.getMerkleRoot( |
| 67 | + await PayoutsController.getLastEpoch() |
| 68 | + ) |
| 69 | + ).to.equal(randomMerkleTree2); |
| 70 | + expect( |
| 71 | + await PayoutsController.getMerkleRoot( |
| 72 | + (await PayoutsController.getLastEpoch()).sub(1) |
| 73 | + ) |
| 74 | + ).to.equal(randomMerkleTree); |
| 75 | + }); |
| 76 | + it('Should fail calling onlyOwner functions', async () => { |
| 77 | + await PayoutsController.transferOwnership( |
| 78 | + '0x15d34AAf54267DB7D7c367839AAf71A00a2C6A65' |
| 79 | + ); |
| 80 | + await expect(PayoutsController.setToken(address1)).to.be.revertedWith( |
| 81 | + ownableError |
| 82 | + ); |
| 83 | + const randomMerkleTree = getRandomBytesHexString(32); |
| 84 | + await expect( |
| 85 | + PayoutsController.addMerkleRoot(randomMerkleTree) |
| 86 | + ).to.be.revertedWith(ownableError); |
| 87 | + }); |
| 88 | + it('Should test claiming functions', async () => { |
| 89 | + const tree = new BalanceTree([ |
| 90 | + { account: address1, amount: BigNumber.from(100) }, |
| 91 | + { account: address2, amount: BigNumber.from(101) }, |
| 92 | + ]); |
| 93 | + await PayoutsController.addMerkleRoot(tree.getHexRoot()); |
| 94 | + const proof1 = tree.getProof(0, address1, BigNumber.from(100)); |
| 95 | + expect(await PayoutsController.isClaimed(1, 0)).to.equal(false); |
| 96 | + expect(await PayoutsController.isClaimed(1, 1)).to.equal(false); |
| 97 | + expect(await GoldenToken.balanceOf(address1)).to.equal('0'); |
| 98 | + expect(await GoldenToken.balanceOf(address2)).to.equal('0'); |
| 99 | + await expect( |
| 100 | + PayoutsController.claim( |
| 101 | + 1, |
| 102 | + 0, |
| 103 | + address1, |
| 104 | + BigNumber.from(100), |
| 105 | + proof1, |
| 106 | + overrides |
| 107 | + ) |
| 108 | + ) |
| 109 | + .to.emit(PayoutsController, 'Claimed') |
| 110 | + .withArgs(1, 0, address1, 100); |
| 111 | + expect(await GoldenToken.balanceOf(address1)).to.equal('100'); |
| 112 | + expect(await GoldenToken.balanceOf(address2)).to.equal('0'); |
| 113 | + expect(await PayoutsController.isClaimed(1, 0)).to.equal(true); |
| 114 | + expect(await PayoutsController.isClaimed(1, 1)).to.equal(false); |
| 115 | + const proof2 = tree.getProof(1, address2, BigNumber.from(101)); |
| 116 | + await expect( |
| 117 | + PayoutsController.claim( |
| 118 | + 1, |
| 119 | + 1, |
| 120 | + address2, |
| 121 | + BigNumber.from(101), |
| 122 | + proof2, |
| 123 | + overrides |
| 124 | + ) |
| 125 | + ) |
| 126 | + .to.emit(PayoutsController, 'Claimed') |
| 127 | + .withArgs(1, 1, address2, 101); |
| 128 | + expect(await PayoutsController.isClaimed(1, 0)).to.equal(true); |
| 129 | + expect(await PayoutsController.isClaimed(1, 1)).to.equal(true); |
| 130 | + expect(await GoldenToken.balanceOf(address1)).to.equal('100'); |
| 131 | + expect(await GoldenToken.balanceOf(address2)).to.equal('101'); |
| 132 | + }); |
| 133 | + }); |
| 134 | +}); |
0 commit comments