/**
 * The input to the contract at each step. Includes
 * input and caller parsed already, the transaction
 * details, and the block information of the transaction.
 *
 */ export interface ContractInteraction { input : any ; caller : string ; } /**
 * The return value for the contracts handle() method.
 */ export interface ContractHandlerResult { result ? : any ; state ? : any ; } /**
 * Function type for the contracts handle() method.
 */ export type ContractHandler = ( state : any , interaction : ContractInteraction ) => ContractHandlerResult | Promise < ContractHandlerResult > ; /**
 * The end result of a single contract interaction.
 * This includes exceptions caught.
 */ export interface ContractInteractionResult { type : 'ok' | 'error' | 'exception' ; result : any ; state : any ; } param contractSrc param input param state param caller export async function execute ( handler , interaction , state ) { try { const stateCopy = JSON . parse ( JSON . stringify ( state ) ) ; const result = await handler ( stateCopy , interaction ) ; if ( result && ( result . state || result . result ) ) { return { type : 'ok' , result : result . result , state : result . state || state , } ; } throw new Error ( `Unexpected result from contract: ${ JSON . stringify ( result ) }` ) ; } catch ( err ) { if ( err . name === 'ContractError' ) { return { type : 'error' , result : err . message , state , } ; } return { type : 'exception' , result : `${ ( err && err . stack ) || ( err && err . message ) }` , state , } ; } }