Restructured Step 2 - 7 to "code along" style

This commit is contained in:
AlexanderBuzz
2023-07-05 10:44:15 +02:00
parent bad6ce54ba
commit b46e8a6ada
9 changed files with 761 additions and 270 deletions

View File

@@ -1,75 +0,0 @@
const async = require('async')
const { app, BrowserWindow } = require('electron')
const path = require('path')
const xrpl = require("xrpl")
const TESTNET_URL = "wss://s.altnet.rippletest.net:51233"
/**
* This function sends a ledger-request and returns the response
*
* @param client
* @returns {Promise<*>}
*/
const getValidatedLedgerData = async (client) => {
// Reference: https://xrpl.org/ledger.html#ledger
const ledgerRequest = {
"command": "ledger",
"ledger_index": "validated"
}
const ledgerResponse = await client.request(ledgerRequest)
return ledgerResponse.result
}
/**
* This function creates our application window
*
* @returns {Electron.CrossProcessExports.BrowserWindow}
*/
const createWindow = () => {
const appWindow = new BrowserWindow({
width: 1024,
height: 768,
webPreferences: {
preload: path.join(__dirname, 'view', '2_preload.js'),
},
})
appWindow.loadFile(path.join(__dirname, 'view', '2_async.html'))
return appWindow
}
/**
* This function creates a XRPL client, continuously polls the XRPL with a ledger-request and broadcasts
* the result by dispatching the 'update-ledger-data' event which will be picked up by the frontend
*
* @returns {Promise<void>}
*/
const main = async () => {
const appWindow = createWindow()
const client = new xrpl.Client(TESTNET_URL)
await client.connect()
async.forever(
function(next) {
getValidatedLedgerData(client).then((value) => {
appWindow.webContents.send('update-ledger-data', value)
})
setTimeout(function() {
next()
}, 2500)
},
function(err) {
// if next is called with a value in its first parameter, it will appear
// in here as 'err', and execution will stop.
}
)
}
app.whenReady().then(main)

View File

@@ -19,7 +19,7 @@ const createWindow = () => {
}, },
}) })
appWindow.loadFile(path.join(__dirname, 'view', '2_async.html')) appWindow.loadFile(path.join(__dirname, 'view', '2_async-subscribe.html'))
return appWindow return appWindow
} }

View File

@@ -47,6 +47,14 @@ const main = async () => {
appWindow.webContents.send('update-ledger-data', ledger) appWindow.webContents.send('update-ledger-data', ledger)
}) })
// Initial Ledger Request -> Get account details on startup
// Reference: https://xrpl.org/ledger.html
const ledgerResponse = await client.request({
"command": "ledger"
})
const initialLedgerData = prepareLedgerData(ledgerResponse.result.closed.ledger)
appWindow.webContents.send('update-ledger-data', initialLedgerData)
// Reference: https://xrpl.org/subscribe.html#transaction-streams // Reference: https://xrpl.org/subscribe.html#transaction-streams
client.on("transaction", async (transaction) => { client.on("transaction", async (transaction) => {
// Reference: https://xrpl.org/account_info.html // Reference: https://xrpl.org/account_info.html
@@ -67,8 +75,8 @@ const main = async () => {
"account": address, "account": address,
"ledger_index": "current" "ledger_index": "current"
}) })
const accountData = prepareAccountData(accountInfoResponse.result.account_data) const initialAccountData = prepareAccountData(accountInfoResponse.result.account_data)
appWindow.webContents.send('update-account-data', accountData) appWindow.webContents.send('update-account-data', initialAccountData)
}) })
} }

View File

@@ -6,7 +6,6 @@ const xrpl = require("xrpl");
// Reference: https://xrpl.org/basic-data-types.html // Reference: https://xrpl.org/basic-data-types.html
const RIPPLE_EPOCH = 946684800; const RIPPLE_EPOCH = 946684800;
const prepareAccountData = (rawAccountData, reserve) => { const prepareAccountData = (rawAccountData, reserve) => {
const numOwners = rawAccountData.OwnerCount || 0 const numOwners = rawAccountData.OwnerCount || 0
@@ -25,7 +24,7 @@ const prepareAccountData = (rawAccountData, reserve) => {
} }
const prepareLedgerData = (rawLedgerData) => { const prepareLedgerData = (rawLedgerData) => {
const timestamp = RIPPLE_EPOCH + rawLedgerData.ledger_time const timestamp = RIPPLE_EPOCH + (rawLedgerData.ledger_time ?? rawLedgerData.close_time)
const dateTime = new Date(timestamp * 1000) const dateTime = new Date(timestamp * 1000)
const dateTimeString = dateTime.toLocaleDateString() + ' ' + dateTime.toLocaleTimeString() const dateTimeString = dateTime.toLocaleDateString() + ' ' + dateTime.toLocaleTimeString()
@@ -36,7 +35,6 @@ const prepareLedgerData = (rawLedgerData) => {
} }
} }
const prepareReserve = (ledger) => { const prepareReserve = (ledger) => {
const reserveBaseXrp = xrpl.dropsToXrp(ledger.reserve_base) const reserveBaseXrp = xrpl.dropsToXrp(ledger.reserve_base)
const reserveIncrementXrp = xrpl.dropsToXrp(ledger.reserve_inc) const reserveIncrementXrp = xrpl.dropsToXrp(ledger.reserve_inc)

View File

@@ -5,7 +5,6 @@
"scripts": { "scripts": {
"hello": "electron ./0_hello.js", "hello": "electron ./0_hello.js",
"ledger-index": "electron ./1_ledger-index.js", "ledger-index": "electron ./1_ledger-index.js",
"async-poll": "electron ./2_async-poll.js",
"async-subscribe": "electron ./2_async-subscribe.js", "async-subscribe": "electron ./2_async-subscribe.js",
"account": "electron ./3_account.js", "account": "electron ./3_account.js",
"tx-history": "electron ./4_tx-history.js", "tx-history": "electron ./4_tx-history.js",

View File

@@ -1,4 +1,3 @@
// Step 3 code additions - start
document.addEventListener('DOMContentLoaded', openAccountAddressDialog); document.addEventListener('DOMContentLoaded', openAccountAddressDialog);
function openAccountAddressDialog(){ function openAccountAddressDialog(){
@@ -19,19 +18,19 @@ function openAccountAddressDialog(){
accountAddressDialog.showModal() accountAddressDialog.showModal()
} }
// Step 3 code additions - end
const ledgerIndexEl = document.getElementById('ledger-index') const ledgerIndexEl = document.getElementById('ledger-index')
const ledgerHashEl = document.getElementById('ledger-hash') const ledgerHashEl = document.getElementById('ledger-hash')
const ledgerCloseTimeEl = document.getElementById('ledger-close-time') const ledgerCloseTimeEl = document.getElementById('ledger-close-time')
// Step 3 - Section start - this remains as it is, the rest is new
window.electronAPI.onUpdateLedgerData((_event, ledger) => { window.electronAPI.onUpdateLedgerData((_event, ledger) => {
ledgerIndexEl.innerText = ledger.ledgerIndex ledgerIndexEl.innerText = ledger.ledgerIndex
ledgerHashEl.innerText = ledger.ledgerHash ledgerHashEl.innerText = ledger.ledgerHash
ledgerCloseTimeEl.innerText = ledger.ledgerCloseTime ledgerCloseTimeEl.innerText = ledger.ledgerCloseTime
}) })
// Step 3 - Section end
// Step 3 code additions - start
const accountAddressClassicEl = document.getElementById('account-address-classic') const accountAddressClassicEl = document.getElementById('account-address-classic')
const accountAddressXEl = document.getElementById('account-address-x') const accountAddressXEl = document.getElementById('account-address-x')
const accountBalanceEl = document.getElementById('account-balance') const accountBalanceEl = document.getElementById('account-balance')
@@ -43,4 +42,3 @@ window.electronAPI.onUpdateAccountData((_event, value) => {
accountBalanceEl.innerText = value.xrpBalance accountBalanceEl.innerText = value.xrpBalance
accountReserveEl.innerText = value.xrpReserve accountReserveEl.innerText = value.xrpReserve
}) })
// Step 3 code additions - end

View File

@@ -101,4 +101,4 @@ window.electronAPI.onSendXrpTransactionFinish((_event) => {
destinationTagEl.value = '' destinationTagEl.value = ''
amountEl.value = '' amountEl.value = ''
}) })
// Step 7 code additions - start // Step 7 code additions - end