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

fix: wallet event data types #646

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
51 changes: 44 additions & 7 deletions apps/walletd/contexts/events/utils.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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
}
Expand Down
15 changes: 10 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,7 @@ export type WalletEventTransactionV2 = WalletEventBase & {
export type WalletEventContractResolutionV1 = WalletEventBase & {
type: 'v1ContractResolution'
data: {
fileContract: FileContractElement
parent: FileContractElement
siacoinElement: SiacoinElement
missed: boolean
}
Expand All @@ -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
Expand All @@ -92,7 +97,7 @@ export type WalletEventContractResolutionV2 = WalletEventBase & {
export type WalletEventMinerPayout = WalletEventBase & {
type: 'miner'
data: {
siacoinOutput: SiacoinElement
siacoinElement: SiacoinElement
}
}

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

Expand Down
Loading