import { Codec } from '@polkadot/types/types' ; import { Fixed18 , convertToFixed18 } from './fixed-18' ; name description export const calcCommunalBonded = ( totalBonded , nextEraClaimedUnbonded ) : Fixed18 => { return totalBonded . sub ( nextEraClaimedUnbonded ) ; } ; name description export const calcCommunalTotal = ( communalBonded , communalFree , unbondingToFree ) : Fixed18 => { return communalBonded . add ( communalFree ) . add ( unbondingToFree ) ; } ; name param { Fixed18 } communalBonded param { Fixed18 } commuanlTotal export const calcCommunalBondedRatio = ( communalBonded , commuanlTotal ) : Fixed18 => { return communalBonded . div ( commuanlTotal ) ; } ; name description param { Fixed18 } liquidTokenIssuance param { Fixed18 } commuanlTotal param { Fixed18 } defaultExchangeRate export const calcLiquidExchangeRate = ( liquidTokenIssuance , commuanlTotal , defaultExchangeRate ) : Fixed18 => { if ( liquidTokenIssuance . isZero ( ) || commuanlTotal . isZero ( ) ) { return defaultExchangeRate ; } return commuanlTotal . div ( liquidTokenIssuance ) ; } ; name description param { Fixed18 } maxClaimFee param { number } targetEra param { number } bondingDuration export const calcClaimFeeRatio = ( targetEra , currentEra , maxClaimFee , bondingDuration ) : Fixed18 => { const claimPeriodPercent = Fixed18 . fromRational ( Math . max ( targetEra - currentEra , 0 ) , bondingDuration + 1 ) ; return Fixed18 . fromNatural ( 1 ) . sub ( claimPeriodPercent ) . mul ( maxClaimFee ) ; } ; name description param { Fixed18 } amount param { Fixed18 } maxClaimFee param { number } targetEra param { number } bondingDuration export const calcClaimFee = ( amount , targetEra , currentEra , maxClaimFee , bondingDuration ) : Fixed18 => { return amount . mul ( calcClaimFeeRatio ( targetEra , currentEra , maxClaimFee , bondingDuration ) ) ; } ; interface StakingPoolParams { totalBonded : Fixed18 | Codec ; communalFree : Fixed18 | Codec ; unbondingToFree : Fixed18 | Codec ; nextEraClaimedUnbonded : Fixed18 | Codec ; liquidTokenIssuance : Fixed18 | Codec ; defaultExchangeRate : Fixed18 | Codec ; maxClaimFee : Fixed18 | Codec ; bondingDuration : number | Codec ; currentEra : number | Codec ; } const convertToNumber = ( data ) : number => { if ( typeof data === 'number' ) { return data ; } return Number ( data . toString ( ) ) ; } ; class classdesc export class StakingPoolHelper { public totalBonded : Fixed18 ; public communalFree : Fixed18 ; public unbondingToFree : Fixed18 ; public nextEraClaimedUnbonded : Fixed18 ; public liquidTokenIssuance : Fixed18 ; public defaultExchangeRate : Fixed18 ; public maxClaimFee : Fixed18 ; public bondingDuration : number ; public currentEra : number ; constructor ( params ) { this . totalBonded = convertToFixed18 ( params . totalBonded ) ; this . communalFree = convertToFixed18 ( params . communalFree ) ; this . unbondingToFree = convertToFixed18 ( params . unbondingToFree ) ; this . nextEraClaimedUnbonded = convertToFixed18 ( params . nextEraClaimedUnbonded ) ; this . liquidTokenIssuance = convertToFixed18 ( params . liquidTokenIssuance ) ; this . defaultExchangeRate = convertToFixed18 ( params . defaultExchangeRate ) ; this . maxClaimFee = convertToFixed18 ( params . maxClaimFee ) ; this . bondingDuration = convertToNumber ( params . bondingDuration ) ; this . currentEra = convertToNumber ( params . currentEra ) ; } get communalBonded ( ) : Fixed18 { return calcCommunalBonded ( this . totalBonded , this . nextEraClaimedUnbonded ) ; } get communalTotal ( ) : Fixed18 { return calcCommunalTotal ( this . communalBonded , this . communalFree , this . unbondingToFree ) ; } get communalBondedRatio ( ) : Fixed18 { return calcCommunalBondedRatio ( this . communalBonded , this . communalTotal ) ; } get liquidExchangeRate ( ) : Fixed18 { return calcLiquidExchangeRate ( this . liquidTokenIssuance , this . communalTotal , this . defaultExchangeRate ) ; } public convertToLiquid ( amount ) { return Fixed18 . fromNatural ( 1 ) . div ( this . liquidExchangeRate ) . mul ( amount ) ; } public claimFeeRatio ( era ) { return calcClaimFeeRatio ( era , this . currentEra , this . maxClaimFee , this . bondingDuration ) ; } public claimFee ( amount , era ) { return calcClaimFee ( amount , era , this . currentEra , this . maxClaimFee , this . bondingDuration ) ; } }