-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change StakeSubRow input value to use BigInt instead of rounded value #78
base: main
Are you sure you want to change the base?
Changes from 5 commits
7fa6f83
42c81eb
b94a789
f69b718
ef5415c
2ab2e9a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import Image from "next/image"; | ||
import React, { FC, useCallback, useState } from "react"; | ||
import * as sdk from "@vanilladefi/stake-sdk"; | ||
import { isAddress } from "@vanilladefi/core-sdk"; | ||
import { isAddress, juiceDecimals } from "@vanilladefi/core-sdk"; | ||
import { Row } from "react-table"; | ||
import { toast } from "react-toastify"; | ||
import { useSnapshot } from "valtio"; | ||
|
@@ -11,6 +11,7 @@ import { | |
emitEvent, | ||
findToken, | ||
getTransactionLink, | ||
limitJuiceAmount, | ||
parseJuice, | ||
} from "../../utils/helpers"; | ||
import Box from "../Box"; | ||
|
@@ -22,6 +23,7 @@ import Text from "../Text"; | |
|
||
import { PolygonScanIcon } from "../../assets"; | ||
import { BigNumber } from "ethers"; | ||
import { formatUnits } from "ethers/lib/utils"; | ||
|
||
export type ColumnType = { | ||
__typename?: "AssetPair"; | ||
|
@@ -54,7 +56,11 @@ const StakeSubRow: FC<SubRowProps> = ({ row, type = "make" }) => { | |
|
||
const staked = row.original.currentStake; | ||
|
||
const [stakeAmount, setStakeAmount] = useState(staked?.juiceValue || ""); | ||
const [stakeAmount, setStakeAmount] = useState( | ||
staked?.rawJuiceValue | ||
? formatUnits(staked.rawJuiceValue, juiceDecimals) | ||
: "" | ||
); | ||
const [stakePosition, setStakePosition] = useState<"long" | "short">( | ||
staked?.sentiment || "long" | ||
); | ||
|
@@ -65,9 +71,8 @@ const StakeSubRow: FC<SubRowProps> = ({ row, type = "make" }) => { | |
const closingDisabled = stakePending; | ||
|
||
const stakeUnchanged = | ||
staked?.sentiment === stakePosition | ||
? staked?.juiceValue === stakeAmount | ||
: false; | ||
staked?.sentiment === stakePosition && | ||
parseJuice(stakeAmount).eq(staked.rawJuiceValue); | ||
|
||
const handleStake = useCallback( | ||
async (type: "close" | "modify" = "modify") => { | ||
|
@@ -230,12 +235,13 @@ const StakeSubRow: FC<SubRowProps> = ({ row, type = "make" }) => { | |
type="number" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After banging my head for some time, turns out this was culprit. On non-chromiums if number input gets a non-number, it just gives empty string to |
||
placeholder="0.0" | ||
value={stakeAmount} | ||
onChange={(e) => setStakeAmount(e.target.value)} | ||
onChange={(e) => setStakeAmount(limitJuiceAmount(e.target.value))} | ||
css={{ | ||
width: "100%", | ||
minWidth: "30px", | ||
maxWidth: "140px", | ||
textAlign: "right", | ||
fontSize: stakeAmount.length > 8 ? "$sm" : "$lg", | ||
mx: "$3", | ||
}} | ||
/> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,4 +36,13 @@ export const getTransactionLink = (txHash: string) => { | |
} | ||
const transactionLink = `${explorerUrl}/tx/${txHash}` | ||
return transactionLink; | ||
} | ||
|
||
export const limitJuiceAmount = (str: string) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems a bit hacky as it is, but seems to work. This said, there could be another feature that would remove spaces (Could be fixable by making the input a number type too btw
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I believe that is already the case! But yeah, we can remove spaces here, chrome does it automatically, but others are not that smart! |
||
let [int = "0", frac = ''] = str.split("."); | ||
if (frac.length > juiceDecimals) { | ||
frac = frac.slice(0, juiceDecimals); | ||
} | ||
const amount = frac ? `${int}.${frac}` : int; | ||
return amount | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this basically exactly the same as before?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, but
staked.juiceValue
truncates the value to 3 decimals?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, basically
juiceValue
is also locale formatted (toLocaleString
). Which adds commas/dots accordingly and rounds off to 3 digits.