-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path09.fast-forward.ava.ts
54 lines (42 loc) · 2.02 KB
/
09.fast-forward.ava.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
import {NearAccount, getNetworkFromEnv} from 'near-workspaces';
import anyTest, {TestFn} from 'ava';
import {Worker} from 'near-workspaces/dist/worker';
// The contract provided contains only one view call, returning the
// block_timestamp and epoch_height of the current block as a tuple.
// Source is here <https://github.com/near/near-workspaces-rs/blob/main/examples/simple-contract/src/lib.rs>
const contract_wasm = '__tests__/build/debug/simple_contract.wasm';
// Represents the timestamp and epoch_height result from the view call.
type EnvData = [number, number];
if (getNetworkFromEnv() === 'sandbox') {
const test = anyTest as TestFn<{
worker: Worker;
contract: NearAccount;
}>;
test.beforeEach(async t => {
const worker = await Worker.init();
const root = worker.rootAccount;
const contract = await root.devDeploy(contract_wasm);
t.context.worker = worker;
t.context.contract = contract;
});
test.afterEach.always(async t => {
await t.context.worker.tearDown().catch(error => {
console.log('Failed to tear down the worker:', error);
});
});
test('Fast Forward', async t => {
const before = await t.context.contract.view('current_env_data');
const env_before = before as EnvData;
console.log(`Before: timestamp = ${env_before[0]}, epoch_height = ${env_before[1]}`);
const forward_height = 10_000;
// Call into fastForward. This will take a bit of time to invoke, but is
// faster than manually waiting for the same amounts of blocks to be produced
await t.context.worker.provider.fastForward(forward_height);
const after = await t.context.contract.view('current_env_data');
const env_after = after as EnvData;
console.log(`After: timestamp = ${env_after[0]}, epoch_height = ${env_after[1]}`);
const block = await t.context.worker.provider.block({finality: 'final'});
// Rounding off to nearest hundred, providing wiggle room incase not perfectly `forward_height`
t.true(Math.ceil(block.header.height / 100) * 100 === forward_height);
});
}