1

I'm working on Todos example in the following link. To Do list smart contract

By using cleos utility, everything works fine.

Now, I'm trying to access the smart contract with Node.js RPC.

How can I implement the following functionalities with Node.js RPC?

$ cleos --wallet-url http://172.18.0.1:8899 -u http://172.18.0.1:8888 push action shlee create '["shlee", 2, "feed dog"]' -p shlee

Basically, I'd like to know how to implement push action command of cleos utility.

Will the following API work?

http://127.0.0.1:8888/v1/chain/push_block

1
  • You can do this via eosjs which is more proffered than RPC calls. If you really want to do it via rpc call please explore push_transaction it requires a packed_transaction Commented Nov 21, 2018 at 9:47

2 Answers 2

2

Here is my final code that is working ...

Thank you leettamna

const { Api, JsonRpc, RpcError, JsSignatureProvider } = require('eosjs');
const fetch = require('node-fetch');                            // node only; not needed in browsers
const { TextDecoder, TextEncoder } = require('text-encoding');  // node, IE11 and IE Edge Browsers

const defaultPrivateKey = "5JUKRhWkNobtgSJSamcY2Yz4dpbAzZ3euTYK2d7rWUP118xyRiG"; // user shlee
const signatureProvider = new JsSignatureProvider([defaultPrivateKey]);

const rpc = new JsonRpc(' http://172.18.0.1:8888', { fetch });

const api = new Api({ rpc, signatureProvider, textDecoder: new TextDecoder(), textEncoder: new TextEncoder() });

try {
(async () => {
  const result = await api.transact({
    actions: [{
      account: 'shlee',
      name: 'create',
      authorization: [{
        actor: 'shlee',
        permission: 'active',
      }],
      data: {
        author: 'shlee',
        id: 4,
        description: "feed dog",
      },
    }]
  }, {
    blocksBehind: 3,
    expireSeconds: 30,
  });
  console.log(result);
})();
} catch (e) {
  console.log('\nCaught exception: ' + e);
  if (e instanceof RpcError)
    console.log(JSON.stringify(e.json, null, 2));
}
1

Try 'eosjs'.

It is official javascript eos rpc library.

repository has simple example. https://github.com/EOSIO/eosjs

modify defaultPrivateKey = "shlee_private_key",
and parameter1 ~ parameter3 to correct parameter name
and http://127.0.0.1:8000 to your node api endpoint

const { Api, JsonRpc, RpcError, JsSignatureProvider } = require('eosjs');
const fetch = require('node-fetch');                            // node only; not needed in browsers
const { TextDecoder, TextEncoder } = require('text-encoding');  // node, IE11 and IE Edge Browsers

const defaultPrivateKey = "5JtUScZK2XEp3g9gh7F8bwtPTRAkASmNrrftmx4AxDKD5K4zDnr"; // useraaaaaaaa
const signatureProvider = new JsSignatureProvider([defaultPrivateKey]);

const rpc = new JsonRpc('http://127.0.0.1:8000', { fetch });

(async () => {
  const result = await api.transact({
    actions: [{
      account: 'shlee',
      name: 'create',
      authorization: [{
        actor: 'shlee',
        permission: 'active',
      }],
      data: {
        parameter1_name: 'shlee',
        parameter2_name: 2,
        parameter3_name: "feed dog",
      },
    }]
  }, {
    blocksBehind: 3,
    expireSeconds: 30,
  });
  console.dir(result);
})();
5
  • Thanks, a lot. I appreciate that you wrote the actual code for me. One more question, cleos command uses --wallet-url 172.18.0.1:8899 option. I guess that this option is used to retrieve the private key of the user. So I need to retrieve the private key from the keosd and then run the code you wrote. Is that correct? Commented Nov 21, 2018 at 7:50
  • in above code, key is privided directly. look at JsSignatureProvider code. :) in this case keosd is not used.
    – leettamna
    Commented Nov 21, 2018 at 7:53
  • const defaultPrivateKey = "5JtUScZK2XEp3g9gh7F8bwtPTRAkASmNrrftmx4AxDKD5K4zDnr"; <- you have to input shlee's private key here.
    – leettamna
    Commented Nov 21, 2018 at 7:54
  • Right. I understand your code. But I'd like to mimic what cleos do. Based on your code and the cloes command that I mention, it seems that cloes retrieves the private key of shlee from the keosd in 172.18.0.1:8899 (--wallet-url 172.18.0.1:8899 option) and contacts the nodeos to push the action with the retrieved private key.. By doing so, I can avoid to put the private key in the code... Commented Nov 21, 2018 at 8:10
  • We can put the private key to encrypted DB and load it when needed. i think . :)
    – leettamna
    Commented Nov 21, 2018 at 8:16

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.