-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGovernmentContract.js
121 lines (106 loc) · 5.58 KB
/
GovernmentContract.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const solc = require('solc');
const fs = require('fs');
const path = require('path');
let getWeb3 = require('./utils/getWeb3.js'); // returns promise object containing web3 connection to a node
// the default value to store in the contract during construction/deployment
const defaultValue = 1000;
// This serves as a js wrapper class for interacting with the GovernmentContract.sol smart contract
class GovernmentContract {
// Instantiates the class, declares some simple properties, and gets web3 communication objects.
constructor() {
this.node1 = getWeb3(0);
this.node2 = getWeb3(1); // this node is not used in this example
this.compiled = false;
this.contractAddress = "";
}
// Compiles the smart contract in preparation of deployment. Compile must be called before
// deploying the contract or interacting with its methods. This js object needs the compiled
// contracts "abi" (application binary interface) in order to interact with it.
compile() {
// Check if it is already compiled
if (!this.compiled) {
// Find the file and read it into memory
let filePath = path.resolve(__dirname, 'contracts', 'GovernmentContract.sol');
let fileSource = fs.readFileSync(filePath, 'UTF-8');
// Compile the contract with the contract source and 1 for optimization
let compiled = solc.compile(fileSource, 1);
// Store the bytecode, as we need it for deployment
this.bytecode = "0x" + compiled.contracts[':governmentcontract'].bytecode;
// Store the abi (application binary interface) for js to interface with the contract on the chain
let contractInterface = compiled.contracts[':governmentcontract'].interface; // this is a json object that web3 uses as an interface
this.abi = JSON.parse(contractInterface);
this.compiled = true;
}
}
// This will initialize and deploy a contract if there is no contract address passed in
// contractAddress should be a valid Ethereum Address containing an instance of this deployed contract
deploy(contractAddress=null) {
// if this isn't compiled yet, we need to compile it for the bytecode and web3 interface (abi)
if (!this.compiled) {
this.compile();
}
// if there is an address passed in then assume that it is already deployed
if (contractAddress !== null) {
this.contractAddress = contractAddress;
return this.node1.then((web3) => {
this.contractInterface = new web3.eth.Contract(this.abi, this.contractAddress);
return this.contractAddress;
});
}
// No address provided so let's deploy a new one onto the chain
return this.node1.then((web3) => {
let contract = new web3.eth.Contract(this.abi);
console.log("Getting default account from node1 to deploy new contract");
return web3.eth.getAccounts().then((accounts) => {
console.log("Deploying new contract from address: " + accounts[0]);
return contract.deploy({data: this.bytecode, arguments: [defaultValue]}).send({
data: this.bytecode,
from: accounts[0],
gasPrice: 0,
gas: 2000000
}).then((response) => {
//console.log(response);
this.contractAddress = response._address;
this.contractInterface = new web3.eth.Contract(this.abi, this.contractAddress);
return this.contractAddress;
})
})
})
}
// Returns a promise object containing the value set in the contract
get() {
// call the get method on the contract and return the value
// this uses call() instead of send() because it is only reading from the chain
return this.contractInterface.methods.get().call().then((response) => {
return response;
})
}
// Sets the value in the contract
// Returns a promise object containing a transaction receipt
set(stringValue) {
// Change json string to int value
let value = parseInt(stringValue);
console.log("Connecting to node1");
// Use node1 to send the transaction
return this.node1.then((web3) => {
console.log("Getting the default account from node1");
// Use the default account on the node
return web3.eth.getAccounts().then(accounts => {
let defaultAccount = accounts[0];
console.log("Estimating the gas required to set a value");
// Estimate the gas required for the transaction
// Ethereum transactions on the chain require an Execution Fee called "gas".
// This gas is used to pay the miners/node for executing a transaction.
// In Kaleido's networks, the gas price is set to 0; so while you must specify an amount of gas to use,
// it doesn't actually cost anything.
return this.contractInterface.methods.set(value).estimateGas({from: defaultAccount}).then((gasAmount) => {
console.log("Sending the transaction to set a value");
// Send the transaction with the estimated gas
return this.contractInterface.methods.set(value).send({from: defaultAccount, gas: gasAmount});
});
});
});
}
}
// Export the class for other files to import and use
module.exports = GovernmentContract;