Skip to content

Commit

Permalink
release (#1039)
Browse files Browse the repository at this point in the history
* fix: refactored bigint usage for delegations

* chore: self review

* chore: self review

* chore: remove parse units

* chore: self review

* chore: refactored vesting

* chore: review proposals
  • Loading branch information
GarageInc authored Dec 17, 2024
1 parent 5b38ff6 commit 520e89f
Show file tree
Hide file tree
Showing 19 changed files with 253 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function DelegateModalSegment() {
const { data: stakingParams } = useStakingParamsQuery();
const { data: balances } = useIndexerBalanceQuery(haqqAddress);
const { data: delegationInfo } = useStakingDelegationQuery(haqqAddress);
const [balance, setBalance] = useState(0);
const [balance, setBalance] = useState<bigint>(0n);

const validatorCommission = useMemo(() => {
return (
Expand All @@ -39,8 +39,8 @@ export default function DelegateModalSegment() {

useEffect(() => {
if (balances) {
const { availableForStake } = balances;
setBalance(availableForStake);
const { availableForStakeBn } = balances;
setBalance(availableForStakeBn);
}
}, [balances]);

Expand All @@ -52,12 +52,10 @@ export default function DelegateModalSegment() {
);

if (delegation) {
return Number.parseFloat(
formatUnits(BigInt(delegation.balance.amount), 18),
);
return BigInt(delegation.balance.amount);
}

return 0;
return 0n;
}, [delegationInfo, address]);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ export default function RedelegateModalSegment() {
const { data: validatorsList } = useStakingValidatorListQuery(1000);
const { data: balances } = useIndexerBalanceQuery(haqqAddress);
const { data: delegationInfo } = useStakingDelegationQuery(haqqAddress);
const [balance, setBalance] = useState(0);
const [balance, setBalance] = useState<bigint>(0n);

useEffect(() => {
if (balances) {
const { availableForStake } = balances;
setBalance(availableForStake);
const { availableForStakeBn } = balances;
setBalance(availableForStakeBn);
}
}, [balances]);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use client';
import { useEffect, useMemo, useState } from 'react';
import { useParams, useRouter } from 'next/navigation';
import { formatUnits } from 'viem';
import {
useAddress,
useIndexerBalanceQuery,
Expand All @@ -17,7 +16,7 @@ export default function UndelegateModalSegment() {
const { data: stakingParams } = useStakingParamsQuery();
const { data: balances } = useIndexerBalanceQuery(haqqAddress);
const { data: delegationInfo } = useStakingDelegationQuery(haqqAddress);
const [balance, setBalance] = useState(0);
const [balance, setBalance] = useState(0n);

const unboundingTime = useMemo(() => {
if (stakingParams?.unbonding_time) {
Expand All @@ -29,8 +28,8 @@ export default function UndelegateModalSegment() {

useEffect(() => {
if (balances) {
const { availableForStake } = balances;
setBalance(availableForStake);
const { availableForStakeBn } = balances;
setBalance(availableForStakeBn);
}
}, [balances]);

Expand All @@ -42,12 +41,10 @@ export default function UndelegateModalSegment() {
);

if (delegation) {
return Number.parseFloat(
formatUnits(BigInt(delegation.balance.amount), 18),
);
return BigInt(delegation.balance.amount);
}

return 0;
return 0n;
}, [delegationInfo, address]);

return (
Expand Down
21 changes: 15 additions & 6 deletions apps/vesting/src/components/liquid-vesting/liquid-vesting.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { FormEvent, useCallback, useEffect, useMemo, useState } from 'react';
import { Link } from 'react-router-dom';
import { formatUnits, parseUnits } from 'viem';
import { useAccount } from 'wagmi';
import { haqqMainnet } from 'wagmi/chains';
import { BroadcastTxResponse, getChainParams } from '@haqq/data-access-cosmos';
Expand Down Expand Up @@ -66,11 +67,11 @@ export function LiquidVestingHooked({
balance,
haqqAddress,
}: {
balance: number;
balance: bigint;
haqqAddress: string;
}) {
const [liquidationAmount, setLiquidationAmount] = useState<
number | undefined
bigint | undefined
>(undefined);
const [amountError, setAmountError] = useState<
undefined | 'min' | 'max' | 'balance'
Expand Down Expand Up @@ -307,8 +308,8 @@ function LiquidVesting({
addedTokens,
onRedeemClick,
}: {
liquidationAmount: number | undefined;
onAmountChange: (value: number) => void;
liquidationAmount: bigint | undefined;
onAmountChange: (value: bigint) => void;
amountError?: 'min' | 'max' | 'balance';
isLiquidationEnabled: boolean;
onSubmit: () => void;
Expand All @@ -328,7 +329,7 @@ function LiquidVesting({
);

if (normalizedAmount) {
onAmountChange(normalizedAmount);
onAmountChange(parseUnits(normalizedAmount.toString(), 18));
}
}
},
Expand Down Expand Up @@ -366,6 +367,14 @@ function LiquidVesting({
[onSubmit],
);

const liquidationAmountNumber = useMemo(() => {
if (liquidationAmount) {
return Number.parseFloat(formatUnits(liquidationAmount, 18));
}

return undefined;
}, [liquidationAmount]);

return (
<Card className="mx-auto w-full max-w-lg overflow-hidden">
<div className="px-[16px] pt-[24px]">
Expand All @@ -383,7 +392,7 @@ function LiquidVesting({
required
label="Amount"
placeholder={`Min ${formatLocaleNumber(MIN_AMOUNT)} ISLM`}
value={liquidationAmount}
value={liquidationAmountNumber}
onChange={handleInputChange}
{...hintAndState}
/>
Expand Down
5 changes: 4 additions & 1 deletion apps/vesting/src/components/vesting-account-info.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ export function VestingAccountInfo({
{/* <DaoParticipate address={ethAddress} /> */}

{isLiquidVestingVisible && (
<LiquidVestingHooked balance={balances.total} haqqAddress={address} />
<LiquidVestingHooked
balance={balances.totalBn}
haqqAddress={address}
/>
)}

{isClawbackVestingAccount(accountInfo) && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export function useLiquidVestingActions() {
const getLiquidateParams = useCallback(
(
liquidateTo: string,
amount: number,
balance: number,
amount: bigint,
balance: bigint,
fee: Fee,
): MsgLiquidateParams => {
return {
Expand All @@ -59,16 +59,16 @@ export function useLiquidVestingActions() {
);

const handleLiquidate = useCallback(
async (address?: string, amount?: number, balance?: number) => {
async (address?: string, amount?: bigint, balance?: bigint) => {
const pubkey = await getPubkey(ethAddress as string);
const sender = await getSender(haqqAddress as string, pubkey);
const memo = '';

if (sender && address && haqqChain) {
const params = getLiquidateParams(
address,
amount ?? 0,
balance ?? 0,
amount ?? 0n,
balance ?? 0n,
VESTING_DEFAULT_FEE,
);

Expand Down
1 change: 1 addition & 0 deletions libs/shared/src/hooks/use-address/use-address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ethToHaqq } from '../../utils/convert-address';

export function useAddress() {
const { address: ethAddress } = useAccount();
// const ethAddress = '0x912A3b8cF600CbDD71ffC4224e80501221482346' as Hex;
const haqqAddress = useMemo(() => {
if (ethAddress) {
return ethToHaqq(ethAddress);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,29 @@ import { useAccount, useChains } from 'wagmi';

export interface IndexerBalances {
available: number;
availableBn: bigint;
availableForStake: number;
availableForStakeBn: bigint;
balance: number;
balanceBn: bigint;
locked: number;
lockedBn: bigint;
staked: number;
stakedBn: bigint;
stakedFree: number;
stakedFreeBn: bigint;
stakedLocked: number;
stakedLockedBn: bigint;
total: number;
totalBn: bigint;
vested: number;
vestedBn: bigint;
daoLocked: number;
daoLockedBn: bigint;
unbonding: number;
unbondingBn: bigint;
rewards: number;
rewardsBn: bigint;
}

type IndexerBalance = Array<[string, number, string]>;
Expand Down Expand Up @@ -82,6 +94,22 @@ function safeParseBalance(
return Number.parseFloat(formatUnits(BigInt(balance[2]), 18));
}

function safeParseBalanceBigInt(
balances: IndexerBalance,
address: string,
chainId: number,
): bigint {
const balance = balances.find(([addr, chain]) => {
return addr === address && chain === chainId;
});

if (!balance) {
return 0n;
}

return BigInt(balance[2]);
}

// Map the balances from the indexer response to the IndexerBalances type
function mapBalances(
balancesResponse: IndexerV2UpdatesResponse,
Expand All @@ -90,29 +118,73 @@ function mapBalances(
): IndexerBalances {
return {
available: safeParseBalance(balancesResponse.available, address, chainId),
availableBn: safeParseBalanceBigInt(
balancesResponse.available,
address,
chainId,
),
availableForStake: safeParseBalance(
balancesResponse.available_for_stake,
address,
chainId,
),
availableForStakeBn: safeParseBalanceBigInt(
balancesResponse.available_for_stake,
address,
chainId,
),
balance: safeParseBalance(balancesResponse.balance, address, chainId),
balanceBn: safeParseBalanceBigInt(
balancesResponse.balance,
address,
chainId,
),
locked: safeParseBalance(balancesResponse.locked, address, chainId),
lockedBn: safeParseBalanceBigInt(balancesResponse.locked, address, chainId),
staked: safeParseBalance(balancesResponse.staked, address, chainId),
stakedBn: safeParseBalanceBigInt(balancesResponse.staked, address, chainId),
stakedFree: safeParseBalance(
balancesResponse.staked_free,
address,
chainId,
),
stakedFreeBn: safeParseBalanceBigInt(
balancesResponse.staked_free,
address,
chainId,
),
stakedLocked: safeParseBalance(
balancesResponse.staked_locked,
address,
chainId,
),
stakedLockedBn: safeParseBalanceBigInt(
balancesResponse.staked_locked,
address,
chainId,
),
total: safeParseBalance(balancesResponse.total, address, chainId),
totalBn: safeParseBalanceBigInt(balancesResponse.total, address, chainId),
vested: safeParseBalance(balancesResponse.vested, address, chainId),
vestedBn: safeParseBalanceBigInt(balancesResponse.vested, address, chainId),
daoLocked: safeParseBalance(balancesResponse.dao_locked, address, chainId),
daoLockedBn: safeParseBalanceBigInt(
balancesResponse.dao_locked,
address,
chainId,
),
unbonding: safeParseBalance(balancesResponse.unbounding, address, chainId),
unbondingBn: safeParseBalanceBigInt(
balancesResponse.unbounding,
address,
chainId,
),
rewards: safeParseBalance(balancesResponse.rewards, address, chainId),
rewardsBn: safeParseBalanceBigInt(
balancesResponse.rewards,
address,
chainId,
),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ interface ProposalActionsHook {
) => Promise<BroadcastTxResponse>;
deposit: (
proposalId: number,
amount: number,
balance?: number,
amount: bigint,
balance?: bigint,
memo?: string,
estimatedFee?: EstimatedFeeResponse,
) => Promise<BroadcastTxResponse>;
Expand Down Expand Up @@ -116,8 +116,8 @@ export function useProposalActions(): ProposalActionsHook {
const handleDeposit = useCallback(
async (
proposalId: number,
amount: number,
balance?: number,
amount: bigint,
balance?: bigint,
memo = '',
estimatedFee?: EstimatedFeeResponse,
) => {
Expand All @@ -128,7 +128,7 @@ export function useProposalActions(): ProposalActionsHook {
const fee = getFee(estimatedFee);
const depositParams: MessageMsgDepositParams = {
proposalId,
deposit: getAmountIncludeFee(amount, balance ?? 0, fee),
deposit: getAmountIncludeFee(amount, balance ?? 0n, fee),
};
const msg = createTxMsgDeposit(
haqqChain,
Expand Down
Loading

0 comments on commit 520e89f

Please sign in to comment.