3

I have worked with both new and previous versions of eosjs. In the previous version I had to make a config object like this:

Eos = require('eosjs')

// Default configuration
config = {
  chainId: null, // 32 byte (64 char) hex string
  keyProvider: ['PrivateKeys...'], // WIF string or array of keys..
  httpEndpoint: 'http://127.0.0.1:8888',
  expireInSeconds: 60,
  broadcast: true,
  verbose: false, // API activity
  sign: true
}

eos = Eos(config)

we would push our transactions like this:

eos.transaction(
  {
    // ...headers,
    // context_free_actions: [],
    actions: [
      {
        account: 'eosio.token',
        name: 'transfer',
        authorization: [{
          actor: 'inita',
          permission: 'active'
        }],
        data: {
          from: 'inita',
          to: 'initb',
          quantity: '7.0000 SYS',
          memo: ''
        }
      }
    ]
  }
  // config -- example: {broadcast: false, sign: true}
)

but in the new version we use the following:

const { Api, JsonRpc, RpcError } = require('eosjs');
const JsSignatureProvider = require('eosjs/dist/eosjs-jssig');  // development only
const fetch = require('node-fetch');                            // node only; not needed in browsers
const { TextEncoder, TextDecoder } = require('util');           // node only; native TextEncoder/Decoder
const { TextEncoder, TextDecoder } = require('text-encoding'); 
const defaultPrivateKey = "5JtUScZK2XEp3g9gh7F8bwtPTRAkASmNrrftmx4AxDKD5K4zDnr"; // useraaaaaaaa
const signatureProvider = new JsSignatureProvider([defaultPrivateKey]);
const rpc = new JsonRpc('http://127.0.0.1:8888', { fetch });
const api = new Api({ rpc, signatureProvider, textDecoder: new TextDecoder(), textEncoder: new TextEncoder() });

then we send our transction :

(async () => {
  const result = await api.transact({
    actions: [{
      account: 'eosio.token',
      name: 'transfer',
      authorization: [{
        actor: 'useraaaaaaaa',
        permission: 'active',
      }],
      data: {
        from: 'useraaaaaaaa',
        to: 'useraaaaaaab',
        quantity: '0.0001 SYS',
        memo: '',
      },
    }]
  }, {
    blocksBehind: 3,
    expireSeconds: 30,
  });
  console.dir(result);
})();

Why don't we need chainId in the new version? If it uses chainId, how did it find it internally?

3

eosjs does require chainId, but if it isn't provided directly it will be retrieved from the host endpoint.

Api accepts chainId as an argument but if chainId isn't passed as an argument when the api is constructed, Api will fetch it from the host endpoint via JsonRpc.

transact() checks to see if chainId is set. If not, it calls rpc.get_info() which returns JSON from <host provided to JsonRpc>/v1/chain/get_info containing the chain_id property.

In eosjs-api.ts:

/** in Api constructor */
public chainId: string;

/** in transact() function */
if (!this.chainId) {
   info = await this.rpc.get_info();
   this.chainId = info.chain_id;
}

eosjs-jsonrpc.ts:

/** `fetch()` prepends the path with the host provided to JsonRpc and returns JSON containing `chain_id` */
public async get_info(): Promise<GetInfoResult> {
    return await this.fetch("/v1/chain/get_info", {});
}

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.