import { BigNumber } from "ethers" ; function padWithZeros ( value , targetLength ) { return ( "0" . repeat ( targetLength ) + value ) . slice ( - targetLength ) ; } export function encodeTransaction ( operation , to , value , data ) { let _operation ; if ( operation === 0 ) { _operation = "00" ; } else if ( operation === 1 ) { _operation = "01" ; } else { throw Error ( `Operation ${ operation } is not a correct value` ) ; } let _to = to ; if ( to . startsWith ( "0x" ) ) { _to = _to . slice ( 2 ) ; } _to = padWithZeros ( _to , 20 * 2 ) ; const _value = padWithZeros ( BigNumber . from ( value ) . toHexString ( ) . slice ( 2 ) , 32 * 2 ) ; let _data = data ; if ( data . startsWith ( "0x" ) ) { _data = _data . slice ( 2 ) ; } if ( _data . length % 2 !== 0 ) { _data = "0" + _data ; } const _dataLength = padWithZeros ( ( _data . length / 2 ) . toString ( 16 ) , 32 * 2 ) ; return "0x" + [ _operation , _to , _value , _dataLength , _data , ] . join ( "" ) ; }