-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSmartContrarBlockNap.js
70 lines (60 loc) · 2.43 KB
/
SmartContrarBlockNap.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
const Web3 = require('web3');
const fs = require('fs');
const solc = require('solc');
const propertiesRaw = fs.readFileSync('properties.json');
const pro = JSON.parse(propertiesRaw);
console.log("provider:"+pro.host)
var web3 = new Web3(new Web3.providers.HttpProvider(pro.host));
console.log("web3_version:"+web3.version);
exports.openAccount = function openAccount() {
const account = web3.eth.accounts.privateKeyToAccount('0x' + pro.private);
web3.eth.accounts.wallet.add(account);
web3.eth.defaultAccount = account.address;
console.log("account.address:https://ropsten.etherscan.io/address/"+account.address)
console.log("gas:"+web3.utils.hexToNumber(pro.gas)+" Limit")
console.log("gasPrice:"+web3.utils.hexToNumber(pro.gasPrice)+" Wei")
}
exports.process = function process(info, res) {
var compile = compileSol();
newContract(info,res,compile);
}
function compileSol() {
var dateInit = new Date();
console.log("> Compiling new contract")
var compile = {};
var input = fs.readFileSync(pro.fileSol);
var output = solc.compile(input.toString(), 1);
var dateEnd = new Date();
console.log("> End compiling contract:"+(dateEnd - dateInit) / 1000+ "seconds");
var contract = output.contracts;
var bytecode = contract[pro.nameSC].bytecode;
var abi = JSON.parse(contract[pro.nameSC].interface);
console.log("abi:"+contract[pro.nameSC].interface);
compile.abi = abi;
compile.bytecode = bytecode;
return compile;
}
function newContract(info, res, compile) {
var dateInit = new Date();
console.log("> Creating new contract")
const contract = new web3.eth.Contract(compile.abi);
contract.deploy({
data: '0x' + compile.bytecode,
arguments: [info.date, info.issuer,info.receiver,info.subject]
})
.send({
from: web3.eth.defaultAccount,
gas: pro.gas,
gasPrice: pro.gasPrice
})
.on('error', function(error){
res.json({'address':'','status':'ERROR','error':error})
})
.then(function(newContractInstance){
var contractAdress = newContractInstance.options.address;
var dateEnd = new Date();
console.log("> End creating contract:"+(dateEnd - dateInit) / 1000+ "seconds");
console.log("contract:https://ropsten.etherscan.io/address/"+contractAdress);
res.json({'address':contractAdress,'status':'OK','error':''})
});
}