Commit b5b4f7f0 authored by smartcontracts's avatar smartcontracts Committed by GitHub

maint(ct): clean up Teleportr tests (#2473)

Cleans up the TeleportrDeposit tests using the same techniques as
previous cleanup PRs.
Co-authored-by: default avatarmergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent eefbf2ee
/* External Imports */
import { ethers } from 'hardhat' import { ethers } from 'hardhat'
import { Signer, Contract, BigNumber } from 'ethers' import { Contract, BigNumber } from 'ethers'
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers'
/* Internal Imports */
import { expect } from '../../../setup' import { expect } from '../../../setup'
import { deploy } from '../../../helpers'
const initialMinDepositAmount = ethers.utils.parseEther('0.01') const initialMinDepositAmount = ethers.utils.parseEther('0.01')
const initialMaxDepositAmount = ethers.utils.parseEther('1') const initialMaxDepositAmount = ethers.utils.parseEther('1')
const initialMaxBalance = ethers.utils.parseEther('2') const initialMaxBalance = ethers.utils.parseEther('2')
describe('TeleportrDeposit', async () => { describe('TeleportrDeposit', async () => {
let teleportrDeposit: Contract let signer1: SignerWithAddress
let signer: Signer let signer2: SignerWithAddress
let signer2: Signer
let contractAddress: string
let signerAddress: string
let signer2Address: string
before(async () => { before(async () => {
;[signer, signer2] = await ethers.getSigners() ;[signer1, signer2] = await ethers.getSigners()
teleportrDeposit = await (
await ethers.getContractFactory('TeleportrDeposit')
).deploy(
initialMinDepositAmount,
initialMaxDepositAmount,
initialMaxBalance
)
contractAddress = teleportrDeposit.address
signerAddress = await signer.getAddress()
signer2Address = await signer2.getAddress()
}) })
let TeleportrDeposit: Contract
before(async () => {
TeleportrDeposit = await deploy('TeleportrDeposit', {
args: [
initialMinDepositAmount,
initialMaxDepositAmount,
initialMaxBalance,
],
})
})
describe('receive', async () => { describe('receive', async () => {
const oneETH = ethers.utils.parseEther('1.0') const oneETH = ethers.utils.parseEther('1.0')
const twoETH = ethers.utils.parseEther('2.0') const twoETH = ethers.utils.parseEther('2.0')
it('should revert if deposit amount is less than min amount', async () => { it('should revert if deposit amount is less than min amount', async () => {
await expect( await expect(
signer.sendTransaction({ signer1.sendTransaction({
to: contractAddress, to: TeleportrDeposit.address,
value: ethers.utils.parseEther('0.001'), value: ethers.utils.parseEther('0.001'),
}) })
).to.be.revertedWith('Deposit amount is too small') ).to.be.revertedWith('Deposit amount is too small')
}) })
it('should revert if deposit amount is greater than max amount', async () => { it('should revert if deposit amount is greater than max amount', async () => {
await expect( await expect(
signer.sendTransaction({ signer1.sendTransaction({
to: contractAddress, to: TeleportrDeposit.address,
value: ethers.utils.parseEther('1.1'), value: ethers.utils.parseEther('1.1'),
}) })
).to.be.revertedWith('Deposit amount is too big') ).to.be.revertedWith('Deposit amount is too big')
}) })
it('should emit EtherReceived if called by non-owner', async () => { it('should emit EtherReceived if called by non-owner', async () => {
await expect( await expect(
signer2.sendTransaction({ signer2.sendTransaction({
to: contractAddress, to: TeleportrDeposit.address,
value: oneETH, value: oneETH,
}) })
) )
.to.emit(teleportrDeposit, 'EtherReceived') .to.emit(TeleportrDeposit, 'EtherReceived')
.withArgs(BigNumber.from('0'), signer2Address, oneETH) .withArgs(BigNumber.from('0'), signer2.address, oneETH)
}) })
it('should increase the contract balance by deposit amount', async () => { it('should increase the contract balance by deposit amount', async () => {
await expect(await ethers.provider.getBalance(contractAddress)).to.equal( expect(
oneETH await ethers.provider.getBalance(TeleportrDeposit.address)
) ).to.equal(oneETH)
}) })
it('should emit EtherReceived if called by owner', async () => { it('should emit EtherReceived if called by owner', async () => {
await expect( await expect(
signer.sendTransaction({ signer1.sendTransaction({
to: contractAddress, to: TeleportrDeposit.address,
value: oneETH, value: oneETH,
}) })
) )
.to.emit(teleportrDeposit, 'EtherReceived') .to.emit(TeleportrDeposit, 'EtherReceived')
.withArgs(BigNumber.from('1'), signerAddress, oneETH) .withArgs(BigNumber.from('1'), signer1.address, oneETH)
}) })
it('should increase the contract balance by deposit amount', async () => { it('should increase the contract balance by deposit amount', async () => {
await expect(await ethers.provider.getBalance(contractAddress)).to.equal( expect(
twoETH await ethers.provider.getBalance(TeleportrDeposit.address)
) ).to.equal(twoETH)
}) })
it('should revert if deposit will exceed max balance', async () => { it('should revert if deposit will exceed max balance', async () => {
await expect( await expect(
signer.sendTransaction({ signer1.sendTransaction({
to: contractAddress, to: TeleportrDeposit.address,
value: initialMinDepositAmount, value: initialMinDepositAmount,
}) })
).to.be.revertedWith('Contract max balance exceeded') ).to.be.revertedWith('Contract max balance exceeded')
}) })
}) })
describe('withdrawBalance', async () => { describe('withdrawBalance', async () => {
let initialContractBalance: BigNumber let initialContractBalance: BigNumber
let initialSignerBalance: BigNumber let initialSignerBalance: BigNumber
before(async () => { before(async () => {
initialContractBalance = await ethers.provider.getBalance(contractAddress) initialContractBalance = await ethers.provider.getBalance(
initialSignerBalance = await ethers.provider.getBalance(signerAddress) TeleportrDeposit.address
)
initialSignerBalance = await signer1.getBalance()
}) })
it('should revert if called by non-owner', async () => { it('should revert if called by non-owner', async () => {
await expect( await expect(
teleportrDeposit.connect(signer2).withdrawBalance() TeleportrDeposit.connect(signer2).withdrawBalance()
).to.be.revertedWith('Ownable: caller is not the owner') ).to.be.revertedWith('Ownable: caller is not the owner')
}) })
it('should emit BalanceWithdrawn if called by owner', async () => { it('should emit BalanceWithdrawn if called by owner', async () => {
await expect(teleportrDeposit.withdrawBalance()) await expect(TeleportrDeposit.withdrawBalance())
.to.emit(teleportrDeposit, 'BalanceWithdrawn') .to.emit(TeleportrDeposit, 'BalanceWithdrawn')
.withArgs(signerAddress, initialContractBalance) .withArgs(signer1.address, initialContractBalance)
}) })
it('should leave the contract with zero balance', async () => { it('should leave the contract with zero balance', async () => {
await expect(await ethers.provider.getBalance(contractAddress)).to.equal( expect(
ethers.utils.parseEther('0') await ethers.provider.getBalance(TeleportrDeposit.address)
) ).to.equal(ethers.utils.parseEther('0'))
}) })
it('should credit owner with contract balance - fees', async () => { it('should credit owner with contract balance - fees', async () => {
const expSignerBalance = initialSignerBalance.add(initialContractBalance) const expSignerBalance = initialSignerBalance.add(initialContractBalance)
await expect( expect(await signer1.getBalance()).to.be.closeTo(
await ethers.provider.getBalance(signerAddress) expSignerBalance,
).to.be.closeTo(expSignerBalance, 10 ** 15) 10 ** 15
)
}) })
}) })
describe('setMinAmount', async () => { describe('setMinAmount', async () => {
const newMinDepositAmount = ethers.utils.parseEther('0.02') const newMinDepositAmount = ethers.utils.parseEther('0.02')
it('should revert if called by non-owner', async () => { it('should revert if called by non-owner', async () => {
await expect( await expect(
teleportrDeposit.connect(signer2).setMinAmount(newMinDepositAmount) TeleportrDeposit.connect(signer2).setMinAmount(newMinDepositAmount)
).to.be.revertedWith('Ownable: caller is not the owner') ).to.be.revertedWith('Ownable: caller is not the owner')
}) })
it('should emit MinDepositAmountSet if called by owner', async () => { it('should emit MinDepositAmountSet if called by owner', async () => {
await expect(teleportrDeposit.setMinAmount(newMinDepositAmount)) await expect(TeleportrDeposit.setMinAmount(newMinDepositAmount))
.to.emit(teleportrDeposit, 'MinDepositAmountSet') .to.emit(TeleportrDeposit, 'MinDepositAmountSet')
.withArgs(initialMinDepositAmount, newMinDepositAmount) .withArgs(initialMinDepositAmount, newMinDepositAmount)
}) })
it('should have updated minDepositAmount after success', async () => { it('should have updated minDepositAmount after success', async () => {
await expect(await teleportrDeposit.minDepositAmount()).to.be.eq( expect(await TeleportrDeposit.minDepositAmount()).to.be.eq(
newMinDepositAmount newMinDepositAmount
) )
}) })
}) })
describe('setMaxAmount', async () => { describe('setMaxAmount', async () => {
const newMaxDepositAmount = ethers.utils.parseEther('2') const newMaxDepositAmount = ethers.utils.parseEther('2')
it('should revert if called non-owner', async () => { it('should revert if called non-owner', async () => {
await expect( await expect(
teleportrDeposit.connect(signer2).setMaxAmount(newMaxDepositAmount) TeleportrDeposit.connect(signer2).setMaxAmount(newMaxDepositAmount)
).to.be.revertedWith('Ownable: caller is not the owner') ).to.be.revertedWith('Ownable: caller is not the owner')
}) })
it('should emit MaxDepositAmountSet if called by owner', async () => { it('should emit MaxDepositAmountSet if called by owner', async () => {
await expect(teleportrDeposit.setMaxAmount(newMaxDepositAmount)) await expect(TeleportrDeposit.setMaxAmount(newMaxDepositAmount))
.to.emit(teleportrDeposit, 'MaxDepositAmountSet') .to.emit(TeleportrDeposit, 'MaxDepositAmountSet')
.withArgs(initialMaxDepositAmount, newMaxDepositAmount) .withArgs(initialMaxDepositAmount, newMaxDepositAmount)
}) })
it('should have an updated maxDepositAmount after success', async () => { it('should have an updated maxDepositAmount after success', async () => {
await expect(await teleportrDeposit.maxDepositAmount()).to.be.eq( expect(await TeleportrDeposit.maxDepositAmount()).to.be.eq(
newMaxDepositAmount newMaxDepositAmount
) )
}) })
}) })
describe('setMaxBalance', async () => { describe('setMaxBalance', async () => {
const newMaxBalance = ethers.utils.parseEther('2000') const newMaxBalance = ethers.utils.parseEther('2000')
it('should revert if called by non-owner', async () => { it('should revert if called by non-owner', async () => {
await expect( await expect(
teleportrDeposit.connect(signer2).setMaxBalance(newMaxBalance) TeleportrDeposit.connect(signer2).setMaxBalance(newMaxBalance)
).to.be.revertedWith('Ownable: caller is not the owner') ).to.be.revertedWith('Ownable: caller is not the owner')
}) })
it('should emit MaxBalanceSet if called by owner', async () => { it('should emit MaxBalanceSet if called by owner', async () => {
await expect(teleportrDeposit.setMaxBalance(newMaxBalance)) await expect(TeleportrDeposit.setMaxBalance(newMaxBalance))
.to.emit(teleportrDeposit, 'MaxBalanceSet') .to.emit(TeleportrDeposit, 'MaxBalanceSet')
.withArgs(initialMaxBalance, newMaxBalance) .withArgs(initialMaxBalance, newMaxBalance)
}) })
it('should have an updated maxBalance after success', async () => { it('should have an updated maxBalance after success', async () => {
await expect(await teleportrDeposit.maxBalance()).to.be.eq(newMaxBalance) expect(await TeleportrDeposit.maxBalance()).to.be.eq(newMaxBalance)
}) })
}) })
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment