UNPKG

fuel-core

Version:

Essential javascript architecture for the Fuel side chain

248 lines (185 loc) 6.34 kB
// require('regenerator-runtime'); const axios = require('axios'); const PubNub = require('pubnub'); // Chain ID const __chainId = '5'; // Listen const listen = async (account, cb) => { try { // CB, empty callback method let __cb = () => {}; // uuid const uuid = PubNub.generateUUID(); // Set global cb __cb = cb; const pubnub = new PubNub({ subscribeKey: "sub-c-11502102-5035-11ea-814d-0ecb550e9de2", uuid, // ssl: true, // option to turn that off? }); pubnub.addListener({ message: async msg => { try { if (cb) { __cb(null, result); } } catch (error) { __cb(error, null); } }, error: msg => { __cb(msg, null); }, }); return pubnub.subscribe({ channels: [ String('0x' + __chainId + account.slice(2)).toLowerCase(), ], }); } catch (error) { console.log(error); } }; // Get users balance from Lambda async function getBalance(account) { try { return (await axios .post('https://fuel-lambda.fuellabs.now.sh/balance', { address: account, tokenID: '0x01', })).data.result; } catch (error) { console.log(error); } } (async ()=> { try { console.log('initial balances', await getBalance('0x1C5A77d9FA7eF466951B2F01F724BCa3A5820b63'), await getBalance('0x5526B0E157f217dB52C9b56268D3e999A9c53bf5')); await listen('0x1C5A77d9FA7eF466951B2F01F724BCa3A5820b63', async () => { console.log('balance 1 updated!', await getBalance('0x1C5A77d9FA7eF466951B2F01F724BCa3A5820b63')); }); await listen('0x5526B0E157f217dB52C9b56268D3e999A9c53bf5', async () => { console.log('balance 2 updated!', await getBalance('0x5526B0E157f217dB52C9b56268D3e999A9c53bf5')); }); } catch (error) { console.log(error); } })(); /* import { Wallet, utils, dbs } from "../index"; const signer = new utils.SigningKey(utils.randomBytes(32)); // warning: not secure entropy generation.. const { faucet, transfer, sync, tokens, balance, db, listen } = new Wallet({ signer, }); (async ()=> { console.log('Pre-faucet balance', utils.formatEther(await balance(tokens.fakeDai))); await listen(async () => { try { console.log('Listen balance', utils.formatEther(await balance(tokens.fakeDai))); } catch (error) { console.error(error); } }); setTimeout(async () => { console.time('Faucet'); await faucet(); // get 100^18 fakeDai console.timeEnd('Faucet'); console.time('Transfer'); await transfer(500, tokens.fakeDai, signer.address); // send 500^1 fakeDai console.timeEnd('Transfer'); console.log('Post-transfer balance', utils.formatEther(await balance(tokens.fakeDai))); console.time('Transfer'); await transfer(500, tokens.fakeDai, signer.address); // send 500^1 fakeDai console.timeEnd('Transfer'); console.time('Transfer'); await transfer(500, tokens.fakeDai, signer.address); // send 500^1 fakeDai console.timeEnd('Transfer'); // await sync(); console.log('Post-sync balance', utils.formatEther(await balance(tokens.fakeDai))); }, 1000); })(); */ /* async function app() { try { const signer = new utils.SigningKey('0xdb96fced0d365dfd65468164d38c82f6af6fb2e8fb2227b0198a370eaa48a1c3'); // warning: not secure entropy generation.. const { faucet, transfer, tokens, balance, blockNumber } = new Wallet({ signer, db: new dbs.Memory(), }); // Test new chain.. /* console.log(await post('https://fuel-lambda.now.sh/get', { key: '0x07', })); */ // console.log(await blockNumber()); // await faucet(); // await transfer(500, tokens.fakeDai, signer.address); // console.log(await balance(tokens.fakeDai)); // const signer = new utils.SigningKey(utils.randomBytes(32)); // warning: not secure entropy generation.. // const { faucet, transfer, tokens, sync, balance } = new Wallet({ signer, db: new dbs.Index(), api: 'https://fuel-lambda.now.sh/' }); // await faucet(); // await sync(); // await transfer(500, tokens.fakeDai, signer.address); // await sync(); // console.log(utils.formatEther(await balance(tokens.fakeDai))); // const signer = new utils.SigningKey(utils.randomBytes(32)); // const signer2 = new utils.SigningKey(utils.randomBytes(32)); // utils.randomBytes(32)); // const { faucet, transfer, balance, tokenID, tokens, post, sync } = new Wallet({ signer, db: new dbs.Index() }); // await sync(); // await faucet(); // console.log(await tokenID(tokens.fakeDai)); // console.log(utils.formatEther(await balance(tokens.fakeDai))); /* await sync(); console.time('Transact'); await transfer(utils.parseEther('1.3'), tokens.fakeDai, signer2.address); console.timeEnd('Transact'); //await transfer(utils.parseEther('1.5'), tokens.fakeDai, signer2.address); await transfer(utils.parseEther('1.4'), tokens.fakeDai, signer2.address); */ // console.log(utils.formatEther(await balance(tokens.fakeDai))); // console.log(utils.formatEther(await balance(tokens.fakeDai))); /* console.log(await post('https://fuel-lambda.now.sh/account', { // https://fuel-lambda.now.sh/account address: '0x99b722ccd2d6baf0325bf9524bc2e5d3411330b3', })); */ /* storage: '0x00', token: '0x01', tokenID: '0x02', block: '0x03', transactionRoot: '0x04', deposit: '0x05', Deposit: '0x05', UTXO: '0x06', blockTip: '0x07', account: '0x08', withdraw: '0x09', transaction: '0x10', ethereumBlockProcessed: '0x11', lastEthereumFraudBlock: '0x12', numTokens: '0x13', mempool: '0x14', mempoolSpend: '0x15', mempoolTransaction: '0x16', withdrawal: '0x17', } catch (error) { console.log(error); } } app() .then(console.log) .catch(console.log); */ /* const signer = new utils.SigningKey(utils.randomBytes(32)); // warning: not secure entropy generation.. const { faucet, transfer, tokens } = new Wallet({ signer, db: new dbs.LocalStorage() }); await faucet(); await transfer(500, tokens.fakeDai, signer.address); */