A lightweight JavaScript library for Unichain
This code is based on giekaton and Libra project
To install Unichain gRPC on Node.js, open your terminal and run:
npm install unichain-grpc --save
var unichain = require('unichain-grpc');
// Init gRPC client
var client = new unichain.Client('testnet-1.unichain.world:8000');
// Get account state
var params = {
address: Buffer.from('435fc8fc85510cf38a5b0cd6595cbb8fbb10aa7bb3fe9ad9820913ba867f79d4', 'hex')
};
client.request('get_account_state', params, function(err, result) {
console.log(err, result);
});
// If a callback is not provided, a Promise is returned
client.request('get_account_state', params).then(function(result) {
console.log(result);
});
Get the latest state for an account.
var params = {
address: Buffer.from('435fc8fc85510cf38a5b0cd6595cbb8fbb10aa7bb3fe9ad9820913ba867f79d4', 'hex')
};
client.request('get_account_state', params, function(err, result) {
console.log(err, result);
});
Get the committed transaction by account and sequence number.
var params = {
account: Buffer.from('435fc8fc85510cf38a5b0cd6595cbb8fbb10aa7bb3fe9ad9820913ba867f79d4', 'hex'),
sequence_number: 1,
fetch_events: true
};
client.request('get_account_transaction_by_sequence_number', params, function(err, result) {
console.log(err, result);
});
Get event by account and path.
var params = {
access_path: {
address: Buffer.from('435fc8fc85510cf38a5b0cd6595cbb8fbb10aa7bb3fe9ad9820913ba867f79d4', 'hex')
},
start_event_seq_num: 2,
ascending: true,
limit: 10
};
client.request('get_events_by_event_access_path', params, function(err, result) {
console.log(err, result);
});
Get the committed transaction by range
var params = {
start_version: 1,
limit: 10,
fetch_events: true
};
client.request('get_transactions', params, function(err, result) {
console.log(err, result);
});
Deserialize the raw bytes into a raw transaction object
var rawTxnBytes = 'CiAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACiQTjj/zqDoBRr0AQq/AUxJQlJBVk0KAQAHAUoAAAAGAAAAA1AAAAAGAAAADFYAAAAFAAAADVsAAAAEAAAABV8AAAAzAAAABJIAAAAgAAAAB7IAAAANAAAAAAAAAQACAAMAAQQAAgACBAIDAgQCBjxTRUxGPgxMaWJyYUFjY291bnQJTGlicmFDb2luBG1haW4PbWludF90b19hZGRyZXNzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQIABAAMAAwBEQECEiQIARIgGw7O7WUWHZkVsSi1zRwRzkktcCs7ls26I/IZpt1t7qYSChIIAOH1BQAAAAA=';
var rawTx = unichain.utils.deserializeRawTxnBytes(rawTxnBytes);
console.log('Raw transaction', rawTx);
MIT.