-
Notifications
You must be signed in to change notification settings - Fork 2
/
deploy-contract.js
74 lines (48 loc) · 1.99 KB
/
deploy-contract.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
const Web3 = require('web3')
const compiler = require('./compiler')
let web3 = new Web3(Web3.givenProvider || "ws://localhost:33333");
//let web3 = new Web3("http://127.0.0.1:8545")
//let web3 = new Web3("http://127.0.0.1:7545")
let returnAccounts = async () => {
return await web3.eth.getAccounts((error,accounts) => {
})
}
let deploy = async (contractFileName, contractName, valueToSendToContract) => {
//TODO: add the support for passing arguments when creating the contract.
return await returnAccounts().then((accounts) => {
output = compiler(contractFileName)
var jsonInterface = JSON.parse(output.contracts[":"+contractName].interface)
var data = output.contracts[":"+contractName].bytecode
var contract = new web3.eth.Contract(jsonInterface, {gasPrice: '12345678', from: accounts[0], value: 30000000000000000000})
return {
data: data,
contract: contract,
accounts: accounts
}
}).then(res => {
data = res.data
contract = res.contract
accounts = res.accounts
if (valueToSendToContract) {
return contract.deploy({data: data}).send({
from: accounts[0],
gas: 3000000,
gasPrice: '3000000000',
value: valueToSendToContract
}, function(error, transactionHash){
}).then(deployedContract => {
return deployedContract.options.address
})
} else {
return contract.deploy({data: data}).send({
from: accounts[0],
gas: 3000000,
gasPrice: '3000000000',
}, function(error, transactionHash){
}).then(deployedContract => {
return deployedContract.options.address
})
}
})
}
module.exports = deploy;