Skip to content

Commit

Permalink
adjust correctly for non-18 decimal tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
eli-d committed Jun 21, 2024
1 parent 709023c commit 1c28bb5
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 19 deletions.
4 changes: 2 additions & 2 deletions web/src/app/stake/pool/confirm-withdraw/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ export default function ConfirmWithdrawLiquidity() {
<div className="mt-[26px] px-[21px]">
<div className="text-[8px] font-semibold">{token0.symbol}</div>
<div className="text-2xl text-white"> {token0Amount} </div>
<div className="text-[10px] text-neutral-400">= ${token0.address === fUSDC.address ? token0Amount : getFormattedPriceFromAmount(token0Amount, tokenPrice, token0.decimals, token1.decimals)}</div>
<div className="text-[10px] text-neutral-400">= ${token0.address === fUSDC.address ? token0Amount : getFormattedPriceFromAmount(token0Amount, tokenPrice, fUSDC.decimals)}</div>
</div>

<div className="mt-[23px] px-[21px]">
<div className={"text-[8px] font-semibold"}>{token1.symbol}</div>
<div className="text-2xl text-white">{token1Amount}</div>
<div className="text-[10px] text-neutral-400">= ${token1.address === fUSDC.address ? token1Amount : getFormattedPriceFromAmount(token1Amount, tokenPrice, token1.decimals, token0.decimals)}</div>
<div className="text-[10px] text-neutral-400">= ${token1.address === fUSDC.address ? token1Amount : getFormattedPriceFromAmount(token1Amount, tokenPrice, fUSDC.decimals)}</div>
</div>

<div>
Expand Down
4 changes: 2 additions & 2 deletions web/src/app/stake/pool/withdraw-liquidity/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { useEffect, useMemo, useState } from "react";
import { usdFormat } from "@/lib/usdFormat";
import { ammAddress } from "@/lib/addresses";
import { sqrtPriceX96ToPrice } from "@/lib/math";
import { fUSDC } from "@/config/tokens";

const PositionsFragment = graphql(`
fragment WithdrawPositionsFragment on Wallet {
Expand Down Expand Up @@ -116,8 +117,7 @@ export default function WithdrawLiquidity() {
const deltaUsd = useMemo(() => {
if (!token0Amount || !token1Amount)
return "$0.00"
const decimalAdjust = 10 ** (token0.decimals - token1.decimals - 18);
const token0AmountScaled = Number(token0Amount) * Number(tokenPrice) * decimalAdjust
const token0AmountScaled = Number(token0Amount) * Number(tokenPrice) * 10 ** fUSDC.decimals;
return usdFormat(token0AmountScaled + parseFloat(token1Amount))
}, [token0Amount, token1Amount])

Expand Down
7 changes: 4 additions & 3 deletions web/src/components/ConfirmStake.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
useWriteContract,
} from "wagmi";
import { output as seawaterContract } from "@/lib/abi/ISeawaterAMM";
import { sqrtPriceX96ToPrice, getLiquidityForAmounts, MAX_TICK, MIN_TICK, snapTickToSpacing } from "@/lib/math";
import { sqrtPriceX96ToPrice, getLiquidityForAmounts, snapTickToSpacing } from "@/lib/math";
import { useEffect, useCallback, useMemo } from "react";
import { erc20Abi, Hash, hexToBigInt, maxUint256 } from "viem";
import { ammAddress } from "@/lib/addresses";
Expand All @@ -24,6 +24,7 @@ import { EnableSpending } from "@/components/sequence/EnableSpending";
import { Fail } from "@/components/sequence/Fail";
import { Success } from "@/components/sequence/Success";
import { getFormattedPriceFromAmount } from "@/lib/amounts";
import { fUSDC } from "@/config/tokens";

type ConfirmStakeProps = {
mode: "new"
Expand Down Expand Up @@ -380,7 +381,7 @@ export const ConfirmStake = ({ mode, positionId }: ConfirmStakeProps) => {
</span>
</div>
<div className="mt-[4px] text-2xl font-medium md:text-3xl">
${getFormattedPriceFromAmount(token0Amount, tokenPrice, token0.decimals, token1.decimals) + Number(token1Amount)}
${getFormattedPriceFromAmount(token0Amount, tokenPrice, fUSDC.decimals) + Number(token1Amount)}
</div>
<div className="mt-[4px] text-3xs font-medium text-gray-2 md:text-2xs">
The amount is split into{" "}
Expand Down Expand Up @@ -463,7 +464,7 @@ export const ConfirmStake = ({ mode, positionId }: ConfirmStakeProps) => {
<div className="mt-1 flex flex-row items-center gap-1 text-2xl">
<Ethereum className={"invert"} /> {token0Amount}
</div>
<div className="mt-0.5 text-2xs text-gray-2 md:text-xs">= ${getFormattedPriceFromAmount(token0Amount, tokenPrice, token0.decimals, token1.decimals)}</div>
<div className="mt-0.5 text-2xs text-gray-2 md:text-xs">= ${getFormattedPriceFromAmount(token0Amount, tokenPrice, fUSDC.decimals)}</div>
</div>

<div
Expand Down
17 changes: 10 additions & 7 deletions web/src/components/StakeForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,14 @@ export const StakeForm = ({ mode, poolId, positionId }: StakeFormProps) => {
if (!curTick)
return
// auto sets the price range to +-10% of the current tick
const priceAtTick = 1.0001 ** Number(curTick.result)
const priceLower = (priceAtTick * 0.9).toFixed(fUSDC.decimals)
const priceHigher = (priceAtTick * 1.1).toFixed(fUSDC.decimals)
const priceAtTick = sqrtPriceX96ToPrice(getSqrtRatioAtTick(curTick.result))
const diff = priceAtTick / 10n
const pu = priceAtTick + diff
const pl = priceAtTick - diff
const priceLower = (Number(pl) / 10 ** fUSDC.decimals).toFixed(fUSDC.decimals)
const priceUpper = (Number(pu) / 10 ** fUSDC.decimals).toFixed(fUSDC.decimals)
setPriceLower(priceLower)
setPriceUpper(priceHigher)
setPriceUpper(priceUpper)
}
}, [
mode,
Expand Down Expand Up @@ -518,7 +521,7 @@ export const StakeForm = ({ mode, poolId, positionId }: StakeFormProps) => {

<div className="mt-[5px] flex w-full flex-row items-center justify-between">
<div className="text-2xs md:text-gray-1">
${token0.address === fUSDC.address ? token0Amount : getFormattedPriceFromAmount(token0Amount, tokenPrice, token0.decimals, token1.decimals)}
${token0.address === fUSDC.address ? token0Amount : getFormattedPriceFromAmount(token0Amount, tokenPrice, fUSDC.decimals)}
</div>

<div className="flex flex-row gap-[8px] text-3xs md:text-2xs">
Expand Down Expand Up @@ -577,7 +580,7 @@ export const StakeForm = ({ mode, poolId, positionId }: StakeFormProps) => {

<div className="mt-[5px] flex w-full flex-row items-center justify-between">
<div className="text-2xs md:text-gray-1">
${token1.address === fUSDC.address ? token1Amount : getFormattedPriceFromAmount(token1Amount, tokenPrice, token1.decimals, token0.decimals)}
${token1.address === fUSDC.address ? token1Amount : getFormattedPriceFromAmount(token1Amount, tokenPrice, fUSDC.decimals)}
</div>
<div className="flex flex-row gap-[8px] text-3xs md:text-2xs">
{token1Balance && (
Expand Down Expand Up @@ -841,7 +844,7 @@ export const StakeForm = ({ mode, poolId, positionId }: StakeFormProps) => {
<LiquidityDistribution /> Liquidity Distribution
</div>
</div>
</div> }
</div>}
</div>

<div className="mt-[21px] flex w-[318px] flex-row justify-end md:w-[392px]">
Expand Down
9 changes: 4 additions & 5 deletions web/src/lib/amounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,12 @@ const getTokenAmountFromFormattedString = (amount: string, decimals: number): bi
/**
* @description scale a formatted amount string by the price of the pool
* @param amount - formatted string
* @param price - the pool price as a regular number, scaled up by 18 decimals
* @param decimals0 - the decimals of the non-fUSDC token
* @param decimals1 - the decimals of fUSDC
* @param price - the pool price as a regular number, scaled up by fUSDC decimals
* @param decimalsFusdc - the decimals of fUSDC
* @returns the scaled price amount in USD
*/
const getFormattedPriceFromAmount = (amount: string, price: string | bigint, decimals0: number, decimals1: number): number =>
Number(amount) * Number(price) * 10 ** (decimals0 - decimals1 - 18)
const getFormattedPriceFromAmount = (amount: string, price: string | bigint, decimalsFusdc: number): number =>
Number(amount) * (Number(price)) * 10 ** decimalsFusdc

// convert a tick to a formatted price, scaled by decimals
const getFormattedPriceFromTick = (tick: number, decimals: number) => {
Expand Down

0 comments on commit 1c28bb5

Please sign in to comment.