implement deposit
This commit is contained in:
parent
12b69398d2
commit
a16608c95d
|
@ -9,11 +9,11 @@ import {
|
|||
ChevronDownIcon,
|
||||
ChevronUpIcon,
|
||||
} from '@heroicons/react/solid'
|
||||
import { nativeToUi, sleep } from '@blockworks-foundation/mango-client'
|
||||
import {
|
||||
nativeToUi,
|
||||
sleep,
|
||||
MerpsAccount as MarginAccount,
|
||||
uiToNative,
|
||||
} from '@blockworks-foundation/mango-client'
|
||||
import { MerpsAccount as MarginAccount, uiToNative } from '@blockworks-foundation/mango-client'
|
||||
import Modal from './Modal'
|
||||
import Input from './Input'
|
||||
import AccountSelect from './AccountSelect'
|
||||
|
@ -35,6 +35,7 @@ import Slider from './Slider'
|
|||
import InlineNotification from './InlineNotification'
|
||||
import { notify } from '../utils/notifications'
|
||||
import useMangoGroupConfig from '../hooks/useMangoGroupConfig'
|
||||
import { QUOTE_INDEX } from '@blockworks-foundation/mango-client/lib/src/MerpsGroup'
|
||||
|
||||
interface DepositModalProps {
|
||||
onClose: () => void
|
||||
|
@ -64,7 +65,7 @@ const DepositModal: FunctionComponent<DepositModalProps> = ({
|
|||
const actions = useMangoStore((s) => s.actions)
|
||||
const [selectedAccount, setSelectedAccount] = useState(walletTokens[0])
|
||||
|
||||
const prices = [];//useMangoStore((s) => s.selectedMangoGroup.prices)
|
||||
const prices = [] //useMangoStore((s) => s.selectedMangoGroup.prices)
|
||||
const selectedMangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
|
||||
const selectedMarginAccount = useMangoStore(
|
||||
(s) => s.selectedMarginAccount.current
|
||||
|
@ -146,13 +147,45 @@ const DepositModal: FunctionComponent<DepositModalProps> = ({
|
|||
}
|
||||
|
||||
const handleDeposit = () => {
|
||||
/*
|
||||
|
||||
setSubmitting(true)
|
||||
const marginAccount = useMangoStore.getState().selectedMarginAccount.current
|
||||
const mangoGroup = useMangoStore.getState().selectedMangoGroup.current
|
||||
const wallet = useMangoStore.getState().wallet.current
|
||||
const client = useMangoStore.getState().mangoClient
|
||||
|
||||
console.log('mango group', mangoGroup.rootBankAccounts)
|
||||
|
||||
if (!marginAccount) {
|
||||
client
|
||||
.initMerpsAccount(mangoGroup, wallet)
|
||||
.then((resp) => console.log('something happened', resp))
|
||||
}
|
||||
if (marginAccount) {
|
||||
console.log('we have a margin acc', marginAccount.publicKey)
|
||||
|
||||
client
|
||||
.deposit(
|
||||
mangoGroup,
|
||||
marginAccount,
|
||||
wallet,
|
||||
mangoGroup.tokens[QUOTE_INDEX].rootBank,
|
||||
mangoGroup.rootBankAccounts[QUOTE_INDEX].nodeBankAccounts[0]
|
||||
.publicKey,
|
||||
mangoGroup.rootBankAccounts[QUOTE_INDEX].nodeBankAccounts[0].vault,
|
||||
selectedAccount.account.publicKey,
|
||||
Number(inputAmount)
|
||||
)
|
||||
.then((resp) => {
|
||||
console.log('desposit status', resp)
|
||||
sleep(2000)
|
||||
|
||||
setSubmitting(false)
|
||||
onClose()
|
||||
actions.fetchMarginAccounts()
|
||||
})
|
||||
}
|
||||
|
||||
/*
|
||||
if (!marginAccount && mangoGroup) {
|
||||
initMarginAccountAndDeposit(
|
||||
connection,
|
||||
|
@ -258,10 +291,12 @@ const DepositModal: FunctionComponent<DepositModalProps> = ({
|
|||
setInputAmount(amount)
|
||||
|
||||
if (!selectedAccount) {
|
||||
setInvalidAmountMessage('Please fund wallet with one of the supported assets.')
|
||||
setInvalidAmountMessage(
|
||||
'Please fund wallet with one of the supported assets.'
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
const max = selectedAccount.uiBalance
|
||||
setSliderPercentage((amount / max) * 100)
|
||||
setInvalidAmountMessage('')
|
||||
|
@ -271,7 +306,9 @@ const DepositModal: FunctionComponent<DepositModalProps> = ({
|
|||
setSliderPercentage(percentage)
|
||||
|
||||
if (!selectedAccount) {
|
||||
setInvalidAmountMessage('Please fund wallet with one of the supported assets.')
|
||||
setInvalidAmountMessage(
|
||||
'Please fund wallet with one of the supported assets.'
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -280,7 +317,9 @@ const DepositModal: FunctionComponent<DepositModalProps> = ({
|
|||
if (percentage === 100) {
|
||||
setInputAmount(amount)
|
||||
} else {
|
||||
setInputAmount(trimDecimals(amount, DECIMALS[selectedAccount.config.symbol]))
|
||||
setInputAmount(
|
||||
trimDecimals(amount, DECIMALS[selectedAccount.config.symbol])
|
||||
)
|
||||
}
|
||||
setInvalidAmountMessage('')
|
||||
validateAmountInput(amount)
|
||||
|
@ -375,7 +414,8 @@ const DepositModal: FunctionComponent<DepositModalProps> = ({
|
|||
onClick={() => setShowSimulation(true)}
|
||||
className="w-full"
|
||||
disabled={
|
||||
inputAmount <= 0 || !selectedAccount ||
|
||||
inputAmount <= 0 ||
|
||||
!selectedAccount ||
|
||||
inputAmount > selectedAccount.uiBalance
|
||||
}
|
||||
>
|
||||
|
|
|
@ -17,6 +17,7 @@ import WithdrawModal from './WithdrawModal'
|
|||
// import BorrowModal from './BorrowModal'
|
||||
import Button from './Button'
|
||||
import Tooltip from './Tooltip'
|
||||
import { QUOTE_INDEX } from '@blockworks-foundation/mango-client/lib/src/MerpsGroup'
|
||||
// import AccountsModal from './AccountsModal'
|
||||
|
||||
export default function MarginBalances() {
|
||||
|
@ -57,6 +58,8 @@ export default function MarginBalances() {
|
|||
// setShowAccountsModal(false)
|
||||
// }, [])
|
||||
|
||||
console.log('margin Account', selectedMarginAccount)
|
||||
|
||||
return (
|
||||
<>
|
||||
<FloatingElement>
|
||||
|
|
|
@ -105,7 +105,7 @@ export default function useWallet() {
|
|||
state.wallet.connected = true
|
||||
})
|
||||
// set connected before fetching data
|
||||
actions.fetchTradeHistory()
|
||||
// actions.fetchTradeHistory()
|
||||
// actions.fetchMangoSrmAccounts()
|
||||
actions.fetchWalletTokens()
|
||||
notify({
|
||||
|
@ -152,7 +152,7 @@ export default function useWallet() {
|
|||
if (connected && marginAccount) {
|
||||
actions.fetchMarginAccounts()
|
||||
actions.fetchWalletTokens()
|
||||
actions.fetchTradeHistory()
|
||||
// actions.fetchTradeHistory()
|
||||
}
|
||||
}, 180 * SECONDS)
|
||||
|
||||
|
|
|
@ -20,7 +20,12 @@ import {
|
|||
PerpMarket,
|
||||
} from '@blockworks-foundation/mango-client'
|
||||
// import { SRM_DECIMALS } from '@project-serum/serum/lib/token-instructions'
|
||||
import { AccountInfo, Connection, PublicKey, TokenAmount } from '@solana/web3.js'
|
||||
import {
|
||||
AccountInfo,
|
||||
Connection,
|
||||
PublicKey,
|
||||
TokenAmount,
|
||||
} from '@solana/web3.js'
|
||||
import { EndpointInfo, WalletAdapter } from '../@types/types'
|
||||
import { getWalletTokenInfo } from '../utils/tokens'
|
||||
import { isDefined } from '../utils'
|
||||
|
@ -85,8 +90,8 @@ interface AccountInfoList {
|
|||
}
|
||||
|
||||
export interface WalletToken {
|
||||
account: TokenAccount,
|
||||
config: TokenConfig,
|
||||
account: TokenAccount
|
||||
config: TokenConfig
|
||||
uiBalance: number
|
||||
}
|
||||
|
||||
|
@ -148,7 +153,7 @@ interface MangoStore extends State {
|
|||
providerUrl: string
|
||||
connected: boolean
|
||||
current: WalletAdapter | undefined
|
||||
tokens: WalletToken[],
|
||||
tokens: WalletToken[]
|
||||
srmAccountsForOwner: any[]
|
||||
contributedSrm: number
|
||||
}
|
||||
|
@ -230,19 +235,16 @@ const useMangoStore = create<MangoStore>((set, get) => ({
|
|||
ownerAddress
|
||||
)
|
||||
const tokens = []
|
||||
ownedTokenAccounts.forEach(account => {
|
||||
const config = getTokenByMint(groupConfig, account.mint);
|
||||
ownedTokenAccounts.forEach((account) => {
|
||||
const config = getTokenByMint(groupConfig, account.mint)
|
||||
if (config) {
|
||||
const uiBalance = nativeToUi(
|
||||
account.amount,
|
||||
config.decimals
|
||||
)
|
||||
tokens.push({account, config, uiBalance})
|
||||
const uiBalance = nativeToUi(account.amount, config.decimals)
|
||||
tokens.push({ account, config, uiBalance })
|
||||
}
|
||||
});
|
||||
})
|
||||
|
||||
set((state) => {
|
||||
state.wallet.tokens = tokens;
|
||||
state.wallet.tokens = tokens
|
||||
})
|
||||
} else {
|
||||
set((state) => {
|
||||
|
@ -266,10 +268,7 @@ const useMangoStore = create<MangoStore>((set, get) => ({
|
|||
}
|
||||
|
||||
return mangoClient
|
||||
.getMarginAccountsForOwner(
|
||||
mangoGroup,
|
||||
wallet.publicKey
|
||||
)
|
||||
.getMarginAccountsForOwner(mangoGroup, wallet.publicKey)
|
||||
.then((marginAccounts) => {
|
||||
if (marginAccounts.length > 0) {
|
||||
const sortedAccounts = marginAccounts
|
||||
|
@ -280,21 +279,22 @@ const useMangoStore = create<MangoStore>((set, get) => ({
|
|||
)
|
||||
set((state) => {
|
||||
state.marginAccounts = sortedAccounts
|
||||
if (state.selectedMarginAccount.current) {
|
||||
state.selectedMarginAccount.current = marginAccounts.find(
|
||||
(ma) =>
|
||||
ma.publicKey.equals(
|
||||
state.selectedMarginAccount.current.publicKey
|
||||
)
|
||||
)
|
||||
} else {
|
||||
const lastAccount = localStorage.getItem('lastAccountViewed')
|
||||
state.selectedMarginAccount.current = sortedAccounts[0]
|
||||
// if (state.selectedMarginAccount.current) {
|
||||
// state.selectedMarginAccount.current = marginAccounts.find(
|
||||
// (ma) =>
|
||||
// ma.publicKey.equals(
|
||||
// state.selectedMarginAccount.current.publicKey
|
||||
// )
|
||||
// )
|
||||
// } else {
|
||||
// const lastAccount = localStorage.getItem('lastAccountViewed')
|
||||
|
||||
state.selectedMarginAccount.current =
|
||||
marginAccounts.find(
|
||||
(ma) => ma.publicKey.toString() === JSON.parse(lastAccount)
|
||||
) || sortedAccounts[0]
|
||||
}
|
||||
// state.selectedMarginAccount.current =
|
||||
// marginAccounts.find(
|
||||
// (ma) => ma.publicKey.toString() === JSON.parse(lastAccount)
|
||||
// ) || sortedAccounts[0]
|
||||
// }
|
||||
})
|
||||
}
|
||||
set((state) => {
|
||||
|
@ -347,34 +347,34 @@ const useMangoStore = create<MangoStore>((set, get) => ({
|
|||
console.log('Could not get mango group: ', err)
|
||||
})
|
||||
},
|
||||
async fetchTradeHistory(marginAccount = null) {
|
||||
const selectedMarginAccount =
|
||||
marginAccount || get().selectedMarginAccount.current
|
||||
const set = get().set
|
||||
// async fetchTradeHistory(marginAccount = null) {
|
||||
// const selectedMarginAccount =
|
||||
// marginAccount || get().selectedMarginAccount.current
|
||||
// const set = get().set
|
||||
|
||||
if (!selectedMarginAccount) return
|
||||
if (selectedMarginAccount.openOrdersAccounts.length === 0) return
|
||||
// if (!selectedMarginAccount) return
|
||||
// if (selectedMarginAccount.openOrdersAccounts.length === 0) return
|
||||
|
||||
const openOrdersAccounts =
|
||||
selectedMarginAccount.openOrdersAccounts.filter(isDefined)
|
||||
const publicKeys = openOrdersAccounts.map((act) =>
|
||||
act.publicKey.toString()
|
||||
)
|
||||
const results = await Promise.all(
|
||||
publicKeys.map(async (pk) => {
|
||||
const response = await fetch(
|
||||
`https://stark-fjord-45757.herokuapp.com/trades/open_orders/${pk.toString()}`
|
||||
)
|
||||
// const openOrdersAccounts =
|
||||
// selectedMarginAccount.openOrdersAccounts.filter(isDefined)
|
||||
// const publicKeys = openOrdersAccounts.map((act) =>
|
||||
// act.publicKey.toString()
|
||||
// )
|
||||
// const results = await Promise.all(
|
||||
// publicKeys.map(async (pk) => {
|
||||
// const response = await fetch(
|
||||
// `https://stark-fjord-45757.herokuapp.com/trades/open_orders/${pk.toString()}`
|
||||
// )
|
||||
|
||||
const parsedResponse = await response.json()
|
||||
return parsedResponse?.data ? parsedResponse.data : []
|
||||
})
|
||||
)
|
||||
set((state) => {
|
||||
state.tradeHistory = results
|
||||
console.log('spot-history', results);
|
||||
})
|
||||
},
|
||||
// const parsedResponse = await response.json()
|
||||
// return parsedResponse?.data ? parsedResponse.data : []
|
||||
// })
|
||||
// )
|
||||
// set((state) => {
|
||||
// state.tradeHistory = results
|
||||
// console.log('spot-history', results);
|
||||
// })
|
||||
// },
|
||||
},
|
||||
}))
|
||||
|
||||
|
|
Loading…
Reference in New Issue