eosjs简明使用手册(最新版)
eosjs简明使用手册(最新版)
以下教程用于EOS正式版,已在DEV开发链和EOS1.0.5-1.0.7版本的主链上测试通过。
官方文档因为版本的原因,内容多有错误,仅供参考。
eosjs的API与http-RPC调用一致,如rpc使用get-block
,则在eosjs中使用getBlock()
方法。即去横岗,首字符大写
eosjs几个容易出错的地方
- asset类型,表达方式为字符串,如:
100.0000 EOS
,千万注意小数点后四位,少一位都不行 - 与链连接时需要指定chainId,可以通过
cleos get info
获得
1- 安装eosjs
eosjs
用于对交易签名、交易等操作。 eosjs-api
用于读取链上数据,只读,如果只需要读取链上数据的话,只需要使用eosjs-api
。
在nodejs中安装:
npm install eosjs
npm install eosjs-api
注意nodejs
需要最新版,如安装过程发生错误,请用node -v
和npm -v
查看版本。
2- 建立eosjs与链的连接
Eos = require('eosjs')// Optional configuration..
config = {keyProvider: ['PrivateKeys...'], // 配置私钥字符串httpEndpoint: 'http://178.62.196.196:8888', //DEV开发链url与端口chainId: "0b08e71a2f8caaccc2dc13244b788f5eba29462ecd5d5dea1ad8cbe9581e885a", // 通过cleos get info可以获取chainIdmockTransactions: () => null, // 如果要广播,需要设为nulltransactionHeaders: (expireInSeconds, callback) => {callback(null/*error*/, headers) //手动设置交易记录头,该方法中的callback回调函数每次交易都会被调用},expireInSeconds: 60,broadcast: true,debug: false,sign: true,authorization: null // 该参数用于在多签名情况下,识别签名帐号与权限,格式如:account@permission
}eos = Eos(config)
3- 建立eosjs-api与链的连接
如果加载了eosjs后,默认加载eosjs-api,无需单独链接eosjs-api
与链
EosApi = require('eosjs-api')// everything is optional
options = {httpEndpoint: 'http://178.62.196.196:8888',verbose: false, // API logginglogger: { // Default logging functions//log: config.verbose ? console.log : '',error: console.error},fetchConfiguration: {}
}eos = EosApi(options)
4- 获取帮助
不添加如何参数,可以获取该方法的使用说明,如:
eos.getBlock()
5- 获取链上最新出块信息
eos.getInfo({}).then(result => { console.log(result)
})
返回:
{ server_version: 'b195012b',chain_id: '0b08e71a2f8caaccc2dc13244b788f5eba29462ecd5d5dea1ad8cbe9581e885a',head_block_num: 209307,last_irreversible_block_num: 209267,last_irreversible_block_id: '00033173a9ccd4bdd60a92d257e9354023b0457b134797be472a236cd908bc31',head_block_id: '0003319ba8ddc60d80c3cd0c7a70695cfd951f51ace9a798c913384cfbae659c',head_block_time: '2018-07-15T01:51:07.500',head_block_producer: 'eoshackathon',virtual_block_cpu_limit: 100000000,virtual_block_net_limit: 1048576000,block_cpu_limit: 99900,block_net_limit: 1048576
}
如果需要拿到某一个数据值,比如:head_block_producer
,则使用:
eos.getInfo({}).then(result => { console.log(result.head_block_producer) //以对象属性方式获取head_block_producer
})
6- 获取指定区块信息
eos.getBlock(200000).then(result => { console.log(result) }) //获取第200000个区块
或者:
eos.getBlock({block_num_or_id: 200000}).then(result => { console.log(result) }) //获取第200000个区块
或者:
eos.getBlock('00030d4011a6744857533a6e6d907037a94c27a2dc006b4d28125f76bed2b355').then(result => { console.log(result) }) //根据id获取区块
或者:
eos.getBlock({block_num_or_id: '00030d4011a6744857533a6e6d907037a94c27a2dc006b4d28125f76bed2b355'}).then(result => { console.log(result) }) //根据id获取区块
或者将返回值传到回调函数callback中处理:
callback = (err, res) => { err ? console.error(err) : console.log(res) }
eos.getBlock(200000, callback)
7- 获取账户余额
eos.getCurrencyBalance({ code: "eosio.token", account: "eosio", symbol: "DEV" }).then(result => console.log(result))
以上命令相当于
cleos get currency balance eosio.token eosio DEV
8- 获取某代币的信息
eos.getCurrencyStats({code: "eosio.token", symbol: "DEV"}, callback) //用上面出现的callback,下同
9- 获取账户信息
eos.getAccount({account_name: "eoshackathon"}, callback) //获取eoshackathon账户的信息
相当于:
cleos get account eoshackathon
返回:
{ account_name: 'eoshackathon',head_block_num: 219997,head_block_time: '2018-07-15T03:20:12.500',privileged: false,last_code_update: '1970-01-01T00:00:00.000',created: '2018-07-13T20:54:28.000',ram_quota: 8148,net_weight: 500000,cpu_weight: 500000,net_limit: { used: 145, available: 120795486, max: 120795631 },cpu_limit: { used: 1511, available: 11518458, max: 11519969 },ram_usage: 3414,permissions:[ { perm_name: 'active', parent: 'owner', required_auth: [Object] },{ perm_name: 'owner', parent: '', required_auth: [Object] } ],total_resources:{ owner: 'eoshackathon',net_weight: '50.0000 DEV',cpu_weight: '50.0000 DEV',ram_bytes: 8148 },self_delegated_bandwidth: null,refund_request: null,voter_info: null
}
10- 获取智能合约代码
eos.getCode({ account_name: "eosio"}, callback)
获取eosio
账户的所有合约代码
11- 获取智能合约ABI
eos.getAbi({ account_name: "eosio"}, callback)
12- 获取Table行数据
eosjs.getTableRows({"scope":contract_name, "code":contract_name, "table":"game", "json": true},callback)
13- 获取账户的Actions列表
eos.getActions({account_name: "eoshackathon", pos: 0, offset: 15}, callback) //pos和offset是指:从第pos条记录开始获取offset条Actions
14- 获取公钥对应的账户
eos.getKeyAccounts({ public_key: 公钥字符串}, callback)
如果查找到帐号,则返回[]
,如果该公钥没有对应帐号,则报错。 相当于:
cleos get accounts 公钥
15- 获取主账号控制的其他帐号
eos.getControlledAccounts({ controlling_account: "eoshackathon"}, callback)
16- 获取transaction交易细节
该指令有bug,慎用!
eos.getTransaction({id: "xxxx"}, callback)
17- 转账交易
首先,在链接配置config
中,keyProvider: [发送方私钥]
其次,可以设置options
参数如下:
options = {authorization: '发送方帐号@active',broadcast: true,sign: true
}
方式1:
eos.transfer('发送方帐号', '接收方帐号', '0.3000 DEV','memo')
如果需要对返回值处理:
eos.transfer('发送方帐号', '接收方帐号', '0.3000 DEV','memo', callback)
如果有options
参数,则:
eos.transfer('发送方帐号', '接收方帐号', '0.3000 DEV','memo', options, callback)
方式2:
使用对象:
eos.transfer({ from: '发送方帐号', to: '接收方帐号', quantity: '0.1000 DEV', memo: '备注', callback })
如果不想广播交易,可以使用以下简便指令:
eos.transfer('发送方帐号', '接收方帐号', '0.3000 DEV','memo', false) //在最后加上false,不广播
方式3:
使用eos.transaction
,构造对象执行
eos.transaction({// ...headers,actions: [{account: 'eosio.token',name: 'transfer',authorization: [{actor: '发送方帐号',permission: 'active'}],data: {from: '发送方帐号',to: '接收方帐号',quantity: '0.3000 DEV',memo: '备注'}}]}// options -- example: {broadcast: false}
)
以上命令与以下cleos
相同:
cleos push action eosio.token transfer '[ "发送方帐号", "接收方帐号", "0.3000 DEV", "备注" ]' -p 发送方帐号
18- 新建帐号
creatoraccount = "testtesttest" //主账号
newaccount = "testtest1113" //新账号
newaccount_pubkey = "EOS5LUYdLZAd3uHF5XGAeE61aTeSXWqvqxBSUq3uhqYH7kY15Drjf" //新账号的公钥//构建transaction对象
eos.transaction(tr => {//新建账号tr.newaccount({creator: creatoraccount,name: newaccount,owner: newaccount_pubkey,active: newaccount_pubkey})//为新账号充值RAMtr.buyrambytes({payer: creatoraccount,receiver: newaccount,bytes: 8192})//为新账号抵押CPU和NET资源tr.delegatebw({from: creatoraccount,receiver: newaccount,stake_net_quantity: '1.0000 DEV',stake_cpu_quantity: '1.0000 DEV',transfer: 0})
})
19- 购买RAM
creatoraccount = "testtesttest" //主账号
newaccount = "testtest1113" //新账号eos.transaction(tr => {tr.buyrambytes({payer: creatoraccount,receiver: newaccount,bytes: 8192 })
或者
eos.transaction(tr => {tr.buyram({payer: creatoraccount,receiver: newaccount,quant: 8 //以k为单位的内存,8k=8192字节})
20- 出售RAM
eos.transaction(tr => {tr.sellram({account: 'testtesttest',bytes: 1024 //出售1k内存})
})
21- 竞拍账号名
eos.transaction(tr => {tr.bidname ({bidder: "testtesttest",newname: "竞拍的名字",bid: 价格})
})
22- 抵押CPU和NET
eos.transaction(tr => {tr.delegatebw({from: "testtesttest",receiver: "testtesttest", //testtesttest账户为自己抵押stake_net_quantity: "1.0000 DEV",stake_cpu_quantity: "1.0000 DEV",transfer: 0})
})
23- 取消抵押(赎回)CPU和NET
eos.transaction(tr => {tr.undelegatebw({from: "testtesttest",receiver: "testtesttest",unstake_net_quantity: "0.1000 DEV", //赎回0.1个DEVunstake_cpu_quantity: "0.1000 DEV"})
})
24- 智能合约部署
如果是加载wasm
合约,不用使用binaryen
,如果加载wast
合约,需要安装并使用binaryen
,如下:
npm install binaryen@39.0.0
并用以下方式导入到js
binaryen = require('binaryen')
eos = Eos({keyProvider, binaryen})
部署合约
以官方自带的hello合约为例
fs = require('fs')
wasm = fs.readFileSync(`contracts/hello/hello.wasm`) //这个文件在客户端上,而不是在服务器上
abi = fs.readFileSync(`contracts/hello/hello.abi`)eos.setcode('contract_name', 0, 0, wasm) // contract_name 为合约名
eos.setabi('contract_name', JSON.parse(abi)) // @returns {Promise}
25- 智能合约的执行
方法一:
eos.contract("contract_name").then(hello => { //hello随便起的变量名hello.hi('axay', { //hi是方法名, 'axay'是该hello合约hi方法的参数authorization: ['testtesttest'] //testtesttest是建立该合约的用户}).then(result => {console.log(result);});
});
方法二:
eos.transaction({actions: [{account: 'contract_name', //合约名name: 'hi', //方法名,该方法在官方的hello合约中有authorization: [{actor: 'testtesttest',permission: 'active'}],data: {user: 'axay'}}]}// options -- example: {broadcast: false}
).then(result => console.log(result))
26- 通过eosjs发行一个代币
发行代币有两种方式,一种是通过cleos
,参考这里,但前提是必须要安装好EOS系统。 另一种方式是通过eosjs
,无需在本机安装EOS系统。
注意:
此操作需要以eosio.token
账户进行操作,因此需将eosio.token
帐号的私钥导入到keyProvider
数组中。但如果出于安全原因,不允许将eosio.token
账户私钥加到程序中的话,则可以由发币的用户先部署eosio.token
合约,然后再做接下去的操作。
第一步:创建代币
eos.transaction({// ...headers,actions: [{account: 'eosio.token', //合约名name: 'create', //调用创建代币的方法authorization: [{actor: 'eosio.token', //必须是eosio.tokenpermission: 'active'}],data: {issuer: 'testtesttest', //代币发行方maximum_supply: '10000.0000 AAA', //代币总量与名称can_freeze: 0,can_recall: 0,can_whitelist: 0}}]}// options -- example: {broadcast: false}
).then(result => console.log(result))
第二步:发行代币
eos.transaction({// ...headers,actions: [{account: 'eosio.token', //合约名name: 'issue', //调用发行代币的方法authorization: [{actor: 'testtesttest', //必须是代币的发行方permission: 'active'}],data: {to: 'testtesttest', //收到代币的帐号quantity: '1000.0000 AAA',memo: "testtesttest issue 1000 AAA"}}]}// options -- example: {broadcast: false}
).then(result => console.log(result))
也可以将以上两步合在一起操作,如:
eos.transaction({// ...headers,actions: [{account: 'eosio.token', //合约name: 'create', //方法authorization: [{actor: 'eosio.token',permission: 'active'}],data: {issuer: 'testtesttest',maximum_supply: '10000000.0000 GAT',can_freeze: 0,can_recall: 0,can_whitelist: 0}},{account: 'eosio.token', //合约name: 'issue', //方法authorization: [{actor: 'testtesttest',permission: 'active'}],data: {to: 'testtest1111',quantity: '1000.0000 GAT',memo: "testtesttest issue 1000 GAT to testtest1111"}}]}// options -- example: {broadcast: false}
).then(result => console.log(result))
27- 获取合约abi和wasm文件
eos.getRawCodeAndAbi(contract_name)
eosjs简明使用手册(最新版)相关推荐
- Juniper 防火墙简明配置手册
Juniper SRX防火墙简明配置手册 SRX系列防火墙是Juniper公司基于JUNOS操作系统的安全系列产品,JUNOS集成了路由.交换.安全性和一系列丰富的网络服务.目前Juniper公司的 ...
- catia过载属性使用方法_catia简明操作手册
Catia 简明操作手册 1 . 登陆方法: 运行,输入以下 IP ,进入 catia 数据的服务器: 用户名及密码均为 designer 进入 YRS 中,将 Map-VPLM 文件拷贝到桌面上,并 ...
- 802d简明调试手册_SINUMERIK-828D简明调试手册.pdf
SINUMERIK 828D / 828D BASIC 简明调试手册 SINUMERIK Answers for industry. SIEMENS A B C 01.2012 A SINUMERIK ...
- LDMS 8.8 简明使用手册之客户端配置及部署
LDMS 8.8 简明使用手册之客户端配置及部署 转载于:https://blog.51cto.com/itlingm/407202
- alibaba java_阿里巴巴JAVA开发手册最新版插件Alibaba Java Coding Guidelines安装及使用...
阿里巴巴JAVA开发手册最新版插件Alibaba Java Coding Guidelines安装及使用 发布时间:2018-04-20 14:40, 浏览次数:887 , 标签: JAVA Alib ...
- Fortran简明自学手册
Fortran简明自学手册 因为CSDN不支持对Fortran代码的高亮显示,这儿贴出 学习手册PDF版百度网盘的下载链接: https://pan.baidu.com/s/1A_6N0TQERg2n ...
- 【2022HVV系列】蓝队手册更新版(建议收藏)
本文首发于[2022HVV系列]蓝队手册更新版(建议收藏) 综合篇 青藤云安全出品: 倒计时!你需要这份<2022攻防演练蓝队防守指南> 攻防演练合集 | 3个阶段,4大要点,蓝队防守全流 ...
- sicktim571操作手册_TIM3xx简明操作手册
1 TIM3xx 简明操作手册 TIM3xx 系列激光扫描器是一款智能区域扫描传感器. 得益于其小巧轻便的外形及对于激光扫描器而言极低的功耗, 非常适合于 AGV/RGV/ 机器 人及其他用于室内设备 ...
- hydrus1d使用说明_hydrus1d简明使用手册.doc
HYDRUS-1D 简明手册 PAGE PAGE 16 HYDRUS-1D简明手册 用HYDRUS-1D模拟剖面变饱和度地下水流(简明手册) 王旭升 中国地质大学(北京) 目 录 TOC \o &qu ...
最新文章
- python培训出来的有公司要吗-参加Python培训后真的能找到工作吗?上海Python培训...
- CTFshow 命令执行 web75
- asp.net前台与后台访问
- UltraVNC反向连接方式的使用
- cron 每年执行一次_循环执行定时任务crontab
- 两款JSON类库Jackson与JSON-lib的性能对比(新增第三款测试)
- 推荐算法--推荐系统架构(06)
- 实体零售全渠道及数字化发展报告(2020)
- BZOJ.3524.[POI2014]Couriers(主席树)
- [摘录]调动员工积极性的七个关键
- 写, 读sdcard目录上的文件
- hadoop的几个常用命令
- 基于Axis1.4的webservice接口开发(环境搭建)
- EAUML日拱一卒-活动图::活动分区
- STM32 TIM1输入捕获配置
- 把华为交换机设置成(NTP)时钟源服务器
- 数据分析师常用工具有哪些?初学者必看
- SpringBoot序幕——SpringBoot概述及其项目创建
- 去掉flash四周虚线框的方法
- 轮胎行业通过APS实现自动排产