0.3.1 • Published 4 years ago

libra-sdk-mol v0.3.1

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

Javascript SDK for Libra Network and Move Smart Contract powered by MoveOnLibra OpenAPI.

Introduction

The MoveOnLibra/mol open API is used to develop Libra applications and smart contracts, aiming to make life as a Libra developer easier. The API is built using RESTful endpoints and standard HTTP verbs.

  • Http status codes are used to indicate the status of the API call.
  • JSON is returned on all our API responses, including errors, with a consistent structure for all messages.
  • Text fields support UTF-8.

Installation

Using npm:

$ npm install libra-sdk-mol

Using bower:

$ bower install libra-sdk-mol

Using yarn:

$ yarn add libra-sdk-mol

Using cdn:

<script src="https://unpkg.com/libra-sdk-mol/dist/moveonlibra.browser.js"></script>

Usage

In nodejs:

const LibraClient = require('libra-sdk-mol');
var client = new LibraClient("testnet");
try{
	address = "000000000000000000000000000000000000000000000000000000000a550c18";
	data = await client.addressAPI.getAccountBalance(address);
    console.log(data.balance);
} catch (error) {
    console.log(error);
}

In browser:

<script src="https://unpkg.com/libra-sdk-mol/dist/moveonlibra.browser.js"></script>
<script>
	var client = new LibraClient("testnet");
	try{
		address = "000000000000000000000000000000000000000000000000000000000a550c18";
		data = await client.addressAPI.getAccountBalance(address);
		console.log(data.balance);
	} catch (error) {
		console.log(error);
	}
</script>

Mint some coins to address

// mint 123 micro-libra to address, the call is blocked until the mint is finished or failed.
tx = await client.transactionAPI.mint(address, 123);
assert 123 == tx.raw_txn.payload.Script.args[1].U64;
// get the transaction status
console.log(tx.transaction_info.major_status)

Query the income and expenditure of an address

For example, get the latest 5 incomes of an address:

events = await client.eventAPI.getAccountEventsLatestReceived(address, 5);
for(var i in events) {
	console.log(events[i].type_tag.Struct.name);//"ReceivedPaymentEvent";
	console.log(events[i].event_data_decode.amount) //received amount in micro-libra
}

Get the latest 5 expenditure of an address:

events = await client.eventAPI.getAccountEventsLatestSent(address, 5);
for(var i in events) {
	console.log(events[i].type_tag.Struct.name);//"SentPaymentEvent"
	console.log(events[i].event_data_decode.amount); //sent amount in micro-libra
}

Get the latest 5 income and expenditure in one call of an address:

events = await client.eventAPI.getAccountEventsLatest(address, 5);
console.log(events["sent"])
console.log(events["received"])

About API Authorization

You can access the MoveOnLibra public API without any prerequisites, there are 3 groups of public API:

  • Address, all address API is public available.
  • Event, all events API is public available.
  • Transaction, all GET method API under Transaction group is public available.

Following API need a token to access:

  • Wallet, all wallet API need API token to access.
  • Move, (TODO) all move API need API token to access, currently not available.
  • Transaction, all POST method API under Transaction group need API token to access except mint.

Access to MoveOnLibra's protected API requires authorization. You need to sign up to get your appkey.

For example, using appkey to create wallet and account:

var client = new LibraClient("testnet", appkey);
wallet = await client.walletAPI.createWallet(name)
account = await client.walletAPI.createWalletAccount(wallet)

Example Project

We wrote an Libra wallet demo using libra-sdk-mol package, the source code is here libra-wallet-demo.

The online access url of the wallet is https://www.moveonlibra.com/wallet.html.

API List

AddressAPI

MethodHTTP requestDescription
getAccountStateGET /v1/address/{address}Get account state from address. also automatically decode account resource of this address. btw, an address can have many other resources.
getAccountResourceGET /v1/address/account_resource/{address}Get account resource from address.
getAccountBalanceGET /v1/address/balance/{address}Get account balance from address
getAccountSequenceGET /v1/address/sequence_number/{address}Get account sequence_number from address

AuthkeyAPI

MethodHTTP requestDescription
postClientKeyPOST /v1/authkey/clientsCreate a new client api key
getClientKeysGET /v1/authkey/clientsGet all activate client api key
debugKeyGET /v1/authkey/debugDebug auth key. get it's appid , type , issue_time and expire_time etc.

EventAPI

MethodHTTP requestDescription
getAccountEventsSentGET /v1/events/sent/{address}Get events sent from this account by address
getAccountEventsReceivedGET /v1/events/received/{address}Get events received from this account by address
getAccountEventsLatestSentGET /v1/events/latest/sent/{address}Get latest events sent from this account by address
getAccountEventsLatestReceivedGET /v1/events/latest/received/{address}Get latest events received from this account by address
getAccountEventsGET /v1/events/{address}Get events both sent and received of this account by address
getAccountEventsLatestGET /v1/events/latest/{address}Get latest events both sent and received of this account by address

LibraAPI

MethodHTTP requestDescription
getVmErrorCodesGET /v1/libra/vm_errorsGet all error codes and explanations of vm errors when execute transactions
getVmErrorCodeGET /v1/libra/vm_errors/{code}Get error explanation for a vm error by code

TransactionAPI

MethodHTTP requestDescription
getTransactionGET /v1/transactions/{version}Get a transaction by version id
getTransactionsGET /v1/transactionsGet a list of transactions by start_version and limit
getLatestVersionGET /v1/transactions/latest_versionGet the latest version number of transactions in libra blockchain
getTransactionLatestGET /v1/transactions/latestGet a list of latest transactions, return number transactions
getByAccountSeqGET /v1/transactions/acc_seqGet transaction by account_address and account sequence_number
waitTransactionGET /v1/transactions/wait_for_transactionWait transaction finish it's execution on blockchain
mintPOST /v1/transactions/mintMint micro-libra coins to an account.
mintMolPOST /v1/transactions/mint_molMol supported mint service to mint micro-libra coins to an account.
createAccountPOST /v1/transactions/create_accountCreate account on the blockchain.
p2pTransferPOST /v1/transactions/transferTransfer micro-libra coins from sender account to receiver_account_address.

WalletAPI

MethodHTTP requestDescription
createWalletPOST /v1/walletsCreate a wallet belongs to the app authorized by auth key
getWalletsGET /v1/walletsGet all wallets managed by the app authorized by auth key
getWalletGET /v1/wallets/{wallet_id}Get a wallet by id which should belongs to authorized app
backupWalletGET /v1/wallets/backup/{wallet_id}Backup a wallet by id which should belongs to authorized app
createWalletAccountPOST /v1/wallets/{wallet_id}/accountsCreate an account in wallet
getWalletAccountsGET /v1/wallets/{wallet_id}/accountsGet all accounts of a wallet
0.3.1

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago