Skip to content

Commit

Permalink
update when creating new position
Browse files Browse the repository at this point in the history
  • Loading branch information
eli-d committed Aug 15, 2024
1 parent d31436e commit 34c0741
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 39 deletions.
45 changes: 26 additions & 19 deletions web/src/app/stake/pool/confirm-withdraw/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,31 @@ export default function ConfirmWithdrawLiquidity() {
updatePosition(BigInt(positionId));
}, [updatePosition, positionId, collectResult.data]);

useEffect(() => {
if (updatePositionResult.isSuccess) {
const position = positions.find(p => p.positionId === Number(positionId))
if (position) {
getUsdTokenAmountsForPosition(position, token0, Number(tokenPrice)).then(([amount0, amount1]) =>
updatePositionLocal({
...position,
served: {
timestamp: Math.round(new Date().getTime() / 1000)
},
liquidity: {
fusdc: {
valueUsd: String(amount1),
},
token1: {
valueUsd: String(amount0),
}
}
})
)
}
}
}, [updatePositionResult.isSuccess])


// step 1 - collect yield from position if emptying entire balance
if (
isWithdrawingEntirePosition &&
Expand Down Expand Up @@ -168,25 +193,7 @@ export default function ConfirmWithdrawLiquidity() {
if (updatePositionResult.data) {
return (
<Success
onDone={async () => {
const position = positions.find(p => p.positionId === Number(positionId))
if (position) {
const [amount0, amount1] = await getUsdTokenAmountsForPosition(position, token0, Number(tokenPrice))
updatePositionLocal({
...position,
served: {
timestamp: Math.round(new Date().getTime() / 1000)
},
liquidity: {
fusdc: {
valueUsd: String(amount1),
},
token1: {
valueUsd: String(amount0),
}
}
})
}
onDone={() => {
resetUpdatePosition();
resetCollect();
router.push("/stake");
Expand Down
53 changes: 34 additions & 19 deletions web/src/components/ConfirmStake.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,38 @@ export const ConfirmStake = ({ mode, positionId }: ConfirmStakeProps) => {
const updatePositionResult = useWaitForTransactionReceipt({
hash: updatePositionData,
});
useEffect(() => {
if (updatePositionResult.isSuccess) {
const id = positionId ?? Number(mintPositionId)
if (id && tickLower && tickUpper) {
const position = positions.find(p => p.positionId === id) ?? {
positionId: id,
pool: {
token: token0,
},
lower: tickLower,
upper: tickUpper,
}
getUsdTokenAmountsForPosition(position, token0, Number(tokenPrice)).then(([amount0, amount1]) =>
updatePositionLocal({
...position,
served: {
timestamp: Math.round(new Date().getTime() / 1000)
},
liquidity: {
fusdc: {
valueUsd: String(amount1),
},
token1: {
valueUsd: String(amount0),
}
}
})
)
}

}
}, [updatePositionResult.isSuccess])

// step 1 pending
if (isMintPending || (mintData && result?.isPending)) {
Expand Down Expand Up @@ -362,29 +394,12 @@ export const ConfirmStake = ({ mode, positionId }: ConfirmStakeProps) => {
/>;
}


// success
if (updatePositionResult.data) {
return <Success
transactionHash={updatePositionResult.data.transactionHash}
onDone={async () => {
const position = positions.find(p => p.positionId === positionId)
if (position) {
const [amount0, amount1] = await getUsdTokenAmountsForPosition(position, token0, Number(tokenPrice))
updatePositionLocal({
...position,
served: {
timestamp: Math.round(new Date().getTime() / 1000)
},
liquidity: {
fusdc: {
valueUsd: String(amount1),
},
token1: {
valueUsd: String(amount0),
}
}
})
}
onDone={() => {
resetUpdatePosition()
resetApproveToken0()
resetApproveToken1()
Expand Down
2 changes: 1 addition & 1 deletion web/src/lib/amounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ const getFormattedPriceFromTick = (
return formattedPrice.length > 20 ? "∞ " : formattedPrice;
};

const getUsdTokenAmountsForPosition = async (position: Position, token0: Token, tokenPrice: number): Promise<[number, number]> => {
const getUsdTokenAmountsForPosition = async (position: Pick<Position, 'positionId' | 'lower' | 'upper'>, token0: Token, tokenPrice: number): Promise<[number, number]> => {
const positionLiquidity = await simulateContract(config, {
address: ammAddress,
abi: seawaterContract.abi,
Expand Down

0 comments on commit 34c0741

Please sign in to comment.