Skip to content

Commit

Permalink
fix: wallet event data types
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfreska committed Jun 21, 2024
1 parent c0fcf04 commit 4e047f4
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 21 deletions.
8 changes: 8 additions & 0 deletions .changeset/clean-eggs-compare.md
Original file line number Diff line number Diff line change
@@ -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
16 changes: 7 additions & 9 deletions apps/walletd/contexts/events/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
38 changes: 31 additions & 7 deletions apps/walletd/contexts/events/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
import BigNumber from 'bignumber.js'
import { WalletEvent } from '@siafoundation/walletd-types'

export function calculateScOutflow(e: WalletEvent) {
export function calculateScValue(e: WalletEvent) {
// TODO: how do we calculate the value of a v2 transaction?
if (e.type === 'v2Transaction') {
return undefined
}
if (e.type === 'v1Transaction') {
return v1TxnCalculateScInflow(e).minus(v1TxnCalculateScOutflow(e))
}
return new BigNumber(e.data.siacoinElement.siacoinOutput.value)
}

export function calculateSfValue(e: WalletEvent) {
if (e.type === 'v2Transaction') {
return undefined
}
if (e.type === 'v1Transaction') {
return v1TxnCalculateSfInflow(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) => {
Expand All @@ -13,7 +34,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) => {
Expand All @@ -26,7 +47,7 @@ export function calculateSfOutflow(e: WalletEvent) {
return 0
}

export function calculateScInflow(e: WalletEvent) {
export function v1TxnCalculateScInflow(e: WalletEvent) {
if ('transaction' in e.data) {
const siacoinOutputs = e.data.transaction.siacoinOutputs || []
return siacoinOutputs.reduce((acc, o) => {
Expand All @@ -39,7 +60,7 @@ export function calculateScInflow(e: WalletEvent) {
return new BigNumber(0)
}

export function calculateSfInflow(e: WalletEvent) {
export function v1TxnCalculateSfInflow(e: WalletEvent) {
if ('transaction' in e.data) {
const siafundOutputs = e.data.transaction.siafundOutputs || []
return siafundOutputs.reduce((acc, o) => {
Expand All @@ -53,17 +74,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
}
Expand Down
16 changes: 11 additions & 5 deletions libs/walletd-types/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
V2Transaction,
V2FileContractResolutionType,
Address,
V2FileContractElement,
} from '@siafoundation/types'

export type GatewayPeer = {
Expand All @@ -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[]
}
Expand Down Expand Up @@ -73,7 +78,8 @@ export type WalletEventTransactionV2 = WalletEventBase & {
export type WalletEventContractResolutionV1 = WalletEventBase & {
type: 'v1ContractResolution'
data: {
fileContract: FileContractElement
// TODO: named `parent` on v1 contract resolution too?
parent: FileContractElement
siacoinElement: SiacoinElement
missed: boolean
}
Expand All @@ -82,7 +88,7 @@ export type WalletEventContractResolutionV1 = WalletEventBase & {
export type WalletEventContractResolutionV2 = WalletEventBase & {
type: 'v2ContractResolution'
data: {
fileContract: FileContractElement
parent: V2FileContractElement
resolution: V2FileContractResolutionType
siacoinElement: SiacoinElement
missed: boolean
Expand All @@ -92,7 +98,7 @@ export type WalletEventContractResolutionV2 = WalletEventBase & {
export type WalletEventMinerPayout = WalletEventBase & {
type: 'miner'
data: {
siacoinOutput: SiacoinElement
siacoinElement: SiacoinElement
}
}

Expand All @@ -106,7 +112,7 @@ export type WalletEventSiafundClaim = WalletEventBase & {
export type WalletEventFoundationSubsidy = WalletEventBase & {
type: 'foundation'
data: {
siacoinOutput: SiacoinElement
siacoinElement: SiacoinElement
}
}

Expand Down

0 comments on commit 4e047f4

Please sign in to comment.