TeleportrDisburser.spec.ts 7.43 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
import { ethers } from 'hardhat'
import { Contract, BigNumber } from 'ethers'
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers'

import { expect } from '../../setup'
import { deploy } from '../../helpers'

const zeroETH = ethers.utils.parseEther('0.0')
const oneETH = ethers.utils.parseEther('1.0')
const twoETH = ethers.utils.parseEther('2.0')

describe('TeleportrDisburser', async () => {
  let signer1: SignerWithAddress
  let signer2: SignerWithAddress
  before(async () => {
    ;[signer1, signer2] = await ethers.getSigners()
  })

  let TeleportrDisburser: Contract
  let FailingReceiver: Contract
  before(async () => {
    TeleportrDisburser = await deploy('TeleportrDisburser')
    FailingReceiver = await deploy('FailingReceiver')
  })

  describe('disburse checks', async () => {
    it('should revert if called by non-owner', async () => {
      await expect(
        TeleportrDisburser.connect(signer2).disburse(0, [], { value: oneETH })
      ).to.be.revertedWith('Ownable: caller is not the owner')
    })

    it('should revert if no disbursements is zero length', async () => {
      await expect(
        TeleportrDisburser.disburse(0, [], { value: oneETH })
      ).to.be.revertedWith('No disbursements')
    })

    it('should revert if nextDepositId does not match expected value', async () => {
      await expect(
        TeleportrDisburser.disburse(1, [[oneETH, signer2.address]], {
          value: oneETH,
        })
      ).to.be.revertedWith('Unexpected next deposit id')
    })

    it('should revert if msg.value does not match total to disburse', async () => {
      await expect(
        TeleportrDisburser.disburse(0, [[oneETH, signer2.address]], {
          value: zeroETH,
        })
      ).to.be.revertedWith('Disbursement total != amount sent')
    })
  })

  describe('disburse single success', async () => {
    let signer1InitialBalance: BigNumber
    let signer2InitialBalance: BigNumber
    before(async () => {
      signer1InitialBalance = await ethers.provider.getBalance(signer1.address)
      signer2InitialBalance = await ethers.provider.getBalance(signer2.address)
    })

    it('should emit DisbursementSuccess for successful disbursement', async () => {
      await expect(
        TeleportrDisburser.disburse(0, [[oneETH, signer2.address]], {
          value: oneETH,
        })
      )
        .to.emit(TeleportrDisburser, 'DisbursementSuccess')
        .withArgs(BigNumber.from(0), signer2.address, oneETH)
    })

    it('should show one total disbursement', async () => {
      expect(await TeleportrDisburser.totalDisbursements()).to.be.equal(
        BigNumber.from(1)
      )
    })

    it('should leave contract balance at zero ETH', async () => {
      expect(
        await ethers.provider.getBalance(TeleportrDisburser.address)
      ).to.be.equal(zeroETH)
    })

    it('should increase recipients balance by disbursement amount', async () => {
      expect(await ethers.provider.getBalance(signer2.address)).to.be.equal(
        signer2InitialBalance.add(oneETH)
      )
    })

    it('should decrease owners balance by disbursement amount - fees', async () => {
      expect(await ethers.provider.getBalance(signer1.address)).to.be.closeTo(
        signer1InitialBalance.sub(oneETH),
        10 ** 15
      )
    })
  })

  describe('disburse single failure', async () => {
    let signer1InitialBalance: BigNumber
    before(async () => {
      signer1InitialBalance = await ethers.provider.getBalance(signer1.address)
    })

    it('should emit DisbursementFailed for failed disbursement', async () => {
      await expect(
        TeleportrDisburser.disburse(1, [[oneETH, FailingReceiver.address]], {
          value: oneETH,
        })
      )
        .to.emit(TeleportrDisburser, 'DisbursementFailed')
        .withArgs(BigNumber.from(1), FailingReceiver.address, oneETH)
    })

    it('should show two total disbursements', async () => {
      expect(await TeleportrDisburser.totalDisbursements()).to.be.equal(
        BigNumber.from(2)
      )
    })

    it('should leave contract with disbursement amount', async () => {
      expect(
        await ethers.provider.getBalance(TeleportrDisburser.address)
      ).to.be.equal(oneETH)
    })

    it('should leave recipients balance at zero ETH', async () => {
      expect(
        await ethers.provider.getBalance(FailingReceiver.address)
      ).to.be.equal(zeroETH)
    })

    it('should decrease owners balance by disbursement amount - fees', async () => {
      expect(await ethers.provider.getBalance(signer1.address)).to.be.closeTo(
        signer1InitialBalance.sub(oneETH),
        10 ** 15
      )
    })
  })

  describe('withdrawBalance', async () => {
    let signer1InitialBalance: BigNumber
    let disburserInitialBalance: BigNumber
    before(async () => {
      signer1InitialBalance = await ethers.provider.getBalance(signer1.address)
      disburserInitialBalance = await ethers.provider.getBalance(
        TeleportrDisburser.address
      )
    })

    it('should revert if called by non-owner', async () => {
      await expect(
        TeleportrDisburser.connect(signer2).withdrawBalance()
      ).to.be.revertedWith('Ownable: caller is not the owner')
    })

    it('should emit BalanceWithdrawn if called by owner', async () => {
      await expect(TeleportrDisburser.withdrawBalance())
        .to.emit(TeleportrDisburser, 'BalanceWithdrawn')
        .withArgs(signer1.address, oneETH)
    })

    it('should leave contract with zero balance', async () => {
      expect(
        await ethers.provider.getBalance(TeleportrDisburser.address)
      ).to.equal(zeroETH)
    })

    it('should credit owner with contract balance - fees', async () => {
      expect(await ethers.provider.getBalance(signer1.address)).to.be.closeTo(
        signer1InitialBalance.add(disburserInitialBalance),
        10 ** 15
      )
    })
  })

  describe('disburse multiple', async () => {
    let signer1InitialBalance: BigNumber
    let signer2InitialBalance: BigNumber
    before(async () => {
      signer1InitialBalance = await ethers.provider.getBalance(signer1.address)
      signer2InitialBalance = await ethers.provider.getBalance(signer2.address)
    })

    it('should emit DisbursementSuccess for successful disbursement', async () => {
      await expect(
        TeleportrDisburser.disburse(
          2,
          [
            [oneETH, signer2.address],
            [oneETH, FailingReceiver.address],
          ],
          { value: twoETH }
        )
      ).to.not.be.reverted
    })

    it('should show four total disbursements', async () => {
      expect(await TeleportrDisburser.totalDisbursements()).to.be.equal(
        BigNumber.from(4)
      )
    })

    it('should leave contract balance with failed disbursement amount', async () => {
      expect(
        await ethers.provider.getBalance(TeleportrDisburser.address)
      ).to.be.equal(oneETH)
    })

    it('should increase success recipients balance by disbursement amount', async () => {
      expect(await ethers.provider.getBalance(signer2.address)).to.be.equal(
        signer2InitialBalance.add(oneETH)
      )
    })

    it('should leave failed recipients balance at zero ETH', async () => {
      expect(
        await ethers.provider.getBalance(FailingReceiver.address)
      ).to.be.equal(zeroETH)
    })

    it('should decrease owners balance by disbursement 2*amount - fees', async () => {
      expect(await ethers.provider.getBalance(signer1.address)).to.be.closeTo(
        signer1InitialBalance.sub(twoETH),
        10 ** 15
      )
    })
  })
})