Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
@glif/filecoin-rpc-client
Advanced tools
a convenience library for interacting with the Lotus jsonrpc
A convenience library for interacting with the Lotus JSON-RPC api. We'll be actively updating and maintaining this library as needed for our use in the Filecoin web wallet. Several of the methods we're using are documented here.
npm i @glif/filecoin-rpc-client
const LotusRPCEngine = require('@glif/filecoin-rpc-client')
const lotusJWT = 'aaaaaaaa.bbbbbbbbbbbb.i_ZZZZZZ-3xYYYYYY'
const config = {
// defaults to local as seen below
apiAddress: 'http://127.0.0.1:1234/rpc/v0',
token: lotusJWT,
}
const lotusRPC = new LotusRpcEngine(config)
const chainHead = await lotusRPC.request('ChainHead')
This library is simply a wrapper around Lotus' JSON-RPC. To send requests, follow this pattern:
lotusRPC.request(rpcmethod, arg1, arg2....etc) where the "methodName" corresponds to each available JSON-RPC method (found here). After the method name, each argument is passed to Lotus' JSON-RPC in the same order.
For example, the WalletBalance
JSON-RPC method takes a single argument, "address". Therefore, we should structure the request like:
const address = 't1jdlfl73voaiblrvn2yfivvn5ifucwwv5f26nfza'
const balance = await lotusRPC.request('WalletBalance', address)
When making a request to Lotus' JSON-RPC server, a success response looks like:
const successResponse = {
jsonrpc: '2.0',
result: 'fake response result',
id: 1,
}
The result
key is the return data as specified in the Lotus api. The Lotus JSON-RPC Engine will just return you the result
, and not any of the other data.
const errorResponse = {
jsonrpc: '2.0',
result: '',
id: 1,
error: {
code: 1,
message: 'Specific error returned by Lotus API',
},
}
This functionality may change, but for now, the Lotus JSON-RPC Engine will throw an error if the Lotus API comes back with an error, so be sure to catch them:
try {
// send a malformed request (need to pass an address)
await lotusRPC.request('WalletBalance')
} catch (err) {
console.log(err)
// 'get actor: GetActor called on undefined address' <== error directly passed from Lotus' API
}
Several JSON-RPC methods take Cid
as arguments. We need to structure a cid as:
const cid = {
'/': 'bafy2bzacedavr434vvbck3nkowrffk2x67lgl7kfujlw3lyj2zjvmihn5rf7i',
}
const message = await lotusRPC.request('ChainGetMessage', cid)
If you're planning on using this library in a web browser communicating with a raw Lotus node, you will run into CORS issues. There are a couple ways around this:
If you're planning on forking this lib or developing with it locally, use the build script:
npm run build
Please make sure an existing issue doesn't already cover your problem before asking. If you don't see a similar issue, go ahead and file a new one in the repository.
FAQs
a convenience library for interacting with the Lotus jsonrpc
The npm package @glif/filecoin-rpc-client receives a total of 524 weekly downloads. As such, @glif/filecoin-rpc-client popularity was classified as not popular.
We found that @glif/filecoin-rpc-client demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.