Skip to content

Commit

Permalink
determine update delta outside of callback to avoid infinite re-render
Browse files Browse the repository at this point in the history
  • Loading branch information
eli-d committed Jun 20, 2024
1 parent 56357b9 commit dcd2020
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions web/src/components/ConfirmStake.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from "wagmi";
import { output as seawaterContract } from "@/lib/abi/ISeawaterAMM";
import { sqrtPriceX96ToPrice, getLiquidityForAmounts } from "@/lib/math";
import { useEffect, useCallback } from "react";
import { useEffect, useCallback, useMemo } from "react";
import { erc20Abi, Hash, hexToBigInt, maxUint256 } from "viem";
import { ammAddress } from "@/lib/addresses";
import LightweightERC20 from "@/lib/abi/LightweightERC20";
Expand Down Expand Up @@ -47,7 +47,7 @@ export const ConfirmStake = ({ mode, positionId }: ConfirmStakeProps) => {
token1AmountRaw,
tickLower,
tickUpper,
multiSingleToken
multiSingleToken,
} = useStakeStore()

// Price of the current pool
Expand Down Expand Up @@ -126,6 +126,16 @@ export const ConfirmStake = ({ mode, positionId }: ConfirmStakeProps) => {
console.log(updatePositionError);
console.log(mintData);

const delta = useMemo(() =>
(!curTick || tickLower === undefined || tickUpper === undefined) ? 0n :
getLiquidityForAmounts(
curTick.result,
BigInt(tickLower),
BigInt(tickUpper),
BigInt(token0AmountRaw),
BigInt(token1AmountRaw),
), [curTick, tickLower, tickUpper, token0AmountRaw, token1AmountRaw]);

/**
* Create a new position in the AMM.
*
Expand All @@ -152,18 +162,6 @@ export const ConfirmStake = ({ mode, positionId }: ConfirmStakeProps) => {

const updatePosition = useCallback(
(id: bigint) => {
if (!curTick || tickLower === undefined || tickUpper === undefined) {
return
}

const delta = getLiquidityForAmounts(
curTick.result,
BigInt(tickLower),
BigInt(tickUpper),
BigInt(token0AmountRaw),
BigInt(token1AmountRaw),
);

writeContractUpdatePosition({
address: ammAddress,
abi: seawaterContract.abi,
Expand All @@ -173,12 +171,8 @@ export const ConfirmStake = ({ mode, positionId }: ConfirmStakeProps) => {
},
[
writeContractUpdatePosition,
token0AmountRaw,
delta,
token0,
token1AmountRaw,
curTick,
tickLower,
tickUpper
],
);

Expand Down

0 comments on commit dcd2020

Please sign in to comment.