1

Here is my full contract's code. The reason I ask here is the function startNewBallot(). As you can see, it accepts bytes32[] dynamical array

pragma solidity ^0.4.8;
contract ibaVoter {

struct Proposal{
    bytes32 name;
    uint16 voteCount;
}

struct Ballot{
    bytes32 name;
    address chainperson;
    bool blind;
}

Ballot[] public ballots;
uint256 public ballotsNum;
mapping (uint256 => Proposal[]) public proposals;

function startNewBallot(bytes32 ballotName, bool blindParam, bytes32[] proposalNames) external returns (bool success){

    ballots.push(Ballot({
        name: ballotName, 
        chainperson: msg.sender, 
        blind: blindParam
    }));

    ballotsNum++;

    for (uint8 i=0;i<proposalNames.length;i++){
        proposals[ballots.length-1].push(Proposal({name:proposalNames[i], voteCount: 0}));
    }

    return true;
}
}

What I try to do in Remix:

enter image description here

I just pass such arguments: "0x7465737473746500000000000000000000000000000000000000000000000000", false, ["0x6164616461646100000000000000000000000000000000000000000000000000", "0x6461646164616461640000000000000000000000000000000000000000000000"]

And it works perfectly. After transaction is executed I try to call public ballots(0) or proposals(0,0) and it returns me values I passed before.

The problem is that I can not do same thing from my dapp-web interface. I create contract with correct ABI (checked it several times), pass the same arguments to it's function contracts.startNewBallot(). Transaction becomes mined but gas and used gas are always the same and new data never been recorded. Spent already two days trying to guess what's wrong.

UPDATE

I'm using private testnet with Geth's following command: "geth --dev --rpc --rpccorsdomain="*" --rpcapi "eth,net,web3,personal" --datadir "C:\testnet" --networkid 8"

Javascript code I'm using

window.addEventListener('load', function() {
window.web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:8545"));
web3.eth.defaultAccount = '0x5908e7068cbf2de89fcb2ea998235fb0e743b846';
var addressc = '0xa736b0794339593f702622de998bff54f8fc9444';
var contract = web3.eth.contract([{"constant":true,"inputs":[],"name":"ballotsNum","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"ballots","outputs":[{"name":"name","type":"bytes32"},{"name":"chainperson","type":"address"},{"name":"blind","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"},{"name":"","type":"uint256"}],"name":"proposals","outputs":[{"name":"name","type":"bytes32"},{"name":"voteCount","type":"uint16"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"ballotName","type":"bytes32"},{"name":"blindParam","type":"bool"},{"name":"proposalNames","type":"bytes32[]"}],"name":"startNewBallot","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"}]).at(addressc);

contract.ballots(0,function(e,d){console.log(d);});
contract.proposals(0,0,function(e,d){console.log(d);});

var filter = web3.eth.filter('latest');
filter.watch(function(error, result) {
    var block = web3.eth.getBlock(result, true);
    console.log('block #' + block.number);
    console.dir(block.transactions);
});

var fromAscii = function(str, padding) {
    var hex = '0x';
    for (var i = 0; i < str.length; i++) {
        var code = str.charCodeAt(i);
        var n = code.toString(16);
        hex += n.length < 2 ? '0' + n : n;
    }
    return hex + '0'.repeat(padding*2 - hex.length + 2);
};

(function(){
    $('#add-option').click(function(){
        $('.options-wrapper').append(
            '<div class=option>' +
            '<input type="text" placeholder="Type option here (max 32)" maxlength="32" class="option-value"></br>' +
            '</div>'
        );
        return false;
    });

    $('#create').click(function(){
        var options = [];
        var name = fromAscii($('#new-ballot-name').val(),32);
        $(document).find('.option-value').each(function(){
            val = $(this).val();
            opt = fromAscii(val,32);
            options.push(opt);
        });
        contract.startNewBallot(name, false, options, function(e,d){
            console.log(e+' '+d);
        });
        console.log(options);
        console.log(name);
        return false;
    });
}())
});
5
  • 1
    Where's your dapp source code?
    – user19510
    Commented Oct 26, 2017 at 15:25
  • How much gas do you pass? Are you testing against a testnet or testrpc? Perhaps your gas it is too low, try increasing it.
    – Ismael
    Commented Oct 26, 2017 at 16:18
  • Maybe you are running out of gas, check how much gas the function is consuming by call the web3 function estimateGas. And adjust the gas sent to the Dapp. Commented Oct 26, 2017 at 20:08
  • Updated with dapp code Commented Oct 27, 2017 at 6:26
  • Eduardo's comment was a right answer. Formalize it as an answer and I'll accept, thanks for you help. I needed to send it as web3.eth.sendTransaction() since contract object doesn't allow us to set up the gas amount. Commented Oct 27, 2017 at 11:24

1 Answer 1

0

I can successfully return those values out of your contract using a basic test. Your solidity works so look in your test code or share it here so we can try and help.

5
  • updated question with dapp code Commented Oct 27, 2017 at 6:27
  • Try using a method call for reading, i.e. contract.ballots.call(0,function(e,d){console.log(d);}); contract.proposals.call(0,0,function(e,d){console.log(d);}); web3js.readthedocs.io/en/1.0/… Commented Oct 27, 2017 at 6:54
  • Without "call" it works fine if there is a data, so I guess problem is not there. The problem is my transaction with calling function startNewBallot() doesn't pass through from js, get mined but fails on execution. Commented Oct 27, 2017 at 7:01
  • Ok, I am actually testing using truffle and not the web3.eth.contract interface but I'd bet the problem is in the dApp code not Solidity. Have you tried passing in the hardcoded values directly in contract.startNewBallot(name, false, options) call? Commented Oct 27, 2017 at 7:10
  • Yep. I've tried it with hardcoded values and what transaction looks like after it's mined: blockNumber: 3317 contractAddress: null cumulativeGasUsed: 90000 gasUsed: 90000 I've checked input filed of transaction to be sure same method is called and it's the same with remix's transaction. Commented Oct 27, 2017 at 8:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.