FilMeta 2.0 RPC api js client
cnpm i @bitrainforest/lotus-client-rpc.js --save
npm i @bitrainforest/lotus-client-rpc.js --save
yarn add @bitrainforest/lotus-client-rpc.js
pnpm add @bitrainforest/lotus-client-rpc.js
本库为调用 filMeta RPC
的接口库,本库必须配合 filmeta-client-protocols.js
库使用。
在使用本库前需使用 filmeta-client-rpc.js
库初始化 HTTP
或 WebSocket
协议。
-
LotusRPC: 接口基类,通过此类进行接口初始化。
Param Type Description Sample provider
filmeta-client-rpc.js
的Protocol
协议
new Protocol("ws://127.0.0.1:1234/rpc/v0", ProtocolType.WebSocket,{})
options
any
初始化选项,目前选项只有 schema
{schema: schema.chain}
-
schema: 模式枚举类型,通过此类来确定具体调用哪个
schema
,用来传入options
。 -
其余的暴露内容都为数据类型声明。
import { Protocol, ProtocolType, JsonRpcV2Error } from "@bitrainforest/filmeta-client-protocols.js";
import { LotusRPC, schema, } from "../src";
const token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IiIsImVtYWlsIjoiIiwicGVybWlzc2lvbnMiOlsicmVhZCIsIndyaXRlIiwiY2x1c3RlciIsInNpZ24iLCJhZG1pbiJdLCJleHBpcmVkX2F0IjoiMDAwMS0wMS0wMVQwMDowMDowMFoifQ.1GojU4WIhgQamPmwGA7szeHcJWlWYP5PE8BV-KdnW1Y"
const protocol = new Protocol("ws://127.0.0.1:1234/rpc/v0", ProtocolType.WebSocket,{auth: {token}})
const chainRpc = new LotusRPC(protocol,{schema: schema.chain})
初始化传参可参考上面的 [ Usage - Export - LotusRPC ]
。
在初始化的时候,需要根据不同的 schema
来初始化不同的 RPC Client
,并使用初始化后的 Client
来调用相关接口。
chainRpc.chainHead().then(
(res: any)=>{
console.log(res)
},
(err: JsonRpcV2Error)=>{
console.log(err)
},
)
直接使用相应 schema
初始化的 Client
来调用相关的接口,参数则直接传入调用的接口中即可。
订阅事件调用后会自动订阅,返回值与协议的订阅返回值相同。
chainRpc.destroy()
如你使用的协议是长链接,则可以使用 destroy
来进行关闭长链接。
const { Protocol, ProtocolType } = require("@bitrainforest/filmeta-client-protocols.js") ;
const { LotusRPC, schema } = require("@bitrainforest/filmeta-client-rpc.js");
const token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IiIsImVtYWlsIjoiIiwicGVybWlzc2lvbnMiOlsicmVhZCIsIndyaXRlIiwiY2x1c3RlciIsInNpZ24iLCJhZG1pbiJdLCJleHBpcmVkX2F0IjoiMDAwMS0wMS0wMVQwMDowMDowMFoifQ.1GojU4WIhgQamPmwGA7szeHcJWlWYP5PE8BV-KdnW1Y"
const protocol = new Protocol("ws://127.0.0.1:1234/rpc/v0", ProtocolType.WebSocket,{auth: {token}})
const chainRpc = new LotusRPC(protocol,{schema: schema.chain})
chainRpc.chainHead().then(
(res)=>{
console.log(res)
},
(err)=>{
console.log(err)
},
)
import { Protocol, ProtocolType, JsonRpcV2Error } from "@bitrainforest/filmeta-client-protocols.js";
import { LotusRPC, schema, } from "@bitrainforest/filmeta-client-rpc.js";
const token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IiIsImVtYWlsIjoiIiwicGVybWlzc2lvbnMiOlsicmVhZCIsIndyaXRlIiwiY2x1c3RlciIsInNpZ24iLCJhZG1pbiJdLCJleHBpcmVkX2F0IjoiMDAwMS0wMS0wMVQwMDowMDowMFoifQ.1GojU4WIhgQamPmwGA7szeHcJWlWYP5PE8BV-KdnW1Y"
const protocol = new Protocol("ws://127.0.0.1:1234/rpc/v0", ProtocolType.WebSocket,{auth: {token}})
const chainRpc = new LotusRPC(protocol,{schema: schema.chain})
chainRpc.chainHead().then(
(res: any)=>{
console.log(res)
},
(err: JsonRpcV2Error)=>{
console.log(err)
},
)