diff --git a/src/layout/Main.tsx b/src/layout/Main.tsx index 21d613c..c0f973f 100644 --- a/src/layout/Main.tsx +++ b/src/layout/Main.tsx @@ -11,7 +11,6 @@ import { Router, RouteComponent } from '../layout/Router'; import { Actions } from '../routes/Actions'; import { Stake } from '../routes/Stake'; import { Unstake } from '../routes/Unstake'; -import { Claim } from '../routes/Claim'; import { Button } from '../components/wallet-adapter/ui/Button'; import { WalletConnectButton } from '../components/wallet-adapter/ui/WalletConnectButton'; import { WalletModalButton } from '../components/wallet-adapter/ui/WalletModalButton'; @@ -25,11 +24,7 @@ const Main = () => { const { publicKey, wallet, connected } = useWallet(); const [active, setActive] = useState(false); const ref = useRef(null); - const { - element, - poolId, - classPrefix, - } = useContext(ConfigContext); + const { element, poolId, classPrefix } = useContext(ConfigContext); const base58 = useMemo(() => publicKey?.toBase58(), [publicKey]); const content = useMemo(() => { if (!wallet || !base58) { @@ -49,7 +44,9 @@ const Main = () => { useEffect(() => { if (connected && element && publicKey && poolId) { (async () => { - const response = await fetch(`${env.GO_API_URL}/subscriptions/${publicKey.toBase58()}`); + const response = await fetch( + `${env.GO_API_URL}/subscriptions/${publicKey.toBase58()}` + ); if (!response.ok) { console.log('ERROR: ', response.statusText); return; @@ -58,21 +55,24 @@ const Main = () => { const json = await response.json(); const data = offchainBasicSubscriptionsSchema.parse(json); - const { staked, bonds, forever } = data.reduce((acc, item) => { - if (item.pool === poolId) { - return { - staked: acc.staked + (item?.locked ?? 0), - bonds: acc.bonds + (item?.bonds ?? 0), - forever: acc.forever + (item?.forever ?? 0), - }; - } else { - return acc; + const { staked, bonds, forever } = data.reduce( + (acc, item) => { + if (item.pool === poolId) { + return { + staked: acc.staked + (item?.locked ?? 0), + bonds: acc.bonds + (item?.bonds ?? 0), + forever: acc.forever + (item?.forever ?? 0), + }; + } else { + return acc; + } + }, + { + staked: 0, + bonds: 0, + forever: 0, } - }, { - staked: 0, - bonds: 0, - forever: 0 - }); + ); const connectedEvent = new CustomEvent('connected', { detail: { @@ -80,7 +80,7 @@ const Main = () => { locked: staked + bonds + forever, staked, bonds, - forever + forever, }, bubbles: true, cancelable: true, @@ -154,7 +154,6 @@ const Main = () => { '/': , '/stake': , '/unstake': , - '/claim': , }} /> diff --git a/src/routes/Actions.tsx b/src/routes/Actions.tsx index 4796777..bf50434 100644 --- a/src/routes/Actions.tsx +++ b/src/routes/Actions.tsx @@ -91,6 +91,10 @@ export const Actions = () => { return (stakedAmount ?? 0) + (bondsAmount ?? 0) > 0; }, [stakedAmount, bondsAmount]); + const openClaimPage = useCallback(() => { + window.open('https://hub.accessprotocol.co', '_blank'); + }, []); + return (
{connected && disconnecting && ( @@ -165,12 +169,12 @@ export const Actions = () => { Unlock ACS )} - - Claim - + View on HUB +
); diff --git a/src/routes/Claim.tsx b/src/routes/Claim.tsx deleted file mode 100644 index cd1a0c1..0000000 --- a/src/routes/Claim.tsx +++ /dev/null @@ -1,208 +0,0 @@ -import { h } from 'preact'; - -import { Header } from '../components/Header'; -import { RouteLink } from '../layout/Router'; -import { useContext, useEffect, useMemo, useState } from 'preact/hooks'; -import { ConfigContext } from '../AppContext'; -import env from '../libs/env'; -import { clsxp, formatPenyACSCurrency } from '../libs/utils'; -import { - BondAccount, - BondV2Account, - getBondV2Accounts, - StakeAccount, - StakePool, -} from '@accessprotocol/js'; -import { - calculateRewardForStaker, - getBondAccounts, - getStakeAccounts, -} from '../libs/program'; -import { PublicKey } from '@solana/web3.js'; -import BN from 'bn.js'; -import { useWallet } from '../components/wallet-adapter/useWallet'; -import { useConnection } from '../components/wallet-adapter/useConnection'; - -export const Claim = () => { - const { poolId, classPrefix } = useContext(ConfigContext); - const { connection } = useConnection(); - const { publicKey } = useWallet(); - - const [stakedAccount, setStakedAccount] = useState< - StakeAccount | null | undefined - >(undefined); - const [bondAccounts, setBondAccounts] = useState([]); - const [bondV2Accounts, setBondV2Accounts] = useState([]); - const [stakePool, setStakePool] = useState(undefined); - - useEffect(() => { - if (!(poolId && connection)) { - return; - } - (async () => { - setStakePool(await StakePool.retrieve(connection, new PublicKey(poolId))); - })(); - }, [poolId, connection]); - - useEffect(() => { - if (!(publicKey && poolId && connection)) { - return; - } - (async () => { - const stakedAccounts = await getStakeAccounts( - connection, - publicKey, - env.PROGRAM_ID - ); - if (stakedAccounts != null && stakedAccounts.length > 0) { - const sAccount = stakedAccounts.find((st) => { - const sa = StakeAccount.deserialize(st.account.data); - return sa.stakePool.toBase58() === poolId; - }); - if (sAccount) { - const sa = StakeAccount.deserialize(sAccount.account.data); - setStakedAccount(sa); - } else { - setStakedAccount(null); - } - } else { - setStakedAccount(null); - } - })(); - }, [publicKey, connection, poolId]); - - useEffect(() => { - if (!(publicKey && poolId && connection)) { - return; - } - (async () => { - const bAccounts = await getBondAccounts( - connection, - publicKey, - env.PROGRAM_ID - ); - if (bAccounts != null && bAccounts.length > 0) { - const bas = bAccounts - .filter((st) => { - const ba = BondAccount.deserialize(st.account.data); - return ba.stakePool.toBase58() === poolId; - }) - .map((bAccount) => BondAccount.deserialize(bAccount.account.data)); - setBondAccounts(bas); - } else { - setBondAccounts([]); - } - })(); - }, [publicKey, connection, poolId]); - - useEffect(() => { - if (!(publicKey && poolId && connection)) { - return; - } - (async () => { - const bV2Accounts = await getBondV2Accounts( - connection, - publicKey, - env.PROGRAM_ID - ); - - setBondV2Accounts( - bV2Accounts - .map((bAccount: any) => - BondV2Account.deserialize(bAccount.account.data) - ) - .filter( - (bAccount: BondV2Account) => bAccount.pool.toBase58() === poolId - ) - ); - })(); - }, [publicKey, connection, poolId]); - - const claimableStakeAmount = useMemo(() => { - if (!(stakedAccount && stakePool)) { - return null; - } - return calculateRewardForStaker( - stakePool.currentDayIdx - stakedAccount.lastClaimedOffset.toNumber(), - stakePool, - stakedAccount.stakeAmount as BN - ); - }, [stakedAccount, stakePool]); - - const claimableBondAmount = useMemo(() => { - if (bondAccounts.length === 0 || !stakePool) { - return null; - } - - return bondAccounts.reduce( - (acc, ba) => - acc + - calculateRewardForStaker( - stakePool.currentDayIdx - ba.lastClaimedOffset.toNumber(), - stakePool, - ba.totalStaked as BN - ), - 0 - ); - }, [bondAccounts, stakePool]); - - const claimableBondV2Amount = useMemo(() => { - if (bondV2Accounts.length === 0 || !stakePool) { - return null; - } - - return bondV2Accounts.reduce( - (acc, ba) => - acc + - calculateRewardForStaker( - stakePool.currentDayIdx - ba.lastClaimedOffset.toNumber(), - stakePool, - ba.amount as BN - ), - 0 - ); - }, [bondV2Accounts, stakePool]); - - const claimableAmount = useMemo(() => { - return ( - (claimableBondAmount ?? 0) + - (claimableStakeAmount ?? 0) + - (claimableBondV2Amount ?? 0) - ); - }, [claimableBondAmount, claimableStakeAmount, claimableBondV2Amount]); - - return ( -
-
- - Cancel - -
- -
Claim ACS Rewards
- -
- {formatPenyACSCurrency(claimableAmount)} ACS -
- -
- ACS reward claim is currently only possible in the Access app. -
- -
- - Claim rewards on Access - - -
- This will redirect you to accessprotocol.co -
-
-
- ); -}; diff --git a/tsconfig.json b/tsconfig.json index 8bf113f..6cd6ecf 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -15,7 +15,6 @@ "strictNullChecks": true, "noImplicitReturns": true, "preserveConstEnums": true, - "suppressImplicitAnyIndexErrors": true, "forceConsistentCasingInFileNames": true, "outDir": ".build", "jsx": "preserve",