Created
July 28, 2019 17:35
-
-
Save totiz/fe6aa7bab15a207c575da56e36a4624f to your computer and use it in GitHub Desktop.
Libra Transfer with javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { LibraWallet, LibraClient, LibraNetwork } = require('kulap-libra') | |
const BigNumber = require('bignumber.js') | |
async function createWallet() { | |
// Generate/get an account | |
// const wallet = new LibraWallet() | |
const wallet = new LibraWallet({mnemonic: 'finish topple cannon hammer eyebrow disorder enter reject long then tragic hammer diagram spy mom weapon amateur cup census sketch review consider galaxy foam'}) | |
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, 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 function transfer(account, address, amount) { | |
const client = new LibraClient({ network: LibraNetwork.Testnet }); | |
const data = await client.transferCoins(account, address, BigNumber(amount).times(1e6)) | |
return data | |
} | |
(async () => { | |
// Create a wallet | |
const wallet = await createWallet() | |
console.log('wallet', wallet) | |
// // Mint 1,000 libra coins | |
// const mintingResult = await mint(result.address, 1000) | |
// console.log('mintingResult', mintingResult) | |
// Check balance | |
const balanceResult = await queryBalance(wallet.address) | |
console.log('balance Before', balanceResult) | |
// Transfer | |
const transferResult = await transfer(wallet.account, 'e498567610d2611539817b19a610867489622a6dfbc97870ddbd2bc368d588d3', 12) | |
console.log('transferResult', transferResult.acStatus) | |
// Check balance | |
const balanceAfterResult = await queryBalance(wallet.address) | |
console.log('balance After', balanceAfterResult) | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment