Skip to content

Instantly share code, notes, and snippets.

@dbuarque
Last active January 2, 2019 01:36
Show Gist options
  • Save dbuarque/dac0ae2e84fd7d6170d399c6f3fac083 to your computer and use it in GitHub Desktop.
Save dbuarque/dac0ae2e84fd7d6170d399c6f3fac083 to your computer and use it in GitHub Desktop.
TokenMock.spec.js
/* global artifacts, contract, it, assert, before, beforeEach, tronWeb, done */
/* eslint-disable prefer-reflect */
const wait = require('./helpers/wait')
const TokenMock = artifacts.require('TokenMock.sol')
const TOKEN_NAME = 'TRONWALLET'
const TOKEN_SYMBOL = 'TWX'
const TOKEN_UNIT = 1000000
const TOKEN_TOTAL_SUPPLY = 20000000000
const TOKEN_DECIMALS = 6
const INVALID_ADDRESS = '0x0'
contract('TokenMock', (accounts) => {
let token
before(async () => {
token = await TokenMock.deployed()
if (accounts.length < 3) {
// Set your own accounts if you are not using Tron Quickstart
}
})
it('should verifies the token symbol after construction', async function () {
let symbol = await token.symbol()
assert.equal(symbol, TOKEN_SYMBOL)
})
it('should start with the correct decimals', async function () {
const decimals = await token.decimals()
assert.equal(decimals, TOKEN_DECIMALS)
})
it('should verifies the initial total supply', async function () {
const totalSupply = await token.totalSupply()
assert.equal(totalSupply.toString(), TOKEN_TOTAL_SUPPLY * TOKEN_UNIT)
})
it('should start with the correct cap', async function () {
const cap = await token.cap()
const totalSupply = await token.totalSupply()
assert.equal(cap.toString(), totalSupply.toString())
})
it('should verifies the token symbol after construction', async function () {
let name = await token.call('name')
assert.equal(name, TOKEN_NAME)
})
it('should verifies the balances after a transfer', async () => {
await token.transfer(accounts[1], 500 * TOKEN_UNIT)
const balanceAcc0 = await token.balanceOf(accounts[0])
assert.equal(balanceAcc0.toString(), 19999999500000000)
const balanceAcc1 = await token.balanceOf(accounts[1])
assert.equal(balanceAcc1.toString(), 500 * TOKEN_UNIT)
})
it('should verifies that a transfer fires a Transfer event', async () => {
return new Promise(async (resolve, reject) => {
try {
const _token = await tronWeb.contract().at(token.address)
wait(3)
const watcher = await _token.Transfer().watch((err, res) => {
if (err) throw err
if (res) {
assert.equal(res.name, 'Transfer')
assert.equal(res.result.from, tronWeb.address.toHex(accounts[0]))
assert.equal(res.result.to, tronWeb.address.toHex(accounts[1]))
assert.equal(res.result.value, 500 * TOKEN_UNIT)
watcher.stop()
resolve()
}
})
await token.transfer(accounts[1], 500 * TOKEN_UNIT)
} catch (e) {
reject(e)
}
})
})
it('should verifies the allowance after an approval', async () => {
await token.approve(accounts[1], 500 * TOKEN_UNIT)
let allowance = await token.allowance.call(accounts[0], accounts[1])
assert.equal(allowance, 500 * TOKEN_UNIT)
})
it('should verifies that an approval fires an Approval event', async () => {
return new Promise(async (resolve, reject) => {
try {
const _token = await tronWeb.contract().at(token.address)
wait(3)
const watcher = await _token.Approval().watch((err, res) => {
if (err) throw err
if (res) {
assert.equal(res.name, 'Approval')
assert.equal(tronWeb.address.fromHex(token.address), res.contract)
assert.equal(res.result.owner, tronWeb.address.toHex(accounts[0]))
assert.equal(res.result.spender, tronWeb.address.toHex(accounts[1]))
assert.equal(res.result.value, 500 * TOKEN_UNIT)
watcher.stop()
resolve()
}
})
await token.approve(accounts[1], 500 * TOKEN_UNIT)
} catch (e) {
reject(e)
}
})
})
it('should verifies the balances after transferring from another account', async () => {
await token.approve(accounts[1], 500 * TOKEN_UNIT)
await token.transferFrom(accounts[0], accounts[2], 50 * TOKEN_UNIT, { from: accounts[1] })
let balance = await token.balanceOf(accounts[0])
assert.equal(balance.toString(), 19999998950000000)
balance = await token.balanceOf(accounts[1])
assert.equal(balance.toString(), 1000000000)
balance = await token.balanceOf(accounts[2])
assert.equal(balance.toString(), 50 * TOKEN_UNIT)
})
it('should verifies that transferring from another account fires a Transfer event', async () => {
return new Promise(async (resolve, reject) => {
try {
const _token = await tronWeb.contract().at(token.address)
wait(3)
const watcher = await _token.Transfer().watch((err, res) => {
if (err) throw err
if (res) {
assert.equal(res.name, 'Transfer')
assert.equal(res.result.from, tronWeb.address.toHex(accounts[0]))
assert.equal(res.result.to, tronWeb.address.toHex(accounts[2]))
assert.equal(res.result.value, 50 * TOKEN_UNIT)
watcher.stop()
resolve()
}
})
await token.approve(accounts[1], 500 * TOKEN_UNIT)
await token.transferFrom(accounts[0], accounts[2], 50 * TOKEN_UNIT, { from: accounts[1] })
} catch (e) {
reject(e)
}
})
})
it('should verifies the new allowance after transferring from another account', async () => {
await token.approve(accounts[1], 500 * TOKEN_UNIT)
await token.transferFrom(accounts[0], accounts[2], 50 * TOKEN_UNIT, { from: accounts[1] })
let allowance = await token.allowance.call(accounts[0], accounts[1])
assert.equal(allowance, 450 * TOKEN_UNIT)
})
it('should throw when attempting to transfer from another account more than the allowance', async () => {
try {
await token.approve(accounts[1], 100 * TOKEN_UNIT)
await token.transferFrom(accounts[0], accounts[2], 200 * TOKEN_UNIT, { from: accounts[1] })
assert(false, "didn't throw attempting to transfer from another account more than the allowance")
} catch (error) {
assert.equal(error.message, "didn't throw attempting to transfer from another account more than the allowance")
}
})
it('should fail to mint if the ammount exceeds the cap', async function () {
try {
await token.mint(accounts[1], 10000, { from: accounts[1], shouldPollResponse: true })
assert(false, "didn't throw to mint if the ammount exceeds the cap")
} catch (error) {
assert.equal(error, 'REVERT opcode executed')
}
})
it('should throw when attempting to transfer more than the balance', async () => {
try {
await token.transfer(accounts[2], 50000 * TOKEN_UNIT, { from: accounts[1], shouldPollResponse: true })
assert(false, "didn't throw when attempting to transfer more than the balance")
} catch (error) {
assert.equal(error, 'REVERT opcode executed')
}
})
it('should throw when attempting to transfer to an invalid address', async () => {
try {
await token.transfer(INVALID_ADDRESS, 100000 * TOKEN_UNIT, { from: accounts[1] })
assert(false, "didn't throw when attempting to define allowance for an invalid address")
} catch (error) {
assert.equal(error.reason, 'invalid address')
}
})
it('should throw when attempting to transfer from an invalid account', async () => {
try {
await token.approve(accounts[1], 100)
await token.transferFrom(INVALID_ADDRESS, accounts[2], 50, { from: accounts[1] })
assert(false, "didn't throw when attempting to transfer from an invalid account")
} catch (error) {
assert.equal(error.reason, 'invalid address')
}
})
it('should throw when attempting to transfer from to an invalid account', async () => {
try {
await token.approve(accounts[1], 100)
await token.transferFrom(accounts[0], INVALID_ADDRESS, 50, { from: accounts[1] })
assert(false, "didn't throw when attempting to transfer from to an invalid account")
} catch (error) {
assert.equal(error.reason, 'invalid address')
}
})
it('should throw when attempting to define allowance for an invalid address', async () => {
try {
await token.approve(INVALID_ADDRESS, 10, { shouldPollResponse: true })
assert(false, "didn't throw when attempting to define allowance for an invalid address")
} catch (error) {
assert.equal(error.reason, 'invalid address')
}
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment