Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
jordaaash committed Apr 22, 2021
1 parent 251f2fe commit 65e65f6
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 54 deletions.
44 changes: 21 additions & 23 deletions packages/lending/src/contexts/lending.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import { contexts, LENDING_PROGRAM_ID, ParsedAccount } from '@oyster/common';
import { AccountInfo, PublicKey } from '@solana/web3.js';
import React, { useCallback, useEffect, useState } from 'react';

import { useLendingReserves } from '../hooks';
import {
LendingMarketParser,
isLendingReserve,
isLendingMarket,
LendingReserveParser,
LendingReserve,
isLendingObligation,
LendingObligationParser,
} from './../models/lending';

import { PublicKey, AccountInfo } from '@solana/web3.js';
isObligation,
isReserve,
LendingMarketParser,
ObligationParser,
Reserve,
ReserveParser,
} from '../models';
import { DexMarketParser } from '../models/dex';
import { usePrecacheMarket } from './market';
import { useLendingReserves } from '../hooks';
import { contexts, utils, ParsedAccount } from '@oyster/common';

const { useConnection } = contexts.Connection;
const { LENDING_PROGRAM_ID } = utils;
const { cache, getMultipleAccounts, MintParser } = contexts.Accounts;

export interface LendingContextState {}
Expand Down Expand Up @@ -46,11 +44,11 @@ export const useLending = () => {

const processAccount = useCallback(
(item: { pubkey: PublicKey; account: AccountInfo<Buffer> }) => {
if (isLendingReserve(item.account)) {
if (isReserve(item.account)) {
const reserve = cache.add(
item.pubkey.toBase58(),
item.account,
LendingReserveParser,
ReserveParser,
);

return reserve;
Expand All @@ -60,11 +58,11 @@ export const useLending = () => {
item.account,
LendingMarketParser,
);
} else if (isLendingObligation(item.account)) {
} else if (isObligation(item.account)) {
return cache.add(
item.pubkey.toBase58(),
item.account,
LendingObligationParser,
ObligationParser,
);
}
},
Expand All @@ -74,7 +72,7 @@ export const useLending = () => {
useEffect(() => {
if (reserveAccounts.length > 0) {
precacheMarkets(
reserveAccounts.map(reserve => reserve.info.liquidityMint.toBase58()),
reserveAccounts.map(reserve => reserve.info.liquidity.mint.toBase58()),
);
}
}, [reserveAccounts, precacheMarkets]);
Expand All @@ -94,24 +92,24 @@ export const useLending = () => {

const lendingReserves = accounts
.filter(
acc => (acc?.info as LendingReserve).lendingMarket !== undefined,
acc => (acc?.info as Reserve).lendingMarket !== undefined,
)
.map(acc => acc as ParsedAccount<LendingReserve>);
.map(acc => acc as ParsedAccount<Reserve>);

const toQuery = [
...lendingReserves.map(acc => {
const result = [
cache.registerParser(
acc?.info.collateralMint.toBase58(),
acc?.info.collateral.mint.toBase58(),
MintParser,
),
cache.registerParser(
acc?.info.liquidityMint.toBase58(),
acc?.info.liquidity.mint.toBase58(),
MintParser,
),
// ignore dex if its not set
cache.registerParser(
acc?.info.dexMarketOption ? acc?.info.dexMarket.toBase58() : '',
acc?.info.liquidity.aggregatorOption ? acc?.info.liquidity.aggregator.toBase58() : '',
DexMarketParser,
),
].filter(_ => _);
Expand Down
66 changes: 35 additions & 31 deletions packages/lending/src/contexts/market.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
import React, { useCallback, useContext, useEffect, useState } from 'react';
import { MINT_TO_MARKET } from './../models/marketOverrides';
import { POOLS_WITH_AIRDROP } from './../models/airdrops';

import { getPoolName } from '../utils/utils';

import { Market, MARKETS, Orderbook, TOKEN_MINTS } from '@project-serum/serum';
import { AccountInfo, Connection, PublicKey } from '@solana/web3.js';
import { useMemo } from 'react';

import {
contexts,
utils,
ParsedAccount,
KnownTokenMap,
convert,
EventEmitter,
fromLamports,
getTokenName,
KnownTokenMap,
ParsedAccount,
STABLE_COINS,
} from '@oyster/common';

import { DexMarketParser } from './../models/dex';
import { LendingMarket, LendingReserve, PoolInfo } from '../models';
import { Market, MARKETS, Orderbook, TOKEN_MINTS } from '@project-serum/serum';
import { AccountInfo, Connection, PublicKey } from '@solana/web3.js';
import React, {
useCallback,
useContext,
useEffect,
useMemo,
useState,
} from 'react';
import { LendingMarket, PoolInfo, Reserve } from '../models';
import { LIQUIDITY_PROVIDER_FEE, SERUM_FEE } from '../utils/pools';
import { getPoolName } from '../utils/utils';
import { POOLS_WITH_AIRDROP } from './../models/airdrops';
import { DexMarketParser } from './../models/dex';
import { MINT_TO_MARKET } from './../models/marketOverrides';

const { useConnectionConfig } = contexts.Connection;
const { convert, fromLamports, getTokenName, STABLE_COINS } = utils;
const { cache, getMultipleAccounts } = contexts.Accounts;

const INITAL_LIQUIDITY_DATE = new Date('2020-10-27');
Expand Down Expand Up @@ -146,7 +150,7 @@ export function MarketProvider({ children = null as any }) {
}

if (!cache.get(decoded.info.baseMint)) {
toQuery.add(decoded.info.quoteMint.toBase58());
toQuery.add(decoded.info.quoteTokenMint.toBase58());
}

toQuery.add(decoded.info.bids.toBase58());
Expand Down Expand Up @@ -509,12 +513,12 @@ export const usePrecacheMarket = () => {

export const simulateMarketOrderFill = (
amount: number,
reserve: LendingReserve,
reserve: Reserve,
dex: PublicKey,
useBBO = false,
) => {
const liquidityMint = cache.get(reserve.liquidityMint);
const collateralMint = cache.get(reserve.collateralMint);
const liquidityMint = cache.get(reserve.liquidity.mint);
const collateralMint = cache.get(reserve.collateral.mint);
if (!liquidityMint || !collateralMint) {
return 0.0;
}
Expand All @@ -527,17 +531,17 @@ export const simulateMarketOrderFill = (

const baseMintDecimals =
cache.get(decodedMarket.baseMint)?.info.decimals || 0;
const quoteMintDecimals =
cache.get(decodedMarket.quoteMint)?.info.decimals || 0;
const quoteTokenMintDecimals =
cache.get(decodedMarket.quoteTokenMint)?.info.decimals || 0;

const lendingMarket = cache.get(
reserve.lendingMarket,
) as ParsedAccount<LendingMarket>;

const dexMarket = new Market(
const aggregator = new Market(
decodedMarket,
baseMintDecimals,
quoteMintDecimals,
quoteTokenMintDecimals,
undefined,
decodedMarket.programId,
);
Expand All @@ -548,10 +552,10 @@ export const simulateMarketOrderFill = (
return 0;
}

const bids = new Orderbook(dexMarket, bidInfo.accountFlags, bidInfo.slab);
const asks = new Orderbook(dexMarket, askInfo.accountFlags, askInfo.slab);
const bids = new Orderbook(aggregator, bidInfo.accountFlags, bidInfo.slab);
const asks = new Orderbook(aggregator, askInfo.accountFlags, askInfo.slab);

const book = lendingMarket.info.quoteMint.equals(reserve.liquidityMint)
const book = lendingMarket.info.quoteTokenMint.equals(reserve.liquidity.mint)
? bids
: asks;

Expand Down Expand Up @@ -617,13 +621,13 @@ const getMidPrice = (marketAddress?: string, mintAddress?: string) => {

const baseMintDecimals =
cache.get(decodedMarket.baseMint)?.info.decimals || 0;
const quoteMintDecimals =
cache.get(decodedMarket.quoteMint)?.info.decimals || 0;
const quoteTokenMintDecimals =
cache.get(decodedMarket.quoteTokenMint)?.info.decimals || 0;

const market = new Market(
decodedMarket,
baseMintDecimals,
quoteMintDecimals,
quoteTokenMintDecimals,
undefined,
decodedMarket.programId,
);
Expand Down

0 comments on commit 65e65f6

Please sign in to comment.