Actually, this is a smart contract ABI, which is a JSON representation of the interface to interact with code deployed on the Ethereum blockchain using the Web3 javascript library.
Because of how Ethereum works, these contracts are (more or less) permanent. So it's nowhere near as bad as you think.
OP, can you explain what's so bad about this? It's dense, sure, but specifically what's the issue?
Yeah but you shouldn’t hardcode it in the same file all on one like like that. Keep it in a separate file and import it, and it’s much cleaner (and reusable).
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
abi = JSON.parse('[{"constant":false,"inputs":[{"name":"candidate","type":"bytes32"}],"name":"totalVotesFor","outputs":[{"name":"","type":"uint8"}],"payable":false,"type":"fu[cut off]
VotingContract = web3.eth.contract(abi);
// In your nodejs console, execute contractInstance.address to get the address at which the contract is deployed and change the line below to use your deployed address
contractInstance = VotingContract.at('0x2a9c1d265d06d47e8f7b00ffa987c9185aecf672');
candidates = {"Rama": "candidate-1", "Nick": "candidate-2", "Jose": "candidate-3"};
function voteForCandidate() {
candidateName = $("#candidate").val();
contractInstance.voteForCandidate(candidateName, {from: web3.eth.accounts[0]}, function() {
let div_id = candidates[candidateName];
$("#" + div_id).html(contractInstance.totalVotesFor.call(candidateName).toString());
});
}
$(document).ready(function() {
candidateNames = Object.keys(candidates);
for (var i = 0; i < candidateNames.length; i++) {
let name = candidateNames[i];
let val = contractInstance.totalVotesFor.call(name).toString();
$("#" + candidates[name]).html(val);
}
});
As in my other comment, I don't see anything here that is absolutely monstrous WITHIN the context of developing smart contracts. Would definitely be interested to hear otherwise though.
49
u/[deleted] Oct 01 '21
Why the fuck is there hardcored json in a string there??? At least add a comment that it's temporary bruh
18
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Oct 01 '21
Image Transcription: Code
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
11
u/jzia93 Oct 01 '21
As in my other comment, I don't see anything here that is absolutely monstrous WITHIN the context of developing smart contracts. Would definitely be interested to hear otherwise though.
7
u/rangeDSP Oct 01 '21
Ew
7
u/__420 Oct 01 '21
thats just how smartcontracts work i guess
3
u/AtlasJan Oct 02 '21
I... don't see the problem?
is it that it's dense?
1
u/tills1993 Oct 02 '21
idk why but
.callbothers me so much. It can be useful but it's not even (AFAICT) doing anything here.1
u/RedditAcc-92975 Oct 01 '21
Finally some fvcking bad code!