import { COption } from '@convergence-rfq/beet'; import { CustomOptionalF64, CustomOptionalPubkey } from '@convergence-rfq/rfq'; import { PublicKey } from '@solana/web3.js'; import { baseAssetsCache } from './cache'; import { Convergence } from '@/Convergence'; export const toCustomOptionalF64 = ( input: COption ): CustomOptionalF64 => { if (input !== null) { return { __kind: 'Some', value: input, }; } return { __kind: 'None', }; }; export const toCustomOptionalPubkey = ( input: COption ): CustomOptionalPubkey => { if (input !== null) { return { __kind: 'Some', value: input, }; } return { __kind: 'None', }; }; export const findVacantBaseAssetIndex = async (cvg: Convergence) => { await baseAssetsCache.clear(); // clear the cache to use up-to-date base assets const getRandomNumber = (min: number, max: number) => { const minCeiled = Math.ceil(min); const maxFloored = Math.floor(max); return Math.floor(Math.random() * (maxFloored - minCeiled) + minCeiled); // The maximum is exclusive and the minimum is inclusive }; let elementsToSkip = getRandomNumber(0, 100); const existingBaseAssets = await cvg.protocol().getBaseAssets(); const existing = existingBaseAssets .map((el) => el.index) .sort((a, b) => a - b); let nextExistingIndex = 0; for (let i = 0; i < 2 ** 16; i++) { const nextExisting = nextExistingIndex < existing.length ? existing[nextExistingIndex] : null; if (i === nextExisting) { nextExistingIndex++; } else if (elementsToSkip > 0) { elementsToSkip--; } else { return i; } } throw new Error('Failed to find a vacant base asset index'); };