Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/components/Request/Pay/Views/Initial.view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@ import Icon from '@/components/Global/Icon'
import MoreInfo from '@/components/Global/MoreInfo'
import * as consts from '@/constants'
import { useCreateLink } from '@/components/Create/useCreateLink'
import { peanut } from '@squirrel-labs/peanut-sdk'
import { peanut, interfaces } from '@squirrel-labs/peanut-sdk'
import TokenSelector from '@/components/Global/TokenSelector/TokenSelector'
import { switchNetwork as switchNetworkUtil } from '@/utils/general.utils'
import { ADDRESS_ZERO, EPeanutLinkType, RequestStatus } from '../utils'

const ERR_NO_ROUTE = 'No route found to pay in this chain and token'

enum RequestStatus {
LOADING = 'LOADING',
CLAIM = 'CLAIM',
NOT_CONNECTED = 'NOT_CONNECTED',
NOT_FOUND = 'NOT_FOUND',
}

export const InitialView = ({
onNext,
requestLinkData,
Expand Down Expand Up @@ -64,7 +70,9 @@ export const InitialView = ({
squidRouterUrl: 'https://apiplus.squidrouter.com/v2/route',
apiUrl: '/api/proxy/get',
provider: await peanut.getDefaultProvider(selectedTokenData!.chainId),
tokenType: selectedTokenData!.address === ADDRESS_ZERO ? EPeanutLinkType.native : EPeanutLinkType.erc20,
tokenType: utils.isAddressZero(selectedTokenData!.address)
? interfaces.EPeanutLinkType.native
: interfaces.EPeanutLinkType.erc20,
fromTokenDecimals: selectedTokenData!.decimals as number,
})
return xchainUnsignedTxs
Expand Down Expand Up @@ -118,9 +126,6 @@ export const InitialView = ({
}
}

// wait for token selector to fetch token price, both effects depend on
// selectedTokenAddress and selectedChainID, but we depend on that
// effect being completed first
if (!isConnected) return

if (isXChain && !selectedTokenData) {
Expand Down Expand Up @@ -168,6 +173,7 @@ export const InitialView = ({
}, [requestLinkData, tokenPriceData])

useEffect(() => {
// Load the token chain pair from the request link data
resetTokenAndChain()
}, [])

Expand Down
19 changes: 0 additions & 19 deletions src/components/Request/Pay/utils.ts

This file was deleted.

5 changes: 3 additions & 2 deletions src/hooks/useBalance.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useAccount } from 'wagmi'
import * as interfaces from '@/interfaces'
import { useEffect, useState, useRef } from 'react'
import * as utils from '@/utils'

/**
* Custom React hook to fetch and manage user's wallet balances across multiple chains,
Expand Down Expand Up @@ -122,9 +123,9 @@ export const useBalance = () => {
const valueB = parseFloat(b.value)

if (valueA === valueB) {
if (a.address.toLowerCase() === '0x0000000000000000000000000000000000000000')
if (utils.isAddressZero(a.address))
return -1
if (b.address.toLowerCase() === '0x0000000000000000000000000000000000000000')
if (utils.isAddressZero(b.address))
return 1

return b.amount - a.amount
Expand Down
8 changes: 3 additions & 5 deletions src/utils/fetch.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ export const fetchTokenPrice = async (
host?: string
): Promise<ITokenPriceData | undefined> => {
try {
if (tokenAddress.toLowerCase() == '0x0000000000000000000000000000000000000000') {
tokenAddress = '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'
}

// Routing mobula api call through nextjs BFF
const mobulaResponse = await fetch(
host ? `${host}/api/mobula/fetch-token-price` : `/api/mobula/fetch-token-price`,
Expand All @@ -63,7 +59,9 @@ export const fetchTokenPrice = async (
'Content-Type': 'application/json',
},
body: JSON.stringify({
tokenAddress,
tokenAddress: utils.isAddressZero(tokenAddress)
? '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'
: tokenAddress,
chainId,
}),
}
Expand Down
8 changes: 6 additions & 2 deletions src/utils/general.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,14 +325,18 @@ export const isTestnetChain = (chainId: string) => {

export const areTokenAddressesEqual = (address1: string, address2: string): boolean => {
if (address1.toLowerCase() === '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE'.toLocaleLowerCase())
address1 = '0x0000000000000000000000000000000000000000'
address1 = ethers.constants.AddressZero
if (address2.toLowerCase() === '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE'.toLocaleLowerCase())
address2 = '0x0000000000000000000000000000000000000000'
address2 = ethers.constants.AddressZero
// By using ethers.getAddress we are safe from different cases
// and other address formatting
return ethers.utils.getAddress(address1) === ethers.utils.getAddress(address2)
}

export const isAddressZero = (address: string): boolean => {
return areTokenAddressesEqual(address, ethers.constants.AddressZero)
}

export const isNativeCurrency = (address: string) => {
if (consts.nativeCurrencyAddresses.includes(address.toLowerCase())) {
return true
Expand Down