This repository has been archived by the owner on Jun 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
template.02_deploy_voting.js
104 lines (94 loc) · 3.35 KB
/
template.02_deploy_voting.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* eslint-disable require-jsdoc */
import fs from 'fs';
import os from 'os';
import path from 'path';
import Web3 from 'web3';
import Enigma from '../../src/Enigma';
import utils from '../../src/enigma-utils';
import VotingETHContract from '../../../build/contracts/VotingETH';
import * as eeConstants from '../../src/emitterConstants';
import {EnigmaContract, EnigmaTokenContract} from './contractLoader';
import * as constants from './testConstants';
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
describe('Enigma tests', () => {
let accounts;
let web3;
let enigma;
let votingETHContract;
let epochSize;
it('initializes', () => {
const provider = new Web3.providers.HttpProvider('http://localhost:9545');
web3 = new Web3(provider);
return web3.eth.getAccounts().then((result) => {
accounts = result;
enigma = new Enigma(
web3,
EnigmaContract.networks['4447'].address,
EnigmaTokenContract.networks['4447'].address,
'http://localhost:3346',
{
gas: 4712388,
gasPrice: 100000000000,
from: accounts[0],
},
);
enigma.admin();
expect(Enigma.version()).toEqual('0.0.1');
});
});
it('should generate and save key/pair', () => {
enigma.setTaskKeyPair('cupcake');
});
it('initializes VotingETH contract', async () => {
votingETHContract = new enigma.web3.eth.Contract(VotingETHContract['abi'],
VotingETHContract.networks['4447'].address);
expect(votingETHContract.options.address).toBeTruthy();
});
let scTask;
let task;
const homedir = os.homedir();
it('should deploy secret contract', async () => {
let scTaskFn = `construct(address)`;
let scTaskArgs = [
[votingETHContract.options.address, 'address'],
];
let scTaskGasLimit = 4000000;
let scTaskGasPx = utils.toGrains(1);
let preCode;
try {
preCode = fs.readFileSync(path.resolve(__dirname,'secretContracts/voting.wasm'));
} catch(e) {
console.log('Error:', e.stack);
}
scTask = await new Promise((resolve, reject) => {
enigma.deploySecretContract(scTaskFn, scTaskArgs, scTaskGasLimit, scTaskGasPx, accounts[0], preCode)
.on(eeConstants.DEPLOY_SECRET_CONTRACT_RESULT, (receipt) => resolve(receipt))
.on(eeConstants.ERROR, (error) => reject(error));
});
fs.writeFile(path.join(homedir, '.enigma', 'addr-voting.txt'), scTask.scAddr, 'utf8', function(err) {
if(err) {
return console.log(err);
}
});
}, constants.TIMEOUT_DEPLOY);
it('should get the confirmed deploy contract task', async () => {
do {
await sleep(1000);
scTask = await enigma.getTaskRecordStatus(scTask);
process.stdout.write('Waiting. Current Task Status is '+scTask.ethStatus+'\r');
} while (scTask.ethStatus != 2);
expect(scTask.ethStatus).toEqual(2);
process.stdout.write('Completed. Final Task Status is '+scTask.ethStatus+'\n');
}, constants.TIMEOUT_DEPLOY);
it('should verify deployed contract', async () => {
const result = await enigma.admin.isDeployed(scTask.scAddr);
expect(result).toEqual(true);
});
it('should get deployed contract bytecode hash', async () => {
const result = await enigma.admin.getCodeHash(scTask.scAddr);
expect(result).toBeTruthy;
console.log('Deployed contract bytecode hash is: '+result);
});
});