import * as _ from 'lodash' ; import { utils } from 'ethers' ; import { Wallet } from './types' ; import * as ethUtils from 'ethereumjs-util' ; import { isSupportedBaseQuote , getSupportedTokens } from './utils/token' ; export const isValidWallet = ( wallet ) : boolean => { if ( ! wallet ) { console . error ( 'wallet not found' ) ; return false ; } const { address , privateKey } = wallet ; if ( ! utils . isAddress ( address ) ) { console . error ( `address(${ address }) is not valid` ) ; return false ; } const addr = ethUtils . privateToAddress ( Buffer . from ( privateKey . startsWith ( '0x' ) ? privateKey . slice ( 2 ) : privateKey , 'hex' ) ) ; return `0x${ addr . toString ( 'hex' ) }` . toLowerCase ( ) === address . toLowerCase ( ) ; } ; function validateRequiredFields ( values , fields ) { return fields . every ( ( key ) => values [ key ] && typeof values [ key ] === 'string' ) ; } export function validateNewOrderRequest ( amount , uniqId , userAddress ) { let errorMessage = null ; if ( _ . isNaN ( + amount ) || + amount <= 0 ) { errorMessage = `order's amount ${ amount } must be a number > 0` ; } else if ( ! _ . isString ( uniqId ) ) { errorMessage = `query.uniqId ${ uniqId }  must be string type` ; } else if ( ! utils . isAddress ( userAddress ) ) { errorMessage = `userAddress:${ userAddress } is not a valid address` ; } return errorMessage ; } export function validateRequest ( params ) { const { side , base , quote , amount } = params ; let errorMessage = null ; if ( ! validateRequiredFields ( params , [ 'base' , 'quote' , 'side' ] ) ) { errorMessage = `base, quote, side must be string type, with ${ JSON . stringify ( params ) }` ; } else if ( side !== 'BUY' && side !== 'SELL' ) { errorMessage = "side must be one of 'BUY' and 'SELL'" ; } else if ( ! isSupportedBaseQuote ( getSupportedTokens ( ) , base , quote ) ) { errorMessage = `Don't support { Base:${ base }, Quote:${ quote } } trade` ; } else if ( amount && ( _ . isNaN ( + amount ) || + amount < 0 ) ) { errorMessage = `getRate's amount ${ amount } must be a number >= 0, or you should not send this amount parameter` ; } return errorMessage ; }