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 14 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)
)
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
)
},

getScheduledRewards: () => wallet.getScheduledRewards(),
getWalletBalance: () => wallet.getBalance(),
Expand All @@ -132,9 +139,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
59 changes: 59 additions & 0 deletions main/test/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict'

const { formatTokenValue } = require('../utils')
const assert = require('assert').strict

describe('formatTokenValue', function () {
it('should correctly round a typical value', function () {
const input = 0.004567687
const expectedOutput = 0.004568
const result = formatTokenValue(input)
assert.equal(result, expectedOutput)
})
it('should reduce a big number to 6 decimal places',
function () {
const input = 123.45678923456
const expectedOutput = 123.456789
const result = formatTokenValue(input)
assert.equal(result, expectedOutput)
})
it('should reduce a very small number to 6 decimal places',
function () {
const input = 0.000123456789
const expectedOutput = 0.000123
const result = formatTokenValue(input)
assert.equal(result, expectedOutput)
})
it('should round up', function () {
const input = 0.0001289
const expectedOutput = 0.000129
const result = formatTokenValue(input)
assert.equal(result, expectedOutput)
})
it('should map 0 to 0', function () {
const input = 0
const expectedOutput = 0
const result = formatTokenValue(input)
assert.equal(result, expectedOutput)
})
it('should leave a big integer alone', function () {
const input = 123456789
const expectedOutput = 123456789
const result = formatTokenValue(input)
assert.equal(result, expectedOutput)
})
it('should show 6 decimal places for numbers smaller than 0.000001',
function () {
const input = 0.000000000123456789
const expectedOutput = 0.000000
const result = formatTokenValue(input)
assert.equal(result, expectedOutput)
})
it('should not add zeroes to a number that is has no trailing zeroes',
function () {
const input = 123.4
const expectedOutput = 123.4
const result = formatTokenValue(input)
assert.equal(result, expectedOutput)
})
})
42 changes: 1 addition & 41 deletions main/test/wallet.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
'use strict'

const {
getTransactionsForUI,
formatWithSixDecimalDigits
} = require('../wallet')
const { getTransactionsForUI } = require('../wallet')
const assert = require('assert').strict
const ethers = require('ethers')

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

Expand Down Expand Up @@ -103,39 +99,3 @@ describe('Wallet', function () {
})
})
})

describe('formatWithSixDecimalDigits', function () {
it('keeps six decimal digits only', function () {
assert.strictEqual(
formatWithSixDecimalDigits(ethers.BigNumber.from('1654759687033008')),
'0.001654'
)
})

it('rounds down', function () {
assert.strictEqual(
formatWithSixDecimalDigits(ethers.BigNumber.from('1999999999999999')),
'0.001999'
)
})

it('strips trailing zeroes after rounding down', function () {
assert.strictEqual(
formatWithSixDecimalDigits(ethers.BigNumber.from('1000000000000001')),
'0.001'
)
})
it('strips trailing zeroes when no rounding is needed', function () {
assert.strictEqual(
formatWithSixDecimalDigits(ethers.BigNumber.from('1000000000000000')),
'0.001'
)
})

it('preserves .0 at the end', function () {
assert.strictEqual(
formatWithSixDecimalDigits(ethers.BigNumber.from('1000000000000000000')),
'1.0'
)
})
})
6 changes: 3 additions & 3 deletions main/tray.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { ipcMainEvents } = require('./ipc')
const path = require('path')
const assert = require('node:assert')
const core = require('./core')
const { roundToSixDecimalPlaces } = require('./utils')
const { formatTokenValue } = require('./utils')

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

Expand Down Expand Up @@ -61,13 +61,13 @@ const createContextMenu = (/** @type {Context} */ ctx) => {
{
label:
`Wallet Balance: ${
roundToSixDecimalPlaces(ctx.getWalletBalance())
formatTokenValue(ctx.getWalletBalance())
} FIL`,
enabled: false
},
{
label: `Scheduled Rewards: ${
roundToSixDecimalPlaces(ctx.getScheduledRewards())
formatTokenValue(ctx.getScheduledRewards())
} FIL`,
enabled: false
},
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;

getScheduledRewards(): string;
getWalletBalance(): string;
Expand All @@ -40,7 +42,6 @@ export interface Context {

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

export interface WalletSeed {
Expand Down
20 changes: 13 additions & 7 deletions main/utils.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
'use strict'

/**
* @param { number | string | undefined } amount
* @returns { string }
*/
* @param {string | number | undefined} input
* @returns {number}
*/
// Keep in sync with renderer/src/number-ops.ts
function formatTokenValue (input) {
const number = Number(input)
if (!input) return 0
if (Number.isInteger(number)) return number
// decimal cases below
return Number(number.toFixed(6))
}

exports.roundToSixDecimalPlaces = (amount) => {
if (!amount) return '0'
const roundedNumber = Number(amount).toFixed(6) // Round to 6 decimal places
return roundedNumber.toString()
module.exports = {
formatTokenValue
}
32 changes: 10 additions & 22 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,6 @@ backend.transactions = loadStoredEntries()
/** @type {Context | null} */
let ctx = null
let balance = loadBalance()
let scheduledRewards = loadScheduledRewards()

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

/**
* @param {ethers.BigNumber} amount
* @returns {string}
*/
function formatWithSixDecimalDigits (amount) {
const fullPrecision = ethers.utils.formatUnits(amount, 18)
const [whole, fraction] = fullPrecision.split('.')
if (fraction === undefined) return fullPrecision
const truncated = fraction
// keep the first 6 digits, discard the rest
.slice(0, 6)
// remove trailing zeroes as long as there are some leading digits
// (we want to preserve .0 if there is no fraction)
.replace(/(\d)0+$/, '$1')
return [whole, truncated].join('.')
return formatEther(loadScheduledRewards())
}

// Inline `p-debounce.promise` from
Expand Down Expand Up @@ -147,9 +130,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,7 +244,7 @@ module.exports = {
getAddress,
getBalance,
getScheduledRewards,
formatWithSixDecimalDigits,
setScheduledRewards,
listTransactions,
transferAllFundsToDestinationWallet,
getTransactionsForUI
Expand Down
Loading