Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Fix liquidity price conversion and fees calculation #15

Merged
merged 4 commits into from Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 11 additions & 19 deletions src/contexts/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,17 @@ export function useUserPositionChart(position, account) {
pairSnapshots,
currentETHPrice
)
if (fetchedData) {
for (let j = 0; j < fetchedData.length; j++) {
let latestAvaxPrice
if (j === fetchedData.length - 1) {
latestAvaxPrice = await getCurrentEthPrice()
} else {
latestAvaxPrice = await getEthPriceAtDate(fetchedData[j].date)
}
fetchedData[j].usdValue = fetchedData[j].usdValue * latestAvaxPrice
}
}
updateUserPairReturns(account, pairAddress, fetchedData)
}
if (
Expand All @@ -315,25 +326,6 @@ export function useUserPositionChart(position, account) {
position.pair.id,
])

const convertPrice = async function () {
if (formattedHistory) {
for (let j = 0; j < formattedHistory.length; j++) {
let latestAvaxPrice
if (j === formattedHistory.length - 1) {
latestAvaxPrice = await getCurrentEthPrice()
} else {
latestAvaxPrice = await getEthPriceAtDate(formattedHistory[j].date)
}
formattedHistory[j].usdValue = formattedHistory[j].usdValue * latestAvaxPrice
}
}
return formattedHistory
}

if (formattedHistory) {
convertPrice().then(result => { console.log("Finished waiting") })
}

return formattedHistory
}

Expand Down
12 changes: 10 additions & 2 deletions src/utils/returns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,16 @@ export function getMetricsForPositionWindow(positionT0: Position, positionT1: Po
positionT1 = formatPricesForEarlyTimestamps(positionT1)

// calculate ownership at ends of window, for end of window we need original LP token balance / new total supply
const t0Ownership = positionT0.liquidityTokenBalance / positionT0.liquidityTokenTotalSupply
const t1Ownership = positionT0.liquidityTokenBalance / positionT1.liquidityTokenTotalSupply
// eslint-disable-next-line eqeqeq
const t0Ownership =
positionT0.liquidityTokenTotalSupply != 0
? positionT0.liquidityTokenBalance / positionT0.liquidityTokenTotalSupply
: 0
// eslint-disable-next-line eqeqeq
const t1Ownership =
positionT1.liquidityTokenTotalSupply != 0
? positionT0.liquidityTokenBalance / positionT1.liquidityTokenTotalSupply
: 0

// get starting amounts of token0 and token1 deposited by LP
const token0_amount_t0 = t0Ownership * positionT0.reserve0
Expand Down