-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathetherjs0.js
122 lines (119 loc) · 6.82 KB
/
etherjs0.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
122
// This code will deploy voting contract on local blockchain Ganache
// Interact with the contract using etherjs
var ethers = require('ethers');
var provider = new ethers.providers.JsonRpcProvider();
provider.listAccounts().then(result => console.log(result))
provider.getBlockNumber().then((result) => {
console.log("Current block number: " + result);
});
async function deploycontract()
{ const abi= [
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "uint256",
"name": "_candidateId",
"type": "uint256"
}
],
"name": "votedEvent",
"type": "event"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"name": "candidates",
"outputs": [
{
"internalType": "uint256",
"name": "id",
"type": "uint256"
},
{
"internalType": "string",
"name": "name",
"type": "string"
},
{
"internalType": "uint256",
"name": "voteCount",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "candidatesCount",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "_candidateId",
"type": "uint256"
}
],
"name": "vote",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"name": "voters",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "view",
"type": "function"
}
];
const bytecode= "608060405234801561001057600080fd5b506100556040518060400160405280600b81526020017f63616e646964617465203100000000000000000000000000000000000000000081525061009e60201b60201c565b6100996040518060400160405280600b81526020017f63616e646964617465203200000000000000000000000000000000000000000081525061009e60201b60201c565b6102a5565b600260008154809291906100b1906101fe565b9190505550604051806060016040528060025481526020018281526020016000815250600080600254815260200190815260200160002060008201518160000155602082015181600101908051906020019061010e92919061011f565b506040820151816002015590505050565b82805461012b906101cc565b90600052602060002090601f01602090048101928261014d5760008555610194565b82601f1061016657805160ff1916838001178555610194565b82800160010185558215610194579182015b82811115610193578251825591602001919060010190610178565b5b5090506101a191906101a5565b5090565b5b808211156101be5760008160009055506001016101a6565b5090565b6000819050919050565b600060028204905060018216806101e457607f821691505b602082108114156101f8576101f7610276565b5b50919050565b6000610209826101c2565b91507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff82141561023c5761023b610247565b5b600182019050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b610667806102b46000396000f3fe608060405234801561001057600080fd5b506004361061004c5760003560e01c80630121b93f146100515780632d35a8a2146100815780633477ee2e1461009f578063a3ec138d146100d1575b600080fd5b61006b6004803603810190610066919061035b565b610101565b60405161007891906103ee565b60405180910390f35b61008961022c565b6040516100969190610424565b60405180910390f35b6100b960048036038101906100b4919061035b565b610232565b6040516100c89392919061043f565b60405180910390f35b6100eb60048036038101906100e6919061032e565b6102e4565b6040516100f89190610409565b60405180910390f35b6000600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060009054906101000a900460ff161561015a57600080fd5b60008211801561016c57506002548211155b61017557600080fd5b60018060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060006101000a81548160ff02191690831515021790555060008083815260200190815260200160002060020160008154809291906101f290610546565b9190505550817ffff3c900d938d21d0990d786e819f29b8d05c1ef587b462b939609625b684b1660405160405180910390a2339050919050565b60025481565b600060205280600052604060002060009150905080600001549080600101805461025b90610514565b80601f016020809104026020016040519081016040528092919081815260200182805461028790610514565b80156102d45780601f106102a9576101008083540402835291602001916102d4565b820191906000526020600020905b8154815290600101906020018083116102b757829003601f168201915b5050505050908060020154905083565b60016020528060005260406000206000915054906101000a900460ff1681565b60008135905061031381610603565b92915050565b6000813590506103288161061a565b92915050565b600060208284031215610344576103436105ed565b5b600061035284828501610304565b91505092915050565b600060208284031215610371576103706105ed565b5b600061037f84828501610319565b91505092915050565b61039181610499565b82525050565b6103a0816104ab565b82525050565b60006103b18261047d565b6103bb8185610488565b93506103cb8185602086016104e1565b6103d4816105f2565b840191505092915050565b6103e8816104d7565b82525050565b60006020820190506104036000830184610388565b92915050565b600060208201905061041e6000830184610397565b92915050565b600060208201905061043960008301846103df565b92915050565b600060608201905061045460008301866103df565b818103602083015261046681856103a6565b905061047560408301846103df565b949350505050565b600081519050919050565b600082825260208201905092915050565b60006104a4826104b7565b9050919050565b60008115159050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000819050919050565b60005b838110156104ff5780820151818401526020810190506104e4565b8381111561050e576000848401525b50505050565b6000600282049050600182168061052c57607f821691505b602082108114156105405761053f6105be565b5b50919050565b6000610551826104d7565b91507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8214156105845761058361058f565b5b600182019050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b600080fd5b6000601f19601f8301169050919050565b61060c81610499565b811461061757600080fd5b50565b610623816104d7565b811461062e57600080fd5b5056fea2646970667358221220d69dd9a6277691af026e171024a10cb24e378aaabd7dbf78e1d9d1d252ee2fe864736f6c63430008070033";
var signer=provider.getSigner(0);
var factory = new ethers.ContractFactory(abi, bytecode, signer)
var contract = null;
contract= await factory.deploy();
console.log(contract.address);
console.log('voting');
contract.vote(1).then((f) => console.log(f))
}
deploycontract();