Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read scheduled rewards from core #1168

Merged
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions main/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const { randomUUID } = require('node:crypto')
const { Activities } = require('./activities')
const { Logs } = require('./logs')
const split2 = require('split2')
const { parseEther } = require('ethers/lib/utils')

/** @typedef {import('./typings').Context} Context */

Expand Down Expand Up @@ -84,6 +85,10 @@ async function start (ctx) {
case 'jobs-completed':
totalJobsCompleted = event.total
ctx.setTotalJobsCompleted(event.total)
wallet.setScheduledRewards(
parseEther(event.rewardsScheduledForAddress || 0)
)
ctx.setScheduledRewardsForAddress(event.rewardsScheduledForAddress)
break
case 'activity:info':
case 'activity:error': {
Expand Down
10 changes: 7 additions & 3 deletions main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ const ctx = {
count
)
},
getScheduledRewardsForAddress: () => wallet.getScheduledRewards(),
setScheduledRewardsForAddress: (balance) => {
ipcMain.emit(
ipcMainEvents.SCHEDULED_REWARDS_UPDATE,
balance
)
},

manualCheckForUpdates: () => { throw new Error('never get here') },
saveModuleLogsAs: () => { throw new Error('never get here') },
Expand All @@ -129,9 +136,6 @@ const ctx = {
},
balanceUpdate: (balance) => {
ipcMain.emit(ipcMainEvents.BALANCE_UPDATE, balance)
},
scheduledRewardsUpdate: (balance) => {
ipcMain.emit(ipcMainEvents.SCHEDULED_REWARDS_UPDATE, balance)
}
}

Expand Down
5 changes: 4 additions & 1 deletion main/ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ function setupIpcMain (/** @type {Context} */ ctx) {
(_event, address) => stationConfig.setDestinationWalletAddress(address)
)
ipcMain.handle('station:getStationWalletBalance', wallet.getBalance)
ipcMain.handle('station:getScheduledRewards', wallet.getScheduledRewards)
ipcMain.handle(
'station:getScheduledRewards',
ctx.getScheduledRewardsForAddress
)
ipcMain.handle(
'station:getStationWalletTransactionsHistory',
wallet.listTransactions
Expand Down
3 changes: 2 additions & 1 deletion main/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export interface Context {
getActivities(): Activity[];
setTotalJobsCompleted(count: number): void;
getTotalJobsCompleted(): number;
setScheduledRewardsForAddress(balance: string): void;
getScheduledRewardsForAddress(): string;

showUI: () => void;
isShowingUI: boolean;
Expand All @@ -37,7 +39,6 @@ export interface Context {

transactionUpdate: (transactions: (FILTransaction|FILTransactionProcessing)[]) => void;
balanceUpdate: (balance:string) => void;
scheduledRewardsUpdate: (balance: string) => void;
}

export interface WalletSeed {
Expand Down
15 changes: 11 additions & 4 deletions main/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const timers = require('node:timers/promises')
const Store = require('electron-store')
const { WalletBackend } = require('./wallet-backend')
const { ethers } = require('ethers')
const { formatEther } = require('ethers/lib/utils')

/** @typedef {import('./typings').Context} Context */
/** @typedef {import('./typings').FILTransaction} FILTransaction */
Expand All @@ -30,7 +31,7 @@ backend.transactions = loadStoredEntries()
/** @type {Context | null} */
let ctx = null
let balance = loadBalance()
let scheduledRewards = loadScheduledRewards()
const scheduledRewards = loadScheduledRewards()

/**
* @param {Context} _ctx
Expand Down Expand Up @@ -84,7 +85,7 @@ function getBalance () {
* @returns {string}
*/
function getScheduledRewards () {
return formatWithSixDecimalDigits(scheduledRewards)
return formatEther(scheduledRewards)
}

/**
Expand Down Expand Up @@ -147,9 +148,14 @@ async function updateScheduledRewards () {

async function _updateScheduledRewards () {
assert(ctx)
scheduledRewards = await backend.fetchScheduledRewards()
ctx.setScheduledRewardsForAddress(getScheduledRewards())
}

/**
* @param {ethers.BigNumber} scheduledRewards
*/
async function setScheduledRewards (scheduledRewards) {
walletStore.set('scheduled_rewards', scheduledRewards.toHexString())
ctx.scheduledRewardsUpdate(getScheduledRewards())
}

function listTransactions () {
Expand Down Expand Up @@ -256,6 +262,7 @@ module.exports = {
getAddress,
getBalance,
getScheduledRewards,
setScheduledRewards,
formatWithSixDecimalDigits,
listTransactions,
transferAllFundsToDestinationWallet,
Expand Down
3 changes: 2 additions & 1 deletion renderer/src/pages/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import ActivityLog from '../components/ActivityLog'
import UpdateBanner from '../components/UpdateBanner'
import WalletWidget from '../components/WalletWidget'
import useStationActivity from '../hooks/StationActivity'
import { roundToSixDecimalPlaces } from '../utils/number-ops'

const scheduledRewardsTooltip = `
This is the reward total you have accrued since your last payout.
Expand Down Expand Up @@ -50,7 +51,7 @@ const Dashboard = (): JSX.Element => {
className="w-fit text-header-m font-bold font-number total-earnings"
title={scheduledRewardsTooltip}
>
{scheduledRewards}<span className="text-header-3xs">&nbsp;FIL</span>
{roundToSixDecimalPlaces(scheduledRewards)}<span className="text-header-3xs">&nbsp;FIL</span>
</p>
</div>
</div>
Expand Down
5 changes: 5 additions & 0 deletions renderer/src/utils/number-ops.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function roundToSixDecimalPlaces (number: string | number | undefined) {
if (!number) return 0
const roundedNumber = Number(number).toFixed(6) // Round to 6 decimal places
return roundedNumber.toString()
}