碎碎念
Dapp开发前端必然是要操作智能合约的,EOSJS扮演的角色就是在前端要对智能合约进行操作的API。以下是官方文档给出的描述。
Javascript API for integration with EOSIO-based blockchains using EOSIO RPC API.
大概在2019年4月,EOSJS@20.0.0稳定版发布,这和之前的版本相差还是非常大的,本博客主要采用稳定版。(稳定版是真的棒,和之前版本比,多了很多很实用的接口
安装
安装就是普通的办法。
npm install eosjs
Basic Usage
Signature Provider
看起来,Signature Provider是有了密钥后可以来为交易提供签名的东西。但是官方文档里也说了,这种方式是非常不安全的,一般也不这么用。协会TokenManager中采用的是拉起scatter钱包来为交易签名,这种方式更安全。
const defaultPrivateKey = "5JtUScZK2XEp3g9gh7F8bwtPTRAkASmNrrftmx4AxDKD5K4zDnr"; // bob
const signatureProvider = new JsSignatureProvider([defaultPrivateKey]);
类 JSON-RPC
const rpc = new JsonRpc('http://127.0.0.1:8888', { fetch });
- 实现接口
- AuthorityProvider
- AbiProvider
rpc准备
get_abi
get_abi(accountName: string): Promise<GetAbiResult>
name | type |
---|---|
accountName | string |
nodejs代码如下。
var express = require('express');
var app = express();
const { Api, JsonRpc } = require('eosjs');
const fetch = require('node-fetch'); // node only
const rpc = new JsonRpc('https://api-kylin.eoslaomao.com', { fetch });
rpc.get_abi("eosio.msig").then(res=>{console.log(res)});
Abi文件是完成智能合约之后可以生成的一个文件,Abi文件中包含了方法,调用方法需要的参数,这些参数是什么格式之类的信息。所以调用一个智能合约,可以看智能合约的Abi文件,然后调用方法的就好了。前端可以通过get_abi来获取Abi信息。
get_account
get_account(accountName: string): Promise<any>
name | type |
---|---|
accountName | string |
获得一个账户的信息。
rpc.get_account("zjubcatokens").then(res=>{console.log(res)});
命令行得到的结果。
get_currency_balance
get_currency_balance(code: string, account: string, symbol?: string): Promise<any>
name | type |
---|---|
code | string |
account | string |
Default value symbol | string |
这个接口可以用来获取特定合约上特定账户余额信息。code为合约名,如下面例子的zjubcatokens
,account表示账户名。
rpc.get_currency_balance("zjubcatokens","yangjiani233").then(res=>{console.log(res)});
结果
history_get_actions
history_get_actions(accountName: string, pos?: number, offset?: number): Promise<any>
name | type |
---|---|
accountName | string |
Default value pos | number |
Default value offset | number |
这个接口用于查询这个账户的历史交易记录。准确的说是action的记录~,pos表示要从哪一条消息开始查,offset表示你要查多少条。
rpc.history_get_actions("zjubcatokens").then(res=>{console.log(res)});
结果
这边有一个坑~因为我们是通过http接口获取的信息,在获取history action的时候,不是所有url都可以用的,这里用的是https://api-kylin.eoslaomao.com,恰好是开放了history action的接口的。
至此,以上是rpc常用的接口叭,其它接口也差不多这么用就ok了,主要看清楚传的参数是啥,如果官方文档里看的不太清楚,可能就要获取abi看看具体的东西了。
API
首先怎么获取api
const { Api, JsonRpc } = require('eosjs');
const { JsSignatureProvider } = require('eosjs/dist/eosjs-jssig'); // development only
const fetch = require('node-fetch'); // node only
const { TextEncoder, TextDecoder } = require('text-encoding'); // React Native, IE11, and Edge Browsers only
const rpc = new JsonRpc('https://api-kylin.eoslaomao.com', { fetch });
const api = new Api({ rpc, textDecoder: new TextDecoder(), textEncoder: new TextEncoder() });
和rpc一样,api中也有很多方法可以调用,调用方式也和rpc差不多,先用一个例子来讲api。这个例子讲述的是一个巨大无比的坑。
const resp = rpc.get_table_rows({
json: true, // Get the response as json
code: 'eosio.msig', // Contract that we target
scope: 'yangjiani233', // Account that owns the data
table: 'proposal', // Table name
limit: 10, // Maximum number of rows that we want to get
// reverse = false, // Optional: Get reversed data
// show_payer = false, // Optional: Show ram payer
});
这一部分的代码是用来获得提案的,(提案用eosio.msig的合约发,然后会存到合约的table里,都可以用get_table_rows的方式来获得。
但是获取到的只是提案的packed_transaction,如下图所示:
在稳定版出来之前,这一串数字好像都没有接口可以直接转换成人类可读的东西,稳定版新增了方法来解决这个问题。
deserializeTransactionWithActions
deserializeTransactionWithActions(transaction: Uint8Array | string): Promise<any>
这个接口可以把序列化后的交易内容去序列化,参数就是那一串序列
var p=resp.rows[0].packed_transaction;
api.deserializeTransactionWithActions(resp.rows[0].packed_transaction).then(res=>{
console.log(res);
});
由此可得到提案的结果: