import { PayoutMod } from 'models/mods' ; import { fromPermyriad } from 'utils/formatNumber' ; import { constants , utils } from 'ethers' ; import { BigNumber } from '@ethersproject/bignumber' ; import { stripCommas , formatWad , parseWad , fromWad , parsePermyriad , } from 'utils/formatNumber' ; import { amountSubFee , amountAddFee } from 'utils/math' ; export type ModalMode = 'Add' | 'Edit' | undefined ; export function getTotalPercentage ( mods ) { return ( mods ?. reduce ( ( acc , curr ) => acc + parseFloat ( fromPermyriad ( curr . percent ?? '0' ) ) , 0 ) ?? 0 ) ; } export function validatePercentage ( percent ) { if ( percent === undefined || percent === 0 ) return Promise . reject ( 'Required' ) ; else if ( percent > 100 ) return Promise . reject ( 'Invalid' ) ; return Promise . resolve ( ) ; } export function validateEthAddress ( address , mods , modalMode , editingModIndex ) { if ( modalMode === 'Edit' && address === mods [ editingModIndex ?? 0 ] ?. beneficiary ) return Promise . resolve ( ) ; else if ( ! address || ! utils . isAddress ( address ) ) return Promise . reject ( 'Address is required' ) ; else if ( address === constants . AddressZero ) return Promise . reject ( 'Cannot use zero address' ) ; else if ( mods . some ( mod => mod . beneficiary === address ) ) return Promise . reject ( 'A payout for this address already exists' ) ; else return Promise . resolve ( ) ; } export const targetToTargetSubFeeFormatted = ( target , fee ) => { const newTargetSubFee = amountSubFee ( parseWad ( target ?? '' ) , fee ) ; return fromWad ( newTargetSubFee ) ; } ; export const targetSubFeeToTargetFormatted = ( targetSubFee , fee ) => { if ( targetSubFee === fromWad ( constants . MaxUint256 ) ) { return fromWad ( constants . MaxUint256 ) ; } const newTarget = amountAddFee ( parseWad ( targetSubFee ?? '0' ) , fee ) ; return fromWad ( newTarget ) ; } ; export function getAmountFromPercent ( percent , target , fee ) { const amount = fromWad ( amountSubFee ( parseWad ( stripCommas ( target ) ) , fee ) ?. mul ( Math . floor ( ( percent ?? 0 ) * 100 ) ) . div ( 10000 ) ) ; return parseFloat ( amount ) ; } export function getPercentFromAmount ( amount , target , fee ) { const targetSubFeeBN = amountSubFee ( parseWad ( stripCommas ( target ) ) , fee ) ; const targetSubFee = stripCommas ( formatWad ( targetSubFeeBN ) ?? '0' ) ; const percent = ( ( ( amount ?? 0 ) * 1.0 ) / parseFloat ( targetSubFee ) ) * 100 ; return parseFloat ( percent . toFixed ( 8 ) ) ; } export function countDecimalPlaces ( value ) { if ( value && Math . floor ( value ?? 1 ) === value ) return 0 ; return value ?. toString ( ) . split ( '.' ) [ 1 ] ?. length || 0 ; } export function roundDown ( value , decimalPlaces ) { if ( ! decimalPlaces ) return Math . floor ( value ) ; return parsePermyriad ( value ) . toNumber ( ) / 100 ; }