Skip to content

Commit 41a0fa0

Browse files
Fix various instances where JS Number types were used incorrectly
1 parent 0981a51 commit 41a0fa0

File tree

5 files changed

+8
-10
lines changed

5 files changed

+8
-10
lines changed

src/hooks/chainge/fetchAggregateQuote.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const API_URL = 'https://api2.chainge.finance/v1/getAggregateQuote'
2222
export const fetchAggregateQuote = async (
2323
fromToken: ChaingeToken,
2424
toToken: ChaingeToken,
25-
fromAmount: bigint,
25+
fromAmount: string,
2626
options: { signal?: AbortSignal } = {},
2727
): Promise<ChaingeAggregateQuote | undefined> => {
2828
try {

src/hooks/chainge/useAggregateQuote.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useEffect, useState } from 'react'
22
import { ChaingeAggregateQuote, fetchAggregateQuote } from '@/hooks/chainge/fetchAggregateQuote'
33
import { formatNumberWithDecimal } from '@/utils/formatting'
44
import { ChaingeToken } from '@/hooks/chainge/useChaingeTokens'
5+
import { parseUnits } from 'ethers'
56

67
// TODO refetch every 15 seconds
78
const useAggregateQuote = (
@@ -18,11 +19,6 @@ const useAggregateQuote = (
1819
const controller = new AbortController()
1920
const { signal } = controller
2021

21-
const formatPayAmountToBigInt = (amount: number, decimals: number): bigint => {
22-
const scaledAmount = Math.round(amount * Math.pow(10, decimals))
23-
return BigInt(scaledAmount)
24-
}
25-
2622
const fetchQuote = async () => {
2723
if (!payAmount || !(Number(payAmount) > 0)) {
2824
setReceiveAmount('')
@@ -34,7 +30,7 @@ const useAggregateQuote = (
3430
await new Promise((resolve) => setTimeout(resolve, 200))
3531
setError(null)
3632
try {
37-
const adjustedPayAmount = formatPayAmountToBigInt(parseFloat(payAmount), payToken.decimals)
33+
const adjustedPayAmount = parseUnits(payAmount, payToken.decimals).toString()
3834
const quote = await fetchAggregateQuote(payToken, receiveToken, adjustedPayAmount, { signal })
3935
setAggregateQuote(quote)
4036
if (quote) {

src/hooks/ghost/fetchPriceV2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const fetchPriceV2 = async (currency: string, names: string): Promise<Pri
2424

2525
const data = await response.json()
2626

27-
console.log('Cloudflare json response:', JSON.stringify(data, null, 2))
27+
//console.log('Cloudflare json response:', JSON.stringify(data, null, 2))
2828

2929
if (!data || typeof data.source !== 'string' || typeof data.currency !== 'string' || !data.prices) {
3030
console.error('Cloudflare response data structure wrong')

src/pages/Wallet/Swap.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import useKaspa from '@/hooks/contexts/useKaspa'
1919
import ErrorMessages from '@/utils/constants/errorMessages'
2020
import { MINIMUM_KAS_FOR_GAS_FEE } from '@/utils/constants/constants'
2121
import { KAS_TICKER, USDT_TICKER } from '@/utils/constants/tickers'
22+
import { parseUnits } from 'ethers'
2223

2324
export default function Swap() {
2425
const location = useLocation()
@@ -56,7 +57,7 @@ export default function Swap() {
5657
{
5758
fromAmount:
5859
payToken.symbol !== KAS_TICKER
59-
? Math.round(Number(payAmount) * Math.pow(10, payToken.decimals)).toString()
60+
? parseUnits(payAmount, payToken.decimals).toString()
6061
: payAmount,
6162
fromToken: payToken,
6263
feeRate,

src/wallet/krc20/KRC20Transactions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import AccountTransactions from '../account/AccountTransactions'
2222
import { KRC20TokenRequest, TokenFromApi } from '@/utils/interfaces'
2323
import { KRC20Inscription } from './KRC20Inscription'
2424
import { KRC20_COMMIT_AMOUNT } from '@/utils/constants/constants'
25+
import { parseUnits } from 'ethers'
2526

2627
export type Token = TokenFromApi
2728

@@ -36,7 +37,7 @@ function setupKrc20Transaction(
3637
const script = new ScriptBuilder()
3738
const inscription = new KRC20Inscription('transfer', {
3839
tick,
39-
amt: BigInt(+amount * 10 ** +dec).toString(),
40+
amt: parseUnits(amount, dec).toString(),
4041
to: recipient,
4142
})
4243

0 commit comments

Comments
 (0)