1
1
import BigNumber from 'bignumber.js'
2
2
import { WalletEvent } from '@siafoundation/walletd-types'
3
3
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 ) {
5
25
if ( 'spentSiacoinElements' in e . data ) {
6
26
const siacoinElements = e . data . spentSiacoinElements
7
27
return siacoinElements . reduce ( ( acc , o ) => {
@@ -13,7 +33,7 @@ export function calculateScOutflow(e: WalletEvent) {
13
33
}
14
34
}
15
35
16
- export function calculateSfOutflow ( e : WalletEvent ) {
36
+ export function v1TxnCalculateSfOutflow ( e : WalletEvent ) {
17
37
if ( 'spentSiafundElements' in e . data ) {
18
38
const siafundElements = e . data . spentSiafundElements || [ ]
19
39
return siafundElements . reduce ( ( acc , o ) => {
@@ -26,7 +46,21 @@ export function calculateSfOutflow(e: WalletEvent) {
26
46
return 0
27
47
}
28
48
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 ) {
30
64
if ( 'transaction' in e . data ) {
31
65
const siacoinOutputs = e . data . transaction . siacoinOutputs || [ ]
32
66
return siacoinOutputs . reduce ( ( acc , o ) => {
@@ -39,7 +73,7 @@ export function calculateScInflow(e: WalletEvent) {
39
73
return new BigNumber ( 0 )
40
74
}
41
75
42
- export function calculateSfInflow ( e : WalletEvent ) {
76
+ export function txnCalculateSfInflow ( e : WalletEvent ) {
43
77
if ( 'transaction' in e . data ) {
44
78
const siafundOutputs = e . data . transaction . siafundOutputs || [ ]
45
79
return siafundOutputs . reduce ( ( acc , o ) => {
@@ -53,17 +87,20 @@ export function calculateSfInflow(e: WalletEvent) {
53
87
}
54
88
55
89
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
57
94
? new BigNumber ( e . data . transaction . minerFees [ 0 ] )
58
95
: undefined
59
96
}
60
97
61
98
export function getContractId ( e : WalletEvent ) {
62
99
if ( e . type === 'v1ContractResolution' ) {
63
- return e . data . fileContract . id
100
+ return e . data . parent . id
64
101
}
65
102
if ( e . type === 'v2ContractResolution' ) {
66
- return e . data . fileContract . id
103
+ return e . data . parent . id
67
104
}
68
105
return undefined
69
106
}
0 commit comments