eosjs的使用

1 安装eosjs

首先建立一个文件夹,进入该文件夹下面执行下列命令安装eosjs库:

~/eosjsPro$ sudo npm install eosjs
npm WARN saveError ENOENT: no such file or directory, open '/home/lzj/eosjsPro/package.json'
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN enoent ENOENT: no such file or directory, open '/home/lzj/eosjsPro/package.json'
npm WARN eosjsPro No description
npm WARN eosjsPro No repository field.
npm WARN eosjsPro No README data
npm WARN eosjsPro No license field.

+ eosjs@16.0.9
added 40 packages from 18 contributors and audited 139 packages in 73.859s
found 0 vulnerabilities

安装以后文件夹下面多了俩个文件夹:

lzj@lzj-VirtualBox:~/eosjsPro$ ls
node_modules  package-lock.json

然后新建一个js文件eosjstest.js。新建一个keys.json文件,文件内容用eosio的公私钥:

[
    "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
    "5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3"
]

2 运行第一个例子

eosjstest.js:

Eos = require('eosjs')
var keys = require("./keys.json");
 
// Optional configuration..
config = {
  keyProvider: keys, // 配置私钥字符串
  httpEndpoint: 'http://192.168.1.75:8888', //DEV开发链url与端口
  chainId: "38dc6e87bb9f6b33a21dd56b9f2c24813d83c71009077279c8d746fbd0eaf09d", // 通过cleos get info可以获取chainId
  expireInSeconds: 60,
  broadcast: true,
  debug: false,
  sign: true,
  authorization: null // 该参数用于在多签名情况下,识别签名帐号与权限,格式如:account@permission
}
 
eos = Eos(config)

eos.getInfo({}).then(result => { 
	console.log(result) 
})

运行程序:

~/eosjsPro$ node eosjstest.js 
{ server_version: '40a20769',
  chain_id:
   '38dc6e87bb9f6b33a21dd56b9f2c24813d83c71009077279c8d746fbd0eaf09d',
  head_block_num: 512936,
  last_irreversible_block_num: 512935,
  last_irreversible_block_id:
   '0007d3a7d5f4a570b573b9e24e22fbcc29d2481beb88fa869ce1d21785640e15',
  head_block_id:
   '0007d3a8015c9dc004580b0779a17e4fe08443d47167584c4842ad7ec7e13c7f',
  head_block_time: '2018-12-12T10:09:35.500',
  head_block_producer: 'eosio',
  virtual_block_cpu_limit: '104857600000',
  virtual_block_net_limit: 1048576000,
  block_cpu_limit: 104857500,
  block_net_limit: 1048576 }

3 eosjs的其它操作

获取区块

eos.getInfo({}).then(result => { 
	console.log(result) 
})

查询余额

eos.getCurrencyBalance({ code: "eosio.token", account: "acca", symbol: "SYS" }).then(result =>{ 
	console.log(result)
})

查询账户交易记录

eos.getActions({account_name: "eosio.token", pos: 0, offset: 6}).then(result=>{
	console.log(result)
})

转账

eos.contract("eosio.token").then((token) => {
        token.transfer("acca", "accb", "100.0000 SYS", "transfer").then((result) => {
            console.log(result);     
        }).catch((err) => {
            console.log(err);
        });
    });

查询交易

eos.getTransaction({id: "6b5160acea47d518f8056119aa718893b36f4c45016090f1ecf4399f0d1bc693"}).then(result =>{
	console.log(result)
})

新建账号

//新建账号
eos.transaction(tr => {
    tr.newaccount({
        creator: "eosio.token",
        name: "accnew",
        owner: "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
        active: "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV"
    })
    
    //为新账号充值RAM
    tr.buyrambytes({
        payer: "eosio.token",
        receiver: "accnew",
        bytes: 8192
    })
 
    //为新账号抵押CPU和NET资源
    tr.delegatebw({
        from: "eosio.token",
        receiver: "accnew",
        stake_net_quantity: '1.0000 DEV',
        stake_cpu_quantity: '1.0000 DEV',
        transfer: 0
    })
})

调用智能合约

eos.contract("acchello").then(hello => {  //hello随便起的变量名
    hello.hi('acchello', {                         //hi是方法名, 'axay'是该hello合约hi方法的参数
    	authorization: ['acchello']        //testtesttest是建立该合约的用户
    }).then(result => {
        console.log("hello",result);
    });
})

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值