Skip to content

Commit

Permalink
Merge pull request makerdao#9 from OasisDEX/kk/escrow
Browse files Browse the repository at this point in the history
  • Loading branch information
krzkaczor authored Mar 10, 2021
2 parents a0f801b + 5afd2c5 commit a5ab2e8
Show file tree
Hide file tree
Showing 11 changed files with 876 additions and 19 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,9 @@ yarn
yarn build
yarn test-e2e # runs unit tests
```

## Development

Run `yarn test:fix` to run linting in fix mode, auto-formatting and unit tests.

Running `yarn test` makes sure that contracts are compiled. Running `yarn test-e2e` doesn't.
9 changes: 6 additions & 3 deletions contracts/l1/L1ERC20Deposit.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ contract L1ERC20Deposit is Abs_L1TokenGateway {
********************************/

iOVM_ERC20 public l1ERC20;
address public escrow;

/***************
* Constructor *
Expand All @@ -25,9 +26,11 @@ contract L1ERC20Deposit is Abs_L1TokenGateway {
constructor(
iOVM_ERC20 _l1ERC20,
address _l2DepositedERC20,
address _l1messenger
address _l1messenger,
address _escrow
) Abs_L1TokenGateway(_l2DepositedERC20, _l1messenger) {
l1ERC20 = _l1ERC20;
escrow = _escrow;
}

/**************
Expand All @@ -48,7 +51,7 @@ contract L1ERC20Deposit is Abs_L1TokenGateway {
uint256 _amount
) internal override {
// Hold on to the newly deposited funds
l1ERC20.transferFrom(_from, address(this), _amount);
l1ERC20.transferFrom(_from, escrow, _amount);
}

/**
Expand All @@ -60,6 +63,6 @@ contract L1ERC20Deposit is Abs_L1TokenGateway {
*/
function _handleFinalizeWithdrawal(address _to, uint256 _amount) internal override {
// Transfer withdrawn funds out to withdrawer
l1ERC20.transfer(_to, _amount);
l1ERC20.transferFrom(escrow, _to, _amount);
}
}
19 changes: 19 additions & 0 deletions contracts/test/externals.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
pragma solidity >0.5.0 <0.8.0;
pragma experimental ABIEncoderV2;

/**
ONLY PURPOSE OF THIS FILE IS TO FORCE HARDHAT TO COMPILE CONTRACT FROM @eth-optmism/contracts.
*/

import {
OVM_L1CrossDomainMessenger
} from '@eth-optimism/contracts/build/contracts/OVM/bridge/messaging/OVM_L1CrossDomainMessenger.sol';
import {
OVM_L2CrossDomainMessenger
} from '@eth-optimism/contracts/build/contracts/OVM/bridge/messaging/OVM_L2CrossDomainMessenger.sol';

contract OVM_L1CrossDomainMessengerLocal is OVM_L1CrossDomainMessenger {}

contract OVM_L2CrossDomainMessengerLocal is OVM_L2CrossDomainMessenger {
constructor(address _libAddressManager) public OVM_L2CrossDomainMessenger(_libAddressManager) {}
}
1 change: 1 addition & 0 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import '@nomiclabs/hardhat-web3'

import '@eth-optimism/plugins/hardhat/compiler'
import '@eth-optimism/plugins/hardhat/ethers'
import '@eth-optimism/smock/build/src/plugins/hardhat-storagelayout'

const config: HardhatUserConfig = {
mocha: {
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@
"clean": "hardhat clean",
"format": "prettier --check \"./**/*.ts\"",
"format:fix": "prettier --write \"./**/*.ts\"",
"lint": "eslint --ext .ts test",
"lint": "eslint --ext .ts test test-e2e",
"lint:fix": "yarn lint --fix",
"typecheck": "tsc --noEmit",
"test": "mocha --config .mocharc.js",
"test": "hardhat test",
"test-e2e": "mocha --config .mocharc-e2e.js",
"test:fix": "yarn lint:fix && yarn format:fix && yarn test && yarn typecheck"
},
"dependencies": {
"@eth-optimism/contracts": "^0.1.10",
"@eth-optimism/plugins": "^0.0.16",
"@eth-optimism/smock": "^0.2.1-alpha.0",
"@eth-optimism/solc": "0.5.16-alpha.7",
"@eth-optimism/watcher": "^0.0.1-alpha.9",
"@nomiclabs/hardhat-ethers": "=2.0.1",
"@nomiclabs/hardhat-waffle": "=2.0.1",
"@nomiclabs/hardhat-web3": "^2.0.0",
"@openzeppelin/contracts": "^3.4.0",
"@openzeppelin/test-helpers": "^0.5.10",
"@types/chai": "^4.2.12",
Expand All @@ -46,7 +48,6 @@
"prettier": "^2.2.1",
"ts-node": "^9.0.0",
"typescript": "^4.0.2",
"@nomiclabs/hardhat-web3": "^2.0.0",
"web3": "^1.3.4"
}
}
17 changes: 8 additions & 9 deletions test-e2e/bridge.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Wallet } from '@ethersproject/wallet'
import { expect } from 'chai'
import { Contract, providers } from 'ethers'
import { Contract } from 'ethers'
import { ethers as l1, l2ethers as l2 } from 'hardhat'

import { optimismConfig } from './helpers/optimismConfig'
import {
deployContract,
printRollupStatus,
MAX_UINT256,
q18,
setupTest,
waitForTx,
Expand All @@ -15,8 +15,8 @@ import {
} from './helpers/utils'

describe('bridge', () => {
let l1Provider: providers.BaseProvider
let l1Signer: Wallet
let l1Escrow: Wallet
let l2Signer: Wallet
let watcher: any

Expand All @@ -27,7 +27,7 @@ describe('bridge', () => {
const initialL1DaiNumber = q18(10000)

beforeEach(async () => {
;({ l1Provider, l1Signer, l2Signer, watcher } = await setupTest())
;({ l1Signer, l2Signer, watcher, l1User: l1Escrow } = await setupTest())
l1Dai = await deployContract(l1Signer, await l1.getContractFactory('Dai'), [])
console.log('L1 DAI: ', l1Dai.address)
await waitForTx(l1Dai.mint(l1Signer.address, initialL1DaiNumber))
Expand All @@ -46,7 +46,9 @@ describe('bridge', () => {
l1Dai.address,
l2Minter.address,
optimismConfig.Proxy__OVM_L1CrossDomainMessenger,
l1Escrow.address,
])
await l1Dai.connect(l1Escrow).approve(l1DaiDeposit.address, MAX_UINT256)
console.log('L1 DAI Deposit: ', l1DaiDeposit.address)

await waitForTx(l2Minter.init(l1DaiDeposit.address))
Expand All @@ -67,17 +69,14 @@ describe('bridge', () => {
await waitForTx(l1Dai.approve(l1DaiDeposit.address, depositAmount))
await waitToRelayTxsToL2(l1DaiDeposit.deposit(depositAmount), watcher)

await printRollupStatus(l1Provider)
const balance = await l2Dai.balanceOf(l1Signer.address)
expect(balance.toString()).to.be.eq(depositAmount)

await printRollupStatus(l1Provider)
await waitForTx(l2Dai.approve(l2Minter.address, depositAmount))

await waitToRelayMessageToL1(l2Minter.withdraw(depositAmount), watcher)

const balanceAfterWithdrawal = await l2Dai.balanceOf(l1Signer.address)
expect(balanceAfterWithdrawal.toString()).to.be.eq('0')
const l2BalanceAfterWithdrawal = await l2Dai.balanceOf(l1Signer.address)
expect(l2BalanceAfterWithdrawal.toString()).to.be.eq('0')
const l1Balance = await l1Dai.balanceOf(l1Signer.address)
expect(l1Balance.toString()).to.be.eq(initialL1DaiNumber)
})
Expand Down
7 changes: 6 additions & 1 deletion test-e2e/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export function q18(n: number) {
return ethers.BigNumber.from(10).pow(18).mul(n).toString()
}

export const MAX_UINT256 = ethers.BigNumber.from(2).pow(256).sub(1)

export const DUMMY_ADDRESS = '0x' + '1234'.repeat(10)

export async function waitToRelayTxsToL2(l1OriginatingTx: Promise<any>, watcher: any) {
Expand Down Expand Up @@ -73,20 +75,22 @@ export async function setupTest(): Promise<{
l1Provider: providers.BaseProvider
l2Provider: providers.BaseProvider
l1Signer: Wallet
l1User: Wallet
l2Signer: Wallet
watcher: any
}> {
const randomWallets = getRandomWallets(3)

const l1Provider = getL1Provider()
const l1Admin = getAdminWallet().connect(l1Provider)
const [l1Deployer] = connectWallets(randomWallets, l1Provider)
const [l1Deployer, l1User] = connectWallets(randomWallets, l1Provider)

const l2Provider = getL2Provider()
const [l2Deployer] = connectWallets(randomWallets, l2Provider)

console.log('Seeding L1 account')
await waitForTx(l1Admin.sendTransaction({ value: ethers.utils.parseEther('1'), to: l1Deployer.address }))
await waitForTx(l1Admin.sendTransaction({ value: ethers.utils.parseEther('1'), to: l1User.address }))

const watcher = new Watcher({
l1: {
Expand All @@ -103,6 +107,7 @@ export async function setupTest(): Promise<{
l1Provider,
l2Provider,
l1Signer: l1Deployer,
l1User,
l2Signer: l2Deployer,
watcher,
}
Expand Down
16 changes: 16 additions & 0 deletions test/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ethers } from 'hardhat'
export const makeHexString = (byte: string, len: number): string => {
return '0x' + byte.repeat(len)
}

export const makeAddress = (byte: string): string => {
return makeHexString(byte, 20)
}

export const MAX_UINT256 = ethers.BigNumber.from(2).pow(256).sub(1)
export const ZERO_ADDRESS = makeAddress('00')
export const NON_ZERO_ADDRESS = makeAddress('11')

export function q18(n: number) {
return ethers.BigNumber.from(10).pow(18).mul(n).toString()
}
Loading

0 comments on commit a5ab2e8

Please sign in to comment.