-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTutorial5 -Executing Smart Contract Functions.js
More file actions
141 lines (127 loc) · 2.84 KB
/
Copy pathTutorial5 -Executing Smart Contract Functions.js
File metadata and controls
141 lines (127 loc) · 2.84 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
var Tx = require("ethereumjs-tx").Transaction;
const Web3 = require("web3");
const web3 = new Web3(
"https://ropsten.infura.io/v3/c89f216154d84b83bb9344a7d0a91108"
);
const account1 = "0xC0847D13b14E9dc83FCa69fD17f066d873035bEd";
let abi = [
{
inputs: [],
name: "doSomeWork",
outputs: [
{
internalType: "string",
name: "",
type: "string"
}
],
stateMutability: "nonpayable",
type: "function"
},
{
anonymous: false,
inputs: [
{
indexed: false,
internalType: "string",
name: "",
type: "string"
}
],
name: "logString",
type: "event"
},
{
inputs: [
{
internalType: "uint256",
name: "_age",
type: "uint256"
}
],
name: "setAge",
outputs: [],
stateMutability: "nonpayable",
type: "function"
},
{
inputs: [],
name: "getAge",
outputs: [
{
internalType: "uint256",
name: "",
type: "uint256"
}
],
stateMutability: "view",
type: "function"
},
{
inputs: [],
name: "getString",
outputs: [
{
internalType: "string",
name: "",
type: "string"
}
],
stateMutability: "view",
type: "function"
}
];
const account2 = "0x1c091a6fa3ed1d51285d0a9df5b7c2004ee33c7a";
const Private_Key_1 = Buffer.from(
"01EF34E359D08E81A228935EA51E4A56477ACD6E8BD6E12405F85F684247A48D",
"hex"
);
const contract = new web3.eth.Contract(abi, account2);
data = contract.methods.setAge(12).encodeABI();
//console.log(web3.eth.accounts.create());
/*
web3.eth.getBalance(account1, (err, _balance) => {
if (err) {
console.log("Error Has occured");
} else {
console.log("Account# 1 balance ", web3.utils.fromWei(_balance, "ether"));
}
});
web3.eth.getBalance(account2, (err, _balance) => {
if (err) {
console.log("Error Has occured");
} else {
console.log("Account# 2 balance ", web3.utils.fromWei(_balance, "ether"));
}
});
*/
web3.eth.getTransactionCount(account1, (err, _count) => {
//build the transaction
const txObject = {
nonce: web3.utils.toHex(_count),
to: account2,
data: data,
gasLimit: web3.utils.toHex(800000),
gasPrice: web3.utils.toHex(web3.utils.toWei("10", "gwei"))
};
//console.log(txObject);
//sign the transaction
const tx = new Tx(txObject, { chain: "ropsten" });
tx.sign(Private_Key_1);
const serialTransaction = tx.serialize();
const raw = "0x" + serialTransaction.toString("hex");
//brodcast the transaction
web3.eth.sendSignedTransaction(raw, (err, txHash) => {
if (err) {
console.log(err.message);
} else {
console.log("txHash:", txHash);
}
});
});
contract.methods.getAge().call(function(err, result) {
if (err) {
console.log(err.message);
}
console.log("This is my age ", result);
});