import { MintInfo } from '@solana/spl-token' ; import { BigNumber } from 'bignumber.js' ; import BN from 'bn.js' ; import { MintMaxVoteWeightSource } from '@solana/spl-governance' ; const SECONDS_PER_DAY = 86400 ; export function getDaysFromTimestamp ( unixTimestamp ) { return unixTimestamp / SECONDS_PER_DAY ; } export function getTimestampFromDays ( days ) { return days * SECONDS_PER_DAY ; } export function formatMintNaturalAmountAsDecimal ( mint , naturalAmount ) { return getMintDecimalAmountFromNatural ( mint , naturalAmount ) . toFormat ( ) ; } export function formatMintSupplyAsDecimal ( mint ) { return getMintDecimalAmountFromNatural ( mint , mint . supply ) . toFormat ( ) ; } export function getMintDecimalAmountFromNatural ( mint , naturalAmount ) { return new BigNumber ( naturalAmount . toString ( ) ) . shiftedBy ( - mint . decimals ) ; } export function parseMintNaturalAmountFromDecimal ( decimalAmount , mintDecimals ) { if ( typeof decimalAmount === 'number' ) { return getMintNaturalAmountFromDecimal ( decimalAmount , mintDecimals ) ; } if ( mintDecimals === 0 ) { return parseInt ( decimalAmount ) ; } const floatAmount = parseFloat ( decimalAmount ) ; return getMintNaturalAmountFromDecimal ( floatAmount , mintDecimals ) ; } export function getMintNaturalAmountFromDecimal ( decimalAmount , decimals ) { return new BigNumber ( decimalAmount . toString ( ) ) . shiftedBy ( decimals ) . toNumber ( ) ; } export function getMintSupplyPercentageAsDecimal ( mint , percentage ) { return new BigNumber ( mint . supply . mul ( new BN ( percentage ) ) . toString ( ) ) . shiftedBy ( - ( mint . decimals + 2 ) ) . toNumber ( ) ; } export function getMintMinAmountAsDecimal ( mint ) { return new BigNumber ( 1 ) . shiftedBy ( - mint . decimals ) . toNumber ( ) ; } export function getMintSupplyAsDecimal ( mint ) { return new BigNumber ( mint . supply . toString ( ) ) . shiftedBy ( - mint . decimals ) . toNumber ( ) ; } export function getMintSupplyFractionAsDecimalPercentage ( mint , naturalAmount ) { return getBigNumberAmount ( naturalAmount ) . multipliedBy ( 100 ) . dividedBy ( new BigNumber ( mint . supply . toString ( ) ) ) . toNumber ( ) ; } export function getAmountFractionAsDecimalPercentage ( totalNaturalAmount , naturalAmount ) { return getBigNumberAmount ( naturalAmount ) . multipliedBy ( 100 ) . dividedBy ( new BigNumber ( getBigNumberAmount ( totalNaturalAmount ) ) ) . toNumber ( ) ; } export function getBigNumberAmount ( amount ) { return typeof amount === 'number' ? new BigNumber ( amount ) : new BigNumber ( amount . toString ( ) ) ; } export function formatPercentage ( percentage ) { if ( percentage === 0 || percentage === Infinity ) { return '0%' ; } if ( percentage < 0.01 ) { return '<0.01%' ; } return `${ + percentage . toFixed ( 2 ) }%` ; } export function getMintVoteWeight ( mint , naturalAmount ) { return new BigNumber ( 100 ) . multipliedBy ( getBigNumberAmount ( naturalAmount ) ) . div ( getBigNumberAmount ( mint . supply ) ) . toNumber ( ) ; } export function getMintSupplyVoteWeight ( supply , naturalAmount ) { return new BigNumber ( 100 ) . multipliedBy ( getBigNumberAmount ( naturalAmount ) ) . div ( getBigNumberAmount ( supply ) ) . toNumber ( ) ; } export function formatMintVoteWeight ( mint , naturalAmount ) { return formatPercentage ( getMintVoteWeight ( mint , naturalAmount ) ) ; } export function formatMintSupplyFractionAsDecimalPercentage ( mint , naturalAmount ) { return formatPercentage ( getMintSupplyFractionAsDecimalPercentage ( mint , naturalAmount ) ) ; } export function formatMintMaxVoteWeight ( mint , maxVoteWeightSource ) { const supplyFraction = maxVoteWeightSource . getSupplyFraction ( ) ; return new BigNumber ( supplyFraction . toString ( ) ) . multipliedBy ( mint . supply . toString ( ) ) . shiftedBy ( - ( mint . decimals + MintMaxVoteWeightSource . SUPPLY_FRACTION_DECIMALS ) ) . toFormat ( mint . decimals ) ; } export function formatMintMaxVotePercentage ( maxVoteWeightSource ) { const supplyFraction = maxVoteWeightSource . getSupplyFraction ( ) ; const percentage = new BigNumber ( supplyFraction . toString ( ) ) . shiftedBy ( - ( MintMaxVoteWeightSource . SUPPLY_FRACTION_DECIMALS - 2 ) ) . toNumber ( ) ; if ( percentage < 0.01 ) { return '<0.01%' ; } const rounded = + percentage . toFixed ( 2 ) ; return rounded === percentage ? `${ rounded }%` : `~${ rounded }%` ; } export function getMintMaxVoteWeight ( mint , maxVoteWeightSource ) { if ( maxVoteWeightSource . isFullSupply ( ) ) { return mint . supply ; } const supplyFraction = maxVoteWeightSource . getSupplyFraction ( ) ; const maxVoteWeight = new BigNumber ( supplyFraction . toString ( ) ) . multipliedBy ( mint . supply . toString ( ) ) . shiftedBy ( - MintMaxVoteWeightSource . SUPPLY_FRACTION_DECIMALS ) ; return new BN ( maxVoteWeight . dp ( 0 , BigNumber . ROUND_DOWN ) . toString ( ) ) ; } export function lamportsToSOL ( lamports ) { return new BigNumber ( lamports ) . shiftedBy ( - 9 ) . toNumber ( ) ; } export function SOLToLamports ( solAmount ) { return new BigNumber ( solAmount ) . shiftedBy ( 9 ) . toNumber ( ) ; }