Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #140 from ethereum-optimism/m/fix-dummy-contract
Browse files Browse the repository at this point in the history
Fix Deposit Feed test
  • Loading branch information
protolambda committed Jan 21, 2022
2 parents d5f9046 + 819b368 commit 6cf5e20
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 61 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ node_modules
yarn-error.log
coverage.out

# hardhat automatically puts console.sol in here when you use it
.deps
10 changes: 7 additions & 3 deletions packages/contracts/contracts/test/DummyContract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ pragma solidity ^0.8.10;
* For use in testing with a call from a contract rather than an EOA.
*/
contract Dummy {
error Failed();

/**
* Forwards a call.
* @param _target Address to call
* @param _data Data to forward
*/
function forward(address _target, bytes calldata _data) external payable {
(bool success, bytes memory ret) = _target.call{ value: msg.value }(_data);
uint256 amount = address(this).balance;
(bool success, ) = _target.call{ value: amount }(_data);
// Silence the 'Return value of low-level calls not used' warning.
success;
ret;
if (!success) {
revert Failed();
}
}
}
200 changes: 142 additions & 58 deletions packages/contracts/test/L1/DepositFeed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const decodeDepositEvent = async (
const events = await depositFeed.queryFilter(
depositFeed.filters.TransactionDeposited()
)

const eventArgs = events[events.length - 1].args

return {
Expand Down Expand Up @@ -63,19 +64,22 @@ describe('DepositFeed', () => {

describe('Should emit the correct log values...', async () => {
it('when an EOA deposits a transaction with 0 value.', async () => {
await depositFeed.depositTransaction(
ZERO_ADDRESS,
ZERO_BIGNUMBER,
NON_ZERO_GASLIMIT,
false,
NON_ZERO_DATA
)
const receipt = await (
await depositFeed.depositTransaction(
NON_ZERO_ADDRESS,
ZERO_BIGNUMBER,
NON_ZERO_GASLIMIT,
false,
NON_ZERO_DATA
)
).wait()
await expect(receipt.status).to.equal(1)

const eventArgs = await decodeDepositEvent(depositFeed)

expect(eventArgs).to.deep.equal({
from: signerAddress,
to: ZERO_ADDRESS,
to: NON_ZERO_ADDRESS,
mint: ZERO_BIGNUMBER,
value: ZERO_BIGNUMBER,
gasLimit: NON_ZERO_GASLIMIT,
Expand All @@ -84,14 +88,48 @@ describe('DepositFeed', () => {
})
})

it('when a contract deposits a transaction with 0 value.', async () => {
// Deploy a dummy contract so we can impersonate it
const dummy = await (await ethers.getContractFactory('Dummy')).deploy()
await dummy.deployed()

await expect(
dummy.forward(
depositFeed.address,
depositFeed.interface.encodeFunctionData('depositTransaction', [
NON_ZERO_ADDRESS,
ZERO_BIGNUMBER,
NON_ZERO_GASLIMIT,
false,
NON_ZERO_DATA,
])
)
).to.not.be.reverted

const eventArgs = await decodeDepositEvent(depositFeed)

expect(eventArgs).to.deep.equal({
from: applyL1ToL2Alias(dummy.address),
to: NON_ZERO_ADDRESS,
value: ZERO_BIGNUMBER,
mint: ZERO_BIGNUMBER,
gasLimit: NON_ZERO_GASLIMIT,
isCreation: false,
data: NON_ZERO_DATA,
})
})

it('when an EOA deposits a contract creation with 0 value.', async () => {
await depositFeed.depositTransaction(
ZERO_ADDRESS,
ZERO_BIGNUMBER,
NON_ZERO_GASLIMIT,
true,
NON_ZERO_DATA
)
const receipt = await (
await depositFeed.depositTransaction(
ZERO_ADDRESS,
ZERO_BIGNUMBER,
NON_ZERO_GASLIMIT,
true,
NON_ZERO_DATA
)
).wait()
await expect(receipt.status).to.equal(1)

const eventArgs = await decodeDepositEvent(depositFeed)

Expand All @@ -111,16 +149,19 @@ describe('DepositFeed', () => {
const dummy = await (await ethers.getContractFactory('Dummy')).deploy()
await dummy.deployed()

await dummy.forward(
depositFeed.address,
depositFeed.interface.encodeFunctionData('depositTransaction', [
ZERO_ADDRESS,
ZERO_BIGNUMBER,
NON_ZERO_GASLIMIT,
true,
NON_ZERO_DATA,
])
)
const receipt = await (
await dummy.forward(
depositFeed.address,
depositFeed.interface.encodeFunctionData('depositTransaction', [
ZERO_ADDRESS,
ZERO_BIGNUMBER,
NON_ZERO_GASLIMIT,
true,
NON_ZERO_DATA,
])
)
).wait()
await expect(receipt.status).to.equal(1)

const eventArgs = await decodeDepositEvent(depositFeed)

Expand All @@ -138,16 +179,20 @@ describe('DepositFeed', () => {
describe('and increase its eth balance...', async () => {
it('when an EOA deposits a transaction with an ETH value.', async () => {
const balBefore = await ethers.provider.getBalance(depositFeed.address)
await depositFeed.depositTransaction(
NON_ZERO_ADDRESS,
ZERO_BIGNUMBER,
NON_ZERO_GASLIMIT,
false,
'0x',
{
value: NON_ZERO_VALUE,
}
)
const receipt = await (
await depositFeed.depositTransaction(
NON_ZERO_ADDRESS,
ZERO_BIGNUMBER,
NON_ZERO_GASLIMIT,
false,
'0x',
{
value: NON_ZERO_VALUE,
}
)
).wait()
await expect(receipt.status).to.equal(1)

const balAfter = await ethers.provider.getBalance(depositFeed.address)

const eventArgs = await decodeDepositEvent(depositFeed)
Expand All @@ -164,18 +209,54 @@ describe('DepositFeed', () => {
})
})

it('when a contract deposits a transaction with an ETH value.', async () => {
// Deploy a dummy contract so we can impersonate it
const dummy = await (await ethers.getContractFactory('Dummy')).deploy()
await dummy.deployed()
await expect(
dummy.forward(
depositFeed.address,
depositFeed.interface.encodeFunctionData('depositTransaction', [
NON_ZERO_ADDRESS,
ZERO_BIGNUMBER,
NON_ZERO_GASLIMIT,
false,
NON_ZERO_DATA,
]),
{
value: NON_ZERO_VALUE,
}
)
).to.not.be.reverted

const eventArgs = await decodeDepositEvent(depositFeed)

expect(eventArgs).to.deep.equal({
from: applyL1ToL2Alias(dummy.address),
to: NON_ZERO_ADDRESS,
value: ZERO_BIGNUMBER,
mint: NON_ZERO_VALUE,
gasLimit: NON_ZERO_GASLIMIT,
isCreation: false,
data: NON_ZERO_DATA,
})
})

it('when an EOA deposits a contract creation with an ETH value.', async () => {
const balBefore = await ethers.provider.getBalance(depositFeed.address)
await depositFeed.depositTransaction(
ZERO_ADDRESS,
ZERO_BIGNUMBER,
NON_ZERO_GASLIMIT,
true,
'0x',
{
value: NON_ZERO_VALUE,
}
)
const receipt = await (
await depositFeed.depositTransaction(
ZERO_ADDRESS,
ZERO_BIGNUMBER,
NON_ZERO_GASLIMIT,
true,
'0x',
{
value: NON_ZERO_VALUE,
}
)
).wait()
await expect(receipt.status).to.equal(1)

const balAfter = await ethers.provider.getBalance(depositFeed.address)
const eventArgs = await decodeDepositEvent(depositFeed)
Expand All @@ -198,19 +279,22 @@ describe('DepositFeed', () => {
await dummy.deployed()

const balBefore = await ethers.provider.getBalance(depositFeed.address)
await dummy.forward(
depositFeed.address,
depositFeed.interface.encodeFunctionData('depositTransaction', [
ZERO_ADDRESS,
ZERO_BIGNUMBER,
NON_ZERO_GASLIMIT,
true,
NON_ZERO_DATA,
]),
{
value: NON_ZERO_VALUE,
}
)
const receipt = await (
await dummy.forward(
depositFeed.address,
depositFeed.interface.encodeFunctionData('depositTransaction', [
ZERO_ADDRESS,
ZERO_BIGNUMBER,
NON_ZERO_GASLIMIT,
true,
NON_ZERO_DATA,
]),
{
value: NON_ZERO_VALUE,
}
)
).wait()
await expect(receipt.status).to.equal(1)

const balAfter = await ethers.provider.getBalance(depositFeed.address)
const eventArgs = await decodeDepositEvent(depositFeed)
Expand Down

0 comments on commit 6cf5e20

Please sign in to comment.