-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🍶 Implement mocking receive function to revert (#807)
- Loading branch information
Showing
7 changed files
with
187 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@ethereum-waffle/mock-contract": patch | ||
--- | ||
|
||
🍶 Implement mocking receive function to revert |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import {waffleChai} from '@ethereum-waffle/chai'; | ||
import {MockProvider} from '@ethereum-waffle/provider'; | ||
import {expect, use} from 'chai'; | ||
import {Contract, ContractFactory, Wallet} from 'ethers'; | ||
import {deployMockContract} from '../src'; | ||
|
||
import EtherForward from './helpers/interfaces/EtherForward.json'; | ||
import IERC20 from './helpers/interfaces/IERC20.json'; | ||
|
||
use(waffleChai); | ||
|
||
describe('Ether Forwarded', () => { | ||
let contractFactory: ContractFactory; | ||
let sender: Wallet; | ||
let mockERC20: Contract; | ||
let contract: Contract; | ||
let provider: MockProvider; | ||
|
||
beforeEach(async () => { | ||
provider = new MockProvider() | ||
;[sender] = provider.getWallets(); | ||
mockERC20 = await deployMockContract(sender, IERC20.abi); | ||
contractFactory = new ContractFactory(EtherForward.abi, EtherForward.bytecode, sender); | ||
contract = await contractFactory.deploy(mockERC20.address); | ||
}); | ||
|
||
it('Can forward ether through call', async () => { | ||
expect(await provider.getBalance(mockERC20.address)).to.be.equal(0); | ||
await contract.forwardByCall({value: 7}); | ||
expect(await provider.getBalance(mockERC20.address)).to.be.equal(7); | ||
}); | ||
|
||
it('Can forward ether through send', async () => { | ||
expect(await provider.getBalance(mockERC20.address)).to.be.equal(0); | ||
await contract.forwardBySend({value: 7}); | ||
expect(await provider.getBalance(mockERC20.address)).to.be.equal(7); | ||
}); | ||
|
||
it('Can forward ether through transfer', async () => { | ||
expect(await provider.getBalance(mockERC20.address)).to.be.equal(0); | ||
await contract.forwardByTransfer({value: 7}); | ||
expect(await provider.getBalance(mockERC20.address)).to.be.equal(7); | ||
}); | ||
|
||
it('Can mock a revert on a receive function', async () => { | ||
expect(await provider.getBalance(mockERC20.address)).to.be.equal(0); | ||
|
||
await mockERC20.mock.receive.revertsWithReason('Receive function rejected ether.'); | ||
|
||
await expect( | ||
contract.forwardByCall({value: 7}) | ||
).to.be.revertedWith('Receive function rejected ether.'); | ||
|
||
await expect( | ||
contract.forwardBySend({value: 7}) | ||
).to.be.revertedWith('forwardBySend failed'); | ||
|
||
await expect( | ||
contract.forwardByTransfer({value: 7}) | ||
).to.be.reverted; | ||
|
||
expect(await provider.getBalance(mockERC20.address)).to.be.equal(0); | ||
}); | ||
}); |
34 changes: 34 additions & 0 deletions
34
waffle-mock-contract/test/helpers/contracts/EtherForward.sol
This file contains 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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
pragma solidity ^0.6.3; | ||
|
||
interface IERC20 { | ||
function balanceOf(address account) external view returns (uint256); | ||
fallback() external payable; | ||
receive() external payable; | ||
} | ||
|
||
contract EtherForward { | ||
IERC20 private tokenContract; | ||
|
||
constructor (IERC20 _tokenContract) public { | ||
tokenContract = _tokenContract; | ||
} | ||
|
||
function forwardByCall() public payable { | ||
(bool sent, bytes memory data) = payable(tokenContract).call{value: msg.value}(""); | ||
if (!sent) { | ||
// https://ethereum.stackexchange.com/a/114140/24330 | ||
// Bubble up the revert from the call. | ||
assembly { | ||
revert(add(data, 32), data) | ||
} | ||
} | ||
} | ||
|
||
function forwardBySend() public payable { | ||
require(payable(tokenContract).send(msg.value), "forwardBySend failed"); | ||
} | ||
|
||
function forwardByTransfer() public payable { | ||
payable(tokenContract).transfer(msg.value); | ||
} | ||
} |