1.0.0 • Published 6 months ago

@taiyinet/ctaiyi v1.0.0

Weekly downloads
-
License
-
Repository
github
Last release
6 months ago

ctaiyi Build Status Coverage Status Package Version

Robust taiyi blockchain client library that runs in both node.js and the browser.

Installation

Via npm

For node.js or the browser with browserify or webpack.

npm install @taiyinet/ctaiyi

From cdn or self-hosted script

Grab dist/ctaiyi.js from a release and include in your html:

<script src="ctaiyi.js"></script>

Or from the unpkg cdn:

<script src="https://unpkg.com/@taiyinet/ctaiyi@^1.0.0/dist/ctaiyi.js"></script>

Make sure to set the version you want when including from the cdn, you can also use ctaiyi@latest but that is not always desirable. See unpkg.com for more information.

Usage

In the browser

<script src="https://unpkg.com/@taiyinet/ctaiyi@latest/dist/ctaiyi.js"></script>
<script>
    var client = new ctaiyi.Client('https://api.taiyi.com')
    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 browserify.

In node.js

With TypeScript:

import {Client} from '@taiyinet/ctaiyi'

const client = new Client('https://api.taiyi.com')

for await (const block of client.blockchain.getBlocks()) {
    console.log(`New block, id: ${ block.block_id }`)
}

With JavaScript:

var ctaiyi = require('@taiyinet/ctaiyi')

var client = new ctaiyi.Client('https://api.taiyi.com')
var key = ctaiyi.PrivateKey.fromLogin('username', 'password', 'posting')

client.broadcast.vote({
    voter: 'username',
    author: 'almost-digital',
    permlink: 'ctaiyi-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('@taiyinet/ctaiyi')

const client = new Client('https://api.taiyi.com')

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 ctaiyi = require('@taiyinet/ctaiyi')
var es = require('event-stream') // npm install event-stream
var util = require('util')

var client = new ctaiyi.Client('https://api.taiyi.com')

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 ctaiyi (with browserify, webpack etc.) is to just npm install @taiyinet/ctaiyi and require('@taiyinet/ctaiyi') which will give you well-tested 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 ctaiyi and your app might have.

To allow for deduplication you can require('@taiyinet/ctaiyi/lib/index-browser'), or if you plan to provide your own polyfills: require('@taiyinet/ctaiyi/lib/index'). See src/index-browser.ts for a list of polyfills expected.


Share and Enjoy!