From 4806f299d3c8f840f96d0d9098483b6fab118c6b Mon Sep 17 00:00:00 2001 From: Alex Freska Date: Fri, 21 Jun 2024 13:39:34 -0400 Subject: [PATCH] fix: wallet event data types --- .changeset/clean-eggs-compare.md | 8 ++++ apps/walletd/contexts/events/index.tsx | 16 ++++---- apps/walletd/contexts/events/utils.ts | 51 ++++++++++++++++++++++---- libs/walletd-types/src/types.ts | 15 +++++--- 4 files changed, 69 insertions(+), 21 deletions(-) create mode 100644 .changeset/clean-eggs-compare.md diff --git a/.changeset/clean-eggs-compare.md b/.changeset/clean-eggs-compare.md new file mode 100644 index 000000000..9c2061e06 --- /dev/null +++ b/.changeset/clean-eggs-compare.md @@ -0,0 +1,8 @@ +--- +'walletd': patch +'@siafoundation/walletd-types': patch +'@siafoundation/walletd-js': patch +'@siafoundation/walletd-react': patch +--- + +Fixed a few inaccuracies in the event data types. Closes https://github.com/SiaFoundation/walletd/issues/141 diff --git a/apps/walletd/contexts/events/index.tsx b/apps/walletd/contexts/events/index.tsx index b3488b650..1052f812a 100644 --- a/apps/walletd/contexts/events/index.tsx +++ b/apps/walletd/contexts/events/index.tsx @@ -21,10 +21,8 @@ import { useSiascanUrl } from '../../hooks/useSiascanUrl' import { defaultDatasetRefreshInterval } from '../../config/swr' import { useSyncStatus } from '../../hooks/useSyncStatus' import { - calculateScInflow, - calculateScOutflow, - calculateSfInflow, - calculateSfOutflow, + calculateScValue, + calculateSfValue, getContractId, getFee, } from './utils' @@ -69,8 +67,8 @@ export function useEventsMain() { return null } const dataTxPool: EventData[] = responseTxPool.data.map((e) => { - const amountSc = calculateScInflow(e).minus(calculateScOutflow(e)) - const amountSf = calculateSfInflow(e) - calculateSfOutflow(e) + const amountSc = calculateScValue(e) + const amountSf = calculateSfValue(e) const fee = getFee(e) const event: EventData = { id: e.id, @@ -84,9 +82,9 @@ export function useEventsMain() { } return event }) - const dataEvents: EventData[] = responseEvents.data.map((e, index) => { - const amountSc = calculateScInflow(e).minus(calculateScOutflow(e)) - const amountSf = calculateSfInflow(e) - calculateSfOutflow(e) + const dataEvents: EventData[] = responseEvents.data.map((e) => { + const amountSc = calculateScValue(e) + const amountSf = calculateSfValue(e) const fee = getFee(e) const contractId = getContractId(e) const isMature = e.maturityHeight <= syncStatus.nodeBlockHeight diff --git a/apps/walletd/contexts/events/utils.ts b/apps/walletd/contexts/events/utils.ts index 592023055..bc6795910 100644 --- a/apps/walletd/contexts/events/utils.ts +++ b/apps/walletd/contexts/events/utils.ts @@ -1,7 +1,27 @@ import BigNumber from 'bignumber.js' import { WalletEvent } from '@siafoundation/walletd-types' -export function calculateScOutflow(e: WalletEvent) { +export function calculateScValue(e: WalletEvent) { + if (e.type === 'v2Transaction') { + return txnCalculateScInflow(e).minus(v2TxnCalculateScOutflow(e)) + } + if (e.type === 'v1Transaction') { + return txnCalculateScInflow(e).minus(v1TxnCalculateScOutflow(e)) + } + return new BigNumber(e.data.siacoinElement.siacoinOutput.value) +} + +export function calculateSfValue(e: WalletEvent) { + if (e.type === 'v2Transaction') { + return txnCalculateSfInflow(e) - v2TxnCalculateSfOutflow(e) + } + if (e.type === 'v1Transaction') { + return txnCalculateSfInflow(e) - v1TxnCalculateSfOutflow(e) + } + return undefined +} + +export function v1TxnCalculateScOutflow(e: WalletEvent) { if ('spentSiacoinElements' in e.data) { const siacoinElements = e.data.spentSiacoinElements return siacoinElements.reduce((acc, o) => { @@ -13,7 +33,7 @@ export function calculateScOutflow(e: WalletEvent) { } } -export function calculateSfOutflow(e: WalletEvent) { +export function v1TxnCalculateSfOutflow(e: WalletEvent) { if ('spentSiafundElements' in e.data) { const siafundElements = e.data.spentSiafundElements || [] return siafundElements.reduce((acc, o) => { @@ -26,7 +46,21 @@ export function calculateSfOutflow(e: WalletEvent) { return 0 } -export function calculateScInflow(e: WalletEvent) { +export function v2TxnCalculateScOutflow(e: WalletEvent) { + if (e.type === 'v2Transaction') { + return new BigNumber(e.data.siacoinInputs[0].parent.siacoinOutput.value) + } + return undefined +} + +export function v2TxnCalculateSfOutflow(e: WalletEvent) { + if (e.type === 'v2Transaction') { + return e.data.siafundInputs[0].parent.siafundOutput.value + } + return undefined +} + +export function txnCalculateScInflow(e: WalletEvent) { if ('transaction' in e.data) { const siacoinOutputs = e.data.transaction.siacoinOutputs || [] return siacoinOutputs.reduce((acc, o) => { @@ -39,7 +73,7 @@ export function calculateScInflow(e: WalletEvent) { return new BigNumber(0) } -export function calculateSfInflow(e: WalletEvent) { +export function txnCalculateSfInflow(e: WalletEvent) { if ('transaction' in e.data) { const siafundOutputs = e.data.transaction.siafundOutputs || [] return siafundOutputs.reduce((acc, o) => { @@ -53,17 +87,20 @@ export function calculateSfInflow(e: WalletEvent) { } export function getFee(e: WalletEvent) { - return 'transaction' in e.data + if (e.type === 'v2Transaction') { + return new BigNumber(e.data.minerFee) + } + return 'transaction' in e.data && e.data.transaction.minerFees?.length ? new BigNumber(e.data.transaction.minerFees[0]) : undefined } export function getContractId(e: WalletEvent) { if (e.type === 'v1ContractResolution') { - return e.data.fileContract.id + return e.data.parent.id } if (e.type === 'v2ContractResolution') { - return e.data.fileContract.id + return e.data.parent.id } return undefined } diff --git a/libs/walletd-types/src/types.ts b/libs/walletd-types/src/types.ts index ab03f5bd0..911b2c0bd 100644 --- a/libs/walletd-types/src/types.ts +++ b/libs/walletd-types/src/types.ts @@ -12,6 +12,7 @@ import { V2Transaction, V2FileContractResolutionType, Address, + V2FileContractElement, } from '@siafoundation/types' export type GatewayPeer = { @@ -25,10 +26,14 @@ export type GatewayPeer = { syncDuration?: number } +export type UnconfirmedChainIndex = { + height: number +} + export type WalletEventBase = { id: Hash256 timestamp: string - index: ChainIndex + index: ChainIndex | UnconfirmedChainIndex maturityHeight: number relevant: Address[] } @@ -73,7 +78,7 @@ export type WalletEventTransactionV2 = WalletEventBase & { export type WalletEventContractResolutionV1 = WalletEventBase & { type: 'v1ContractResolution' data: { - fileContract: FileContractElement + parent: FileContractElement siacoinElement: SiacoinElement missed: boolean } @@ -82,7 +87,7 @@ export type WalletEventContractResolutionV1 = WalletEventBase & { export type WalletEventContractResolutionV2 = WalletEventBase & { type: 'v2ContractResolution' data: { - fileContract: FileContractElement + parent: V2FileContractElement resolution: V2FileContractResolutionType siacoinElement: SiacoinElement missed: boolean @@ -92,7 +97,7 @@ export type WalletEventContractResolutionV2 = WalletEventBase & { export type WalletEventMinerPayout = WalletEventBase & { type: 'miner' data: { - siacoinOutput: SiacoinElement + siacoinElement: SiacoinElement } } @@ -106,7 +111,7 @@ export type WalletEventSiafundClaim = WalletEventBase & { export type WalletEventFoundationSubsidy = WalletEventBase & { type: 'foundation' data: { - siacoinOutput: SiacoinElement + siacoinElement: SiacoinElement } }