Skip to content

Commit 4a27247

Browse files
committed
fix: wallet event data types
1 parent c0fcf04 commit 4a27247

File tree

4 files changed

+69
-21
lines changed

4 files changed

+69
-21
lines changed

.changeset/clean-eggs-compare.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'walletd': patch
3+
'@siafoundation/walletd-types': patch
4+
'@siafoundation/walletd-js': patch
5+
'@siafoundation/walletd-react': patch
6+
---
7+
8+
Fixed a few inaccuracies in the event data types. Closes https://github.com/SiaFoundation/walletd/issues/141

apps/walletd/contexts/events/index.tsx

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ import { useSiascanUrl } from '../../hooks/useSiascanUrl'
2121
import { defaultDatasetRefreshInterval } from '../../config/swr'
2222
import { useSyncStatus } from '../../hooks/useSyncStatus'
2323
import {
24-
calculateScInflow,
25-
calculateScOutflow,
26-
calculateSfInflow,
27-
calculateSfOutflow,
24+
calculateScValue,
25+
calculateSfValue,
2826
getContractId,
2927
getFee,
3028
} from './utils'
@@ -69,8 +67,8 @@ export function useEventsMain() {
6967
return null
7068
}
7169
const dataTxPool: EventData[] = responseTxPool.data.map((e) => {
72-
const amountSc = calculateScInflow(e).minus(calculateScOutflow(e))
73-
const amountSf = calculateSfInflow(e) - calculateSfOutflow(e)
70+
const amountSc = calculateScValue(e)
71+
const amountSf = calculateSfValue(e)
7472
const fee = getFee(e)
7573
const event: EventData = {
7674
id: e.id,
@@ -84,9 +82,9 @@ export function useEventsMain() {
8482
}
8583
return event
8684
})
87-
const dataEvents: EventData[] = responseEvents.data.map((e, index) => {
88-
const amountSc = calculateScInflow(e).minus(calculateScOutflow(e))
89-
const amountSf = calculateSfInflow(e) - calculateSfOutflow(e)
85+
const dataEvents: EventData[] = responseEvents.data.map((e) => {
86+
const amountSc = calculateScValue(e)
87+
const amountSf = calculateSfValue(e)
9088
const fee = getFee(e)
9189
const contractId = getContractId(e)
9290
const isMature = e.maturityHeight <= syncStatus.nodeBlockHeight

apps/walletd/contexts/events/utils.ts

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,27 @@
11
import BigNumber from 'bignumber.js'
22
import { WalletEvent } from '@siafoundation/walletd-types'
33

4-
export function calculateScOutflow(e: WalletEvent) {
4+
export function calculateScValue(e: WalletEvent) {
5+
if (e.type === 'v2Transaction') {
6+
return txnCalculateScInflow(e).minus(v2TxnCalculateScOutflow(e))
7+
}
8+
if (e.type === 'v1Transaction') {
9+
return txnCalculateScInflow(e).minus(v1TxnCalculateScOutflow(e))
10+
}
11+
return new BigNumber(e.data.siacoinElement.siacoinOutput.value)
12+
}
13+
14+
export function calculateSfValue(e: WalletEvent) {
15+
if (e.type === 'v2Transaction') {
16+
return txnCalculateSfInflow(e) - v2TxnCalculateSfOutflow(e)
17+
}
18+
if (e.type === 'v1Transaction') {
19+
return txnCalculateSfInflow(e) - v1TxnCalculateSfOutflow(e)
20+
}
21+
return undefined
22+
}
23+
24+
export function v1TxnCalculateScOutflow(e: WalletEvent) {
525
if ('spentSiacoinElements' in e.data) {
626
const siacoinElements = e.data.spentSiacoinElements
727
return siacoinElements.reduce((acc, o) => {
@@ -13,7 +33,7 @@ export function calculateScOutflow(e: WalletEvent) {
1333
}
1434
}
1535

16-
export function calculateSfOutflow(e: WalletEvent) {
36+
export function v1TxnCalculateSfOutflow(e: WalletEvent) {
1737
if ('spentSiafundElements' in e.data) {
1838
const siafundElements = e.data.spentSiafundElements || []
1939
return siafundElements.reduce((acc, o) => {
@@ -26,7 +46,21 @@ export function calculateSfOutflow(e: WalletEvent) {
2646
return 0
2747
}
2848

29-
export function calculateScInflow(e: WalletEvent) {
49+
export function v2TxnCalculateScOutflow(e: WalletEvent) {
50+
if (e.type === 'v2Transaction') {
51+
return new BigNumber(e.data.siacoinInputs[0].parent.siacoinOutput.value)
52+
}
53+
return undefined
54+
}
55+
56+
export function v2TxnCalculateSfOutflow(e: WalletEvent) {
57+
if (e.type === 'v2Transaction') {
58+
return e.data.siafundInputs[0].parent.siafundOutput.value
59+
}
60+
return undefined
61+
}
62+
63+
export function txnCalculateScInflow(e: WalletEvent) {
3064
if ('transaction' in e.data) {
3165
const siacoinOutputs = e.data.transaction.siacoinOutputs || []
3266
return siacoinOutputs.reduce((acc, o) => {
@@ -39,7 +73,7 @@ export function calculateScInflow(e: WalletEvent) {
3973
return new BigNumber(0)
4074
}
4175

42-
export function calculateSfInflow(e: WalletEvent) {
76+
export function txnCalculateSfInflow(e: WalletEvent) {
4377
if ('transaction' in e.data) {
4478
const siafundOutputs = e.data.transaction.siafundOutputs || []
4579
return siafundOutputs.reduce((acc, o) => {
@@ -53,17 +87,20 @@ export function calculateSfInflow(e: WalletEvent) {
5387
}
5488

5589
export function getFee(e: WalletEvent) {
56-
return 'transaction' in e.data
90+
if (e.type === 'v2Transaction') {
91+
return new BigNumber(e.data.minerFee)
92+
}
93+
return 'transaction' in e.data && e.data.transaction.minerFees?.length
5794
? new BigNumber(e.data.transaction.minerFees[0])
5895
: undefined
5996
}
6097

6198
export function getContractId(e: WalletEvent) {
6299
if (e.type === 'v1ContractResolution') {
63-
return e.data.fileContract.id
100+
return e.data.parent.id
64101
}
65102
if (e.type === 'v2ContractResolution') {
66-
return e.data.fileContract.id
103+
return e.data.parent.id
67104
}
68105
return undefined
69106
}

libs/walletd-types/src/types.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
V2Transaction,
1313
V2FileContractResolutionType,
1414
Address,
15+
V2FileContractElement,
1516
} from '@siafoundation/types'
1617

1718
export type GatewayPeer = {
@@ -25,10 +26,14 @@ export type GatewayPeer = {
2526
syncDuration?: number
2627
}
2728

29+
export type UnconfirmedChainIndex = {
30+
height: number
31+
}
32+
2833
export type WalletEventBase = {
2934
id: Hash256
3035
timestamp: string
31-
index: ChainIndex
36+
index: ChainIndex | UnconfirmedChainIndex
3237
maturityHeight: number
3338
relevant: Address[]
3439
}
@@ -73,7 +78,7 @@ export type WalletEventTransactionV2 = WalletEventBase & {
7378
export type WalletEventContractResolutionV1 = WalletEventBase & {
7479
type: 'v1ContractResolution'
7580
data: {
76-
fileContract: FileContractElement
81+
parent: FileContractElement
7782
siacoinElement: SiacoinElement
7883
missed: boolean
7984
}
@@ -82,7 +87,7 @@ export type WalletEventContractResolutionV1 = WalletEventBase & {
8287
export type WalletEventContractResolutionV2 = WalletEventBase & {
8388
type: 'v2ContractResolution'
8489
data: {
85-
fileContract: FileContractElement
90+
parent: V2FileContractElement
8691
resolution: V2FileContractResolutionType
8792
siacoinElement: SiacoinElement
8893
missed: boolean
@@ -92,7 +97,7 @@ export type WalletEventContractResolutionV2 = WalletEventBase & {
9297
export type WalletEventMinerPayout = WalletEventBase & {
9398
type: 'miner'
9499
data: {
95-
siacoinOutput: SiacoinElement
100+
siacoinElement: SiacoinElement
96101
}
97102
}
98103

@@ -106,7 +111,7 @@ export type WalletEventSiafundClaim = WalletEventBase & {
106111
export type WalletEventFoundationSubsidy = WalletEventBase & {
107112
type: 'foundation'
108113
data: {
109-
siacoinOutput: SiacoinElement
114+
siacoinElement: SiacoinElement
110115
}
111116
}
112117

0 commit comments

Comments
 (0)