Skip to content
Snippets Groups Projects
votar1.js 6.45 KiB
Newer Older
var     hasplugin               =   0;

var     mainaddr                =   {
    '5445':         '0xe5bf7c3e8aa529e42fbd99428137b68db75d85f9',
    '47525974938':  '0xe3e08934b6fa0b68972c08e0f545cee31ed039c6'
};

window.addEventListener('load', init1 );

async function init1()
{
    bfa_setup( init2 );
}

async function init2( err, web3 )
{
    if ( web3 == undefined )
    {
        window.alert( err );
        return;
    }
    if ( err == undefined )
        hasplugin               =   1;
    window.web3                 =   web3;
    console.log( 'window.web3.version.api is ' + window.web3.version.api );
    status_start();
    show_section('startdiv');
    update_create_new_ballot();
}

function update_create_new_ballot()
{
    // Find our div
    var     div                 =   document.getElementById('div_create_ballot');
    // We get this from status.js
    var     netid               =   netinfo['number'];
    // if we do not have the address of the mother contract, we will
    // not present the user with a choice to create a new form.
    if ( mainaddr[netid] == undefined )
        div.setAttribute(    'class', 'hidden' );
    else
        div.removeAttribute( 'class', 'hidden' );
}

function setup_existing_contract( abi, addr)
{
    if ( api_version() == 0 )
    {
        var     MyContract      =   window.web3.eth.contract( abiNew ); 
        contract                =   MyContract.at( addr );
        return contract;
    }
    window.alert( 'Do not know how to deal with contracts at this API version (' +window.web3.version.api+ ').' );
    return;
}

async function  ask_for_ballot_details()
{
    show_section('newcontract');
}

async function nueva_votacion()
{
    // We get this from status.js
    var     netid               =   netinfo['number'];
    //
    if ( mainaddr[netid] == undefined )
        return;
    var     f                   =   document.forms['form_create_new_ballot'];
    var     arguments           =   [];
    //
    var     title               =   f['title'].value;
    if ( !title || title == '' )
        throw new Error("No title is set");
    arguments.push( f['title']                                  .value );
    //
    var     voteStarts          =   new Date( f['voteStarts'].value );
    voteStarts = new Date( Date.now() + 300000 );
    if ( voteStarts == 'Invalid Date' )
        throw new Error( voteStarts );
    arguments.push( Math.floor(voteStarts.valueOf() / 1000) );
    //
    var     voteBefore          =   new Date( f['voteBefore'].value );
    voteBefore = new Date( Date.now() + 86700000 );
    if ( voteBefore == 'Invalid Date' )
        throw new Error( voteBefore );
    arguments.push( Math.floor(voteBefore.valueOf() / 1000) );
    //
    arguments.push( f['percentOfRegisteredVotersReqToBeValid']  .value );
    arguments.push( f['percentOfVotesCastToWin']                .value );
    //
    arguments.push( f['countNonvotesAsBlanks'].value == 'Yes' );
    //
    arguments.push( f['maxVotesPerVoter']                       .value );
    arguments.push( f['maxVotesPerProposal']                    .value );
    var     proposals           =   [];
    var     p;
    for ( var i = 1; p = f['proposal'+i]; i++ )
        if ( p.value != '' )
            proposals.push( p.value );
    popup( 'Trying to access your account.' );
    setTimeout( nueva_votacion2, 1000, netid, arguments, proposals );
}

async function nueva_votacion2( netid, arguments, proposals )
{
    await access_accounts();
    popup( 'Creating new voting contract.' );
    var     contract            =   setup_existing_contract( abiNew, mainaddr[netid] );
    if ( ! contract )
        return;
    var     rcpt;
    if ( api_version() == 0 )
    {
        console.log( contract );
        try
        {
            contract.newBallot(
                arguments[0], arguments[1], arguments[2], arguments[3],
                arguments[4], arguments[5], arguments[6], arguments[7],
                proposals, { gas: 2111000 }, contract_created
            );
        }
        catch (error)
        {
            popup(error);
            setTimeout( popup, 5000, '' );
        }
    }
}

async function contract_created(err,rcpt)
{
    popup( err ); // the actual error, or undefined
    console.log( rcpt );
    if ( !rcpt )
        return;
    if( !rcpt.address )
        // The hash of the transaction, which deploys the contract
        console.log( rcpt.transactionHash );
        // check address on the second call (contract deployed)
    else
        console.log( rcpt.address ); // the contract address
    //ver_votacion( newaddr );
}

async function ver_votacion( addr )
{
    if ( addr == "" )
        addr                    =
            document
            .getElementById('input_address')
            .value;
    popup( 'Loading contract details, if possible.' );
    if ( addr && addr.length == 40 )
        addr                    =   '0x' + addr;
    window.web3.eth.getCode( addr, function(err,code) {
        if ( err )
        {
            popup( 'Unable to get bytecode of contract: ' + err );
            return;
        }
        ver_votacion2( addr, code );
    });
}

async function ver_votacion2( addr, code )
{
    popup( undefined );
    console.log( code );
    if ( code == '0x' )
    {
        popup( 'There is no contract at that address' );
        return;
    }
    var     keccak3             =   window.web3.sha3( code, {'encoding':'hex'} );
    console.log( keccak3 );
    if ( keccak3 != '0xabcd' )
    {
        popup( 'There is no recognised Ballot contract at that address.' );
        return;
    }
    var     contract            =   setup_existing_contract( abiBallot, addr );
    if ( !contract )
        return;
    //var     detailpromises;
    // make sure we have just a single verdiv
    //var     verdiv;
    //while ( verdiv = document.getElementById('verdiv')) 
    //    verdiv.parent.removeChild( verdiv );
    //verdiv                      =   addchild( rootdiv, 'div', undefined, { 'id': 'verdiv' });
    // Get headline
    //var     proposaldiv         =   addchild( verdiv, 'p', 'Proposal:')
    //contract.methods.proposal().call( {} )
    //.then(
    //    function showproposal(result)
    //    {
    //        proposaldiv.innerHTML   = 'Proposal: ' + result;
    //    },
    //    function failproposal(err)
    //    {
    //        proposaldiv.innerHTML   = 'Failed to get proposal: ' + err
    //    }
    //);
}

async function de_acuerdo_y_firmar()
{
    access_accounts();
    if ( ! accExposed )
        return;
    // web3.eth.sendTransaction({/* ... */});
}