@@ -30,6 +30,24 @@ export const makeThorchainYieldAdapter = (policyConfig: StakePolicyConfig<Thorch
30
30
headers [ 'x-client-id' ] = ninerealmsClientId
31
31
}
32
32
33
+ const getStakedTcyAmount = async ( wallet : EdgeCurrencyWallet ) : Promise < string > => {
34
+ const addresses = await wallet . getAddresses ( { tokenId : null } )
35
+ const address = addresses [ 0 ] . publicAddress
36
+
37
+ const tcyStakerResponse = await fetchWaterfall ( thornodeServers , `thorchain/tcy_staker/${ address } ` , { headers } )
38
+
39
+ if ( ! tcyStakerResponse . ok ) {
40
+ const responseText = await tcyStakerResponse . text ( )
41
+ if ( responseText . includes ( "fail to tcy staker: TCYStaker doesn't exist" ) ) {
42
+ return '0'
43
+ }
44
+ throw new Error ( `Thorchain could not fetch /tcy_staker: ${ responseText } ` )
45
+ }
46
+ const stakerJson = await tcyStakerResponse . json ( )
47
+ const staker = asTcyStaker ( stakerJson )
48
+ return staker . amount
49
+ }
50
+
33
51
const instance : StakePolicyAdapter = {
34
52
stakePolicyId,
35
53
@@ -50,38 +68,24 @@ export const makeThorchainYieldAdapter = (policyConfig: StakePolicyConfig<Thorch
50
68
} ,
51
69
52
70
async fetchStakePosition ( wallet : EdgeCurrencyWallet ) : Promise < StakePosition > {
53
- const addresses = await wallet . getAddresses ( { tokenId : null } )
54
- const address = addresses [ 0 ] . publicAddress
55
-
56
- const tcyStakerResponse = await fetchWaterfall ( thornodeServers , `thorchain/tcy_staker/${ address } ` , { headers } )
71
+ const balance = wallet . balanceMap . get ( 'tcy' ) ?? '0'
72
+ const tcyStakedAmount = await getStakedTcyAmount ( wallet )
57
73
58
74
const position : StakePosition = {
59
- allocations : [ ] ,
60
- canStake : false ,
61
- canUnstake : false ,
75
+ allocations : [
76
+ {
77
+ pluginId : 'thorchainrune' ,
78
+ currencyCode : 'TCY' ,
79
+ allocationType : 'staked' ,
80
+ nativeAmount : tcyStakedAmount
81
+ }
82
+ ] ,
83
+ canStake : gt ( balance , '0' ) ,
84
+ canUnstake : gt ( tcyStakedAmount , '0' ) ,
62
85
canUnstakeAndClaim : false ,
63
86
canClaim : false
64
87
}
65
88
66
- if ( ! tcyStakerResponse . ok ) {
67
- const responseText = await tcyStakerResponse . text ( )
68
- if ( responseText . includes ( "fail to tcy staker: TCYStaker doesn't exist" ) ) {
69
- return position
70
- }
71
- throw new Error ( `Thorchain could not fetch /tcy_staker: ${ responseText } ` )
72
- }
73
- const stakerJson = await tcyStakerResponse . json ( )
74
- const staker = asTcyStaker ( stakerJson )
75
-
76
- if ( gt ( staker . amount , '0' ) ) {
77
- position . allocations . push ( {
78
- pluginId : 'thorchainrune' ,
79
- currencyCode : 'TCY' ,
80
- allocationType : 'staked' ,
81
- nativeAmount : staker . amount
82
- } )
83
- }
84
-
85
89
return position
86
90
} ,
87
91
0 commit comments