Skip to content

Commit

Permalink
Some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
TorstenStueber committed Jun 14, 2024
1 parent 1cd589c commit 9ba0981
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/components/InputKeys/From.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface FromProps<FormFieldValues extends FieldValues, TFieldName extends Fiel
inputHasError: boolean;
fromFormFieldName: TFieldName;
form: UseFormReturn<FormFieldValues>;
fromTokenBalances: { [key: string]: BalanceInfo };
tokenBalances: { [key: string]: BalanceInfo };
offrampStarted: boolean;
}

Expand All @@ -27,11 +27,11 @@ export function From<FormFieldValues extends FieldValues, TFieldName extends Fie
inputHasError,
fromFormFieldName,
form,
fromTokenBalances,
tokenBalances,
offrampStarted,
}: FromProps<FormFieldValues, TFieldName>) {
const { setValue } = useFormContext<SwapFormValues>();
const fromTokenBalance = tokenId ? fromTokenBalances[tokenId] : undefined;
const fromTokenBalance = tokenId ? tokenBalances[tokenId] : undefined;

return (
<div
Expand Down
14 changes: 8 additions & 6 deletions src/components/InputKeys/To.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useFormContext } from 'react-hook-form';
import Big from 'big.js';

import pendulumIcon from '../../assets/pendulum-icon.svg';
import { NumberLoader } from '../Nabla/TokenBalance';
import { NumberLoader, TokenBalance } from '../Nabla/TokenBalance';
import { Skeleton } from '../Skeleton';
import { SwapFormValues } from '../Nabla/schema';
import { UseTokenOutAmountResult } from '../../hooks/nabla/useTokenAmountOut';
Expand All @@ -20,7 +20,7 @@ export interface ToProps {
toToken: TokenDetails | undefined;
toAmountQuote: UseTokenOutAmountResult;
fromAmount: Big | undefined;
fromTokenBalances: { [key: string]: BalanceInfo };
tokenBalances: { [key: string]: BalanceInfo };
}

export function To({
Expand All @@ -30,10 +30,9 @@ export function To({
onOpenSelector,
toAmountQuote,
fromAmount,
fromTokenBalances,
tokenBalances,
}: ToProps): JSX.Element | null {
const toTokenBalance =
fromTokenBalances && tokenId && fromTokenBalances[tokenId] ? fromTokenBalances[tokenId].approximateNumber : 0;
const toTokenBalance = tokenBalances && tokenId ? tokenBalances[tokenId] : undefined;

// replace with use state
const [isOpen, { toggle }] = useBoolean(true);
Expand Down Expand Up @@ -84,7 +83,10 @@ export function To({
</div>
<div className="flex justify-between items-center mt-1 dark:text-neutral-300 text-neutral-500">
{/* <div className="text-sm mt-px">{toToken ? <NablaTokenPrice address={toToken.id} fallback="$ -" /> : '$ -'}</div> */}
<div className="flex gap-1 text-sm">Your balance: {toTokenBalance}</div>
<div className="flex gap-1 text-sm">
Your balance:{' '}
{toTokenBalance ? <TokenBalance query={toTokenBalance} symbol={toToken?.assetCode}></TokenBalance> : '0'}
</div>
</div>
<div className="mt-4 h-px -mx-4 bg-[rgba(0,0,0,0.15)]" />
<div
Expand Down
7 changes: 4 additions & 3 deletions src/components/InputKeys/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ const InputBox: React.FC<InputBoxProps> = ({ onSubmit, dAppName }) => {
}

// check balance of the asset used to offramp directly or to pay for the swap
console.log('balances', balances);
if (balances[from].preciseBigDecimal.lt(fromAmount)) {
alert(
`Insufficient balance to offramp. Current balance is ${
Expand Down Expand Up @@ -253,13 +254,13 @@ const InputBox: React.FC<InputBoxProps> = ({ onSubmit, dAppName }) => {
inputHasError={inputHasErrors}
form={form}
fromFormFieldName="fromAmount"
fromTokenBalances={balances}
tokenBalances={balances}
/>
<div>{formErrorMessage !== undefined && <p className="text-red-600">{formErrorMessage}</p>}</div>
<div className="separator mt-10 mb-10"></div>
<To
tokenId={to}
fromTokenBalances={balances}
tokenBalances={balances}
toToken={toToken}
fromToken={fromToken}
toAmountQuote={
Expand Down Expand Up @@ -287,7 +288,7 @@ const InputBox: React.FC<InputBoxProps> = ({ onSubmit, dAppName }) => {
inputHasError={inputHasErrors}
form={form}
fromFormFieldName="fromAmount"
fromTokenBalances={balances}
tokenBalances={balances}
/>
</Card>
)}
Expand Down
2 changes: 1 addition & 1 deletion src/components/Nabla/BalanceState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const useAccountBalance = (address?: string): UseAccountBalanceResponse =

try {
for (const [key, config] of Object.entries(TOKEN_CONFIG)) {
const response = (await apiComponents.api.query.tokens.accounts(address, config.currencyId)).toHuman() as any;
const response = (await apiComponents.api.query.tokens.accounts(address, config.currencyId)) as any;

const rawBalance = response?.free || '0';
const contractBalance = parseContractBalanceResponse(TOKEN_CONFIG[key].decimals, rawBalance);
Expand Down
12 changes: 10 additions & 2 deletions src/hooks/nabla/useTokenAmountOut.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import BigNumber from 'big.js';
import { activeOptions, cacheKeys } from '../../constants/cache';
import { routerAbi } from '../../contracts/Router';
import { ContractBalance, multiplyByPowerOfTen, parseContractBalanceResponse } from '../../helpers/contracts';
import {
ContractBalance,
multiplyByPowerOfTen,
parseContractBalanceResponse,
stringifyBigWithSignificantDecimals,
} from '../../helpers/contracts';
import { NABLA_ROUTER } from '../../constants/constants';
import { useContractRead } from './useContractRead';
import { UseQueryResult } from '@tanstack/react-query';
Expand Down Expand Up @@ -106,7 +111,10 @@ export function useTokenOutAmount<FormFieldValues extends FieldValues>({

return {
amountOut,
effectiveExchangeRate: amountOut.preciseBigDecimal.div(debouncedAmountBigDecimal).toString(),
effectiveExchangeRate: stringifyBigWithSignificantDecimals(
amountOut.preciseBigDecimal.div(debouncedAmountBigDecimal),
4,
),
swapFee,
};
},
Expand Down

0 comments on commit 9ba0981

Please sign in to comment.