This repository has been archived by the owner on Jun 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
73 lines (62 loc) · 2.13 KB
/
index.js
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import Web3 from "web3";
import PrivateKeyProvider from "truffle-privatekey-provider";
// this is exactly the same as ganache.sh file in localnode
const ACCOUNT_1_PRIV = "0x47be2b1589bb515b76b47c514be96b23cd60ee37e81d63c2ae9c92f7d7667e1a";
export const ACCOUNT_3_PRIV = "0x1ff8271bf14ac9bef0b641cced40dc2a7ebd2e37d8e16d25b4aa1911364219af";
export let web3;
export let lastSnapshotId = 1;
export function createWeb3Provider(privKey, providerUrl) {
const provider = new PrivateKeyProvider(privKey.replace("0x", ""), providerUrl);
return new Web3(provider);
}
export function cypressVisitWithWeb3(path = "") {
web3 = createWeb3Provider(ACCOUNT_3_PRIV, Cypress.env("ETH_PROVIDER"));
return cy
.then(() => {
cy.log(`Reverting blockchain to snapshot #${lastSnapshotId}`);
return restoreBlockchain(web3)(lastSnapshotId);
})
.then(() => saveBlockchain(web3)())
.then(r => {
cy.log(`Saving snapshot #${lastSnapshotId}`);
lastSnapshotId = parseInt(r.result, 16);
return cy.visit(path, {
onBeforeLoad: win => {
win.web3 = web3;
},
});
});
}
export function cypressVisitApp() {
cypressVisitWithWeb3();
cy.get(tid('wallets-continue')).contains('Continue').click();
}
// helper to generate quickly selector for data-test-ids
export function tid(id, rest = "") {
return `[data-test-id="${id}"]` + (rest ? ` ${rest}` : "");
}
export const promisify = func => async (...args) =>
new Promise((accept, reject) => func(...args, (error, result) => (error ? reject(error) : accept(result))));
export const rpcCommand = method => web3 => (...params) => {
return new Promise((resolve, reject) => {
web3.currentProvider.sendAsync(
{
jsonrpc: "2.0",
method,
params,
id: Date.now(),
},
(err, res) => {
if (err) {
reject(err);
} else {
resolve(res);
}
},
);
});
};
export const mineBlock = rpcCommand("evm_mine");
export const increaseTime = rpcCommand("evm_increaseTime");
export const saveBlockchain = rpcCommand("evm_snapshot");
export const restoreBlockchain = rpcCommand("evm_revert");