Skip to content

Commit

Permalink
prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
BenoistP committed Dec 27, 2024
1 parent 156cd9d commit 54555ac
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 45 deletions.
7 changes: 5 additions & 2 deletions src/hooks/useREG.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import {
selectUserAddressList,
selectUserIncludesEth,
} from 'src/store/features/settings/settingsSelector'
import { REGRealtoken, updateBalanceValues } from 'src/store/features/wallets/walletsSelector'
import {
REGRealtoken,
updateBalanceValues,
} from 'src/store/features/wallets/walletsSelector'
import { APIRealTokenProductType } from 'src/types/APIRealToken'
import { Currency } from 'src/types/Currencies'
import { ERC20ABI } from 'src/utils/blockchain/abi/ERC20ABI'
Expand Down Expand Up @@ -163,7 +166,7 @@ const getREG = async (
value,
totalInvestment,
unitPriceCost: tokenPrice,
balance: balances.balance
balance: balances.balance,
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useREGVotingPower.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const getRegVotingPower = async (
value,
totalInvestment,
unitPriceCost: tokenPrice,
balance: balances.balance
balance: balances.balance,
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/hooks/useRWA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import {
import {
selectUserAddressList, // selectUserIncludesEth,
} from 'src/store/features/settings/settingsSelector'
import { RWARealtoken, updateBalanceValues } from 'src/store/features/wallets/walletsSelector'
import {
RWARealtoken,
updateBalanceValues,
} from 'src/store/features/wallets/walletsSelector'
import { APIRealTokenProductType } from 'src/types/APIRealToken'
import { Currency } from 'src/types/Currencies'
import { ERC20ABI } from 'src/utils/blockchain/abi/ERC20ABI'
Expand Down
8 changes: 6 additions & 2 deletions src/repositories/RpcProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ export async function getTransactionReceipt(
* @param provider (Ethers) RPC provider
* @returns Chain ID as number | undefined
*/
export const getChainId = (provider: JsonRpcProvider | undefined): number | undefined => {
return (provider?._network?.chainId ? (Number(provider?._network?.chainId)) : undefined)
export const getChainId = (
provider: JsonRpcProvider | undefined,
): number | undefined => {
return provider?._network?.chainId
? Number(provider?._network?.chainId)
: undefined
}
32 changes: 17 additions & 15 deletions src/store/features/wallets/walletsSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,16 @@ import {
CHAIN_NAME_ETHEREUM,
CHAIN_NAME_GNOSIS_XDAI,
} from '../../../utils/blockchain/consts/otherTokens'

import { selectRealtokens } from '../realtokens/realtokensSelector'
import { selectUserRentCalculation } from '../settings/settingsSelector'

export type BalanceByWalletType =
Record<
WalletType,
{
amount: number
value: number
}
>
export type BalanceByWalletType = Record<
WalletType,
{
amount: number
value: number
}
>

export interface UserRealtoken extends RealToken {
id: string
Expand Down Expand Up @@ -69,14 +67,18 @@ export interface OtherRealtoken {

/**
* Updates all balance values with token price
* @param balance
* @param tokenPrice
* @param balance
* @param tokenPrice
*/
export const updateBalanceValues = (balance: BalanceByWalletType, tokenPrice:number) => {
export const updateBalanceValues = (
balance: BalanceByWalletType,
tokenPrice: number,
) => {
// Loop on each record
Object.keys(balance).forEach((key) => {
balance[key as WalletType].value = balance[key as WalletType].amount * tokenPrice;
});
balance[key as WalletType].value =
balance[key as WalletType].amount * tokenPrice
})
}

// eslint-disable-next-line @typescript-eslint/no-empty-interface
Expand Down Expand Up @@ -278,4 +280,4 @@ export const getWalletChainName = (chainId: number) => {
case CHAIN_ID_GNOSIS_XDAI:
return CHAIN_NAME_GNOSIS_XDAI
}
}
}
62 changes: 38 additions & 24 deletions src/utils/blockchain/erc20Infos.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
import { Contract, JsonRpcProvider } from 'ethers'

import { getChainId } from 'src/repositories/RpcProvider'
import { BalanceByWalletType } from 'src/store/features/wallets/walletsSelector'
import { getWalletChainName } from 'src/store/features/wallets/walletsSelector'
import { getErc20AbiBalanceOfOnly } from 'src/utils/blockchain/ERC20'

import { batchCallOneContractOneFunctionMultipleParams } from './contract'
import { BalanceByWalletType } from 'src/store/features/wallets/walletsSelector'
import { getWalletChainName } from 'src/store/features/wallets/walletsSelector'
import { getChainId } from 'src/repositories/RpcProvider'

export interface Balances {
totalAmount: number,
balance: BalanceByWalletType,
totalAmount: number
balance: BalanceByWalletType
}

const getAddressesBalances = async (
contractAddress: string,
addressList: string[],
providers: JsonRpcProvider[],
consoleWarnOnError = false,
):Promise<Balances> => {
): Promise<Balances> => {
const balances: Balances = {
totalAmount: 0,
balance: {
gnosis: {
amount: 0,
value : 0,
value: 0,
},
ethereum: {
amount: 0,
value : 0,
value: 0,
},
rmm: {
amount: 0,
value : 0,
value: 0,
},
levinSwap: {
amount: 0,
value : 0,
}
}
value: 0,
},
},
}
try {
if (!contractAddress) {
Expand Down Expand Up @@ -71,23 +71,37 @@ const getAddressesBalances = async (
})
const balancesArray = await Promise.all(balancesPromises.flat())
// warn but don't stop
balancesArray?.length !== providers.length && consoleWarnOnError && console.warn(`Invalid balances array length (${balancesArray?.length}) (inconsistent with providers length (${providers.length}))`)
balancesArray?.length !== providers.length &&
consoleWarnOnError &&
console.warn(
`Invalid balances array length (${balancesArray?.length}) (inconsistent with providers length (${providers.length}))`,
)
providers.forEach((provider: JsonRpcProvider, providerIdx) => {
const chainId = getChainId(provider)
const wt = chainId ? getWalletChainName(chainId) : null
if (wt) {
balances.balance[wt].value = 0;
balances.balance[wt].value = 0
// warn but don't stop
balancesArray[providerIdx]?.length !== addressList.length && consoleWarnOnError && console.warn('Invalid balances array (inconsistent addressList length)');
(balancesArray[providerIdx] as unknown as bigint[])?.forEach((addressBalanceBI: bigint, addressIdx) => {
try {
const addressBalance = Number(addressBalanceBI)
balances.balance[wt].amount += addressBalance
} catch (error) {
// warn but don't stop
consoleWarnOnError && console.warn(`Invalid balance conversion for address ${addressList[addressIdx]} on chain ${chainId}`, error)
}
})
balancesArray[providerIdx]?.length !== addressList.length &&
consoleWarnOnError &&
console.warn(
'Invalid balances array (inconsistent addressList length)',
)
;(balancesArray[providerIdx] as unknown as bigint[])?.forEach(
(addressBalanceBI: bigint, addressIdx) => {
try {
const addressBalance = Number(addressBalanceBI)
balances.balance[wt].amount += addressBalance
} catch (error) {
// warn but don't stop
consoleWarnOnError &&
console.warn(
`Invalid balance conversion for address ${addressList[addressIdx]} on chain ${chainId}`,
error,
)
}
},
)
balances.totalAmount += balances.balance[wt].amount
}
})
Expand Down

0 comments on commit 54555ac

Please sign in to comment.