|
| 1 | +import gasCost from "./helpers/gasCost"; |
| 2 | +import { latestTimestamp } from "./helpers/latestTime"; |
| 3 | +import increaseTime from "./helpers/increaseTime"; |
| 4 | +import moment from "moment"; |
| 5 | +import expectThrow from "./helpers/expectThrow"; |
| 6 | + |
| 7 | +const SnapshotTest = artifacts.require("./test/SnapshotTest.sol"); |
| 8 | + |
| 9 | +const day = 24 * 3600; |
| 10 | + |
| 11 | +contract("Snapshot", accounts => { |
| 12 | + let value; |
| 13 | + |
| 14 | + const createSnapshot = async () => { |
| 15 | + const r = await value.createSnapshot(); |
| 16 | + assert.equal(r.logs.length, 1); |
| 17 | + assert.equal(r.logs[0].event, "SnapshotCreated"); |
| 18 | + return r.logs[0].args.snapshot; |
| 19 | + }; |
| 20 | + |
| 21 | + beforeEach(async () => { |
| 22 | + value = await SnapshotTest.new(); |
| 23 | + }); |
| 24 | + |
| 25 | + it("should deploy", async () => { |
| 26 | + console.log(`\tSnapshot took ${gasCost(value)}.`); |
| 27 | + }); |
| 28 | + |
| 29 | + it("should be initially unset", async () => { |
| 30 | + assert.isFalse(await value.hasValue.call()); |
| 31 | + }); |
| 32 | + |
| 33 | + it("should initially return default", async () => { |
| 34 | + assert.isFalse(await value.hasValue.call()); |
| 35 | + }); |
| 36 | + |
| 37 | + it("should initially return default", async () => { |
| 38 | + assert.equal(12, await value.getValue.call(12)); |
| 39 | + assert.equal(42, await value.getValue.call(42)); |
| 40 | + }); |
| 41 | + |
| 42 | + it("should create a snapshot", async () => { |
| 43 | + const r = await value.createSnapshot(); |
| 44 | + console.log(`\tcreateSnapshot took ${gasCost(r)}`); |
| 45 | + }); |
| 46 | + |
| 47 | + it("should set a new value", async () => { |
| 48 | + const r = await value.setValue(1234); |
| 49 | + console.log(`\tsetValue took ${gasCost(r)}`); |
| 50 | + |
| 51 | + assert.equal(1234, await value.getValue.call(12)); |
| 52 | + assert.isTrue(await value.hasValue.call()); |
| 53 | + }); |
| 54 | + |
| 55 | + it("should reset value", async () => { |
| 56 | + await value.setValue(1234); |
| 57 | + |
| 58 | + const r = await value.setValue(12345); |
| 59 | + console.log(`\tsetValue took ${gasCost(r)}`); |
| 60 | + |
| 61 | + assert.equal(12345, await value.getValue.call(12)); |
| 62 | + assert.isTrue(await value.hasValue.call()); |
| 63 | + }); |
| 64 | + |
| 65 | + it("should keep values in snapshots", async () => { |
| 66 | + const before = await createSnapshot(); |
| 67 | + await value.setValue(100); |
| 68 | + const middle = await createSnapshot(); |
| 69 | + await value.setValue(200); |
| 70 | + const after = await createSnapshot(); |
| 71 | + |
| 72 | + assert.isFalse(await value.hasValueAt.call(before)); |
| 73 | + assert.isTrue(await value.hasValueAt.call(middle)); |
| 74 | + assert.isTrue(await value.hasValueAt.call(after)); |
| 75 | + assert.equal(41, await value.getValueAt.call(before, 41)); |
| 76 | + assert.equal(100, await value.getValueAt.call(middle, 41)); |
| 77 | + assert.equal(200, await value.getValueAt.call(after, 41)); |
| 78 | + }); |
| 79 | + |
| 80 | + it("should create daily snapshots", async () => { |
| 81 | + const day0 = await value.snapshotAt.call(latestTimestamp() + 0 * day); |
| 82 | + const day1 = await value.snapshotAt.call(latestTimestamp() + 1 * day); |
| 83 | + const day2 = await value.snapshotAt.call(latestTimestamp() + 2 * day); |
| 84 | + const day3 = await value.snapshotAt.call(latestTimestamp() + 3 * day); |
| 85 | + await value.setValue(100); |
| 86 | + await increaseTime(moment.duration({ days: 1 })); |
| 87 | + await value.setValue(200); |
| 88 | + await increaseTime(moment.duration({ days: 1 })); |
| 89 | + await value.setValue(300); |
| 90 | + |
| 91 | + assert.equal(41, await value.getValueAt.call(day0, 41)); |
| 92 | + assert.equal(100, await value.getValueAt.call(day1, 41)); |
| 93 | + assert.equal(200, await value.getValueAt.call(day2, 41)); |
| 94 | + }); |
| 95 | + |
| 96 | + it("should throw when queried in the future", async () => { |
| 97 | + const day1 = await value.snapshotAt.call(latestTimestamp() + 1 * day); |
| 98 | + await expectThrow(value.getValueAt.call(day1, 41)); |
| 99 | + }); |
| 100 | +}); |
0 commit comments