Skip to content

Instantly share code, notes, and snippets.

@totiz
Last active July 28, 2019 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save totiz/d59716151d343c96354351011357b8b8 to your computer and use it in GitHub Desktop.
Save totiz/d59716151d343c96354351011357b8b8 to your computer and use it in GitHub Desktop.
Query Balance in Libra Blockchain with javascript
const { LibraWallet, LibraClient, LibraNetwork } = require('kulap-libra')
const BigNumber = require('bignumber.js')
async function createWallet() {
// Generate an account
const wallet = new LibraWallet()
const account = wallet.newAccount()
return {
address: account.getAddress().toHex(),
mnemonic: wallet.config.mnemonic
}
}
async function mint(address, amount) {
const client = new LibraClient({ network: LibraNetwork.Testnet });
const data = await client.mintWithFaucetService(address, BigNumber(amount).times(1e6));
return data
}
async function queryBalance(address) {
const client = new LibraClient({ network: LibraNetwork.Testnet });
// Fetch latest ledger state from blockchain
const accountState = await client.getAccountState(address)
// balance in micro libras
const balanceInMicroLibras = BigNumber(accountState.balance.toString())
// balance in base unit
const balace = balanceInMicroLibras.dividedBy(BigNumber(1e6))
return balace.toString(10)
}
(async () => {
// Create a wallet
const wallet = await createWallet()
console.log('wallet', wallet)
// Mint 1,000 libra coins
const mintingResult = await mint(wallet.address, 1000)
console.log('mintingResult', mintingResult)
// Check balance
const balanceResult = await queryBalance(wallet.address)
console.log('balanceResult', balanceResult)
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment