3

I have written a HTML interface which uses web3.js library. For my testing, I dropped web3.js library into the CoinCode directory and import it in HTML file.

<script type="text/javascript" src="bignumber.js/bignumber.min.js"></script>
<script type="text/javascript" src="web3//dist/web3-light.js"></script>
    var Web3 = require('web3');
    var web3 = new Web3();
    web3.setProvider(new web3.providers.HttpProvider("http://localhost:8019"));

Then, I connected to my contract there by using:

var contract1=web3.eth.contract(abiDefination).at(address);

and I was able to use all the functions of my contract by:

contract1.transfer(..)
contract1.checkBalance("0x..")
contract1.sell(..)

But, how can I make rpc calls to my functions. Like I can use following call to list accounts:

curl -X POST --data '{"jsonrpc":"2.0","method":"eth_accounts","params":[],"id":6‌​5}' http://localhost:8019/

Can I use functions of my contract in similar fashion, like:

curl -X POST --data '{"jsonrpc":"2.0","method":"contract1_checkBalance","params":["0x.."],"id":6‌​5}' http://localhost:8019/

and get desired output. Currently when I use such post calls I get:

{
    "jsonrpc": "2.0"
    "id": 67
    "error": {
        "code": -32601
        "message": "The method contract1_checkBalance does not exist/is not available"
    }
}
2

Web3 will take care of the RPC calls, so to make a call without a transaction, for instance for checkBalance(), you call it thus:

// If `checkBalance` is `constant`:
var balance = contract1.checkBalance("0x..");
// otherwise
var balance = contract1.checkBalance.call("0x..");

To make a call with a transaction:

var txHash = contract1.transfer(param1, param2, { from: senderAccount });
// or
var txHash = contract1.transfer.sendTransaction(param1, param2, { from: senderAccount });

Edit:

On the other hand, if you want to make a straight URL call, you need to prepare the data to send with:

var callData = contract1.checkBalance.getData("0x..");

Then your curl call looks like:

curl -X POST --data '{"jsonrpc":"2.0","method":"eth_call","params":[{"to": contractAddress, "data": callData}],"id":6‌​5}' http://localhost:8019/

And if you want to send a transaction to your contract, similarly:

var callData = contract1.transfer.getData(param1, param2);

Then you curl call looks like:

curl -X POST --data '{"jsonrpc":"2.0","method":"eth_sendTransaction","params":[{"from": account, "to": contractAddress, "data": callData}],"id":6‌​5}' http://localhost:8019/

After you have looked at how data is packaged in this callData, you should be able to assemble it without even the help of contract1.

  • Probably you got me wrong,or I was unclear with my question. I am able to call all my functions like transfer(..), as you are saying, in a JS code. But, i want to know how can I access my functions on localhost:8019/. Currently, I have written code for a HTML page which I rum by double-clicking it. I want to access it on a url like localhost:8019. How can I do that? I have given scenarios like I am able to access eth_accounts method there, but is there anyway I can post a request on localhost:8019 with function name like contract1.checkBalance instead of eth_accounts. – Prashant Prabhakar Singh Sep 15 '16 at 12:37
  • I updated the answer with further examples. – Xavier Leprêtre B9lab Sep 15 '16 at 13:23
  • sorry but I did't get desired output, when I used, curl -X POST --data '{"jsonrpc":"2.0","method":"eth_sendTransaction","params":[{from: "0x1c2DDB63F2084B70a27Ec7da01126dadbE084945", to: "0xcEEFFdd02D22b315ACa57D0180F99C0e5BDA5255", data: contract1.transfer.getData("0x936335307DEfa5a611a00eb0775F176469d5D288", 1100)}],"id":65}' http://localhost:8019 I got {"jsonrpc":"2.0","error":{"code":-32600,"message":"invalid character 'f' looking for beginning of object key string"}} as response. – Prashant Prabhakar Singh Sep 16 '16 at 4:29
  • Sorry: must be {"from":... – Xavier Leprêtre B9lab Sep 16 '16 at 11:16
  • 1
    Note that you may have to add a block parameter to eth_call - see ethereum.stackexchange.com/a/9349/1268. – The Officious BokkyPooBah Oct 17 '16 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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