add 20 sec interval to oracle price updates

This commit is contained in:
Tyler Shipe 2021-06-05 15:38:17 -04:00
parent b67f676383
commit c86ee0157f
2 changed files with 13 additions and 215 deletions

View File

@ -1,9 +1,12 @@
import { useEffect, useState } from 'react'
import { useCallback, useEffect, useState } from 'react'
import useMangoStore from '../stores/useMangoStore'
import useConnection from './useConnection'
import useInterval from './useInterval'
import useMarket from './useMarket'
import useMarketList from './useMarketList'
const SECONDS = 1000
export default function useOraclePrice() {
const selectedMangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
const { connection } = useConnection()
@ -11,17 +14,23 @@ export default function useOraclePrice() {
const { getMarketIndex } = useMarketList()
const [oraclePrice, setOraclePrice] = useState(null)
useEffect(() => {
const fetchOraclePrice = useCallback(() => {
if (selectedMangoGroup) {
const marketIndex = getMarketIndex(marketAddress)
selectedMangoGroup.getPrices(connection).then((prices) => {
const oraclePriceForMarket = prices[marketIndex]
setOraclePrice(oraclePriceForMarket)
console.log('yoooo', marketAddress, marketIndex, oraclePriceForMarket)
})
}
}, [selectedMangoGroup, marketAddress])
console.log('oracle prices', oraclePrice)
useEffect(() => {
fetchOraclePrice()
}, [fetchOraclePrice])
useInterval(() => {
fetchOraclePrice()
}, 20 * SECONDS)
return oraclePrice
}

View File

@ -826,217 +826,6 @@ export async function withdrawSrm(
)
}
export async function placeOrderAndSettle(
connection: Connection,
programId: PublicKey,
mangoGroup: MangoGroup,
marginAccount: MarginAccount,
spotMarket: Market,
wallet: Wallet,
side: 'buy' | 'sell',
price: number,
size: number,
orderType?: 'limit' | 'ioc' | 'postOnly',
clientId?: BN
): Promise<TransactionSignature> {
orderType = orderType == undefined ? 'limit' : orderType
// orderType = orderType ?? 'limit'
const limitPrice = spotMarket.priceNumberToLots(price)
const maxBaseQuantity = spotMarket.baseSizeNumberToLots(size)
const feeTier = getFeeTier(
0,
nativeToUi(mangoGroup.nativeSrm || 0, SRM_DECIMALS)
)
const rates = getFeeRates(feeTier)
const maxQuoteQuantity = new BN(
spotMarket['_decoded'].quoteLotSize.toNumber() * (1 + rates.taker)
).mul(
spotMarket
.baseSizeNumberToLots(size)
.mul(spotMarket.priceNumberToLots(price))
)
if (maxBaseQuantity.lte(new BN(0))) {
throw new Error('size too small')
}
if (limitPrice.lte(new BN(0))) {
throw new Error('invalid price')
}
const selfTradeBehavior = 'decrementTake'
const marketIndex = mangoGroup.getMarketIndex(spotMarket)
const vaultIndex = side === 'buy' ? mangoGroup.vaults.length - 1 : marketIndex
// Add all instructions to one atomic transaction
const transaction = new Transaction()
// Specify signers in addition to the wallet
const signers: Account[] = []
// Create a Solana account for the open orders account if it's missing
const openOrdersKeys: PublicKey[] = []
for (let i = 0; i < marginAccount.openOrders.length; i++) {
if (
i === marketIndex &&
marginAccount.openOrders[marketIndex].equals(zeroKey)
) {
// open orders missing for this market; create a new one now
const openOrdersSpace = OpenOrders.getLayout(mangoGroup.dexProgramId).span
const openOrdersLamports =
await connection.getMinimumBalanceForRentExemption(
openOrdersSpace,
'singleGossip'
)
const accInstr = await createAccountInstruction(
connection,
wallet.publicKey,
openOrdersSpace,
mangoGroup.dexProgramId,
openOrdersLamports
)
transaction.add(accInstr.instruction)
signers.push(accInstr.account)
openOrdersKeys.push(accInstr.account.publicKey)
} else {
openOrdersKeys.push(marginAccount.openOrders[i])
}
}
const keys = [
{ isSigner: false, isWritable: true, pubkey: mangoGroup.publicKey },
{ isSigner: true, isWritable: false, pubkey: wallet.publicKey },
{ isSigner: false, isWritable: true, pubkey: marginAccount.publicKey },
{ isSigner: false, isWritable: false, pubkey: SYSVAR_CLOCK_PUBKEY },
{ isSigner: false, isWritable: false, pubkey: spotMarket.programId },
{ isSigner: false, isWritable: true, pubkey: spotMarket.publicKey },
{
isSigner: false,
isWritable: true,
pubkey: spotMarket['_decoded'].requestQueue,
},
{
isSigner: false,
isWritable: true,
pubkey: spotMarket['_decoded'].eventQueue,
},
{ isSigner: false, isWritable: true, pubkey: spotMarket['_decoded'].bids },
{ isSigner: false, isWritable: true, pubkey: spotMarket['_decoded'].asks },
{
isSigner: false,
isWritable: true,
pubkey: mangoGroup.vaults[vaultIndex],
},
{ isSigner: false, isWritable: false, pubkey: mangoGroup.signerKey },
{
isSigner: false,
isWritable: true,
pubkey: spotMarket['_decoded'].baseVault,
},
{
isSigner: false,
isWritable: true,
pubkey: spotMarket['_decoded'].quoteVault,
},
{ isSigner: false, isWritable: false, pubkey: TOKEN_PROGRAM_ID },
{ isSigner: false, isWritable: false, pubkey: SYSVAR_RENT_PUBKEY },
{ isSigner: false, isWritable: true, pubkey: mangoGroup.srmVault },
...openOrdersKeys.map((pubkey) => ({
isSigner: false,
isWritable: true,
pubkey,
})),
...mangoGroup.oracles.map((pubkey) => ({
isSigner: false,
isWritable: false,
pubkey,
})),
]
const data = encodeMangoInstruction({
PlaceOrder: clientId
? {
side,
limitPrice,
maxBaseQuantity,
maxQuoteQuantity,
selfTradeBehavior,
orderType,
clientId,
limit: 65535,
}
: {
side,
limitPrice,
maxBaseQuantity,
maxQuoteQuantity,
selfTradeBehavior,
orderType,
limit: 65535,
},
})
const placeOrderInstruction = new TransactionInstruction({
keys,
data,
programId,
})
transaction.add(placeOrderInstruction)
const dexSigner = await PublicKey.createProgramAddress(
[
spotMarket.publicKey.toBuffer(),
spotMarket['_decoded'].vaultSignerNonce.toArrayLike(Buffer, 'le', 8),
],
spotMarket.programId
)
const settleFundsIns = makeSettleFundsInstruction(
programId,
mangoGroup.publicKey,
wallet.publicKey,
marginAccount.publicKey,
spotMarket.programId,
spotMarket.publicKey,
openOrdersKeys[marketIndex],
mangoGroup.signerKey,
spotMarket['_decoded'].baseVault,
spotMarket['_decoded'].quoteVault,
mangoGroup.vaults[marketIndex],
mangoGroup.vaults[mangoGroup.vaults.length - 1],
dexSigner
)
transaction.add(settleFundsIns)
const baseTokenIndex = marketIndex
const quoteTokenIndex = NUM_TOKENS - 1
const tokenIndex = side === 'buy' ? baseTokenIndex : quoteTokenIndex
const quantity = marginAccount.getUiBorrow(mangoGroup, tokenIndex)
const nativeQuantity = uiToNative(
quantity,
mangoGroup.mintDecimals[tokenIndex]
)
const settleBorrowIns = makeSettleBorrowInstruction(
programId,
mangoGroup.publicKey,
marginAccount.publicKey,
wallet.publicKey,
tokenIndex,
nativeQuantity
)
transaction.add(settleBorrowIns)
return await packageAndSend(
transaction,
connection,
wallet,
signers,
'PlaceOrder'
)
}
export async function placeAndSettle(
connection: Connection,
programId: PublicKey,