Skip to content

Commit

Permalink
small enhancement
Browse files Browse the repository at this point in the history
fix: Balances details (by chain) missing #111
  • Loading branch information
BenoistP committed Jan 28, 2025
1 parent 4eaf297 commit bff27c8
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 14 deletions.
35 changes: 33 additions & 2 deletions src/hooks/useREG.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useSelector } from 'react-redux'

import { Contract } from 'ethers'

import { WalletType } from 'src/repositories'
import { initializeProviders } from 'src/repositories/RpcProvider'
import {
selectCurrencyRates,
Expand All @@ -12,7 +13,11 @@ import {
selectUserAddressList,
selectUserIncludesEth,
} from 'src/store/features/settings/settingsSelector'
import { REGRealtoken } from 'src/store/features/wallets/walletsSelector'
import {
BalanceByWalletType,
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 @@ -64,18 +69,38 @@ const getREG = async (
ERC20ABI,
GnosisRpcProvider,
)
const balance: BalanceByWalletType = {
[WalletType.Gnosis]: {
amount: 0,
value: 0,
},
[WalletType.Ethereum]: {
amount: 0,
value: 0,
},
[WalletType.RMM]: {
amount: 0,
value: 0,
},
[WalletType.LevinSwap]: {
amount: 0,
value: 0,
},
}
let availableBalance = await getAddressesBalances(
REG_ContractAddress,
addressList,
GnosisRpcProvider,
)
balance[WalletType.Gnosis].amount = availableBalance

if (includeETH) {
availableBalance += await getAddressesBalances(
balance[WalletType.Ethereum].amount = await getAddressesBalances(
REG_ContractAddress,
addressList,
EthereumRpcProvider,
)
availableBalance += balance[WalletType.Ethereum].amount
}

const regVaultAbiGetUserGlobalStateOnly =
Expand Down Expand Up @@ -110,6 +135,7 @@ const getREG = async (
providers,
)

balance[WalletType.Gnosis].amount += lockedBalance
const totalAmount = availableBalance + lockedBalance
const contractRegTotalSupply = await RegContract_Gnosis.totalSupply()
const totalTokens = Number(contractRegTotalSupply) / 10 ** REGtokenDecimals
Expand Down Expand Up @@ -151,6 +177,10 @@ const getREG = async (
: DEFAULT_REG_PRICE / userRate
const value = tokenPrice * amount
const totalInvestment = totalTokens * tokenPrice
// Update all balance values with token price
updateBalanceValues(balance, tokenPrice)

console.dir(balance)

return {
id: `${REG_asset_ID}`,
Expand All @@ -167,6 +197,7 @@ const getREG = async (
value,
totalInvestment,
unitPriceCost: tokenPrice,
balance,
}
}

Expand Down
28 changes: 26 additions & 2 deletions src/hooks/useREGVotingPower.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ import { useSelector } from 'react-redux'

import { Contract } from 'ethers'

import { WalletType } from 'src/repositories'
import { initializeProviders } from 'src/repositories/RpcProvider'
import { selectUserAddressList } from 'src/store/features/settings/settingsSelector'
import { REGVotingPowertoken } from 'src/store/features/wallets/walletsSelector'
import {
BalanceByWalletType,
REGVotingPowertoken,
} from 'src/store/features/wallets/walletsSelector'
import { APIRealTokenProductType } from 'src/types/APIRealToken'
import { ERC20ABI } from 'src/utils/blockchain/abi/ERC20ABI'
import {
Expand All @@ -25,12 +29,30 @@ const getRegVotingPower = async (
ERC20ABI,
GnosisRpcProvider,
)
const balance: BalanceByWalletType = {
[WalletType.Gnosis]: {
amount: 0,
value: 0,
},
[WalletType.Ethereum]: {
amount: 0,
value: 0,
},
[WalletType.RMM]: {
amount: 0,
value: 0,
},
[WalletType.LevinSwap]: {
amount: 0,
value: 0,
},
}
const totalAmount = await getAddressesBalances(
RegVotingPower_Gnosis_ContractAddress,
addressList,
GnosisRpcProvider,
)

balance[WalletType.Gnosis].amount = totalAmount
const contractRegVotePowerTotalSupply =
await RegVotingPowerContract.totalSupply()
const totalTokens =
Expand All @@ -39,6 +61,7 @@ const getRegVotingPower = async (
const tokenPrice = DEFAULT_REGVotingPower_PRICE
const value = tokenPrice * amount
const totalInvestment = tokenPrice * totalTokens
// NO need yo update all balance values: token has no value

return {
id: `${REGVotingPower_asset_ID}`,
Expand All @@ -55,6 +78,7 @@ const getRegVotingPower = async (
value,
totalInvestment,
unitPriceCost: tokenPrice,
balance,
}
}

Expand Down
29 changes: 28 additions & 1 deletion src/hooks/useRWA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useSelector } from 'react-redux'

import { Contract } from 'ethers'

import { WalletType } from 'src/repositories'
import { initializeProviders } from 'src/repositories/RpcProvider'
import {
selectCurrencyRates,
Expand All @@ -11,7 +12,11 @@ import {
import {
selectUserAddressList, // selectUserIncludesEth,
} from 'src/store/features/settings/settingsSelector'
import { RWARealtoken } from 'src/store/features/wallets/walletsSelector'
import {
BalanceByWalletType,
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 Expand Up @@ -48,11 +53,30 @@ const getRWA = async (
ERC20ABI,
GnosisRpcProvider,
)
const balance: BalanceByWalletType = {
[WalletType.Gnosis]: {
amount: 0,
value: 0,
},
[WalletType.Ethereum]: {
amount: 0,
value: 0,
},
[WalletType.RMM]: {
amount: 0,
value: 0,
},
[WalletType.LevinSwap]: {
amount: 0,
value: 0,
},
}
const totalAmount = await getAddressesBalances(
RWA_ContractAddress,
addressList,
GnosisRpcProvider,
)
balance[WalletType.Gnosis].amount = totalAmount
const RwaContractTotalSupply = await contractRwa_Gnosis.totalSupply()
const totalTokens = Number(RwaContractTotalSupply) / 10 ** RWAtokenDecimals
const amount = totalAmount / 10 ** RWAtokenDecimals
Expand Down Expand Up @@ -91,6 +115,8 @@ const getRWA = async (
const tokenPrice = (assetAveragePriceUSD ?? DEFAULT_RWA_PRICE) / userRate
const value = tokenPrice * amount
const totalInvestment = totalTokens * tokenPrice
// Update all balance values with token price
updateBalanceValues(balance, tokenPrice)

return {
id: `${RWA_asset_ID}`,
Expand All @@ -112,6 +138,7 @@ const getRWA = async (
timezone_type: 3,
timezone: 'UTC',
},
balance,
}
}

Expand Down
34 changes: 26 additions & 8 deletions src/store/features/wallets/walletsSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import _max from 'lodash/max'
import _sumBy from 'lodash/sumBy'
import moment from 'moment'

import { AssetProductType } from 'src/components/assetsView'
import { WalletBalances, WalletType } from 'src/repositories'
import { UserRealTokenTransfer } from 'src/repositories/transfers/transfers.type'
import { RootState } from 'src/store/store'
Expand All @@ -23,6 +22,14 @@ import { computeUCP } from 'src/utils/transfer/computeUCP'
import { selectRealtokens } from '../realtokens/realtokensSelector'
import { selectUserRentCalculation } from '../settings/settingsSelector'

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

export interface UserRealtoken extends RealToken {
id: string
isWhitelisted: boolean
Expand All @@ -33,13 +40,7 @@ export interface UserRealtoken extends RealToken {
priceCost?: number
unrealizedCapitalGain?: number
lastChanges: string
balance: Record<
WalletType,
{
amount: number
value: number
}
>
balance: BalanceByWalletType
}

export interface OtherRealtoken {
Expand All @@ -55,6 +56,23 @@ export interface OtherRealtoken {
imageLink: string[]
isRmmAvailable: boolean
unitPriceCost: number
balance: BalanceByWalletType
}

/**
* Updates all balance values with token price
* @param balance
* @param tokenPrice
*/
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
})
}

// eslint-disable-next-line @typescript-eslint/no-empty-interface
Expand Down
2 changes: 1 addition & 1 deletion src/utils/blockchain/erc20Infos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const getAddressesBalances = async (
addressList: string[],
provider: JsonRpcProvider,
consoleWarnOnError = false,
) => {
): Promise<number> => {
let totalAmount = 0
try {
if (!contractAddress) {
Expand Down

0 comments on commit bff27c8

Please sign in to comment.