Skip to content
This repository was archived by the owner on Jun 7, 2021. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
waffle-ovm.json
optimism-integration
optimism

# Logs
logs
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Getting Started with the Optimistic Ethereum: Simple ERC20 Token Waffle Tutorial

[![Discord](https://img.shields.io/discord/667044843901681675.svg?color=768AD4&label=discord&logo=https%3A%2F%2Fdiscordapp.com%2Fassets%2F8c9701b98ad4372b58f13fd9f65f966e.svg)](https://discord.com/channels/667044843901681675)
[![Twitter Follow](https://img.shields.io/twitter/follow/optimismPBC.svg?label=optimismPBC&style=social)](https://twitter.com/optimismPBC)

Hi there! Welcome to our Optimistic Ethereum ERC20 Waffle example!

If your preferred smart contract testing framework is Truffle, see our Optimistic Ethereum ERC20 Truffle tutorial [here](https://github.com/ethereum-optimism/Truffle-ERC20-Example).
Expand Down Expand Up @@ -67,10 +70,10 @@ Here, `build-ovm` signifies that the contracts contained in this directory have

## Step 2: Testing your Optimistic Ethereum contracts

Testing with Waffle is easy. We've included a simple set of ERC20 tests inside [`Waffle-ERC20-Example/test/erc20.spec.js`](https://github.com/ethereum-optimism/Waffle-ERC20-Example/blob/main/test/erc20.test.js). Let's run these tests with `waffle`:
Testing with Waffle is easy. We've included a simple set of ERC20 tests inside [`Waffle-ERC20-Example/test/erc20.test.js`](https://github.com/ethereum-optimism/Waffle-ERC20-Example/blob/main/test/erc20.test.js). Let's run these tests with `waffle`:

```sh
yarn mocha 'test/*.spec.js' --timeout 10000
yarn mocha 'test/*.test.js' --timeout 10000
```

If everything went well, you should see a bunch of green checkmarks.
Expand Down Expand Up @@ -114,7 +117,7 @@ With your local instance of Optimistic Ethereum up and running, let's test your
To do that, run:

```sh
yarn TARGET=OVM mocha 'test/*.spec.js' --timeout 50000
yarn TARGET=OVM mocha 'test/*.test.js' --timeout 50000
```

Notice that we use the `TARGET=OVM` flag to let `mocha` know that we want to use the `build-ovm` folder as our path to our JSON files.
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"version": "0.0.1-alpha.1",
"description": "Basic example of how to test a basic token contract with Waffle in the OVM",
"scripts": {
"clean": "rimraf build"
"clean": "rimraf build",
"ci-setup": "yarn add @eth-optimism/solc@0.7.6-alpha.1"
},
"keywords": [
"optimism",
Expand All @@ -25,7 +26,6 @@
},
"devDependencies": {
"chai": "^4.3.4",
"dotenv": "^8.2.0",
"ethereum-waffle": "^3.0.0",
"mocha": "^7.0.1",
"rimraf": "^2.6.3"
Expand Down
38 changes: 24 additions & 14 deletions test/erc20.spec.js → test/erc20.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* External imports */
require('dotenv/config')
const { use, expect } = require('chai')
const { ethers } = require('ethers')
const { solidity } = require('ethereum-waffle')
Expand All @@ -19,7 +18,8 @@ describe('ERC20 smart contract', () => {
walletToAddress,
walletEmptyAddress

const privateKey = ethers.Wallet.createRandom().privateKey
const privateKey1 = ethers.Wallet.createRandom().privateKey
const privateKey2 = ethers.Wallet.createRandom().privateKey
const privateKeyEmpty = ethers.Wallet.createRandom().privateKey
const useL2 = (process.env.TARGET === 'OVM')

Expand All @@ -29,19 +29,15 @@ describe('ERC20 smart contract', () => {
provider = new ethers.providers.JsonRpcProvider('http://0.0.0.0:9545')
}

wallet = new ethers.Wallet(
'0x754fde3f5e60ef2c7649061e06957c29017fe21032a8017132c0078e37f6193a',
provider
)
walletTo = new ethers.Wallet(privateKey, provider)
wallet = new ethers.Wallet(privateKey1, provider)
walletTo = new ethers.Wallet(privateKey2, provider)
walletEmpty = new ethers.Wallet(privateKeyEmpty, provider)

// parameters to use for our test coin
const COIN_NAME = 'OVM Test Coin'
const TICKER = 'OVM'
const NUM_DECIMALS = 1


describe('when using a deployed contract instance', () => {
before(async () => {
walletAddress = await wallet.getAddress()
Expand All @@ -57,7 +53,9 @@ describe('ERC20 smart contract', () => {

ERC20 = await Factory__ERC20
.connect(wallet)
.deploy(1000, COIN_NAME, NUM_DECIMALS, TICKER)
.deploy(1000, COIN_NAME, NUM_DECIMALS, TICKER, {
gasPrice: 0
})

ERC20.deployTransaction.wait()
})
Expand All @@ -79,36 +77,48 @@ describe('ERC20 smart contract', () => {


it('should transfer amount to destination account', async () => {
const tx = await ERC20.connect(wallet).transfer(walletToAddress, 7)
const tx = await ERC20.connect(wallet).transfer(walletToAddress, 7, {
gasPrice: 0
})
await tx.wait()
const walletToBalance = await ERC20.balanceOf(walletToAddress)
expect(walletToBalance.toString()).to.equal('7')
})

it('should emit Transfer event', async () => {
const tx = ERC20.connect(wallet).transfer(walletToAddress, 7)
const tx = ERC20.connect(wallet).transfer(walletToAddress, 7, {
gasPrice: 0
})
await expect(tx)
.to.emit(ERC20, 'Transfer')
.withArgs(walletAddress, walletToAddress, 7)
})

// Setting `gasPrice` to 0 causes the transfer txns to fail.
it('should not transfer above the amount', async () => {
const walletToBalanceBefore = await ERC20.balanceOf(walletToAddress)
const tx = await ERC20.transfer(walletToAddress, 1007)
const tx = await ERC20.transfer(walletToAddress, 1007, {
gasPrice: 0
})
const walletToBalanceAfter = await ERC20.balanceOf(walletToAddress)
expect(walletToBalanceBefore).to.eq(walletToBalanceAfter)
})

// Setting `gasPrice` to 0 causes the transfer txns to fail.
it('should not transfer from empty account', async () => {
if (useL2 == true) {
const walletToBalanceBefore = await ERC20.balanceOf(walletEmptyAddress)
const ERC20FromOtherWallet = ERC20.connect(walletEmpty)
const tx = await ERC20FromOtherWallet.transfer(walletEmptyAddress, 1)
const tx = await ERC20FromOtherWallet.transfer(walletEmptyAddress, 1, {
gasPrice: 0
})
const walletToBalanceAfter = await ERC20.balanceOf(walletEmptyAddress)
expect(walletToBalanceBefore).to.eq(walletToBalanceAfter)
} else {
const ERC20FromOtherWallet = ERC20.connect(walletTo)
const tx = ERC20FromOtherWallet.transfer(walletAddress, 1)
const tx = ERC20FromOtherWallet.transfer(walletAddress, 1, {
gasPrice: 0
})
await expect(tx).to.be.reverted
}
})
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2169,11 +2169,6 @@ dom-walk@^0.1.0:
resolved "https://registry.yarnpkg.com/dom-walk/-/dom-walk-0.1.2.tgz#0c548bef048f4d1f2a97249002236060daa3fd84"
integrity sha512-6QvTW9mrGeIegrFXdtQi9pk7O/nSK6lSdXW2eqUspN5LWD7UTji2Fqw5V2YLjBpHEoU9Xl/eUWNpDeZvoyOv2w==

dotenv@^8.2.0:
version "8.2.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-8.2.0.tgz#97e619259ada750eea3e4ea3e26bceea5424b16a"
integrity sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==

dotignore@~0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/dotignore/-/dotignore-0.1.2.tgz#f942f2200d28c3a76fbdd6f0ee9f3257c8a2e905"
Expand Down