Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const { LibraWallet, LibraClient, LibraNetwork } = require('kulap-libra')
const axios = require('axios')
const BigNumber = require('bignumber.js')
async function createWallet() {
// Generate an account
// const wallet = new LibraWallet()
const wallet = new LibraWallet({
mnemonic: 'order curtain live pact market tissue rally run dolphin shuffle convince napkin spot main suffer segment excite toilet ceiling cute master six agent narrow'
});
const account = wallet.newAccount()
return {
account: account,
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, amount * 1e6);
// const { data } = await axios.post('https://libraservice3.kulap.io/mint', { address: address, amount: amount })
return data
}
async function transfer(account, address, amount) {
const client = new LibraClient({ network: LibraNetwork.Testnet });
const data = await client.transferCoins(account, address, amount)
return data
}
async function queryBalance(address) {
// const client = new LibraClient({
// transferProtocol: 'https',
// host: 'ac-libra-testnet.kulap.io',
// port: '443',
// dataProtocol: 'grpc-web-text'
// })
const client = new LibraClient({ network: LibraNetwork.Testnet });
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('create', wallet)
// Mint 1,000 libra coins
// const mintingResult = await mint(wallet.address, 250)
// console.log('mintingResult', mintingResult)
const trnasferResult = await transfer(wallet.account, '449e2293e3c9599c2cb4db181d5d193f7acb6a6682e3c25fbad7502f3b989bf3', 10e6)
console.log('trnasferResult', trnasferResult)
// Query balance
const balance = await queryBalance(wallet.address)
console.log('aaa')
console.log('balance', balance)
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.