-
Notifications
You must be signed in to change notification settings - Fork 3.9k
maint(ct): clean up Teleportr tests #2473
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
150 changes: 87 additions & 63 deletions
150
packages/contracts/test/contracts/L1/teleportr/TeleportrDeposit.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,171 +1,195 @@ | ||
| /* External Imports */ | ||
| 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 { deploy } from '../../../helpers' | ||
|
|
||
| const initialMinDepositAmount = ethers.utils.parseEther('0.01') | ||
| const initialMaxDepositAmount = ethers.utils.parseEther('1') | ||
| const initialMaxBalance = ethers.utils.parseEther('2') | ||
|
|
||
| describe('TeleportrDeposit', async () => { | ||
| let teleportrDeposit: Contract | ||
| let signer: Signer | ||
| let signer2: Signer | ||
| let contractAddress: string | ||
| let signerAddress: string | ||
| let signer2Address: string | ||
| let signer1: SignerWithAddress | ||
| let signer2: SignerWithAddress | ||
| before(async () => { | ||
| ;[signer, 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() | ||
| ;[signer1, signer2] = await ethers.getSigners() | ||
| }) | ||
|
|
||
| let TeleportrDeposit: Contract | ||
| before(async () => { | ||
| TeleportrDeposit = await deploy('TeleportrDeposit', { | ||
| args: [ | ||
| initialMinDepositAmount, | ||
| initialMaxDepositAmount, | ||
| initialMaxBalance, | ||
| ], | ||
| }) | ||
| }) | ||
|
|
||
| describe('receive', async () => { | ||
| const oneETH = ethers.utils.parseEther('1.0') | ||
| const twoETH = ethers.utils.parseEther('2.0') | ||
|
|
||
| it('should revert if deposit amount is less than min amount', async () => { | ||
| await expect( | ||
| signer.sendTransaction({ | ||
| to: contractAddress, | ||
| signer1.sendTransaction({ | ||
| to: TeleportrDeposit.address, | ||
| value: ethers.utils.parseEther('0.001'), | ||
| }) | ||
| ).to.be.revertedWith('Deposit amount is too small') | ||
| }) | ||
|
|
||
| it('should revert if deposit amount is greater than max amount', async () => { | ||
| await expect( | ||
| signer.sendTransaction({ | ||
| to: contractAddress, | ||
| signer1.sendTransaction({ | ||
| to: TeleportrDeposit.address, | ||
| value: ethers.utils.parseEther('1.1'), | ||
| }) | ||
| ).to.be.revertedWith('Deposit amount is too big') | ||
| }) | ||
|
|
||
| it('should emit EtherReceived if called by non-owner', async () => { | ||
| await expect( | ||
| signer2.sendTransaction({ | ||
| to: contractAddress, | ||
| to: TeleportrDeposit.address, | ||
| value: oneETH, | ||
| }) | ||
| ) | ||
| .to.emit(teleportrDeposit, 'EtherReceived') | ||
| .withArgs(BigNumber.from('0'), signer2Address, oneETH) | ||
| .to.emit(TeleportrDeposit, 'EtherReceived') | ||
| .withArgs(BigNumber.from('0'), signer2.address, oneETH) | ||
| }) | ||
|
|
||
| it('should increase the contract balance by deposit amount', async () => { | ||
| await expect(await ethers.provider.getBalance(contractAddress)).to.equal( | ||
| oneETH | ||
| ) | ||
| expect( | ||
| await ethers.provider.getBalance(TeleportrDeposit.address) | ||
| ).to.equal(oneETH) | ||
| }) | ||
|
|
||
| it('should emit EtherReceived if called by owner', async () => { | ||
| await expect( | ||
| signer.sendTransaction({ | ||
| to: contractAddress, | ||
| signer1.sendTransaction({ | ||
| to: TeleportrDeposit.address, | ||
| value: oneETH, | ||
| }) | ||
| ) | ||
| .to.emit(teleportrDeposit, 'EtherReceived') | ||
| .withArgs(BigNumber.from('1'), signerAddress, oneETH) | ||
| .to.emit(TeleportrDeposit, 'EtherReceived') | ||
| .withArgs(BigNumber.from('1'), signer1.address, oneETH) | ||
| }) | ||
|
|
||
| it('should increase the contract balance by deposit amount', async () => { | ||
| await expect(await ethers.provider.getBalance(contractAddress)).to.equal( | ||
| twoETH | ||
| ) | ||
| expect( | ||
| await ethers.provider.getBalance(TeleportrDeposit.address) | ||
| ).to.equal(twoETH) | ||
| }) | ||
|
|
||
| it('should revert if deposit will exceed max balance', async () => { | ||
| await expect( | ||
| signer.sendTransaction({ | ||
| to: contractAddress, | ||
| signer1.sendTransaction({ | ||
| to: TeleportrDeposit.address, | ||
| value: initialMinDepositAmount, | ||
| }) | ||
| ).to.be.revertedWith('Contract max balance exceeded') | ||
| }) | ||
| }) | ||
|
|
||
| describe('withdrawBalance', async () => { | ||
| let initialContractBalance: BigNumber | ||
| let initialSignerBalance: BigNumber | ||
| before(async () => { | ||
| initialContractBalance = await ethers.provider.getBalance(contractAddress) | ||
| initialSignerBalance = await ethers.provider.getBalance(signerAddress) | ||
| initialContractBalance = await ethers.provider.getBalance( | ||
| TeleportrDeposit.address | ||
| ) | ||
| initialSignerBalance = await signer1.getBalance() | ||
| }) | ||
|
|
||
| it('should revert if called by non-owner', async () => { | ||
| await expect( | ||
| teleportrDeposit.connect(signer2).withdrawBalance() | ||
| TeleportrDeposit.connect(signer2).withdrawBalance() | ||
| ).to.be.revertedWith('Ownable: caller is not the owner') | ||
| }) | ||
|
|
||
| it('should emit BalanceWithdrawn if called by owner', async () => { | ||
| await expect(teleportrDeposit.withdrawBalance()) | ||
| .to.emit(teleportrDeposit, 'BalanceWithdrawn') | ||
| .withArgs(signerAddress, initialContractBalance) | ||
| await expect(TeleportrDeposit.withdrawBalance()) | ||
| .to.emit(TeleportrDeposit, 'BalanceWithdrawn') | ||
| .withArgs(signer1.address, initialContractBalance) | ||
| }) | ||
|
|
||
| it('should leave the contract with zero balance', async () => { | ||
| await expect(await ethers.provider.getBalance(contractAddress)).to.equal( | ||
| ethers.utils.parseEther('0') | ||
| ) | ||
| expect( | ||
| await ethers.provider.getBalance(TeleportrDeposit.address) | ||
| ).to.equal(ethers.utils.parseEther('0')) | ||
| }) | ||
|
|
||
| it('should credit owner with contract balance - fees', async () => { | ||
| const expSignerBalance = initialSignerBalance.add(initialContractBalance) | ||
| await expect( | ||
| await ethers.provider.getBalance(signerAddress) | ||
| ).to.be.closeTo(expSignerBalance, 10 ** 15) | ||
| expect(await signer1.getBalance()).to.be.closeTo( | ||
| expSignerBalance, | ||
| 10 ** 15 | ||
| ) | ||
| }) | ||
| }) | ||
|
|
||
| describe('setMinAmount', async () => { | ||
| const newMinDepositAmount = ethers.utils.parseEther('0.02') | ||
|
|
||
| it('should revert if called by non-owner', async () => { | ||
| await expect( | ||
| teleportrDeposit.connect(signer2).setMinAmount(newMinDepositAmount) | ||
| TeleportrDeposit.connect(signer2).setMinAmount(newMinDepositAmount) | ||
| ).to.be.revertedWith('Ownable: caller is not the owner') | ||
| }) | ||
|
|
||
| it('should emit MinDepositAmountSet if called by owner', async () => { | ||
| await expect(teleportrDeposit.setMinAmount(newMinDepositAmount)) | ||
| .to.emit(teleportrDeposit, 'MinDepositAmountSet') | ||
| await expect(TeleportrDeposit.setMinAmount(newMinDepositAmount)) | ||
| .to.emit(TeleportrDeposit, 'MinDepositAmountSet') | ||
| .withArgs(initialMinDepositAmount, newMinDepositAmount) | ||
| }) | ||
|
|
||
| it('should have updated minDepositAmount after success', async () => { | ||
| await expect(await teleportrDeposit.minDepositAmount()).to.be.eq( | ||
| expect(await TeleportrDeposit.minDepositAmount()).to.be.eq( | ||
| newMinDepositAmount | ||
| ) | ||
| }) | ||
| }) | ||
|
|
||
| describe('setMaxAmount', async () => { | ||
| const newMaxDepositAmount = ethers.utils.parseEther('2') | ||
| it('should revert if called non-owner', async () => { | ||
| await expect( | ||
| teleportrDeposit.connect(signer2).setMaxAmount(newMaxDepositAmount) | ||
| TeleportrDeposit.connect(signer2).setMaxAmount(newMaxDepositAmount) | ||
| ).to.be.revertedWith('Ownable: caller is not the owner') | ||
| }) | ||
|
|
||
| it('should emit MaxDepositAmountSet if called by owner', async () => { | ||
| await expect(teleportrDeposit.setMaxAmount(newMaxDepositAmount)) | ||
| .to.emit(teleportrDeposit, 'MaxDepositAmountSet') | ||
| await expect(TeleportrDeposit.setMaxAmount(newMaxDepositAmount)) | ||
| .to.emit(TeleportrDeposit, 'MaxDepositAmountSet') | ||
| .withArgs(initialMaxDepositAmount, newMaxDepositAmount) | ||
| }) | ||
|
|
||
| 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 | ||
| ) | ||
| }) | ||
| }) | ||
|
|
||
| describe('setMaxBalance', async () => { | ||
| const newMaxBalance = ethers.utils.parseEther('2000') | ||
|
|
||
| it('should revert if called by non-owner', async () => { | ||
| await expect( | ||
| teleportrDeposit.connect(signer2).setMaxBalance(newMaxBalance) | ||
| TeleportrDeposit.connect(signer2).setMaxBalance(newMaxBalance) | ||
| ).to.be.revertedWith('Ownable: caller is not the owner') | ||
| }) | ||
|
|
||
| it('should emit MaxBalanceSet if called by owner', async () => { | ||
| await expect(teleportrDeposit.setMaxBalance(newMaxBalance)) | ||
| .to.emit(teleportrDeposit, 'MaxBalanceSet') | ||
| await expect(TeleportrDeposit.setMaxBalance(newMaxBalance)) | ||
| .to.emit(TeleportrDeposit, 'MaxBalanceSet') | ||
| .withArgs(initialMaxBalance, newMaxBalance) | ||
| }) | ||
|
|
||
| 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) | ||
| }) | ||
| }) | ||
| }) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.