import { add0x , getLogger , serializeObject } from '@pigi/core-utils' ; import { CreateAndTransferTransition , isSwapTransaction , isTransferTransaction , SignedTransaction , State , StateReceipt , Swap , SwapTransition , RollupTransaction , Transfer , TransferTransition , RollupTransition , isSwapTransition , isTransferTransition , isCreateAndTransferTransition , } from '../../types' ; import { abi , createAndTransferTransitionAbiTypes , signedTransactionAbiTypes , stateAbiTypes , stateReceiptAbiTypes , swapAbiTypes , swapTransitionAbiTypes , transferAbiTypes , transferTransitionAbiTypes , } from './common' ; import { PIGI_TOKEN_TYPE , UNI_TOKEN_TYPE } from '../utils' ; const log = getLogger ( 'abiEncoders' ) ; param signedTransaction returns export const abiEncodeSignedTransaction = ( signedTransaction ) : string => { log . debug ( `ABI encoding SignedTransaction: ${ serializeObject ( signedTransaction ) }` ) ; return abi . encode ( signedTransactionAbiTypes , [ signedTransaction . signature , abiEncodeTransaction ( signedTransaction . transaction ) , ] ) ; } ; param transaction returns export const abiEncodeTransaction = ( transaction ) : string => { if ( isSwapTransaction ( transaction ) ) { return abiEncodeSwap ( transaction ) ; } else if ( isTransferTransaction ( transaction ) ) { return abiEncodeTransfer ( transaction ) ; } const message = `Unknown transaction type: ${ JSON . stringify ( transaction ) }` ; log . error ( message ) ; throw Error ( message ) ; } ; param transition returns export const abiEncodeTransition = ( transition ) : string => { if ( isSwapTransition ( transition ) ) { return abiEncodeSwapTransition ( transition ) ; } else if ( isTransferTransition ( transition ) ) { return abiEncodeTransferTransition ( transition ) ; } else if ( isCreateAndTransferTransition ( transition ) ) { return abiEncodeCreateAndTransferTransition ( transition ) ; } const message = `Unknown transition type: ${ JSON . stringify ( transition ) }` ; log . error ( message ) ; throw Error ( message ) ; } ; param state returns export const abiEncodeState = ( state ) : string => { log . debug ( `ABI encoding State: ${ serializeObject ( state ) }` ) ; return abi . encode ( stateAbiTypes , [ state . pubkey , state . balances [ UNI_TOKEN_TYPE ] , state . balances [ PIGI_TOKEN_TYPE ] , ] ) ; } ; param stateReceipt returns export const abiEncodeStateReceipt = ( stateReceipt ) : string => { log . debug ( `ABI encoding StateReceipt: ${ serializeObject ( stateReceipt ) }` ) ; return abi . encode ( stateReceiptAbiTypes , [ add0x ( stateReceipt . stateRoot ) , stateReceipt . blockNumber , stateReceipt . transitionIndex , stateReceipt . slotIndex , stateReceipt . inclusionProof . map ( ( hex ) => add0x ( hex ) ) , abiEncodeState ( stateReceipt . state ) , ] ) ; } ; /*********************
 * PRIVATE FUNCTIONS *
 *********************/ param transfer returns const abiEncodeTransfer = ( transfer ) : string => { log . debug ( `ABI encoding Transfer: ${ serializeObject ( transfer ) }` ) ; return abi . encode ( transferAbiTypes , [ transfer . sender , transfer . recipient , transfer . tokenType , transfer . amount , ] ) ; } ; param swap returns const abiEncodeSwap = ( swap ) : string => { log . debug ( `ABI encoding Swap: ${ serializeObject ( swap ) }` ) ; return abi . encode ( swapAbiTypes , [ swap . sender , swap . tokenType , swap . inputAmount , swap . minOutputAmount , swap . timeout , ] ) ; } ; param trans returns const abiEncodeSwapTransition = ( trans ) : string => { log . debug ( `ABI encoding SwapTransition: ${ serializeObject ( trans ) }` ) ; return abi . encode ( swapTransitionAbiTypes , [ add0x ( trans . stateRoot ) , trans . senderSlotIndex , trans . uniswapSlotIndex , trans . tokenType , trans . inputAmount , trans . minOutputAmount , trans . timeout , add0x ( trans . signature ) , ] ) ; } ; param trans returns const abiEncodeTransferTransition = ( trans ) : string => { log . debug ( `ABI encoding TransferTransition: ${ serializeObject ( trans ) }` ) ; return abi . encode ( transferTransitionAbiTypes , [ add0x ( trans . stateRoot ) , trans . senderSlotIndex , trans . recipientSlotIndex , trans . tokenType , trans . amount , add0x ( trans . signature ) , ] ) ; } ; param trans returns const abiEncodeCreateAndTransferTransition = ( trans ) : string => { log . debug ( `ABI encoding CreateAndTransferTransition: ${ serializeObject ( trans ) }` ) ; return abi . encode ( createAndTransferTransitionAbiTypes , [ add0x ( trans . stateRoot ) , trans . senderSlotIndex , trans . recipientSlotIndex , trans . createdAccountPubkey , trans . tokenType , trans . amount , add0x ( trans . signature ) , ] ) ; } ;