Skip to content

Commit

Permalink
Fixes: token prices in USD, avoid fetching REG vault balances on Eth
Browse files Browse the repository at this point in the history
- Tokens values fix: Updated assets prices by using currenciesRates and user currency rate (previously considering usdc = usd and xdai = usd as well),
now assets prices are converted from their respective currencies (e.g. usdc, xdai) to usd, then total value is priced in user choosen currency.
- Smol fix: getAddressesLockedBalances was fetching vault balances on Eth, throwing errors as there is not such vault on Eth.
Added parameters for fetching any number of vault by provider(s).
  • Loading branch information
BenoistP committed Dec 3, 2024
1 parent 387a2e2 commit df0a89e
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 57 deletions.
82 changes: 71 additions & 11 deletions src/hooks/useREG.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@ import { useSelector } from 'react-redux'
import { Contract } from 'ethers'

import { initializeProviders } from 'src/repositories/RpcProvider'
import { selectUserCurrency } from 'src/store/features/currencies/currenciesSelector'
import {
selectCurrencyRates,
selectUserCurrency,
} from 'src/store/features/currencies/currenciesSelector'
import {
selectUserAddressList,
selectUserIncludesEth,
} from 'src/store/features/settings/settingsSelector'
import { REGRealtoken } from 'src/store/features/wallets/walletsSelector'
import { Currency } from 'src/types/Currencies'
import { ERC20ABI } from 'src/utils/blockchain/abi/ERC20ABI'
import {
DEFAULT_REG_PRICE,
DEFAULT_USDC_USD_RATE,
DEFAULT_XDAI_USD_RATE,
HoneySwapFactory_Address,
REG_ContractAddress,
REG_VaultContractAddress,
REG_Vault_Gnosis_ContractAddress,
REG_asset_ID,
REGtokenDecimals,
USDConXdai_ContractAddress,
Expand All @@ -28,11 +34,23 @@ import {
averageValues,
getUniV2AssetPrice,
} from 'src/utils/blockchain/poolPrice'
import { getAddressesLockedBalances } from 'src/utils/blockchain/regVault'
import {
getAddressesLockedBalances,
getRegVaultAbiGetUserGlobalStateOnly,
} from 'src/utils/blockchain/regVault'

/**
*
* @param addressList : user addresses list
* @param userRate : user selected currency rate
* @param currenciesRates : currencies rates
* @param includeETH : include balances on ETH in the calculation
* @returns
*/
const getREG = async (
addressList: string[],
rate: number,
userRate: number,
currenciesRates: Record<Currency, number>,
includeETH = false,
): Promise<REGRealtoken> => {
const { GnosisRpcProvider, EthereumRpcProvider } = await initializeProviders()
Expand All @@ -50,8 +68,35 @@ const getREG = async (
addressList,
providers,
)

const regVaultAbiGetUserGlobalStateOnly =
getRegVaultAbiGetUserGlobalStateOnly()

const lockedBalance = await getAddressesLockedBalances(
REG_VaultContractAddress,
[
// First provider
[
// First vault
[
REG_Vault_Gnosis_ContractAddress, // Contract address
regVaultAbiGetUserGlobalStateOnly, // Contract ABI
'getUserGlobalState', // Contract method for getting balance
],
// Second vault ...
],
/*
// Second provider
[
// First vault ...
[
// Contract address
// Contract ABI
// Contract method for getting balance
],
],
// ...
*/
],
addressList,
providers,
)
Expand All @@ -61,6 +106,7 @@ const getREG = async (
const totalTokens = Number(contractRegTotalSupply) / 10 ** REGtokenDecimals
const amount = totalAmount / 10 ** REGtokenDecimals

// Get REG token prices in USDC and WXDAI from LPs
const regPriceUsdc = await getUniV2AssetPrice(
HoneySwapFactory_Address,
REG_ContractAddress,
Expand All @@ -78,9 +124,22 @@ const getREG = async (
GnosisRpcProvider,
)

const averagePrice = averageValues([regPriceUsdc, regPriceWxdai])
const tokenPrice = averagePrice ? averagePrice / rate : DEFAULT_REG_PRICE

// Get rates for XDAI and USDC against USD
const rateXdaiUsd = currenciesRates?.XDAI
? currenciesRates.XDAI
: DEFAULT_XDAI_USD_RATE
const rateUsdcUsd = currenciesRates?.USDC
? currenciesRates.USDC
: DEFAULT_USDC_USD_RATE
// Convert token prices to USD
const assetPriceUsd1 = regPriceUsdc ? regPriceUsdc * rateUsdcUsd : null
const assetPriceUsd2 = regPriceWxdai ? regPriceWxdai * rateXdaiUsd : null
// Get average token prices in USD
const assetAveragePriceUSD = averageValues([assetPriceUsd1, assetPriceUsd2])
// Convert prices in Currency by applying rate
const tokenPrice = assetAveragePriceUSD
? assetAveragePriceUSD / userRate
: DEFAULT_REG_PRICE / userRate
const value = tokenPrice * amount
const totalInvestment = totalTokens * tokenPrice

Expand All @@ -104,14 +163,15 @@ const getREG = async (
export const useREG = () => {
const [reg, setReg] = useState<REGRealtoken | null>(null)
const addressList = useSelector(selectUserAddressList)
const { rate } = useSelector(selectUserCurrency)
const { rate: userRate } = useSelector(selectUserCurrency)
const includeETH = useSelector(selectUserIncludesEth)
const currenciesRates = useSelector(selectCurrencyRates)

useEffect(() => {
if (addressList.length) {
getREG(addressList, rate, includeETH).then(setReg)
getREG(addressList, userRate, currenciesRates, includeETH).then(setReg)
}
}, [addressList])
}, [addressList, userRate, currenciesRates, includeETH])

return reg
}
6 changes: 3 additions & 3 deletions src/hooks/useREGVotingPower.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
DEFAULT_REGVotingPower_PRICE,
REGVotingPower_asset_ID,
REGVotingPowertokenDecimals,
RegVotingPower_ContractAddress,
RegVotingPower_Gnosis_ContractAddress,
} from 'src/utils/blockchain/consts/otherTokens'
import { getAddressesBalances } from 'src/utils/blockchain/erc20Infos'

Expand All @@ -21,12 +21,12 @@ const getRegVotingPower = async (
const { GnosisRpcProvider } = await initializeProviders()
const providers = [GnosisRpcProvider]
const RegVotingPowerContract = new Contract(
RegVotingPower_ContractAddress,
RegVotingPower_Gnosis_ContractAddress,
ERC20ABI,
GnosisRpcProvider,
)
const totalAmount = await getAddressesBalances(
RegVotingPower_ContractAddress,
RegVotingPower_Gnosis_ContractAddress,
addressList,
providers,
)
Expand Down
50 changes: 34 additions & 16 deletions src/hooks/useRWA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@ import { useSelector } from 'react-redux'
import { Contract } from 'ethers'

import { initializeProviders } from 'src/repositories/RpcProvider'
import { selectUserCurrency } from 'src/store/features/currencies/currenciesSelector'
import {
selectUserAddressList,
selectUserIncludesEth,
selectCurrencyRates,
selectUserCurrency,
} from 'src/store/features/currencies/currenciesSelector'
import {
selectUserAddressList, // selectUserIncludesEth,
} from 'src/store/features/settings/settingsSelector'
import { RWARealtoken } from 'src/store/features/wallets/walletsSelector'
import { Currency } from 'src/types/Currencies'
import { ERC20ABI } from 'src/utils/blockchain/abi/ERC20ABI'
import {
DEFAULT_RWA_PRICE,
DEFAULT_USDC_USD_RATE,
DEFAULT_XDAI_USD_RATE,
HoneySwapFactory_Address,
RWA_ContractAddress,
RWA_asset_ID,
Expand All @@ -30,14 +35,13 @@ import {

const getRWA = async (
addressList: string[],
rate: number,
includeETH = false,
userRate: number,
currenciesRates: Record<Currency, number>,
// includeETH = false,
): Promise<RWARealtoken> => {
const { GnosisRpcProvider, EthereumRpcProvider } = await initializeProviders()
const { GnosisRpcProvider /* , EthereumRpcProvider */ } =
await initializeProviders()
const providers = [GnosisRpcProvider]
if (includeETH) {
providers.push(EthereumRpcProvider)
}

const contractRwa_Gnosis = new Contract(
RWA_ContractAddress,
Expand All @@ -53,6 +57,7 @@ const getRWA = async (
const totalTokens = Number(RwaContractTotalSupply) / 10 ** RWAtokenDecimals
const amount = totalAmount / 10 ** RWAtokenDecimals

// RWA token prices in USDC and WXDAI from LPs
const rwaPriceUsdc = await getUniV2AssetPrice(
HoneySwapFactory_Address,
RWA_ContractAddress,
Expand All @@ -69,8 +74,21 @@ const getRWA = async (
WXDAItokenDecimals,
GnosisRpcProvider,
)
const averagePrice = averageValues([rwaPriceUsdc, rwaPriceWxdai])
const tokenPrice = (averagePrice ?? DEFAULT_RWA_PRICE) / rate

// Get rates for XDAI and USDC against USD
const rateXdaiUsd = currenciesRates?.XDAI
? currenciesRates.XDAI
: DEFAULT_XDAI_USD_RATE
const rateUsdcUsd = currenciesRates?.USDC
? currenciesRates.USDC
: DEFAULT_USDC_USD_RATE
// Convert token prices to USD
const assetPriceUsd1 = rwaPriceUsdc ? rwaPriceUsdc * rateUsdcUsd : null
const assetPriceUsd2 = rwaPriceWxdai ? rwaPriceWxdai * rateXdaiUsd : null
// Get average token price in USD
const assetAveragePriceUSD = averageValues([assetPriceUsd1, assetPriceUsd2])
// Convert price in Currency by applying rate
const tokenPrice = (assetAveragePriceUSD ?? DEFAULT_RWA_PRICE) / userRate
const value = tokenPrice * amount
const totalInvestment = totalTokens * tokenPrice

Expand Down Expand Up @@ -99,14 +117,14 @@ const getRWA = async (
export const useRWA = () => {
const [rwa, setRwa] = useState<RWARealtoken | null>(null)
const addressList = useSelector(selectUserAddressList)
const { rate } = useSelector(selectUserCurrency)
const includeETH = useSelector(selectUserIncludesEth)

const { rate: userRate } = useSelector(selectUserCurrency)
const currenciesRates = useSelector(selectCurrencyRates)
// const includeETH = useSelector(selectUserIncludesEth) // useless: RWA does not (yet ?) exist on Ethereum
useEffect(() => {
if (addressList.length) {
getRWA(addressList, rate, includeETH).then(setRwa)
getRWA(addressList, userRate, currenciesRates).then(setRwa)
}
}, [addressList])
}, [addressList, userRate, currenciesRates])

return rwa
}
15 changes: 11 additions & 4 deletions src/utils/blockchain/consts/otherTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ const REGVotingPower_asset_ID = 2
const RWA_ContractAddress = '0x0675e8F4A52eA6c845CB6427Af03616a2af42170'
// Gnosis/xDai, Ethereum
const REG_ContractAddress = '0x0AA1e96D2a46Ec6beB2923dE1E61Addf5F5f1dce'
const REG_VaultContractAddress = '0xe1877d33471e37fe0f62d20e60c469eff83fb4a0'
// Reg Vault only deployed on Gnosis/xDai
const REG_Vault_Gnosis_ContractAddress =
'0xe1877d33471e37fe0f62d20e60c469eff83fb4a0'
// Reg Voting Power only deployed on Gnosis/xDai
const RegVotingPower_ContractAddress =
const RegVotingPower_Gnosis_ContractAddress =
'0x6382856a731Af535CA6aea8D364FCE67457da438'

// Gnosis/xDai tokens for prices calculation
Expand All @@ -19,6 +21,9 @@ const DEFAULT_RWA_PRICE = 50 // USD
const DEFAULT_REG_PRICE = 0 // USD
const DEFAULT_REGVotingPower_PRICE = 0 // USD

const DEFAULT_XDAI_USD_RATE = 1
const DEFAULT_USDC_USD_RATE = 1

const RWAtokenDecimals = 9
const REGtokenDecimals = 18
const REGVotingPowertokenDecimals = 18
Expand All @@ -30,8 +35,8 @@ const HoneySwapFactory_Address = '0xA818b4F111Ccac7AA31D0BCc0806d64F2E0737D7'
export {
RWA_ContractAddress,
REG_ContractAddress,
REG_VaultContractAddress,
RegVotingPower_ContractAddress,
REG_Vault_Gnosis_ContractAddress,
RegVotingPower_Gnosis_ContractAddress,
WXDAI_ContractAddress,
USDConXdai_ContractAddress,
WXDAItokenDecimals,
Expand All @@ -43,6 +48,8 @@ export {
DEFAULT_RWA_PRICE,
DEFAULT_REG_PRICE,
DEFAULT_REGVotingPower_PRICE,
DEFAULT_XDAI_USD_RATE,
DEFAULT_USDC_USD_RATE,
RWA_asset_ID,
REG_asset_ID,
REGVotingPower_asset_ID,
Expand Down
Loading

0 comments on commit df0a89e

Please sign in to comment.