erc20.spec.ts 2.95 KB
Newer Older
1 2
import { Contract, ContractFactory, Wallet } from 'ethers'
import { ethers } from 'hardhat'
Mark Tyneway's avatar
Mark Tyneway committed
3
import { TxGasLimit, TxGasPrice } from '@eth-optimism/core-utils'
4
import chai, { expect } from 'chai'
5 6
import { GWEI } from './shared/utils'
import { OptimismEnv } from './shared/env'
7 8 9
import { solidity } from 'ethereum-waffle'

chai.use(solidity)
10 11 12 13 14 15 16 17 18 19 20 21 22

describe('Basic ERC20 interactions', async () => {
  const initialAmount = 1000
  const tokenName = 'OVM Test'
  const tokenDecimals = 8
  const TokenSymbol = 'OVM'

  let wallet: Wallet
  let other: Wallet
  let Factory__ERC20: ContractFactory
  let ERC20: Contract

  before(async () => {
23 24
    const env = await OptimismEnv.new()
    wallet = env.l2Wallet
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    other = Wallet.createRandom().connect(ethers.provider)
    Factory__ERC20 = await ethers.getContractFactory('ERC20', wallet)
  })

  beforeEach(async () => {
    ERC20 = await Factory__ERC20.deploy(
      initialAmount,
      tokenName,
      tokenDecimals,
      TokenSymbol
    )
  })

  it('should set the total supply', async () => {
    const totalSupply = await ERC20.totalSupply()
    expect(totalSupply.toNumber()).to.equal(initialAmount)
  })

  it('should get the token name', async () => {
    const name = await ERC20.name()
    expect(name).to.equal(tokenName)
  })

  it('should get the token decimals', async () => {
    const decimals = await ERC20.decimals()
    expect(decimals).to.equal(tokenDecimals)
  })

  it('should get the token symbol', async () => {
    const symbol = await ERC20.symbol()
    expect(symbol).to.equal(TokenSymbol)
  })

  it('should assign initial balance', async () => {
    const balance = await ERC20.balanceOf(wallet.address)
    expect(balance.toNumber()).to.equal(initialAmount)
  })

  it('should transfer amount to destination account', async () => {
    const transfer = await ERC20.transfer(other.address, 100)
    const receipt = await transfer.wait()

67
    // The expected fee paid is the value returned by eth_estimateGas
Mark Tyneway's avatar
Mark Tyneway committed
68 69 70 71
    const gasLimit = await ERC20.estimateGas.transfer(other.address, 100)
    const gasPrice = await wallet.getGasPrice()
    expect(gasPrice).to.deep.equal(TxGasPrice)
    const expectedFeePaid = gasLimit.mul(gasPrice)
72

73
    // There are two events from the transfer with the first being
74
    // the ETH fee paid and the second of the value transfered (100)
75
    expect(receipt.events.length).to.equal(2)
76
    expect(receipt.events[0].args._value).to.deep.equal(expectedFeePaid)
77 78 79 80 81 82 83 84 85
    expect(receipt.events[1].args._from).to.equal(wallet.address)
    expect(receipt.events[1].args._value.toNumber()).to.equal(100)

    const receiverBalance = await ERC20.balanceOf(other.address)
    const senderBalance = await ERC20.balanceOf(wallet.address)

    expect(receiverBalance.toNumber()).to.equal(100)
    expect(senderBalance.toNumber()).to.equal(900)
  })
86 87 88 89 90 91

  it('should revert if trying to transfer too much', async () => {
    await expect(
      ERC20.transfer(other.address, initialAmount * 2)
    ).to.be.revertedWith('insufficient balance')
  })
92
})