export const wait = (ms: number): Promise => { return new Promise((resolve) => { setTimeout(resolve, ms); }); }; interface SpecialAmount { s?: number; // sign e?: number; // exponent c?: number[]; // coefficient } export const safeBigInt = ( value: string | number | bigint | SpecialAmount | undefined, ): bigint => { if (!value) { return BigInt(0); } try { // Handle already BigInt values if (typeof value === 'bigint') { return value; } // Handle special amount format from Jupiter if (typeof value === 'object' && 'e' in value && 'c' in value) { const specialAmount = value as SpecialAmount; if (!specialAmount.c?.length) { return BigInt(0); } // Reconstruct number from scientific notation parts const sign = specialAmount.s === -1 ? '-' : ''; const coeff = specialAmount.c.join(''); const exp = specialAmount.e || 0; if (exp >= 0) { // Add zeros for positive exponent const result = sign + coeff + '0'.repeat(exp - (coeff.length - 1)); return BigInt(result); } else { // Handle negative exponent if needed const result = sign + '0.' + '0'.repeat(Math.abs(exp) - 1) + coeff; return BigInt(result.split('.')[0]); // Take only the integer part } } // Handle string that might end with 'n' (BigInt notation) if (typeof value === 'string' && value.endsWith('n')) { return BigInt(value.slice(0, -1)); } // Handle regular number or string if (typeof value === 'string' || typeof value === 'number') { const normalizedValue = Number(value).toString(); return BigInt(normalizedValue.split('.')[0]); // Take only the integer part } return BigInt(0); } catch { console.warn('Failed to convert to BigInt:', value); return BigInt(0); } };