Last active
May 2, 2020 23:35
-
-
Save SilentCicero/fb854c440dbc615df6ff419f2c33bd06 to your computer and use it in GitHub Desktop.
The NodeJS example code from the recent Fuel tutorial blog post.
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 { Wallet, utils, dbs } = require("fuel-core"); | |
(async () => { | |
// DB and Key Setup | |
const db = new dbs.Level(); // persisting db | |
const privateKey = (await db.get('key')) || utils.randomBytes(32); | |
await db.put('key', privateKey); | |
const signer = new utils.SigningKey(privateKey); | |
console.log('My Fuel address is :', signer.address); | |
// Wallet and Faucet of Fake Dai | |
const wallet = new Wallet({ db, signer }); | |
// We allow the faucet to throw once the amount per IP limit is reached | |
try { await wallet.faucet(); } catch (e) {} | |
// Listen for Incoming Transaction Data | |
await wallet.listen(async () => { | |
console.log('Balance updates in transit', | |
utils.formatEther(await wallet.balance(wallet.tokens.fakeDai))); | |
}); | |
// Transfer of Fake Dai to Wallets | |
await wallet.transfer(utils.parseEther('1.5'), wallet.tokens.fakeDai, wallet.address); | |
// Fin. | |
console.log('You transfered 1.5 Fake Dai to yourself, congrats!'); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment