How to use web3.js to sign a contract call

AscarioQAscarioQ Posts: 3
edited March 2016 in web3-js
I'v implemented the beginners token example from the Ethereum website and this works well from geth:
contract token { 
    mapping (address => uint) public coinBalanceOf;
    event CoinTransfer(address sender, address receiver, uint amount);

  /* Initializes contract with initial supply tokens to the creator of the contract */
  function token(uint supply) {
        if (supply == 0) supply = 10000;
        coinBalanceOf[msg.sender] = supply;
    }

  /* Very simple trade function */
    function sendCoin(address receiver, uint amount) returns(bool sufficient) {
        if (coinBalanceOf[msg.sender] < amount) return false;
        coinBalanceOf[msg.sender] -= amount;
        coinBalanceOf[receiver] += amount;
        CoinTransfer(msg.sender, receiver, amount);
        return true;
    }
}
When using web3 I can interact with the contract's public token.coinBalanceOf. So that is also working.
Now I'd like to use the private key which I have on the browser side, to sign the transfer of a token from one account to the other. Via geth this works: "token.sendCoin.sendTransaction(0x00001, 10, {from: 0x00002})".

My current approach is the following, but I don't see how to pass on the contract address of my token contract on which this transaction should execute to the sendRawTransaction:
trx = {
    ????address: tokenAddres????,
    sender: fromAccount,
    receiver: toAccount,
    amount: 3333
};

var trx_hex = web3.toHex(trx);
console.log(web3.toAscii(trx_hex));


var rawTx = {
    nonce: '0x06',
    gasPrice: gasPriceHex,
    gasLimit: gasLimitHex,
    to: toAccount,
    value: '0x00',
    data: trx_hex
};

var tx = new Tx(rawTx);
tx.sign(privateKey);

var serializedTx = tx.serialize();

web3.eth.sendRawTransaction(serializedTx.toString('hex'), function (err, hash) {
    if (err) {
        console.log(err)
    }
    else {
        console.log(hash); 
    }
});

Or is there a way to tell web3 the current private key to use, which would allow me to do the following:
var token= web3.eth.contract(tokenABI).at(tokenaddress);
token.sendCoin.sendTransaction(0x00001, 10, {from: 0x00002})
Thank you in advance!
Post edited by AscarioQ on

Best Answer

Answers

  • andrasandras Posts: 14
    Thanks @AscarioQ, I managed to get the same results, albeit in a slightly different way. I'm stuck now though, as I'm not sure how to convert the transaction hash I received into the value returned by the contract. Any thuoghts>
  • mattbmattb Posts: 2
    actually i'm unable to send a raw tx using (geth interface on eth, ethconsole on eth or even a full geth node)..

    8 times out of 10, using Geth i get a "balance too low" or "missing account" errors.. using Geth/ethconsole on top of eth i get the following:
    
    > eth.sendRawTransaction('f87a831001888504a817c800830493e09454a25919a915bbaeb58a96679428f80d733af89392313030303030303030303030303030303030801ca0ecd622603d87d290b7440a8a03413d865dd67783e90cde0ef33d3f5752888ffda031adeee357777c7c39f3edb8920c91aec0cced05ad429d8aa4be627604bd820b');
    > "0x42fbb8e551be1aaefd6d768d7798467d7978e3d06e0dd3db20f88e5b14387328"
    > eth.getTransaction("0x42fbb8e551be1aaefd6d768d7798467d7978e3d06e0dd3db20f88e5b14387328")
    > null
    
  • taz002devtaz002dev 0x26ff3ed5c5025b5a3c288b9139d0396f9de5891fPosts: 47
    @AscarioQ - thanks for sharing it all. Will be attempting the same so it'll be very useful.
  • priyankadlpriyankadl Posts: 1
    edited July 2016
    @AscarioQ how to connect beginners token example from geth.. i have installed truffle but couldnt connect properly ..help me to do this
    Post edited by priyankadl on
  • AjoyBhatiaAjoyBhatia Posts: 5
    Instead of using the undocumented internal SolidityFunction to get the data for the function call, you can use getData as follows:
    var token = web3.eth.contract(tokenABI).at(tokenaddress);
    // contract-instance.<method-name>.getData(arg1, arg2)
    var payloadData = token.sendCoin.getData(0x00001, 10); // 
    I found this way of getting the data in this section of the Ethereum wiki:
    https://github.com/ethereum/wiki/wiki/JavaScript-API#contract-methods
  • AjoyBhatiaAjoyBhatia Posts: 5
    @AscarioQ - I have a question about your code. From where did you get the data to construct the private key? Also, you constructed a new key but how is that key associated with the 'from' account address '0x231f75c6aabf4fb72ed225986b1128dbbe4ba6c5'?
Sign In or Register to comment.