-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add multichain support to pools user data queries
- Loading branch information
1 parent
f57e6e0
commit b351cf0
Showing
8 changed files
with
137 additions
and
107 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { createMulticall, multicallAddresses } from '@pancakeswap/multicall' | ||
import multiCallAbi from '@pancakeswap/multicall/Multicall.json' | ||
import { ChainId } from '@pancakeswap/sdk' | ||
import BigNumber from 'bignumber.js' | ||
import uniq from 'lodash/uniq' | ||
import fromPairs from 'lodash/fromPairs' | ||
|
||
import { getPoolsConfig } from '../constants' | ||
import sousChefABI from '../abis/ISousChef.json' | ||
import erc20ABI from '../abis/IERC20.json' | ||
import { OnChainProvider, SerializedPool } from '../types' | ||
|
||
// Pool 0, Cake / Cake is a different kind of contract (master chef) | ||
// BNB pools use the native BNB token (wrapping ? unwrapping is done at the contract level) | ||
const getPoolsFactory = (filter: (pool: SerializedPool) => boolean) => (chainId: ChainId) => { | ||
const poolsConfig = getPoolsConfig(chainId) | ||
if (!poolsConfig) { | ||
throw new Error(`Unable to get pools config on chain ${chainId}`) | ||
} | ||
return poolsConfig.filter(filter) | ||
} | ||
const getNonBnbPools = getPoolsFactory((pool) => pool.stakingToken.symbol !== 'BNB') | ||
const getBnbPools = getPoolsFactory((pool) => pool.stakingToken.symbol === 'BNB') | ||
const getNonMasterPools = getPoolsFactory((pool) => pool.sousId !== 0) | ||
|
||
interface FetchUserDataParams { | ||
account: string | ||
chainId: ChainId | ||
provider: OnChainProvider | ||
} | ||
|
||
export const fetchPoolsAllowance = async ({ account, chainId, provider }: FetchUserDataParams) => { | ||
const nonBnbPools = getNonBnbPools(chainId) | ||
const calls = nonBnbPools.map(({ contractAddress, stakingToken }) => ({ | ||
address: stakingToken.address, | ||
name: 'allowance', | ||
params: [account, contractAddress[chainId]], | ||
})) | ||
|
||
const { multicall } = createMulticall(provider) | ||
const allowances = await multicall(erc20ABI, calls) | ||
return fromPairs(nonBnbPools.map((pool, index) => [pool.sousId, new BigNumber(allowances[index]).toJSON()])) | ||
} | ||
|
||
export const fetchUserBalances = async ({ account, chainId, provider }: FetchUserDataParams) => { | ||
const nonBnbPools = getNonBnbPools(chainId) | ||
const bnbPools = getBnbPools(chainId) | ||
// Non BNB pools | ||
const tokens = uniq(nonBnbPools.map((pool) => pool.stakingToken.address)) | ||
const tokenBalanceCalls = tokens.map((token) => ({ | ||
abi: erc20ABI, | ||
address: token, | ||
name: 'balanceOf', | ||
params: [account], | ||
})) | ||
const bnbBalanceCall = { | ||
abi: multiCallAbi, | ||
address: multicallAddresses[chainId], | ||
name: 'getEthBalance', | ||
params: [account], | ||
} | ||
const { multicallv3 } = createMulticall(provider) | ||
const tokenBnbBalancesRaw = await multicallv3({ calls: [...tokenBalanceCalls, bnbBalanceCall] }) | ||
const bnbBalance = tokenBnbBalancesRaw.pop() | ||
const tokenBalances = fromPairs(tokens.map((token, index) => [token, tokenBnbBalancesRaw[index]])) | ||
|
||
const poolTokenBalances = fromPairs( | ||
nonBnbPools | ||
.map<[number, string] | null>((pool) => { | ||
if (!tokenBalances[pool.stakingToken.address]) return null | ||
return [pool.sousId, new BigNumber(tokenBalances[pool.stakingToken.address]).toJSON()] | ||
}) | ||
.filter((p): p is [number, string] => Boolean(p)), | ||
) | ||
|
||
// BNB pools | ||
const bnbBalanceJson = new BigNumber(bnbBalance.toString()).toJSON() | ||
const bnbBalances = fromPairs(bnbPools.map((pool) => [pool.sousId, bnbBalanceJson])) | ||
|
||
return { ...poolTokenBalances, ...bnbBalances } | ||
} | ||
|
||
export const fetchUserStakeBalances = async ({ account, chainId, provider }: FetchUserDataParams) => { | ||
const nonMasterPools = getNonMasterPools(chainId) | ||
const calls = nonMasterPools.map(({ contractAddress }) => ({ | ||
address: contractAddress[chainId], | ||
name: 'userInfo', | ||
params: [account], | ||
})) | ||
const { multicall } = createMulticall(provider) | ||
const userInfo = await multicall(sousChefABI, calls) | ||
return fromPairs( | ||
nonMasterPools.map((pool, index) => [pool.sousId, new BigNumber(userInfo[index].amount._hex).toJSON()]), | ||
) | ||
} | ||
|
||
export const fetchUserPendingRewards = async ({ account, chainId, provider }: FetchUserDataParams) => { | ||
const nonMasterPools = getNonMasterPools(chainId) | ||
const calls = nonMasterPools.map(({ contractAddress }) => ({ | ||
address: contractAddress[chainId], | ||
name: 'pendingReward', | ||
params: [account], | ||
})) | ||
const { multicall } = createMulticall(provider) | ||
const res = await multicall(sousChefABI, calls) | ||
return fromPairs(nonMasterPools.map((pool, index) => [pool.sousId, new BigNumber(res[index]).toJSON()])) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './fetchPools' | ||
export * from './fetchUserPoolsData' |