Skip to content
This repository was archived by the owner on Feb 18, 2020. It is now read-only.

Commit c0accc8

Browse files
authored
Merge pull request #51 from gnosis/refactor/testrpc-time
Refactor/testrpc time
2 parents 5ab3663 + 19b9eea commit c0accc8

File tree

13 files changed

+178
-44
lines changed

13 files changed

+178
-44
lines changed

contracts/DutchExchange.sol

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,13 @@ contract DutchExchange {
263263

264264
// --- For Testing only! ---
265265

266-
uint256 public now = 1508473469;
267-
function increaseTimeBy(uint256 byHours, uint256 bySeconds) public {
268-
now += byHours * 1 hours;
269-
now += bySeconds;
270-
}
271-
272-
function setTime(uint256 newTime) public {
273-
now = newTime;
274-
}
266+
// uint256 public now = 1508473469;
267+
// function increaseTimeBy(uint256 byHours, uint256 bySeconds) public {
268+
// now += byHours * 1 hours;
269+
// now += bySeconds;
270+
// }
271+
272+
// function setTime(uint256 newTime) public {
273+
// now = newTime;
274+
// }
275275
}

src/test/browser/ETH2GNO.test.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ declare module 'expect' {
88
}
99

1010
import { delay, metamaskWarning } from '../utils'
11+
import web3Utils from '../../../trufflescripts/utils'
1112

1213
import DXart from '../../../build/contracts/DutchExchangeETHGNO.json'
1314
import ETHart from '../../../build/contracts/TokenETH.json'
@@ -53,6 +54,7 @@ const withLocalProvider = async (func: () => void) => {
5354
setCurrentProvider()
5455
}
5556

57+
const { getTime, increaseTimeBy, setTime } = web3Utils(web3)
5658

5759
describe('ETH 2 GNO contract standalone', () => {
5860
// TODO: proper types
@@ -170,14 +172,10 @@ describe('ETH 2 GNO contract standalone', () => {
170172
const sellerGNOBalance = await gno.balanceOf(seller)
171173
const buyerGNOBalance = await gno.balanceOf(buyer)
172174

173-
expect(masterETHBalance.add(sellerETHBalance)).toEqual(ETHtotal)
174-
expect(masterGNOBalance.add(buyerGNOBalance)).toEqual(GNOtotal)
175-
176-
expect(sellerGNOBalance.toNumber()).toBe(0)
177-
expect(buyerETHBalance.toNumber()).toBe(0)
175+
expect(masterETHBalance.add(sellerETHBalance).add(buyerETHBalance)).toEqual(ETHtotal)
176+
expect(masterGNOBalance.add(sellerGNOBalance).add(buyerGNOBalance)).toEqual(GNOtotal)
178177
})
179178

180-
// TODO: rework to make a part of submit -> buy -> claim flow
181179
it('seller can submit order to an auction', async () => {
182180
const amount = 30
183181

@@ -217,16 +215,16 @@ describe('ETH 2 GNO contract standalone', () => {
217215
// still on the first auction
218216
expect(auctionIndex).toBe(1)
219217
const auctionStart = (await dx.auctionStart()).toNumber()
220-
let now = (await dx.now()).toNumber()
218+
let now = getTime()
221219

222220
// auction hasn't started yet
223221
expect(auctionStart).toBeGreaterThan(now)
224222
const timeUntilStart = auctionStart - now
225223

226224
await withLocalProvider(async () => {
227225
// move time to start + 1 hour
228-
await dx.increaseTimeBy(1, timeUntilStart, { from: master })
229-
now = (await dx.now()).toNumber()
226+
increaseTimeBy(timeUntilStart + 3600)
227+
now = getTime()
230228
})
231229

232230
// auction has started
@@ -351,7 +349,7 @@ describe('ETH 2 GNO contract standalone', () => {
351349
const timeWhenAuctionClears = Math.ceil(72000 * sellVolume / buyVolume - 18000 + auctionStart)
352350

353351
await withLocalProvider(async () => {
354-
await dx.setTime(timeWhenAuctionClears, { from: master })
352+
setTime(timeWhenAuctionClears)
355353
const buyerBalance = (await dx.buyerBalances(auctionIndex, buyer)).toNumber()
356354
const amount = 1
357355
await gno.approve(dxa, amount, { from: buyer })
@@ -378,7 +376,7 @@ describe('ETH 2 GNO contract standalone', () => {
378376
// still on the first auction
379377
expect(auctionIndex).toBe(2)
380378
const auctionStart = (await dx.auctionStart()).toNumber()
381-
const now = (await dx.now()).toNumber()
379+
const now = getTime()
382380

383381
// next auction hasn't started yet
384382
expect(auctionStart).toBeGreaterThan(now)

src/test/browser/ETH2GNO_Abstract.test.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import expect from 'expect'
22
import { delay, metamaskWarning } from '../utils'
3+
import web3Utils from '../../../trufflescripts/utils'
34

45
import { initDutchXConnection } from 'api/dutchx'
56
import DXart from '../../../build/contracts/DutchExchangeETHGNO.json'
@@ -20,6 +21,8 @@ const web3 = new Web3(localProvider)
2021
// Set ONLY DutchExchangeETHGNO to the localProvider
2122
DX.setProvider(localProvider)
2223

24+
const { getTime, increaseTimeBy, setTime } = web3Utils(web3)
25+
2326
describe('ETH 2 GNO contract via DutchX Class', () => {
2427
// TODO: proper types
2528
let dxClass: any
@@ -147,16 +150,17 @@ describe('ETH 2 GNO contract via DutchX Class', () => {
147150
const ETHtotal = await eth.getTotalSupply()
148151
const masterETHBalance = await eth.balanceOf(master)
149152
const sellerETHBalance = await eth.balanceOf(seller)
153+
const buyerETHBalance = await eth.balanceOf(buyer)
150154

151155
const GNOtotal = await gno.getTotalSupply()
152156
const masterGNOBalance = await gno.balanceOf(master)
157+
const sellerGNOBalance = await gno.balanceOf(seller)
153158
const buyerGNOBalance = await gno.balanceOf(buyer)
154159

155-
expect(masterETHBalance.add(sellerETHBalance)).toEqual(ETHtotal)
156-
expect(masterGNOBalance.add(buyerGNOBalance)).toEqual(GNOtotal)
160+
expect(masterETHBalance.add(sellerETHBalance).add(buyerETHBalance)).toEqual(ETHtotal)
161+
expect(masterGNOBalance.add(sellerGNOBalance).add(buyerGNOBalance)).toEqual(GNOtotal)
157162
})
158163

159-
// TODO: rework to make a part of submit -> buy -> claim flow
160164
it('seller can submit order to an auction', async () => {
161165
const amount = 30
162166

@@ -194,15 +198,15 @@ describe('ETH 2 GNO contract via DutchX Class', () => {
194198
// still on the first auction
195199
expect(auctionIndex).toBe(1)
196200
const auctionStart = (await dx.auctionStart()).toNumber()
197-
let now = (await dx.now()).toNumber()
201+
let now = getTime()
198202

199203
// auction hasn't started yet
200204
expect(auctionStart).toBeGreaterThan(now)
201205
const timeUntilStart = auctionStart - now
202206

203207
// move time to start + 1 hour
204-
await dx.increaseTimeBy(1, timeUntilStart, { from: master })
205-
now = (await dx.now()).toNumber()
208+
increaseTimeBy(timeUntilStart + 3600)
209+
now = getTime()
206210

207211
// auction has started
208212
expect(auctionStart).toBeLessThan(now)
@@ -278,8 +282,6 @@ describe('ETH 2 GNO contract via DutchX Class', () => {
278282
// balance is kept as a record, just can't be claimed twice
279283
expect(buyerBalance).toBe(buyerBalancesAfter)
280284
expect(buyVolumeAfter).toBe(buyVolume)
281-
282-
283285
})
284286

285287

@@ -322,7 +324,7 @@ describe('ETH 2 GNO contract via DutchX Class', () => {
322324
const timeWhenAuctionClears = Math.ceil(72000 * sellVolume / buyVolume - 18000 + auctionStart)
323325

324326

325-
await dx.setTime(timeWhenAuctionClears, { from: master })
327+
setTime(timeWhenAuctionClears)
326328
const buyerBalance = (await dx.buyerBalances(auctionIndex, buyer)).toNumber()
327329
const amount = 1
328330
await gno.approve(dxa, amount, { from: buyer })
@@ -345,7 +347,7 @@ describe('ETH 2 GNO contract via DutchX Class', () => {
345347
// still on the first auction
346348
expect(auctionIndex).toBe(2)
347349
const auctionStart = (await dx.auctionStart()).toNumber()
348-
const now = (await dx.now()).toNumber()
350+
const now = getTime()
349351

350352
// next auction hasn't started yet
351353
expect(auctionStart).toBeGreaterThan(now)

stories/TopAuctions.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ storiesOf(`TopAuctions`, module)
2626
.addDecorator(CenterDecor)
2727
.addWithJSX('5 random pairs', () => {
2828
const top5Pairs = getTop5Pairs(ratioPairs.map((pair, i) => object(`Pair ${i}`, pair)))
29-
return <TopAuctions pairs={top5Pairs} selectTokenPair={stringifyAction('SELECT_TOKEN_PAIR')} />
29+
return <TopAuctions pairs={top5Pairs} selectTokenPairAndSetClosingPrice={stringifyAction('SELECT_TOKEN_PAIR')} />
3030
})

test/ETH2GNO.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,15 @@ contract('ETH to GNO auctionz', async (accounts) => {
5252
const ETHtotal = await ETH.getTotalSupply()
5353
const initialiserETHBalance = await ETH.balanceOf(initialiser)
5454
const sellerETHBalance = await ETH.balanceOf(seller)
55+
const buyerETHBalance = await ETH.balanceOf(buyer)
5556

5657
const GNOtotal = await GNO.getTotalSupply()
5758
const initialiserGNOBalance = await GNO.balanceOf(initialiser)
59+
const sellerGNOBalance = await GNO.balanceOf(seller)
5860
const buyerGNOBalance = await GNO.balanceOf(buyer)
5961

60-
assert.deepEqual(initialiserETHBalance.add(sellerETHBalance), ETHtotal)
61-
assert.deepEqual(initialiserGNOBalance.add(buyerGNOBalance), GNOtotal)
62+
assert.deepEqual(initialiserETHBalance.add(sellerETHBalance).add(buyerETHBalance), ETHtotal)
63+
assert.deepEqual(initialiserGNOBalance.add(sellerGNOBalance).add(buyerGNOBalance), GNOtotal)
6264
})
6365

6466

trufflescripts/buy_order.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ module.exports = async () => {
7878

7979
[buyVolume, buyerBalance, buyerGNOBalance] = await buyerStats()
8080

81-
console.log(`now:
81+
console.log(` now:
8282
buyVolume:\t${buyVolume}
8383
buyerBalance:\t${buyerBalance} in auction
8484
\t\t\t${buyerGNOBalance} GNO in account

trufflescripts/get_auction_stats.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
const DutchExchangeETHGNO = artifacts.require('./DutchExchangeETHGNO.sol')
2+
const TokenETH = artifacts.require('./TokenETH.sol')
3+
const TokenGNO = artifacts.require('./TokenGNO.sol')
4+
const { getTime } = require('./utils')(web3)
25

36
const getTimeStr = (timestamp) => {
47
const date = new Date(Math.abs(timestamp))
@@ -16,8 +19,16 @@ const getTimeStr = (timestamp) => {
1619

1720
module.exports = async () => {
1821
const dx = await DutchExchangeETHGNO.deployed()
22+
const eth = await TokenETH.deployed()
23+
const gno = await TokenGNO.deployed()
24+
25+
const dxETHBalance = (await eth.balanceOf(dx.address)).toNumber()
26+
const dxGNOBalance = (await gno.balanceOf(dx.address)).toNumber()
27+
28+
console.log(`Auction holds:\t${dxETHBalance} ETH\t${dxGNOBalance} GNO`)
29+
1930
const auctionStart = (await dx.auctionStart()).toNumber()
20-
const now = (await dx.now()).toNumber()
31+
const now = getTime()
2132

2233
const timeUntilStart = auctionStart - now
2334
const timeStr = getTimeStr(timeUntilStart * 1000)

trufflescripts/increase_time.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const DutchExchangeETHGNO = artifacts.require('./DutchExchangeETHGNO.sol')
2+
const { getTime, increaseTimeBy, setTime } = require('./utils')(web3)
23

34
const argv = require('minimist')(process.argv.slice(2))
45

@@ -30,7 +31,7 @@ module.exports = async () => {
3031

3132
const printAuctionTimes = async () => {
3233
const auctionStart = (await dx.auctionStart()).toNumber()
33-
const now = (await dx.now()).toNumber()
34+
const now = getTime()
3435

3536
const timeUntilStart = auctionStart - now
3637
const timeStr = getTimeStr(timeUntilStart * 1000)
@@ -71,13 +72,13 @@ module.exports = async () => {
7172
console.log(`Setting time to ${argv.start ? 'AUCTION_START' : argv.clear ? 'AUCTION_END' : ''} ${incTimeBy ? `+ ${getTimeStr(incTimeBy * 1000)}` : ''}`)
7273

7374
if (argv.start) {
74-
await dx.setTime(auctionStart)
75+
setTime(auctionStart)
7576
} else if (argv.clear && timeWhenAuctionClears !== Infinity) {
76-
await dx.setTime(timeWhenAuctionClears)
77+
setTime(timeWhenAuctionClears)
7778
}
7879

7980
if (incTimeBy) {
80-
await dx.increaseTimeBy(0, incTimeBy)
81+
increaseTimeBy(incTimeBy)
8182
}
8283

8384
console.log('==========================')

trufflescripts/revert.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* eslint no-console:0 prefer-const:1 */
2+
const { revertSnapshot } = require('./utils')(web3)
3+
4+
const argv = require('minimist')(process.argv.slice(2), { string: 'a' })
5+
6+
/**
7+
* truffle exec trufflescripts/claim_funds.js
8+
* to claim funds for the current auction for both seller and buyer,
9+
* from auction's sellerBalances and buyerBalances respectively
10+
* @flags:
11+
* --seller sellerBalance for seller only
12+
* --buyer buyerBalance for buyer only
13+
* -a seller|buyer|<address> for the given address
14+
* -i <index> for auction with given index
15+
* --last for last auction
16+
*/
17+
18+
module.exports = async () => {
19+
let snapshotID
20+
21+
snapshotID = argv.b || '0x01'
22+
23+
const timeout = new Promise((resolve, reject) => setTimeout(() => reject(new Error('TIMED-OUT')), 1500))
24+
const race = Promise.race([timeout, revertSnapshot(snapshotID)])
25+
try {
26+
await race
27+
console.warn(`
28+
CAUTION: Reverting does NOT roll back time to snapshot time. You've been warned...
29+
`)
30+
console.log(`
31+
REVERTED TO SNAPSHOT-ID: # ${snapshotID}
32+
BLOCKNUMBER: ${web3.eth.blockNumber}
33+
`)
34+
} catch (e) {
35+
console.log(e)
36+
37+
// Due to lock in rpc, kill w/Node
38+
process.on('exit', () => {
39+
console.log('KILLING SCRIPT')
40+
})
41+
process.exit()
42+
}
43+
}

trufflescripts/snapshot.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const { makeSnapshot } = require('./utils')(web3)
2+
3+
/**
4+
* truffle exec trufflescripts/snapshot.js
5+
* Created snapshot of blockchain state and assigns a Block-ID
6+
* Block-ID can be reverted back to via revert.js
7+
*/
8+
9+
module.exports = () => {
10+
const snapshot = makeSnapshot()
11+
console.log(`
12+
SNAPSHOT CREATED: # ${snapshot}
13+
BLOCK-NUMBER: ${web3.eth.blockNumber}
14+
`)
15+
}

trufflescripts/start_auction.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1+
/* eslint no-console:0 */
12
const DutchExchangeETHGNO = artifacts.require('./DutchExchangeETHGNO.sol')
3+
const { getTime, increaseTimeBy } = require('./utils')(web3)
24

35
/**
46
* truffle exec trufflescripts/start_auction.js
57
* if auction isn't running,
68
* sets time to auction start + 1 hour
79
*/
810

11+
const hour = 3600
12+
913
module.exports = async () => {
1014
const dx = await DutchExchangeETHGNO.deployed()
1115
const auctionStart = (await dx.auctionStart()).toNumber()
12-
const now = (await dx.now()).toNumber()
16+
const now = getTime()
1317
const timeUntilStart = auctionStart - now
1418

1519
const auctionIndex = (await dx.auctionIndex()).toNumber()
1620

1721
// auctionStart is in the future
1822
if (timeUntilStart > 0) {
19-
await dx.increaseTimeBy(1, timeUntilStart)
23+
increaseTimeBy(timeUntilStart + hour)
2024
console.log(`ETH -> GNO auction ${auctionIndex} started`)
2125
} else {
2226
console.log(`ETH -> GNO auction ${auctionIndex} is already running`)

0 commit comments

Comments
 (0)