import BigNumber from 'bignumber.js' ; import { Op } from './models' ; import { ScriptBuilder } from './ScriptBuilder' ; export const CheckSigPrice = new BigNumber ( 1 << 15 ) ; const opCodePrices = { [ Op . PUSHINT8 ] : new BigNumber ( 1 << 0 ) , [ Op . PUSHINT16 ] : new BigNumber ( 1 << 0 ) , [ Op . PUSHINT32 ] : new BigNumber ( 1 << 0 ) , [ Op . PUSHINT64 ] : new BigNumber ( 1 << 0 ) , [ Op . PUSHINT128 ] : new BigNumber ( 1 << 2 ) , [ Op . PUSHINT256 ] : new BigNumber ( 1 << 2 ) , [ Op . PUSHA ] : new BigNumber ( 1 << 2 ) , [ Op . PUSHNULL ] : new BigNumber ( 1 << 0 ) , [ Op . PUSHDATA1 ] : new BigNumber ( 1 << 3 ) , [ Op . PUSHDATA2 ] : new BigNumber ( 1 << 9 ) , [ Op . PUSHDATA4 ] : new BigNumber ( 1 << 12 ) , [ Op . PUSHM1 ] : new BigNumber ( 1 << 0 ) , [ Op . PUSH0 ] : new BigNumber ( 1 << 0 ) , [ Op . PUSH1 ] : new BigNumber ( 1 << 0 ) , [ Op . PUSH2 ] : new BigNumber ( 1 << 0 ) , [ Op . PUSH3 ] : new BigNumber ( 1 << 0 ) , [ Op . PUSH4 ] : new BigNumber ( 1 << 0 ) , [ Op . PUSH5 ] : new BigNumber ( 1 << 0 ) , [ Op . PUSH6 ] : new BigNumber ( 1 << 0 ) , [ Op . PUSH7 ] : new BigNumber ( 1 << 0 ) , [ Op . PUSH8 ] : new BigNumber ( 1 << 0 ) , [ Op . PUSH9 ] : new BigNumber ( 1 << 0 ) , [ Op . PUSH10 ] : new BigNumber ( 1 << 0 ) , [ Op . PUSH11 ] : new BigNumber ( 1 << 0 ) , [ Op . PUSH12 ] : new BigNumber ( 1 << 0 ) , [ Op . PUSH13 ] : new BigNumber ( 1 << 0 ) , [ Op . PUSH14 ] : new BigNumber ( 1 << 0 ) , [ Op . PUSH15 ] : new BigNumber ( 1 << 0 ) , [ Op . PUSH16 ] : new BigNumber ( 1 << 0 ) , [ Op . NOP ] : new BigNumber ( 1 << 0 ) , [ Op . JMP ] : new BigNumber ( 1 << 1 ) , [ Op . JMP_L ] : new BigNumber ( 1 << 1 ) , [ Op . JMPIF ] : new BigNumber ( 1 << 1 ) , [ Op . JMPIF_L ] : new BigNumber ( 1 << 1 ) , [ Op . JMPIFNOT ] : new BigNumber ( 1 << 1 ) , [ Op . JMPIFNOT_L ] : new BigNumber ( 1 << 1 ) , [ Op . JMPEQ ] : new BigNumber ( 1 << 1 ) , [ Op . JMPEQ_L ] : new BigNumber ( 1 << 1 ) , [ Op . JMPNE ] : new BigNumber ( 1 << 1 ) , [ Op . JMPNE_L ] : new BigNumber ( 1 << 1 ) , [ Op . JMPGT ] : new BigNumber ( 1 << 1 ) , [ Op . JMPGT_L ] : new BigNumber ( 1 << 1 ) , [ Op . JMPGE ] : new BigNumber ( 1 << 1 ) , [ Op . JMPGE_L ] : new BigNumber ( 1 << 1 ) , [ Op . JMPLT ] : new BigNumber ( 1 << 1 ) , [ Op . JMPLT_L ] : new BigNumber ( 1 << 1 ) , [ Op . JMPLE ] : new BigNumber ( 1 << 1 ) , [ Op . JMPLE_L ] : new BigNumber ( 1 << 1 ) , [ Op . CALL ] : new BigNumber ( 1 << 9 ) , [ Op . CALL_L ] : new BigNumber ( 1 << 9 ) , [ Op . CALLA ] : new BigNumber ( 1 << 9 ) , [ Op . CALLT ] : new BigNumber ( 1 << 15 ) , [ Op . ABORT ] : new BigNumber ( 0 ) , [ Op . ASSERT ] : new BigNumber ( 1 << 0 ) , [ Op . THROW ] : new BigNumber ( 1 << 9 ) , [ Op . TRY ] : new BigNumber ( 1 << 2 ) , [ Op . TRY_L ] : new BigNumber ( 1 << 2 ) , [ Op . ENDTRY ] : new BigNumber ( 1 << 2 ) , [ Op . ENDTRY_L ] : new BigNumber ( 1 << 2 ) , [ Op . ENDFINALLY ] : new BigNumber ( 1 << 2 ) , [ Op . RET ] : new BigNumber ( 0 ) , [ Op . SYSCALL ] : new BigNumber ( 0 ) , [ Op . DEPTH ] : new BigNumber ( 1 << 1 ) , [ Op . DROP ] : new BigNumber ( 1 << 1 ) , [ Op . NIP ] : new BigNumber ( 1 << 1 ) , [ Op . XDROP ] : new BigNumber ( 1 << 4 ) , [ Op . CLEAR ] : new BigNumber ( 1 << 4 ) , [ Op . DUP ] : new BigNumber ( 1 << 1 ) , [ Op . OVER ] : new BigNumber ( 1 << 1 ) , [ Op . PICK ] : new BigNumber ( 1 << 1 ) , [ Op . TUCK ] : new BigNumber ( 1 << 1 ) , [ Op . SWAP ] : new BigNumber ( 1 << 1 ) , [ Op . ROT ] : new BigNumber ( 1 << 1 ) , [ Op . ROLL ] : new BigNumber ( 1 << 4 ) , [ Op . REVERSE3 ] : new BigNumber ( 1 << 1 ) , [ Op . REVERSE4 ] : new BigNumber ( 1 << 1 ) , [ Op . REVERSEN ] : new BigNumber ( 1 << 4 ) , [ Op . INITSSLOT ] : new BigNumber ( 1 << 4 ) , [ Op . INITSLOT ] : new BigNumber ( 1 << 6 ) , [ Op . LDSFLD0 ] : new BigNumber ( 1 << 1 ) , [ Op . LDSFLD1 ] : new BigNumber ( 1 << 1 ) , [ Op . LDSFLD2 ] : new BigNumber ( 1 << 1 ) , [ Op . LDSFLD3 ] : new BigNumber ( 1 << 1 ) , [ Op . LDSFLD4 ] : new BigNumber ( 1 << 1 ) , [ Op . LDSFLD5 ] : new BigNumber ( 1 << 1 ) , [ Op . LDSFLD6 ] : new BigNumber ( 1 << 1 ) , [ Op . LDSFLD ] : new BigNumber ( 1 << 1 ) , [ Op . STSFLD0 ] : new BigNumber ( 1 << 1 ) , [ Op . STSFLD1 ] : new BigNumber ( 1 << 1 ) , [ Op . STSFLD2 ] : new BigNumber ( 1 << 1 ) , [ Op . STSFLD3 ] : new BigNumber ( 1 << 1 ) , [ Op . STSFLD4 ] : new BigNumber ( 1 << 1 ) , [ Op . STSFLD5 ] : new BigNumber ( 1 << 1 ) , [ Op . STSFLD6 ] : new BigNumber ( 1 << 1 ) , [ Op . STSFLD ] : new BigNumber ( 1 << 1 ) , [ Op . LDLOC0 ] : new BigNumber ( 1 << 1 ) , [ Op . LDLOC1 ] : new BigNumber ( 1 << 1 ) , [ Op . LDLOC2 ] : new BigNumber ( 1 << 1 ) , [ Op . LDLOC3 ] : new BigNumber ( 1 << 1 ) , [ Op . LDLOC4 ] : new BigNumber ( 1 << 1 ) , [ Op . LDLOC5 ] : new BigNumber ( 1 << 1 ) , [ Op . LDLOC6 ] : new BigNumber ( 1 << 1 ) , [ Op . LDLOC ] : new BigNumber ( 1 << 1 ) , [ Op . STLOC0 ] : new BigNumber ( 1 << 1 ) , [ Op . STLOC1 ] : new BigNumber ( 1 << 1 ) , [ Op . STLOC2 ] : new BigNumber ( 1 << 1 ) , [ Op . STLOC3 ] : new BigNumber ( 1 << 1 ) , [ Op . STLOC4 ] : new BigNumber ( 1 << 1 ) , [ Op . STLOC5 ] : new BigNumber ( 1 << 1 ) , [ Op . STLOC6 ] : new BigNumber ( 1 << 1 ) , [ Op . STLOC ] : new BigNumber ( 1 << 1 ) , [ Op . LDARG0 ] : new BigNumber ( 1 << 1 ) , [ Op . LDARG1 ] : new BigNumber ( 1 << 1 ) , [ Op . LDARG2 ] : new BigNumber ( 1 << 1 ) , [ Op . LDARG3 ] : new BigNumber ( 1 << 1 ) , [ Op . LDARG4 ] : new BigNumber ( 1 << 1 ) , [ Op . LDARG5 ] : new BigNumber ( 1 << 1 ) , [ Op . LDARG6 ] : new BigNumber ( 1 << 1 ) , [ Op . LDARG ] : new BigNumber ( 1 << 1 ) , [ Op . STARG0 ] : new BigNumber ( 1 << 1 ) , [ Op . STARG1 ] : new BigNumber ( 1 << 1 ) , [ Op . STARG2 ] : new BigNumber ( 1 << 1 ) , [ Op . STARG3 ] : new BigNumber ( 1 << 1 ) , [ Op . STARG4 ] : new BigNumber ( 1 << 1 ) , [ Op . STARG5 ] : new BigNumber ( 1 << 1 ) , [ Op . STARG6 ] : new BigNumber ( 1 << 1 ) , [ Op . STARG ] : new BigNumber ( 1 << 1 ) , [ Op . NEWBUFFER ] : new BigNumber ( 1 << 8 ) , [ Op . MEMCPY ] : new BigNumber ( 1 << 11 ) , [ Op . CAT ] : new BigNumber ( 1 << 11 ) , [ Op . SUBSTR ] : new BigNumber ( 1 << 11 ) , [ Op . LEFT ] : new BigNumber ( 1 << 11 ) , [ Op . RIGHT ] : new BigNumber ( 1 << 11 ) , [ Op . INVERT ] : new BigNumber ( 1 << 2 ) , [ Op . AND ] : new BigNumber ( 1 << 3 ) , [ Op . OR ] : new BigNumber ( 1 << 3 ) , [ Op . XOR ] : new BigNumber ( 1 << 3 ) , [ Op . EQUAL ] : new BigNumber ( 1 << 5 ) , [ Op . NOTEQUAL ] : new BigNumber ( 1 << 5 ) , [ Op . SIGN ] : new BigNumber ( 1 << 2 ) , [ Op . ABS ] : new BigNumber ( 1 << 2 ) , [ Op . NEGATE ] : new BigNumber ( 1 << 2 ) , [ Op . INC ] : new BigNumber ( 1 << 2 ) , [ Op . DEC ] : new BigNumber ( 1 << 2 ) , [ Op . ADD ] : new BigNumber ( 1 << 3 ) , [ Op . SUB ] : new BigNumber ( 1 << 3 ) , [ Op . MUL ] : new BigNumber ( 1 << 3 ) , [ Op . DIV ] : new BigNumber ( 1 << 3 ) , [ Op . MOD ] : new BigNumber ( 1 << 3 ) , [ Op . POW ] : new BigNumber ( 1 << 6 ) , [ Op . SQRT ] : new BigNumber ( 1 << 6 ) , [ Op . SHL ] : new BigNumber ( 1 << 3 ) , [ Op . SHR ] : new BigNumber ( 1 << 3 ) , [ Op . NOT ] : new BigNumber ( 1 << 2 ) , [ Op . BOOLAND ] : new BigNumber ( 1 << 3 ) , [ Op . BOOLOR ] : new BigNumber ( 1 << 3 ) , [ Op . NZ ] : new BigNumber ( 1 << 2 ) , [ Op . NUMEQUAL ] : new BigNumber ( 1 << 3 ) , [ Op . NUMNOTEQUAL ] : new BigNumber ( 1 << 3 ) , [ Op . LT ] : new BigNumber ( 1 << 3 ) , [ Op . LE ] : new BigNumber ( 1 << 3 ) , [ Op . GT ] : new BigNumber ( 1 << 3 ) , [ Op . GE ] : new BigNumber ( 1 << 3 ) , [ Op . MIN ] : new BigNumber ( 1 << 3 ) , [ Op . MAX ] : new BigNumber ( 1 << 3 ) , [ Op . WITHIN ] : new BigNumber ( 1 << 3 ) , [ Op . PACK ] : new BigNumber ( 1 << 11 ) , [ Op . UNPACK ] : new BigNumber ( 1 << 11 ) , [ Op . NEWARRAY0 ] : new BigNumber ( 1 << 4 ) , [ Op . NEWARRAY ] : new BigNumber ( 1 << 9 ) , [ Op . NEWARRAY_T ] : new BigNumber ( 1 << 9 ) , [ Op . NEWSTRUCT0 ] : new BigNumber ( 1 << 4 ) , [ Op . NEWSTRUCT ] : new BigNumber ( 1 << 9 ) , [ Op . NEWMAP ] : new BigNumber ( 1 << 3 ) , [ Op . SIZE ] : new BigNumber ( 1 << 2 ) , [ Op . HASKEY ] : new BigNumber ( 1 << 6 ) , [ Op . KEYS ] : new BigNumber ( 1 << 4 ) , [ Op . VALUES ] : new BigNumber ( 1 << 13 ) , [ Op . PICKITEM ] : new BigNumber ( 1 << 6 ) , [ Op . APPEND ] : new BigNumber ( 1 << 13 ) , [ Op . SETITEM ] : new BigNumber ( 1 << 13 ) , [ Op . REVERSEITEMS ] : new BigNumber ( 1 << 13 ) , [ Op . REMOVE ] : new BigNumber ( 1 << 4 ) , [ Op . CLEARITEMS ] : new BigNumber ( 1 << 4 ) , [ Op . POPITEM ] : new BigNumber ( 1 << 4 ) , [ Op . ISNULL ] : new BigNumber ( 1 << 1 ) , [ Op . ISTYPE ] : new BigNumber ( 1 << 1 ) , [ Op . CONVERT ] : new BigNumber ( 1 << 13 ) , [ Op . PRINT ] : new BigNumber ( 0 ) } ; export const getOpCodePrice = ( value ) : BigNumber => { const fee = opCodePrices [ value ] ; if ( fee === undefined ) { throw new Error ( `Unknown Opcode: ${ value }` ) ; } return fee ; } ; export const signatureContractCost = getOpCodePrice ( Op . PUSHDATA1 ) . multipliedBy ( 2 ) . plus ( getOpCodePrice ( Op . SYSCALL ) ) . plus ( CheckSigPrice ) ; export const multiSignatureContractCost = ( m , n ) => getOpCodePrice ( Op . PUSHDATA1 ) . multipliedBy ( m + n ) . plus ( getOpCodePrice ( new ScriptBuilder ( ) . emitPushInt ( m ) . build ( ) [ 0 ] ) ) . plus ( getOpCodePrice ( new ScriptBuilder ( ) . emitPushInt ( n ) . build ( ) [ 0 ] ) ) . plus ( getOpCodePrice ( Op . SYSCALL ) ) . plus ( CheckSigPrice . multipliedBy ( n ) ) ;