Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: optimize RPC calls #114

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
},
"dependencies": {
"@apollo/client": "^3.9.5",
"@bgd-labs/aave-address-book": "^4.7.3",
"@mantine/core": "^7.5.3",
"@mantine/dates": "^7.5.3",
"@mantine/form": "^7.5.3",
Expand Down
12 changes: 10 additions & 2 deletions src/hooks/useREG.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,20 @@ const getREG = async (
ERC20ABI,
GnosisRpcProvider,
)
const availableBalance = await getAddressesBalances(
let availableBalance = await getAddressesBalances(
REG_ContractAddress,
addressList,
providers,
GnosisRpcProvider,
)

if (includeETH) {
availableBalance += await getAddressesBalances(
REG_ContractAddress,
addressList,
EthereumRpcProvider,
)
}

const regVaultAbiGetUserGlobalStateOnly =
getRegVaultAbiGetUserGlobalStateOnly()

Expand Down
3 changes: 1 addition & 2 deletions src/hooks/useREGVotingPower.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const getRegVotingPower = async (
addressList: string[],
): Promise<REGVotingPowertoken> => {
const { GnosisRpcProvider } = await initializeProviders()
const providers = [GnosisRpcProvider]
const RegVotingPowerContract = new Contract(
RegVotingPower_Gnosis_ContractAddress,
ERC20ABI,
Expand All @@ -29,7 +28,7 @@ const getRegVotingPower = async (
const totalAmount = await getAddressesBalances(
RegVotingPower_Gnosis_ContractAddress,
addressList,
providers,
GnosisRpcProvider,
)

const contractRegVotePowerTotalSupply =
Expand Down
3 changes: 1 addition & 2 deletions src/hooks/useRWA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ const getRWA = async (
): Promise<RWARealtoken> => {
const { GnosisRpcProvider /* , EthereumRpcProvider */ } =
await initializeProviders()
const providers = [GnosisRpcProvider]

const contractRwa_Gnosis = new Contract(
RWA_ContractAddress,
Expand All @@ -52,7 +51,7 @@ const getRWA = async (
const totalAmount = await getAddressesBalances(
RWA_ContractAddress,
addressList,
providers,
GnosisRpcProvider,
)
const RwaContractTotalSupply = await contractRwa_Gnosis.totalSupply()
const totalTokens = Number(RwaContractTotalSupply) / 10 ** RWAtokenDecimals
Expand Down
12 changes: 2 additions & 10 deletions src/repositories/RpcProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
ZeroAddress,
} from 'ethers'

import { getErc20AbiBalanceOfOnly } from 'src/utils/blockchain/ERC20'
import { ERC20ABI } from 'src/utils/blockchain/abi/ERC20ABI'
import { REG_ContractAddress } from 'src/utils/blockchain/consts/otherTokens'
import { batchCallOneContractOneFunctionMultipleParams } from 'src/utils/blockchain/contract'
import { wait } from 'src/utils/general'
Expand Down Expand Up @@ -80,18 +80,10 @@ async function testRpcThresholds(
if (concurrentRequestsMin > concurrentRequestsMax) {
throw new Error('concurrentMin cannot be greater than concurrentMax')
}
const erc20AbiBalanceOfOnly = getErc20AbiBalanceOfOnly()
if (!erc20AbiBalanceOfOnly) {
throw new Error('balanceOf ABI not found')
}

// Create a dummy array of addresses filled with 'ZeroAddress' for fetching balances
const batchAddressesArray = Array(requestsBatchSize).fill([ZeroAddress])
const contract = new Contract(
REG_ContractAddress,
erc20AbiBalanceOfOnly,
provider,
)
const contract = new Contract(REG_ContractAddress, ERC20ABI, provider)
// Loop from max to min concurrent requests
for (
let currentThresold = concurrentRequestsMax;
Expand Down
8 changes: 0 additions & 8 deletions src/utils/blockchain/ERC20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,3 @@ export const ERC20 = {
isTransferEvent,
parseTransferEvent,
}

export const getErc20AbiBalanceOfOnly = (): object[] | null => {
const Erc20AbiBalanceOfOnly = ERC20ABI.find((abi) => abi.name === 'balanceOf')
if (!Erc20AbiBalanceOfOnly) {
throw new Error('balanceOf not found in ERC20 ABI')
}
return [Erc20AbiBalanceOfOnly]
}
36 changes: 36 additions & 0 deletions src/utils/blockchain/abi/WalletBalanceProviderABI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export const WalletBalanceProviderABI = [
{
inputs: [
{ internalType: 'address', name: 'user', type: 'address' },
{ internalType: 'address', name: 'token', type: 'address' },
],
name: 'balanceOf',
outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
{
inputs: [
{ internalType: 'address[]', name: 'users', type: 'address[]' },
{ internalType: 'address[]', name: 'tokens', type: 'address[]' },
],
name: 'batchBalanceOf',
outputs: [{ internalType: 'uint256[]', name: '', type: 'uint256[]' }],
stateMutability: 'view',
type: 'function',
},
{
inputs: [
{ internalType: 'address', name: 'provider', type: 'address' },
{ internalType: 'address', name: 'user', type: 'address' },
],
name: 'getUserWalletBalances',
outputs: [
{ internalType: 'address[]', name: '', type: 'address[]' },
{ internalType: 'uint256[]', name: '', type: 'uint256[]' },
],
stateMutability: 'view',
type: 'function',
},
{ stateMutability: 'payable', type: 'receive' },
]
62 changes: 31 additions & 31 deletions src/utils/blockchain/erc20Infos.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Contract, JsonRpcProvider } from 'ethers'
import { AaveV3Ethereum, AaveV3Gnosis } from '@bgd-labs/aave-address-book'

import { getErc20AbiBalanceOfOnly } from 'src/utils/blockchain/ERC20'
import { Contract, JsonRpcProvider } from 'ethers'

import { batchCallOneContractOneFunctionMultipleParams } from './contract'
import { WalletBalanceProviderABI } from './abi/WalletBalanceProviderABI'

const getAddressesBalances = async (
contractAddress: string,
addressList: string[],
providers: JsonRpcProvider[],
provider: JsonRpcProvider,
consoleWarnOnError = false,
) => {
let totalAmount = 0
Expand All @@ -20,38 +20,38 @@ const getAddressesBalances = async (
consoleWarnOnError && console.error('Invalid address list')
return totalAmount
}
if (!providers?.length) {
if (!provider) {
consoleWarnOnError && console.error('Invalid providers')
return totalAmount
}
const erc20AbiBalanceOfOnly = getErc20AbiBalanceOfOnly()
if (!erc20AbiBalanceOfOnly) {
throw new Error('balanceOf ABI not found')

let walletBalanceProviderAddress
switch (provider._network.chainId) {
case 100n:
walletBalanceProviderAddress = AaveV3Gnosis.WALLET_BALANCE_PROVIDER
break
case 1n:
walletBalanceProviderAddress = AaveV3Ethereum.WALLET_BALANCE_PROVIDER
break
default:
consoleWarnOnError && console.error('Invalid provider')
return totalAmount
}
const balancesPromises = providers.map((provider: JsonRpcProvider) => {
const Erc20BalanceContract = new Contract(
contractAddress,
erc20AbiBalanceOfOnly,
provider,
)
const balances = batchCallOneContractOneFunctionMultipleParams(
Erc20BalanceContract,
'balanceOf',
addressList.map((address: string) => [address as unknown as object]),
)
return balances
})

const balancesArray = await Promise.all(balancesPromises.flat())
const balances = balancesArray.flat()
// Sum all valid balances
balances.forEach((balance: object | null | undefined) => {
try {
if (balance) {
totalAmount += Number(balance)
}
} catch (error) {}
})
const balanceWalletProviderContract = new Contract(
walletBalanceProviderAddress,
WalletBalanceProviderABI,
provider,
)

const balances = await balanceWalletProviderContract.batchBalanceOf(
addressList,
[contractAddress],
)
totalAmount = balances.reduce(
(acc: number, b: bigint) => acc + Number(b),
0,
)
return totalAmount
} catch (error) {
console.warn('Failed to get balances', error)
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@
"@babel/helper-validator-identifier" "^7.22.20"
to-fast-properties "^2.0.0"

"@bgd-labs/aave-address-book@^4.7.3":
version "4.7.3"
resolved "https://registry.yarnpkg.com/@bgd-labs/aave-address-book/-/aave-address-book-4.7.3.tgz#8a2ca1ca3b2e67474c0b07ea8e681a1400f3bc5b"
integrity sha512-4/QS5hLcjKuHAfdOAgaxSWVvi6RKu/U5ZHyndptvvKcNmzcC60tczN3NALCqWNXCBmzFY5e6cg82KsZJqVEjOQ==

"@coinbase/wallet-sdk@^3.6.5":
version "3.7.2"
resolved "https://registry.npmjs.org/@coinbase/wallet-sdk/-/wallet-sdk-3.7.2.tgz"
Expand Down
Loading