import { getLogger , remove0x , serializeObject } from '@pigi/core-utils' ; import { CreateAndTransferTransition , InvalidTokenTypeError , SignedTransaction , State , StateReceipt , Swap , SwapTransition , TokenType , RollupTransaction , Transfer , TransferTransition , RollupTransition , } 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 abiEncoded returns export const parseSignedTransactionFromABI = ( abiEncoded ) : SignedTransaction => { log . debug ( `ABI decoding SignedTransaction: ${ serializeObject ( abiEncoded ) }` ) ; const [ signature , tx ] = abi . decode ( signedTransactionAbiTypes , abiEncoded ) ; return { signature , transaction : parseTransactionFromABI ( tx ) , } ; } ; param abiEncoded returns export const parseTransactionFromABI = ( abiEncoded ) : RollupTransaction => { try { return parseSwapFromABI ( abiEncoded ) ; } catch ( err ) { return parseTransferFromABI ( abiEncoded ) ; } } ; param abiEncoded returns export const parseTransitionFromABI = ( abiEncoded ) : RollupTransition => { try { return parseSwapTransitionFromABI ( abiEncoded ) ; } catch ( err ) { try { return parseTransferTransitionFromABI ( abiEncoded ) ; } catch ( e ) { return parseCreateAndTransferTransitionFromABI ( abiEncoded ) ; } } } ; param abiEncoded returns export const parseStateFromABI = ( abiEncoded ) : State => { log . debug ( `ABI decoding State: ${ serializeObject ( abiEncoded ) }` ) ; const [ pubkey , uniBalance , pigiBalance ] = abi . decode ( stateAbiTypes , abiEncoded ) ; return { pubkey , balances : { [ UNI_TOKEN_TYPE ] : uniBalance , [ PIGI_TOKEN_TYPE ] : pigiBalance , } , } ; } ; param abiEncoded returns export const parseStateReceiptFromABI = ( abiEncoded ) : StateReceipt => { log . debug ( `ABI decoding StateReceipt: ${ serializeObject ( abiEncoded ) }` ) ; const [ stateRoot , blockNumber , transitionIndex , slotIndex , inclusionProof , state , ] = abi . decode ( stateReceiptAbiTypes , abiEncoded ) ; return { stateRoot : remove0x ( stateRoot ) , blockNumber : + blockNumber , transitionIndex : + transitionIndex , slotIndex , inclusionProof : inclusionProof . map ( ( hex ) => remove0x ( hex ) ) , state : parseStateFromABI ( state ) , } ; } ; /*********************
 * PRIVATE FUNCTIONS *
 *********************/ const getTokenType = ( tokenType ) : TokenType => { const tokenTypeNumber = + tokenType ; if ( tokenTypeNumber !== UNI_TOKEN_TYPE && tokenTypeNumber !== PIGI_TOKEN_TYPE ) { log . error ( `Received invalid token type parsing ABI encoded input -- this should never happen. Token Type: ${ tokenType }` ) ; throw new InvalidTokenTypeError ( tokenTypeNumber ) ; } return tokenTypeNumber ; } ; param abiEncoded returns const parseTransferFromABI = ( abiEncoded ) : Transfer => { log . debug ( `ABI decoding Transfer: ${ serializeObject ( abiEncoded ) }` ) ; const [ sender , recipient , tokenType , amount ] = abi . decode ( transferAbiTypes , abiEncoded ) ; return { sender , recipient , tokenType : getTokenType ( tokenType ) , amount , } ; } ; param abiEncoded returns const parseSwapFromABI = ( abiEncoded ) : Swap => { log . debug ( `ABI decoding Swap: ${ serializeObject ( abiEncoded ) }` ) ; const [ sender , tokenType , inputAmount , minOutputAmount , timeout ] = abi . decode ( swapAbiTypes , abiEncoded ) ; return { sender , tokenType : getTokenType ( tokenType ) , inputAmount , minOutputAmount , timeout : + timeout , } ; } ; param abiEncoded returns const parseSwapTransitionFromABI = ( abiEncoded ) : SwapTransition => { log . debug ( `ABI decoding SwapTransition: ${ serializeObject ( abiEncoded ) }` ) ; const [ stateRoot , senderSlot , recipientSlot , tokenType , inputAmount , minOutputAmount , timeout , signature , ] = abi . decode ( swapTransitionAbiTypes , abiEncoded ) ; return { stateRoot : remove0x ( stateRoot ) , senderSlotIndex : senderSlot , uniswapSlotIndex : recipientSlot , tokenType : getTokenType ( tokenType ) , inputAmount , minOutputAmount , timeout : + timeout , signature , } ; } ; param abiEncoded returns const parseTransferTransitionFromABI = ( abiEncoded ) : TransferTransition => { log . debug ( `ABI decoding TransferTransition: ${ serializeObject ( abiEncoded ) }` ) ; const [ stateRoot , senderSlot , recipientSlot , tokenType , amount , signature , ] = abi . decode ( transferTransitionAbiTypes , abiEncoded ) ; return { stateRoot : remove0x ( stateRoot ) , senderSlotIndex : senderSlot , recipientSlotIndex : recipientSlot , tokenType : getTokenType ( tokenType ) , amount , signature , } ; } ; param abiEncoded returns const parseCreateAndTransferTransitionFromABI = ( abiEncoded ) : CreateAndTransferTransition => { log . debug ( `ABI decoding CreateAndTransferTransition: ${ serializeObject ( abiEncoded ) }` ) ; const [ stateRoot , senderSlot , recipientSlot , createdAccountPubkey , tokenType , amount , signature , ] = abi . decode ( createAndTransferTransitionAbiTypes , abiEncoded ) ; return { stateRoot : remove0x ( stateRoot ) , senderSlotIndex : senderSlot , recipientSlotIndex : recipientSlot , createdAccountPubkey , tokenType : getTokenType ( tokenType ) , amount , signature , } ; } ;