Skip to content

Commit 0c69d36

Browse files
committed
Break out staked balance fetching into it's own function
We'll need the balance when crafting the unstake transaction. Also, fix missing allocation in position (we always need to have an allocation, even if it's zero).
1 parent e1227d3 commit 0c69d36

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

src/plugins/stake-plugins/generic/policyAdapters/ThorchainYieldAdaptor.ts

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,24 @@ export const makeThorchainYieldAdapter = (policyConfig: StakePolicyConfig<Thorch
3030
headers['x-client-id'] = ninerealmsClientId
3131
}
3232

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+
3351
const instance: StakePolicyAdapter = {
3452
stakePolicyId,
3553

@@ -50,38 +68,24 @@ export const makeThorchainYieldAdapter = (policyConfig: StakePolicyConfig<Thorch
5068
},
5169

5270
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)
5773

5874
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'),
6285
canUnstakeAndClaim: false,
6386
canClaim: false
6487
}
6588

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-
8589
return position
8690
},
8791

0 commit comments

Comments
 (0)