import { BigNumber } from "bignumber.js" ; export interface SimpleInterestContractTerms { principalTokenIndex : BigNumber ; principalAmount : BigNumber ; interestRateFixedPoint : BigNumber ; amortizationUnitType : BigNumber ; termLengthUnits : BigNumber ; } export interface CollateralizedContractTerms { collateralTokenIndex : BigNumber ; collateralAmount : BigNumber ; gracePeriodInDays : BigNumber ; } export interface ERC721CollateralizedContractTerms { isEnumerable : BigNumber ; erc721ContractIndex : BigNumber ; tokenReference : BigNumber ; } class TermsContractParameters { public static bitShiftLeft ( target , numPlaces ) { const binaryTargetString = target . toString ( 2 ) ; const binaryTargetStringShifted = binaryTargetString + "0" . repeat ( numPlaces ) ; return new BigNumber ( binaryTargetStringShifted , 2 ) ; } } export class SimpleInterestParameters extends TermsContractParameters { public static pack ( terms ) { const principalTokenIndexShifted = TermsContractParameters . bitShiftLeft ( terms . principalTokenIndex , 248 ) ; const principalAmountShifted = TermsContractParameters . bitShiftLeft ( terms . principalAmount , 152 ) ; const interestRateShifted = TermsContractParameters . bitShiftLeft ( terms . interestRateFixedPoint , 128 ) ; const amortizationUnitTypeShifted = TermsContractParameters . bitShiftLeft ( terms . amortizationUnitType , 124 ) ; const termLengthShifted = TermsContractParameters . bitShiftLeft ( terms . termLengthUnits , 108 ) ; const baseTenParameters = principalTokenIndexShifted . plus ( principalAmountShifted ) . plus ( interestRateShifted ) . plus ( amortizationUnitTypeShifted ) . plus ( termLengthShifted ) ; return `0x${ baseTenParameters . toString ( 16 ) . padStart ( 64 , "0" ) }` ; } } export class CollateralizedSimpleInterestTermsParameters extends TermsContractParameters { public static pack ( collateralTerms , contractTerms ? ) { const encodedCollateralToken = collateralTerms . collateralTokenIndex . toString ( 16 ) . padStart ( 2 , "0" ) ; const encodedCollateralAmount = collateralTerms . collateralAmount . toString ( 16 ) . padStart ( 23 , "0" ) ; const encodedGracePeriodInDays = collateralTerms . gracePeriodInDays . toString ( 16 ) . padStart ( 2 , "0" ) ; const packedCollateralParameters = encodedCollateralToken + encodedCollateralAmount + encodedGracePeriodInDays ; if ( contractTerms ) { const packedTermsParameters = SimpleInterestParameters . pack ( contractTerms ) ; return `${ packedTermsParameters . substr ( 0 , 39 ) }${ packedCollateralParameters . padStart ( 27 , "0" ) }` ; } else { return `0x${ packedCollateralParameters . padStart ( 64 , "0" ) }` ; } } } export class ERC721CollateralizedSimpleInterestTermsParameters extends TermsContractParameters { public static pack ( collateralTerms , contractTerms ? ) { const encodedIsEnumerable = collateralTerms . isEnumerable . toString ( 16 ) ; const encodedCollateralToken = collateralTerms . erc721ContractIndex . toString ( 16 ) . padStart ( 12 , "0" ) ; const encodedTokenIndex = collateralTerms . tokenReference . toString ( 16 ) . padStart ( 14 , "0" ) ; const packedCollateralParameters = encodedCollateralToken + encodedTokenIndex + encodedIsEnumerable ; if ( contractTerms ) { const packedTermsParameters = SimpleInterestParameters . pack ( contractTerms ) ; return `${ packedTermsParameters . substr ( 0 , 39 ) }${ packedCollateralParameters . padStart ( 27 , "0" ) }` ; } else { return `0x${ packedCollateralParameters . padStart ( 64 , "0" ) }` ; } } }