Skip to content

Commit

Permalink
fix: crash farm when data wrong (#2343)
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenhoaidanh authored Oct 28, 2023
1 parent ef33987 commit a49f162
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
12 changes: 9 additions & 3 deletions src/pages/Farm/ElasticFarmv2/components/FarmCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,15 @@ function FarmCard({

const currentTimestamp = Math.floor(Date.now() / 1000)
const stakedPos = useUserFarmV2Info(farm.farmAddress, farm.fId)

let amountToken0 = CurrencyAmount.fromRawAmount(farm.token0.wrapped, 0)
let amountToken1 = CurrencyAmount.fromRawAmount(farm.token1.wrapped, 0)

stakedPos.forEach(item => {
amountToken0 = amountToken0.add(item.position.amount0)
amountToken1 = amountToken1.add(item.position.amount1)
if (item.position.amount0?.currency.equals(amountToken0.currency))
amountToken0 = amountToken0.add(item.position.amount0)
if (item.position.amount1?.currency.equals(amountToken1.currency))
amountToken1 = amountToken1.add(item.position.amount1)
})

const canUnstake = stakedPos.length > 0
Expand All @@ -131,7 +134,10 @@ function FarmCard({
const userTotalRewards = farm.totalRewards.map((item, index) => {
return stakedPos
.map(item => item.unclaimedRewards[index])
.reduce((total, cur) => total.add(cur), CurrencyAmount.fromRawAmount(item.currency, 0))
.reduce(
(total, cur) => (cur?.currency?.equals(total.currency) ? total.add(cur) : total),
CurrencyAmount.fromRawAmount(item.currency, 0),
)
})

const myDepositUSD = stakedPos.reduce((total, item) => item.stakedUsdValue + total, 0)
Expand Down
11 changes: 8 additions & 3 deletions src/pages/Farm/ElasticFarmv2/components/ListView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ export const ListView = ({
let amountToken1 = CurrencyAmount.fromRawAmount(farm.token1.wrapped, 0)

stakedPos.forEach(item => {
amountToken0 = amountToken0.add(item.position.amount0)
amountToken1 = amountToken1.add(item.position.amount1)
if (item.position.amount0?.currency.equals(amountToken0.currency))
amountToken0 = amountToken0.add(item.position.amount0)
if (item.position.amount1?.currency.equals(amountToken1.currency))
amountToken1 = amountToken1.add(item.position.amount1)
})

const canUnstake = stakedPos.length > 0
Expand All @@ -84,7 +86,10 @@ export const ListView = ({
const userTotalRewards = farm.totalRewards.map((item, index) => {
return stakedPos
.map(item => item.unclaimedRewards[index])
.reduce((total, cur) => total.add(cur), CurrencyAmount.fromRawAmount(item.currency, 0))
.reduce(
(total, cur) => (cur?.currency?.equals(total.currency) ? total.add(cur) : total),
CurrencyAmount.fromRawAmount(item.currency, 0),
)
})

const myDepositUSD = stakedPos.reduce((total, item) => item.stakedUsdValue + total, 0)
Expand Down

0 comments on commit a49f162

Please sign in to comment.