import strategies from '..' ; import { ConstantOperand , migrateLegacyOptions , Operand , OperandType , Operation , OptionalOptions , Options , StrategyOperand , validateOptions } from './options' ; export const author = 'xJonathanLEI' ; export const version = '0.2.1' ; export async function strategy ( space , network , provider , addresses , options , snapshot ) { const rawOptions = migrateLegacyOptions ( options ) ; const strategyOptions = validateOptions ( rawOptions ) ; const operandPromises = strategyOptions . operands . map ( ( item ) => resolveOperand ( item , addresses , space , network , provider , snapshot ) ) ; const resolvedOperands = await Promise . all ( operandPromises ) ; const finalResult = resolveOperation ( strategyOptions . operation , resolvedOperands ) ; return finalResult ; } function resolveOperation ( operation , resolvedOperands ) { switch ( operation ) { case Operation . SquareRoot : { return Object . fromEntries ( Object . entries ( resolvedOperands [ 0 ] ) . map ( ( [ address , score ] ) => [ address , Math . sqrt ( score ) ] ) ) ; } case Operation . CubeRoot : { return Object . fromEntries ( Object . entries ( resolvedOperands [ 0 ] ) . map ( ( [ address , score ] ) => [ address , Math . cbrt ( score ) ] ) ) ; } case Operation . Min : { return Object . fromEntries ( Object . entries ( resolvedOperands [ 0 ] ) . map ( ( [ address , score ] ) => [ address , Math . min ( score , resolvedOperands [ 1 ] [ address ] ) ] ) ) ; } case Operation . Max : { return Object . fromEntries ( Object . entries ( resolvedOperands [ 0 ] ) . map ( ( [ address , score ] ) => [ address , Math . max ( score , resolvedOperands [ 1 ] [ address ] ) ] ) ) ; } case Operation . AIfLtB : { return Object . fromEntries ( Object . entries ( resolvedOperands [ 0 ] ) . map ( ( [ address , score ] ) => [ address , score < resolvedOperands [ 2 ] [ address ] ? resolvedOperands [ 1 ] [ address ] : score ] ) ) ; } case Operation . AIfLteB : { return Object . fromEntries ( Object . entries ( resolvedOperands [ 0 ] ) . map ( ( [ address , score ] ) => [ address , score <= resolvedOperands [ 2 ] [ address ] ? resolvedOperands [ 1 ] [ address ] : score ] ) ) ; } case Operation . AIfGtB : { return Object . fromEntries ( Object . entries ( resolvedOperands [ 0 ] ) . map ( ( [ address , score ] ) => [ address , score > resolvedOperands [ 2 ] [ address ] ? resolvedOperands [ 1 ] [ address ] : score ] ) ) ; } case Operation . AIfGteB : { return Object . fromEntries ( Object . entries ( resolvedOperands [ 0 ] ) . map ( ( [ address , score ] ) => [ address , score >= resolvedOperands [ 2 ] [ address ] ? resolvedOperands [ 1 ] [ address ] : score ] ) ) ; } } } async function resolveOperand ( operand , addresses , space , network , provider , snapshot ) { switch ( operand . type ) { case OperandType . Strategy : { const strategyOperand = operand as StrategyOperand ; const upstreamResult = await strategies [ strategyOperand . strategy . name ] . strategy ( space , network , provider , addresses , strategyOperand . strategy . params , snapshot ) ; return upstreamResult ; } case OperandType . Constant : { const constantOperand = operand as ConstantOperand ; return Object . fromEntries ( addresses . map ( ( address ) => [ address , constantOperand . value ] ) ) ; } } }