Skip to content

Commit

Permalink
Lifetime volume on landing page (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
brittcyr authored Oct 29, 2024
1 parent 8a832d5 commit edb1ee7
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 8 deletions.
26 changes: 26 additions & 0 deletions client/ts/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
sendAndConfirmTransaction,
AccountInfo,
TransactionSignature,
GetProgramAccountsResponse,
} from '@solana/web3.js';
import {
Mint,
Expand Down Expand Up @@ -138,6 +139,31 @@ export class ManifestClient {
return accounts.map((a) => a.pubkey);
}

/**
* Get all market program accounts. This is expensive RPC load..
*
* @param connection Connection
* @returns GetProgramAccountsResponse
*/
public static async getMarketProgramAccounts(
connection: Connection,
): Promise<GetProgramAccountsResponse> {
const accounts: GetProgramAccountsResponse =
await connection.getProgramAccounts(PROGRAM_ID, {
filters: [
{
memcmp: {
offset: 0,
bytes: marketDiscriminator.toString('base64'),
encoding: 'base64',
},
},
],
});

return accounts;
}

/**
* Create a new client which creates a wrapper and claims seat if needed.
*
Expand Down
19 changes: 18 additions & 1 deletion client/ts/src/market.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ export interface MarketData {
asks: RestingOrder[];
/** Array of all claimed seats. */
claimedSeats: ClaimedSeat[];
/** Quote volume in atoms. */
quoteVolumeAtoms: bigint;
}

/**
Expand Down Expand Up @@ -398,6 +400,15 @@ export class Market {
return [...this.data.bids, ...this.data.asks];
}

/**
* Gets the quote volume traded over the lifetime of the market.
*
* @returns bigint
*/
public quoteVolume(): bigint {
return this.data.quoteVolumeAtoms;
}

/**
* Print all information loaded about the market in a human readable format.
*/
Expand Down Expand Up @@ -493,7 +504,12 @@ export class Market {
const _freeListHeadIndex = data.readUInt32LE(offset);
offset += 4;

// _padding2: [u32; 3],
const _padding2 = data.readUInt32LE(offset);
offset += 4;

const quoteVolumeAtoms: bigint = data.readBigUInt64LE(offset);
offset += 8;

// _padding3: [u64; 8],

const bids: RestingOrder[] =
Expand Down Expand Up @@ -594,6 +610,7 @@ export class Market {
bids,
asks,
claimedSeats,
quoteVolumeAtoms,
};
}

Expand Down
42 changes: 37 additions & 5 deletions debug-ui/app/components/AppWalletProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ import {
import { WalletAdapterNetwork } from '@solana/wallet-adapter-base';
import { WalletModalProvider } from '@solana/wallet-adapter-react-ui';
import WalletConnection from './WalletConnection';
import { ManifestClient } from '@cks-systems/manifest-sdk';
import { Connection } from '@solana/web3.js';
import { ManifestClient, Market } from '@cks-systems/manifest-sdk';
import {
Connection,
GetProgramAccountsResponse,
PublicKey,
} from '@solana/web3.js';
import { ToastContainer, toast } from 'react-toastify';
import { ensureError } from '@/lib/error';
import {
Expand All @@ -38,9 +42,11 @@ interface AppStateContextValue {
loading: boolean;
network: WalletAdapterNetwork | null;
marketAddrs: string[];
marketVolumes: [string, number][];
labelsByAddr: LabelsByAddr;
setMarketAddrs: Dispatch<SetStateAction<string[]>>;
setLabelsByAddr: Dispatch<SetStateAction<LabelsByAddr>>;
setMarketVolumes: Dispatch<SetStateAction<[string, number][]>>;
}

const AppStateContext = createContext<AppStateContextValue | undefined>(
Expand All @@ -62,6 +68,7 @@ const AppWalletProvider = ({
}): ReactElement => {
const [network, setNetwork] = useState<WalletAdapterNetwork | null>(null);
const [marketAddrs, setMarketAddrs] = useState<string[]>([]);
const [marketVolumes, setMarketVolumes] = useState<[string, number][]>([]);
const [labelsByAddr, setLabelsByAddr] = useState<LabelsByAddr>({});
const [loading, setLoading] = useState<boolean>(false);
const setupRun = useRef(false);
Expand Down Expand Up @@ -109,10 +116,33 @@ const AppWalletProvider = ({
const detectedNetwork = await determineNetworkFromRpcUrl(rpcUrl);
setNetwork(detectedNetwork);

const conn = new Connection(rpcUrl, 'confirmed');
const marketPubs = await ManifestClient.listMarketPublicKeys(conn);
const marketAddrs = marketPubs.map((p) => p.toBase58());
const conn: Connection = new Connection(rpcUrl, 'confirmed');
const marketProgramAccounts: GetProgramAccountsResponse =
await ManifestClient.getMarketProgramAccounts(conn);
const marketPubs: PublicKey[] = marketProgramAccounts.map(
(a) => a.pubkey,
);
const marketAddrs: string[] = marketPubs.map((p) => p.toBase58());
const marketVolumes: [string, number][] = marketProgramAccounts.map(
(a) => {
const market: Market = Market.loadFromBuffer({
address: a.pubkey,
buffer: a.account.data,
});
if (
market.quoteMint().toBase58() ==
'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v'
) {
return [
market.address.toBase58(),
Number(market.quoteVolume()) / 10 ** 6,
];
}
return [market.address.toBase58(), 0];
},
);
setMarketAddrs(marketAddrs);
setMarketVolumes(marketVolumes);
fetchAndSetMfxAddrLabels(conn, marketAddrs, setLabelsByAddr);
} catch (e) {
console.error('fetching app state:', e);
Expand Down Expand Up @@ -144,9 +174,11 @@ const AppWalletProvider = ({
value={{
network,
marketAddrs,
marketVolumes,
labelsByAddr,
setLabelsByAddr,
setMarketAddrs,
setMarketVolumes,
loading,
}}
>
Expand Down
7 changes: 5 additions & 2 deletions debug-ui/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { addrToLabel } from '@/lib/address-labels';

const Home = (): ReactElement => {
const readOnly = process.env.NEXT_PUBLIC_READ_ONLY === 'true';
const { marketAddrs, loading, labelsByAddr } = useAppState();
console.log(labelsByAddr);
const { marketAddrs, loading, labelsByAddr, marketVolumes } = useAppState();
const marketVolumesMap: Map<string, number> = new Map(marketVolumes);

return (
<main className="flex min-h-screen flex-col items-center justify-center bg-gray-900 text-gray-200 p-8">
Expand Down Expand Up @@ -43,6 +43,9 @@ const Home = (): ReactElement => {
>
{addrToLabel(market, labelsByAddr)}
</Link>
{marketVolumesMap.get(market) != 0
? ': $' + marketVolumesMap.get(market)?.toFixed(2)
: ''}
</li>
))}
</ul>
Expand Down

0 comments on commit edb1ee7

Please sign in to comment.