Skip to content

Commit

Permalink
feat: add multichain support to user ifo data queries
Browse files Browse the repository at this point in the history
  • Loading branch information
chefjackson committed Apr 20, 2023
1 parent b351cf0 commit d02c17e
Show file tree
Hide file tree
Showing 12 changed files with 210 additions and 66 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ debug.log*

**/apps/web/config/abi/types
**/packages/smart-router/evm/abis/types
**/packages/pools/src/abis/types

# Sentry
.sentryclirc
Expand All @@ -49,4 +50,4 @@ debug.log*
.vercel

# store for local dev scripts
scripts/dev/**
scripts/dev/**
4 changes: 2 additions & 2 deletions apps/web/src/hooks/useCakeEnable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ export const useCakeEnable = (enableAmount: BigNumber) => {

useEffect(() => {
if (pendingEnableTx && transactionHash && !isTransactionPending) {
dispatch(updateUserBalance({ sousId: 0, account }))
dispatch(updateUserBalance({ sousId: 0, account, chainId }))
setPendingEnableTx(isTransactionPending)
}
}, [account, dispatch, transactionHash, pendingEnableTx, isTransactionPending])
}, [account, dispatch, transactionHash, pendingEnableTx, isTransactionPending, chainId])

const handleEnable = useCallback(() => {
if (!swapCallback) {
Expand Down
27 changes: 0 additions & 27 deletions apps/web/src/state/pools/fetchUserIfo.ts

This file was deleted.

12 changes: 7 additions & 5 deletions apps/web/src/state/pools/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ export const usePoolsPageFetch = () => {
batch(() => {
dispatch(fetchCakeVaultPublicData())
dispatch(fetchCakeFlexibleSideVaultPublicData())
dispatch(fetchIfoPublicDataAsync())
if (chainId) {
dispatch(fetchIfoPublicDataAsync(chainId))
}
if (account && chainId) {
dispatch(fetchPoolsUserDataAsync({ account, chainId }))
dispatch(fetchCakeVaultUserData({ account }))
Expand Down Expand Up @@ -160,14 +162,14 @@ export const useFetchIfo = () => {
const dispatch = useAppDispatch()

useSWRImmutable(
'fetchIfoPublicData',
chainId && ['fetchIfoPublicData', chainId],
async () => {
const cakePriceFarms = await getCakePriceFarms(chainId)
await dispatch(fetchFarmsPublicDataAsync({ pids: cakePriceFarms, chainId }))
batch(() => {
dispatch(fetchCakePoolPublicDataAsync())
dispatch(fetchCakeVaultPublicData())
dispatch(fetchIfoPublicDataAsync())
dispatch(fetchIfoPublicDataAsync(chainId))
})
},
{
Expand All @@ -176,12 +178,12 @@ export const useFetchIfo = () => {
)

useSWRImmutable(
account && ['fetchIfoUserData', account],
account && chainId && ['fetchIfoUserData', account, chainId],
async () => {
batch(() => {
dispatch(fetchCakePoolUserDataAsync(account))
dispatch(fetchCakeVaultUserData({ account }))
dispatch(fetchUserIfoCreditDataAsync(account))
dispatch(fetchUserIfoCreditDataAsync({ account, chainId }))
})
},
{
Expand Down
48 changes: 27 additions & 21 deletions apps/web/src/state/pools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
fetchUserBalances,
fetchUserPendingRewards,
fetchUserStakeBalances,
fetchPublicIfoData,
fetchUserIfoCredit,
} from '@pancakeswap/pools'
import { ChainId } from '@pancakeswap/sdk'

Expand Down Expand Up @@ -41,7 +43,6 @@ import getFarmsPrices from '../farms/getFarmsPrices'
import { fetchPublicVaultData, fetchVaultFees, fetchPublicFlexibleSideVaultData } from './fetchVaultPublic'
import { getTokenPricesFromFarm } from './helpers'
import { resetUserState } from '../global/actions'
import { fetchUserIfoCredit, fetchPublicIfoData } from './fetchUserIfo'
import { fetchVaultUser, fetchFlexibleSideVaultUser } from './fetchVaultUser'

export const initialPoolVaultState = Object.freeze({
Expand Down Expand Up @@ -288,25 +289,25 @@ export const updateUserAllowance = createAsyncThunk<

export const updateUserBalance = createAsyncThunk<
{ sousId: number; field: string; value: any },
{ sousId: number; account: string }
>('pool/updateUserBalance', async ({ sousId, account }) => {
const tokenBalances = await fetchUserBalances(account)
{ sousId: number; account: string; chainId: ChainId }
>('pool/updateUserBalance', async ({ sousId, account, chainId }) => {
const tokenBalances = await fetchUserBalances({ account, chainId, provider })
return { sousId, field: 'stakingTokenBalance', value: tokenBalances[sousId] }
})

export const updateUserStakedBalance = createAsyncThunk<
{ sousId: number; field: string; value: any },
{ sousId: number; account: string }
>('pool/updateUserStakedBalance', async ({ sousId, account }) => {
const stakedBalances = await fetchUserStakeBalances(account)
{ sousId: number; account: string; chainId: ChainId }
>('pool/updateUserStakedBalance', async ({ sousId, account, chainId }) => {
const stakedBalances = await fetchUserStakeBalances({ account, chainId, provider })
return { sousId, field: 'stakedBalance', value: stakedBalances[sousId] }
})

export const updateUserPendingReward = createAsyncThunk<
{ sousId: number; field: string; value: any },
{ sousId: number; account: string }
>('pool/updateUserPendingReward', async ({ sousId, account }) => {
const pendingRewards = await fetchUserPendingRewards(account)
{ sousId: number; account: string; chainId: ChainId }
>('pool/updateUserPendingReward', async ({ sousId, account, chainId }) => {
const pendingRewards = await fetchUserPendingRewards({ chainId, account, provider })
return { sousId, field: 'pendingReward', value: pendingRewards[sousId] }
})

Expand Down Expand Up @@ -347,19 +348,24 @@ export const fetchCakeVaultUserData = createAsyncThunk<SerializedLockedVaultUser
},
)

export const fetchIfoPublicDataAsync = createAsyncThunk<PublicIfoData>('ifoVault/fetchIfoPublicDataAsync', async () => {
const publicIfoData = await fetchPublicIfoData()
return publicIfoData
})
export const fetchIfoPublicDataAsync = createAsyncThunk<PublicIfoData, ChainId>(
'ifoVault/fetchIfoPublicDataAsync',
async (chainId) => {
const publicIfoData = await fetchPublicIfoData(chainId, provider)
return publicIfoData
},
)

export const fetchUserIfoCreditDataAsync = (account: string) => async (dispatch) => {
try {
const credit = await fetchUserIfoCredit(account)
dispatch(setIfoUserCreditData(credit))
} catch (error) {
console.error('[Ifo Credit Action] Error fetching user Ifo credit data', error)
export const fetchUserIfoCreditDataAsync =
({ account, chainId }: { account: string; chainId: ChainId }) =>
async (dispatch) => {
try {
const credit = await fetchUserIfoCredit({ account, chainId, provider })
dispatch(setIfoUserCreditData(credit))
} catch (error) {
console.error('[Ifo Credit Action] Error fetching user Ifo credit data', error)
}
}
}
export const fetchCakeFlexibleSideVaultUserData = createAsyncThunk<SerializedVaultUser, { account: string }>(
'cakeFlexibleSideVault/fetchUser',
async ({ account }) => {
Expand Down
22 changes: 18 additions & 4 deletions apps/web/src/views/Pools/components/Modals/CollectModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { ToastDescriptionWithTx } from 'components/Toast'
import useCatchTxError from 'hooks/useCatchTxError'
import { useAppDispatch } from 'state'
import { updateUserBalance, updateUserPendingReward, updateUserStakedBalance } from 'state/pools'
import { useActiveChainId } from 'hooks/useActiveChainId'

import useHarvestPool from '../../hooks/useHarvestPool'

export const CollectModalContainer = ({
Expand All @@ -16,6 +18,7 @@ export const CollectModalContainer = ({
...rest
}: React.PropsWithChildren<Pool.CollectModalProps>) => {
const { t } = useTranslation()
const { chainId } = useActiveChainId()
const { toastSuccess } = useToast()
const { address: account } = useAccount()
const dispatch = useAppDispatch()
Expand All @@ -33,12 +36,23 @@ export const CollectModalContainer = ({
{t('Your %symbol% earnings have been sent to your wallet!', { symbol: earningTokenSymbol })}
</ToastDescriptionWithTx>,
)
dispatch(updateUserStakedBalance({ sousId, account }))
dispatch(updateUserPendingReward({ sousId, account }))
dispatch(updateUserBalance({ sousId, account }))
dispatch(updateUserStakedBalance({ sousId, account, chainId }))
dispatch(updateUserPendingReward({ sousId, account, chainId }))
dispatch(updateUserBalance({ sousId, account, chainId }))
onDismiss?.()
}
}, [account, dispatch, earningTokenSymbol, fetchWithCatchTxError, onDismiss, onReward, sousId, t, toastSuccess])
}, [
account,
dispatch,
earningTokenSymbol,
fetchWithCatchTxError,
onDismiss,
onReward,
sousId,
t,
toastSuccess,
chainId,
])

return (
<Pool.CollectModal
Expand Down
8 changes: 4 additions & 4 deletions apps/web/src/views/Pools/components/Modals/StakeModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ const StakeModalContainer = ({
)

const onDone = useCallback(() => {
dispatch(updateUserStakedBalance({ sousId, account }))
dispatch(updateUserPendingReward({ sousId, account }))
dispatch(updateUserBalance({ sousId, account }))
}, [dispatch, sousId, account])
dispatch(updateUserStakedBalance({ sousId, account, chainId }))
dispatch(updateUserPendingReward({ sousId, account, chainId }))
dispatch(updateUserBalance({ sousId, account, chainId }))
}, [dispatch, sousId, account, chainId])

const handleConfirmClick = useCallback(
async (stakeAmount: string) => {
Expand Down
8 changes: 6 additions & 2 deletions packages/pools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
"sideEffects": false,
"private": true,
"scripts": {
"build": "tsup",
"test": "vitest --run"
"build": "yarn typechain && tsup",
"test": "vitest --run",
"typechain": "typechain --out-dir src/abis/types --target=ethers-v5 \"src/abis/*.json\"",
"postinstall": "yarn typechain"
},
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down Expand Up @@ -38,8 +40,10 @@
"@pancakeswap/token-lists": "*",
"@pancakeswap/tokens": "*",
"@pancakeswap/utils": "*",
"@typechain/ethers-v5": "^8.0.5",
"tsconfig": "*",
"tsup": "^6.6.3",
"typechain": "^6.1.0",
"typescript": "^4.9.4",
"vitest": "^0.27.2"
}
Expand Down
83 changes: 83 additions & 0 deletions packages/pools/src/abis/ICake.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
[
{
"inputs": [
{ "internalType": "contract ICaKePool", "name": "_cakePool", "type": "address" },
{ "internalType": "address", "name": "_admin", "type": "address" },
{ "internalType": "uint256", "name": "_ceiling", "type": "uint256" }
],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{ "indexed": true, "internalType": "address", "name": "previousOwner", "type": "address" },
{ "indexed": true, "internalType": "address", "name": "newOwner", "type": "address" }
],
"name": "OwnershipTransferred",
"type": "event"
},
{
"anonymous": false,
"inputs": [{ "indexed": false, "internalType": "uint256", "name": "newCeiling", "type": "uint256" }],
"name": "UpdateCeiling",
"type": "event"
},
{
"inputs": [],
"name": "MIN_CEILING_DURATION",
"outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "admin",
"outputs": [{ "internalType": "address", "name": "", "type": "address" }],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "cakePool",
"outputs": [{ "internalType": "contract ICaKePool", "name": "", "type": "address" }],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "ceiling",
"outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [{ "internalType": "address", "name": "_user", "type": "address" }],
"name": "getUserCredit",
"outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "owner",
"outputs": [{ "internalType": "address", "name": "", "type": "address" }],
"stateMutability": "view",
"type": "function"
},
{ "inputs": [], "name": "renounceOwnership", "outputs": [], "stateMutability": "nonpayable", "type": "function" },
{
"inputs": [{ "internalType": "address", "name": "newOwner", "type": "address" }],
"name": "transferOwnership",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [{ "internalType": "uint256", "name": "_newCeiling", "type": "uint256" }],
"name": "updateCeiling",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]
11 changes: 11 additions & 0 deletions packages/pools/src/constants/contracts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ChainId } from '@pancakeswap/sdk'

import { PoolsSupportedChainId } from './pools'

export type ContractAddresses<T extends ChainId> = {
[chainId in T]: string
}

export const ICAKE = {
[ChainId.BSC]: '0x3C458828D1622F5f4d526eb0d24Da8C4Eb8F07b1',
} satisfies ContractAddresses<PoolsSupportedChainId>
Loading

0 comments on commit d02c17e

Please sign in to comment.