Skip to content

Commit

Permalink
Improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
antoncoding committed Nov 24, 2020
1 parent 3abcbdc commit a28aba3
Showing 1 changed file with 49 additions and 23 deletions.
72 changes: 49 additions & 23 deletions test/unit-tests/trade0xCallee.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,31 @@ import {
} from '../../build/types/truffle-types'
import {createTokenAmount} from '../utils'
import BigNumber from 'bignumber.js'

const {expectRevert} = require('@openzeppelin/test-helpers')
const WETH9 = artifacts.require('WETH9.sol')
const MockERC20 = artifacts.require('MockERC20.sol')
const Mock0xExchange = artifacts.require('Mock0xExchange')
const Trade0x = artifacts.require('Trade0x.sol')

const OrderStruct = {
makerAddress: 'address',
takerAddress: 'address',
feeRecipientAddress: 'address',
senderAddress: 'address',

makerAssetAmount: 'uint256',
takerAssetAmount: 'uint256',
makerFee: 'uint256',
takerFee: 'uint256',
expirationTimeSeconds: 'uint256',
salt: 'uint256',

makerAssetData: 'bytes',
takerAssetData: 'bytes',
makerFeeAssetData: 'bytes',
takerFeeAssetData: 'bytes',
}

contract('Trade0xCallee', ([maker, payableProxy, controller, taker, staking]) => {
// ERC20 mocks
let weth: WETH9Instance
Expand All @@ -22,7 +41,7 @@ contract('Trade0xCallee', ([maker, payableProxy, controller, taker, staking]) =>
let data: string
let makerToken: MockERC20Instance
let takerToken: MockERC20Instance

let order: any
const fillAmount = new BigNumber('1000000')
const signature =
'0x1c698be1f76b87c5a7f8aed7836374fdacca342416862f8c393565037fa506a43e0b71f8b8f45b359bb534ef7074cc24bdb863a633751fefeadb5c22e24425ba6802'
Expand All @@ -43,7 +62,7 @@ contract('Trade0xCallee', ([maker, payableProxy, controller, taker, staking]) =>
// deploy AddressBook token
callee = await Trade0x.new(mockExchange.address, proxyAddr, weth.address, staking)

const order1 = {
order = {
expirationTimeSeconds: '1764547200',
feeRecipientAddress: '0x1000000000000000000000000000000000000011',
makerAddress: maker,
Expand All @@ -62,37 +81,21 @@ contract('Trade0xCallee', ([maker, payableProxy, controller, taker, staking]) =>
data = web3.eth.abi.encodeParameters(
[
{
Order: {
makerAddress: 'address',
takerAddress: 'address',
feeRecipientAddress: 'address',
senderAddress: 'address',

makerAssetAmount: 'uint256',
takerAssetAmount: 'uint256',
makerFee: 'uint256',
takerFee: 'uint256',
expirationTimeSeconds: 'uint256',
salt: 'uint256',

makerAssetData: 'bytes',
takerAssetData: 'bytes',
makerFeeAssetData: 'bytes',
takerFeeAssetData: 'bytes',
},
Order: OrderStruct,
},
'uint256',
'bytes',
'address',
],
[order1, fillAmount.toString(), signature, payableProxy], // pay weth from payable proxy
[order, fillAmount.toString(), signature, payableProxy], // pay weth from payable proxy
)
})

describe('Run Trade0xCallee directly', () => {
it('should take maker asset from maker', async () => {
it('should take taker asset from taker', async () => {
// taker needs to approve callee
await takerToken.approve(callee.address, createTokenAmount(10000, 8), {from: taker})
const takerAssetBefore = new BigNumber(await takerToken.balanceOf(taker))

// deposit some weth in payable proxy
const amountEth = createTokenAmount(1, 18)
Expand All @@ -103,6 +106,9 @@ contract('Trade0xCallee', ([maker, payableProxy, controller, taker, staking]) =>
// call action from the controller
await callee.callFunction(taker, data, {from: controller})

const takerAssetAfter = new BigNumber(await takerToken.balanceOf(taker))
assert.equal(takerAssetAfter.plus(fillAmount).toString(), takerAssetBefore.toString())

// check the data feed into 0x exchange is correct
const parsedSignature = await mockExchange.signature()
const parsedTakerAmount = await mockExchange.takerAmount()
Expand All @@ -111,5 +117,25 @@ contract('Trade0xCallee', ([maker, payableProxy, controller, taker, staking]) =>
assert.equal(parsedTakerAmount.toString(), fillAmount.toString())
assert.equal(parsedMakerAmount.toString(), '100000000')
})
it('should execute the same trade again when 0x already have allowance for TakerAsset', async () => {
await callee.callFunction(taker, data, {from: controller})
})
it('should revert if the data is wrongly encoded', async () => {
// deep copy order
const badOrder = JSON.parse(JSON.stringify(order))
badOrder.makerAssetData = makerToken.address
const badData = web3.eth.abi.encodeParameters(
[
{
Order: OrderStruct,
},
'uint256',
'bytes',
'address',
],
[badOrder, fillAmount.toString(), signature, payableProxy],
)
await expectRevert(callee.callFunction(taker, badData, {from: controller}), 'LENGTH_65_REQUIRED')
})
})
})

0 comments on commit a28aba3

Please sign in to comment.