Skip to content

Commit

Permalink
move selected token to store
Browse files Browse the repository at this point in the history
  • Loading branch information
saml33 committed Sep 15, 2023
1 parent 5d20e0e commit e576822
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 21 deletions.
2 changes: 1 addition & 1 deletion components/Positions.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import useMangoGroup from 'hooks/useMangoGroup'
import { useMemo } from 'react'
import useMangoAccount from 'hooks/useMangoAccount'
import { STAKEABLE_TOKENS } from 'pages'
import { STAKEABLE_TOKENS } from 'utils/constants'

const Positions = () => {
const { mangoAccount } = useMangoAccount()
Expand Down
25 changes: 7 additions & 18 deletions components/Stake.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,26 @@
import { STAKEABLE_TOKENS } from 'pages'
import TokenButton from './TokenButton'
import { useCallback, useEffect, useState } from 'react'
import mangoStore from '@store/mangoStore'
import { ACCOUNT_PREFIX } from 'utils/transactions'
import { useCallback, useState } from 'react'
import TabUnderline from './shared/TabUnderline'
import StakeForm from '@components/StakeForm'
import UnstakeForm from '@components/UnstakeForm'
import AccountStats from './AccountStats'
import mangoStore from '@store/mangoStore'
import { STAKEABLE_TOKENS } from 'utils/constants'

const set = mangoStore.getState().set

const Stake = () => {
const [activeFormTab, setActiveFormTab] = useState('Add')
const [selectedToken, setSelectedToken] = useState(STAKEABLE_TOKENS[0])

useEffect(() => {
const mangoAccounts = mangoStore.getState().mangoAccounts
const selectedMangoAccount = mangoAccounts.find(
(ma) => ma.name === `${ACCOUNT_PREFIX}${selectedToken}`,
)
console.log('selectedMangoAccount', selectedMangoAccount)

set((s) => {
s.mangoAccount.current = selectedMangoAccount
})
}, [selectedToken])
const selectedToken = mangoStore((s) => s.selectedToken)

const onClose = useCallback(() => {
console.log('StakeForm onSuccess')
}, [])

const handleTokenSelect = useCallback((token: string) => {
setSelectedToken(token)
set((state) => {
state.selectedToken = token
})
}, [])

return (
Expand Down
19 changes: 17 additions & 2 deletions pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import Positions from '@components/Positions'
import Stake from '@components/Stake'
import TransactionHistory from '@components/TransactionHistory'
import mangoStore from '@store/mangoStore'
import type { NextPage } from 'next'
import { serverSideTranslations } from 'next-i18next/serverSideTranslations'
import { useState } from 'react'
import { useEffect, useState } from 'react'
import { ACCOUNT_PREFIX } from 'utils/transactions'

export const STAKEABLE_TOKENS = ['mSOL', 'JitoSOL', 'stSOL', 'bSOL', 'LDO']
const set = mangoStore.getState().set

export async function getStaticProps({ locale }: { locale: string }) {
return {
Expand All @@ -17,6 +19,19 @@ export async function getStaticProps({ locale }: { locale: string }) {

const Index: NextPage = () => {
const [activeTab, setActiveTab] = useState('Stake')
const selectedToken = mangoStore((s) => s.selectedToken)

useEffect(() => {
const mangoAccounts = mangoStore.getState().mangoAccounts
const selectedMangoAccount = mangoAccounts.find(
(ma) => ma.name === `${ACCOUNT_PREFIX}${selectedToken}`,
)
console.log('selectedMangoAccount', selectedMangoAccount)

set((s) => {
s.mangoAccount.current = selectedMangoAccount
})
}, [selectedToken])

return (
<div className="mx-auto max-w-4xl flex-col pb-20">
Expand Down
7 changes: 7 additions & 0 deletions store/mangoStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
OUTPUT_TOKEN_DEFAULT,
PAGINATION_PAGE_LENGTH,
RPC_PROVIDER_KEY,
STAKEABLE_TOKENS,
SWAP_MARGIN_KEY,
} from '../utils/constants'
import {
Expand Down Expand Up @@ -140,6 +141,9 @@ export const DEFAULT_TRADE_FORM: TradeForm = {
}

export type MangoStore = {
// leverage stake
selectedToken: string
//end
activityFeed: {
feed: Array<ActivityFeed>
loading: boolean
Expand Down Expand Up @@ -307,6 +311,9 @@ const mangoStore = create<MangoStore>()(
const client = initMangoClient(provider)

return {
// leverage stake
selectedToken: STAKEABLE_TOKENS[0],
//end
activityFeed: {
feed: [],
loading: true,
Expand Down
4 changes: 4 additions & 0 deletions utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { PublicKey } from '@metaplex-foundation/js'

// lev stake
export const STAKEABLE_TOKENS = ['mSOL', 'JitoSOL', 'stSOL', 'bSOL', 'LDO']
// end

export const LAST_ACCOUNT_KEY = 'mangoAccount-0.4'

export const CLIENT_TX_TIMEOUT = 90000
Expand Down

0 comments on commit e576822

Please sign in to comment.