Skip to content
Snippets Groups Projects
Commit a1022748 authored by Robert Martin-Legene's avatar Robert Martin-Legene
Browse files

interfaz text para votar

parent 3f05b145
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/node
"use strict"
const Libbfa = require( process.env.BFAHOME + '/bin/libbfa.js');
const rl = require('readline').createInterface(
{ input: process.stdin, output: process.stdout }
);
var child = require( "child_process" );
var web3;
var bfa;
function init()
{
bfa = new Libbfa();
web3 = bfa.newweb3();
mainmenu();
}
function mainmenu()
{
console.log(
"Type \"new\" to create a new contract, or type the address of an\n"
+ " existing contract. An address must start with \"0x\"."
);
rl.question(
"new/address: ",
(answer) => {
if ( answer.toLowerCase() == "new" )
deployNew();
else
if ( bfa.isAddr(answer) )
useExisting( answer );
else
bfa.fatal( "I have no idea what to do with \""+answer+"\"." );
rl.close;
}
);
}
function deployNew()
{
var ballot = bfa.deploy
var ballotsrc = bfa.home + "/src/" + "Ballot.sol";
var jsonstr = child.execFileSync(
"solc", [ "--optimize", "--combined-json", "abi,bin", ballotsrc ]
)
.toString();
var json = JSON.parse( jsonstr );
if ( json == undefined )
fatal( "I can not read that json: " + jsonstr );
console.log( json.contract.keys );
var name = json.contracts.keys[0];
var abi = json.contracts.name.abi;
var bin = json.contracts.name.bin;
var contract = new web3.eth.Contract(
abi,
{
from: bfa.account,
gas: 4000000,
data: "0x"+bin
}
);
contract.deploy(
{
arguments: [
"Vote for Trump?",
[
"Yes",
"No"
]
]
}
)
.send()
.then(
function(newContractInstance)
{
var addr = newContractInstance.options.address;
bfa.fs.writeFileSync( bfa.networkdir + "/contracts/" + addr + "/abi", abi );
bfa.fs.writeFileSync( bfa.networkdir + "/contracts/" + addr + "/bin", bin );
useExisting( addr );
}
);
}
function useExiting( nameaddr )
{
var ballot = bfa.contract( web3, nameaddr );
if ( undefined == ballot )
fatal( 'Can not initialise Ballot contact. Did you remember to import it first?' );
// showballot -- show contract details
var isChairman = bfa.account == ballot.ballotChairman;
var you = ballot.ballotChairman;
if ( isChairman )
you = "You";
var now = Math.floor( Date.now() / 1000 );
var starts = new Date( Date.setTime( ballot.ballotVoteStarts * 1000 ));
var ends = new Date( Date.setTime( ballot.ballotVoteBefore * 1000 ));
console.log( "Title : " + ballot.ballotTitle );
console.log( "Chairman : " + you );
console.log( "Voting starts: " + starts.getDate() );
console.log( "Vote before : " + ends .getDate() );
console.log( "[etcetc]" );
console.log( "Current results are: [blabla]" );
if ( isChairman && starts < now )
console.log( "You are the chairman of this ballot and can register allowed voters." );
if ( now >= starts && now < ends )
console.log( "You should try to vote." );
}
init();
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment