UNPKG

bdledger-sdk

Version:
95 lines (69 loc) 2.15 kB
# BDLedger SDK for Node.js [npm package](https://www.npmjs.com/package/bdledger-sdk) ## Installation ```shell npm install --save bdledger-sdk ``` Set [esModuleInterop](https://www.typescriptlang.org/tsconfig#esModuleInterop) to `true` in your `tsconfig.json. ## Usage Example: ```typescript import { Bdledger, blockToHexView, transactionToHexView } from 'bdledger-sdk' const bdledger = new Bdledger('localhost:2401') test() async function test() { const version = await bdledger.clientVersion() console.log(version) const ledgers = await bdledger.getLedgers() console.log(ledgers) const tx = await bdledger.getTransactionByHash({ ledger: 'default', hash: Buffer.from('52e050aa413dc5a3b32488c4fd0aefb103f8e667', 'hex'), }) if (tx) { console.dir(transactionToHexView(tx)) } let b = await bdledger.getBlockByHash({ ledger: 'default', hash: Buffer.from('572b7531addccbee30ebb5d9a77544a0f2229cb0', 'hex'), fullTransactions: false, }) if (b) { console.dir(blockToHexView(b)) } b = await bdledger.getBlockByHash({ ledger: 'default', hash: Buffer.from('572b7531addccbee30ebb5d9a77544a0f2229cb0', 'hex'), fullTransactions: true, }) if (b) { console.dir(blockToHexView(b)) } } ``` Please refer to: - `test/test.js` or `test/test.ts` - [gRPC API](https://phabricator.internetapi.cn/w/public/bdledger/grpc-api/) - [gRPC examples for Node.js](https://github.com/grpc/grpc/tree/master/examples/node/static_codegen) - [`grpc` package](https://www.npmjs.com/package/grpc) - [`google-protobuf` package](https://www.npmjs.com/package/google-protobuf) ## Development ```shell npm install git submodule update --remote ``` Run in `api/grpc` ```shell npm install ``` Generate gRPC code with: ```shell npm run genGrpc ``` Generated gRPC code will be in `src/grpc` Reference: [gRPC examples for Node.js](https://github.com/grpc/grpc/tree/master/examples/node/static_codegen) ## Testing (TODO) 1. Download [mock server](https://public.internetapi.cn/?dir=bdledger/test) 2. Run mock servers with `--port={{port}}` 3. Set the port in `test/client.test.js` 4. Run `npm test`