Skip to content

Commit

Permalink
Merge branch 'main' into deposit-duration-estimation
Browse files Browse the repository at this point in the history
  • Loading branch information
kkosiorowska authored Oct 29, 2024
2 parents 878b1f4 + 2fad5b4 commit b7f6dd2
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ function UnstakeFormModal({
const { decimals } = getCurrencyByType("bitcoin")
const inputPlaceholder = `Minimum ${fixedPointNumberToString(minTokenAmount, decimals)} BTC`
const tokenAmountLabel = "Acre balance"
const defaultAmount =
status === PROCESS_STATUSES.REFINE_AMOUNT ? balance : undefined

return (
<TokenAmountForm
Expand All @@ -34,9 +36,7 @@ function UnstakeFormModal({
minTokenAmount={minTokenAmount}
onSubmitForm={onSubmitForm}
withMaxButton
defaultAmount={
status === PROCESS_STATUSES.REFINE_AMOUNT ? balance : undefined
}
defaultAmount={defaultAmount}
>
<UnstakeDetails balance={balance} currency="bitcoin" />
<FormSubmitButton mt={10}>Withdraw</FormSubmitButton>
Expand Down
14 changes: 3 additions & 11 deletions dapp/src/components/shared/Form/FormTokenBalanceInput.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
import React, { useEffect } from "react"
import React from "react"
import { useFormField } from "#/hooks"
import TokenBalanceInput, { TokenBalanceInputProps } from "../TokenBalanceInput"

export type FormTokenBalanceInputProps = {
name: string
defaultValue?: bigint
} & Omit<TokenBalanceInputProps, "setAmount" | "defaultValue">
} & Omit<TokenBalanceInputProps, "setAmount">

export function FormTokenBalanceInput({
name,
defaultValue,
...restProps
}: FormTokenBalanceInputProps) {
const { field, value, errorMsgText, hasError, onChange } = useFormField<
bigint | undefined
>(name)

useEffect(() => {
if (defaultValue) {
onChange(defaultValue)
}
}, [defaultValue, onChange])

return (
<TokenBalanceInput
{...restProps}
{...field}
amount={defaultValue ?? value}
amount={value}
setAmount={onChange}
hasError={hasError}
errorMsgText={errorMsgText}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default function TokenAmountFormBase({
tokenAmountLabel={tokenAmountLabel}
currency={currency}
withMaxButton={withMaxButton}
defaultValue={defaultAmount}
defaultAmount={defaultAmount}
autoFocus
/>
{children}
Expand Down
15 changes: 14 additions & 1 deletion dapp/src/components/shared/TokenBalanceInput/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useRef, useState } from "react"
import React, { useEffect, useRef, useState } from "react"
import {
Box,
Button,
Expand Down Expand Up @@ -61,6 +61,7 @@ function FiatCurrencyBalance({

export type TokenBalanceInputProps = {
amount?: bigint
defaultAmount?: bigint
currency: CurrencyType
tokenBalance: bigint
placeholder?: string
Expand All @@ -75,6 +76,7 @@ export type TokenBalanceInputProps = {

export default function TokenBalanceInput({
amount,
defaultAmount,
currency,
tokenBalance,
placeholder,
Expand Down Expand Up @@ -114,6 +116,16 @@ export default function TokenBalanceInput({
typeof errorMsgText === "string" &&
isFormError("EXCEEDED_VALUE", errorMsgText)

const defaultValue = defaultAmount
? fixedPointNumberToString(defaultAmount, decimals)
: undefined

useEffect(() => {
if (!defaultAmount) return

setAmount(defaultAmount)
}, [defaultAmount, setAmount])

return (
<FormControl isInvalid={hasError} isDisabled={inputProps.isDisabled}>
<FormLabel htmlFor={inputProps.name} size={size} mr={0}>
Expand Down Expand Up @@ -144,6 +156,7 @@ export default function TokenBalanceInput({
{...inputProps}
isInvalid={hasError}
value={displayedValue}
defaultValue={defaultValue}
onValueChange={onValueChange}
onChange={onChange}
/>
Expand Down
14 changes: 5 additions & 9 deletions dapp/src/hooks/useAcrePoints.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useMutation, useQuery } from "@tanstack/react-query"
import { acreApi, bigIntToUserAmount } from "#/utils"
import { acreApi } from "#/utils"
import { queryKeysFactory } from "#/constants"
import { MODAL_TYPES } from "#/types"
import { useWallet } from "./useWallet"
Expand Down Expand Up @@ -38,8 +38,8 @@ export default function useAcrePoints(): UseAcrePointsReturnType {
await userPointsDataQuery.refetch()
},
onSuccess: (data) => {
const claimedAmount = bigIntToUserAmount(data.claimed, 0)
const totalAmount = bigIntToUserAmount(data.total, 0)
const claimedAmount = Number(data.claimed)
const totalAmount = Number(data.total)

openModal(MODAL_TYPES.ACRE_POINTS_CLAIM, {
claimedAmount,
Expand All @@ -50,13 +50,9 @@ export default function useAcrePoints(): UseAcrePointsReturnType {
// onError: (error) => {},
})

const { data } = userPointsDataQuery
const totalBalance = bigIntToUserAmount(data?.claimed ?? 0n, 0)
const claimableBalance = bigIntToUserAmount(data?.unclaimed ?? 0n, 0)

return {
totalBalance,
claimableBalance,
totalBalance: userPointsDataQuery.data?.claimed ?? 0,
claimableBalance: userPointsDataQuery.data?.unclaimed ?? 0,
nextDropTimestamp: pointsDataQuery.data?.dropAt,
isCalculationInProgress: pointsDataQuery.data?.isCalculationInProgress,
claimPoints,
Expand Down
4 changes: 2 additions & 2 deletions dapp/src/utils/acreApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ const getPointsDataByUser = async (address: string) => {
const response = await axios.get<PointsDataByUserResponse>(url)

return {
claimed: BigInt(response.data.claimed),
unclaimed: BigInt(response.data.unclaimed),
claimed: Number(response.data.claimed),
unclaimed: Number(response.data.unclaimed),
isEligible: response.data.isEligible,
}
}
Expand Down

0 comments on commit b7f6dd2

Please sign in to comment.