libra-sdk-mol v0.3.1
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
Method | HTTP request | Description |
---|---|---|
getAccountState | GET /v1/address/{address} | Get account state from address. also automatically decode account resource of this address. btw, an address can have many other resources. |
getAccountResource | GET /v1/address/account_resource/{address} | Get account resource from address. |
getAccountBalance | GET /v1/address/balance/{address} | Get account balance from address |
getAccountSequence | GET /v1/address/sequence_number/{address} | Get account sequence_number from address |
AuthkeyAPI
Method | HTTP request | Description |
---|---|---|
postClientKey | POST /v1/authkey/clients | Create a new client api key |
getClientKeys | GET /v1/authkey/clients | Get all activate client api key |
debugKey | GET /v1/authkey/debug | Debug auth key. get it's appid , type , issue_time and expire_time etc. |
EventAPI
Method | HTTP request | Description |
---|---|---|
getAccountEventsSent | GET /v1/events/sent/{address} | Get events sent from this account by address |
getAccountEventsReceived | GET /v1/events/received/{address} | Get events received from this account by address |
getAccountEventsLatestSent | GET /v1/events/latest/sent/{address} | Get latest events sent from this account by address |
getAccountEventsLatestReceived | GET /v1/events/latest/received/{address} | Get latest events received from this account by address |
getAccountEvents | GET /v1/events/{address} | Get events both sent and received of this account by address |
getAccountEventsLatest | GET /v1/events/latest/{address} | Get latest events both sent and received of this account by address |
LibraAPI
Method | HTTP request | Description |
---|---|---|
getVmErrorCodes | GET /v1/libra/vm_errors | Get all error codes and explanations of vm errors when execute transactions |
getVmErrorCode | GET /v1/libra/vm_errors/{code} | Get error explanation for a vm error by code |
TransactionAPI
Method | HTTP request | Description |
---|---|---|
getTransaction | GET /v1/transactions/{version} | Get a transaction by version id |
getTransactions | GET /v1/transactions | Get a list of transactions by start_version and limit |
getLatestVersion | GET /v1/transactions/latest_version | Get the latest version number of transactions in libra blockchain |
getTransactionLatest | GET /v1/transactions/latest | Get a list of latest transactions, return number transactions |
getByAccountSeq | GET /v1/transactions/acc_seq | Get transaction by account_address and account sequence_number |
waitTransaction | GET /v1/transactions/wait_for_transaction | Wait transaction finish it's execution on blockchain |
mint | POST /v1/transactions/mint | Mint micro-libra coins to an account. |
mintMol | POST /v1/transactions/mint_mol | Mol supported mint service to mint micro-libra coins to an account. |
createAccount | POST /v1/transactions/create_account | Create account on the blockchain. |
p2pTransfer | POST /v1/transactions/transfer | Transfer micro-libra coins from sender account to receiver_account_address. |
WalletAPI
Method | HTTP request | Description |
---|---|---|
createWallet | POST /v1/wallets | Create a wallet belongs to the app authorized by auth key |
getWallets | GET /v1/wallets | Get all wallets managed by the app authorized by auth key |
getWallet | GET /v1/wallets/{wallet_id} | Get a wallet by id which should belongs to authorized app |
backupWallet | GET /v1/wallets/backup/{wallet_id} | Backup a wallet by id which should belongs to authorized app |
createWalletAccount | POST /v1/wallets/{wallet_id}/accounts | Create an account in wallet |
getWalletAccounts | GET /v1/wallets/{wallet_id}/accounts | Get all accounts of a wallet |