Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write the eosjs transfer code

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Eosjs transfer code how to write, I believe that many inexperienced people do not know what to do, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

The following describes the idea of using V20 version of eosjs to achieve token transfer, and gives the implementation code of eosjs transfer.

Let's first introduce the class to be used in nodejs:

Const {Api,JsonRpc,RpcError} = require ('eosjs') const JsSignatureProvider = require (' eosjs/dist/eosjs-jssig'); const fetch = require ('node-fetch'); const {TextEncoder, TextDecoder} = require (' util')

In EOS, token transfer is a call to the transfer () method of a token contract, so the next step is to prepare an action call to the token contract. For example, the following action transfers 0.0001 SYS tokens from the account useraaaaaaaa to the account userbbbbbbbb:

Const action = {account: 'eosio.token', name:' transfer', authorization: [{actor: 'useraaaaaaaa', permission:' active',}], data: {from: 'useraaaaaaaa', to:' userbbbbbbbb', quantity: '0.0001 SYS', memo:',},}

Next, we need to prepare a signer to sign the transaction. During development, we can use eosj's built-in signer to implement JsSignatureProvider. Its instantiation requires passing in a set of private keys that may be used for the transaction:

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

It is important to note that JsSignatureProvider does not have any protection for the private key, so please do not use this signer in a production environment. In the tutorial Eos Smart contract and introduction to Dapp Development, we present a keosd-based signature provider for use in a production environment.

Once the transaction is signed, the transaction needs to be submitted using the RPC interface of the EOS node, so initialize a JsonRpc object:

Const rpc = new JsonRpc ('http://127.0.0.1:8888', {fetch})

The above procedures for signing, serializing, and broadcasting transactions are encapsulated by the Api class in eosjs, so we only need to call the transact () method of the Api instance:

Const api = new Api ({rpc, signatureProvider, textDecoder: new TextDecoder (), textEncoder: new TextEncoder ()}); api.transact ({actions: [action]}) .then (ret = > console.log (ret)) .catch (err = > console.log (err))

In the implementation of the transact () method, the push_transaction () method of the JsonRpc object is called to submit the signed serialized transaction to the node and broadcast to the entire EOS network.

After reading the above, have you mastered how to write the eosjs transfer code? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report