@pixagram/dpixa
v1.3.8
Published
Pixa blockchain RPC client library
Maintainers
Readme
dpixa
Robust pixa client library that runs in both node.js and the browser.
Needs test net urls, chain id
note As of version 0.7.0 WebSocket support has been removed. The only transport provided now is HTTP(2). For most users the only change required is to swap wss:// to https:// in the address. If you run your own full node make sure to set the proper CORS headers if you plan to access it from a browser.
Browser compatibility
Installation
Via npm
For node.js or the browser with browserify or webpack.
npm install @pixagram/dpixaFrom cdn or self-hosted script
Grab dist/dpixa.js from git and include in your html:
<script src="@pixagram/dpixa.js"></script>Or from the unpkg cdn:
<script src="https://unpkg.com/@pixagram/dpixa@latest/dist/dpixa.js"></script>Make sure to set the version you want when including from the cdn, you can also use dpixa@latest but that is not always desirable. See unpkg.com for more information.
Usage
In the browser
<script src="https://unpkg.com/@pixagram/dpixa@latest/dist/dpixa.js"></script>
<script>
var client = new dpixa.Client(["https://api.pixagram.io", "https://api.hivekings.com", "https://anyx.io", "https://api.openhive.network"]);
client.database
.getDiscussions("trending", { tag: "writing", limit: 1 })
.then(function(discussions) {
document.body.innerHTML += "<h1>" + discussions[0].title + "</h1>";
document.body.innerHTML += "<h2>by " + discussions[0].author + "</h2>";
document.body.innerHTML +=
'<pre style="white-space: pre-wrap">' + discussions[0].body + "</pre>";
});
</script>See the demo source for an example on how to setup a livereloading TypeScript pipeline with wintersmith and browserify.
In node.js
With TypeScript:
import { Client } from "@pixagram/dpixa";
const client = new Client(["https://api.pixagram.io", "https://api.hivekings.com", "https://anyx.io", "https://api.openhive.network"]);
for await (const block of client.blockchain.getBlocks()) {
console.log(`New block, id: ${block.block_id}`);
}With JavaScript:
var dpixa = require("@pixagram/dpixa");
var client = new dpixa.Client(["https://api.pixagram.io", "https://api.hivekings.com", "https://anyx.io", "https://api.openhive.network"]);
var key = dpixa.PrivateKey.fromLogin("username", "password", "posting");
client.broadcast
.vote(
{
voter: "username",
author: "almost-digital",
permlink: "dpixa-is-the-best",
weight: 10000
},
key
)
.then(
function(result) {
console.log("Included in block: " + result.block_num);
},
function(error) {
console.error(error);
}
);With ES2016 (node.js 7+):
const { Client } = require("@pixagram/dpixa");
const client = new Client(["https://api.pixagram.io", "https://api.hivekings.com", "https://anyx.io", "https://api.openhive.network"]);
async function main() {
const props = await client.database.getChainProperties();
console.log(`Maximum blocksize consensus: ${props.maximum_block_size} bytes`);
client.disconnect();
}
main().catch(console.error);With node.js streams:
var dpixa = require("@pixagram/dpixa");
var es = require("event-stream"); // npm install event-stream
var util = require("util");
var client = new dpixa.Client(["https://api.pixagram.io", "https://api.hivekings.com", "https://anyx.io", "https://api.openhive.network"]);
var stream = client.blockchain.getBlockStream();
stream
.pipe(
es.map(function(block, callback) {
callback(null, util.inspect(block, { colors: true, depth: null }) + "\n");
})
)
.pipe(process.stdout);Bundling
The easiest way to bundle dpixa (with browserify, webpack etc.) is to just npm install @pixagram/dpixa and require('@pixagram/dpixa') which will give you well-tested (see browser compatibility matrix above) pre-bundled code guaranteed to JustWork™. However, that is not always desirable since it will not allow your bundler to de-duplicate any shared dependencies dpixa and your app might have.
To allow for deduplication you can require('@pixagram/dpixa/lib/index-browser'), or if you plan to provide your own polyfills: require('@pixagram/dpixa/lib/index'). See src/index-browser.ts for a list of polyfills expected.
Share and Enjoy!
