import * as radspec from 'radspec' ; import { providers as ethersProviders } from 'ethers' ; import { addressesEqual } from '../address' ; import { findAppMethodFromData } from '../app' ; import { filterAndDecodeAppUpgradeIntents } from '../intent' ; import { Abi , AppMethod , StepDecoded , StepDescribed } from '../../types' ; import App from '../../entities/App' ; interface FoundMethod { method ? : AppMethod ; abi ? : Abi ; } /**
 * Attempt to describe intent via radspec.
 */ export async function tryEvaluatingRadspec ( intent , installedApps , provider ) { const app = installedApps . find ( ( app ) => addressesEqual ( app . address , intent . to ) ) ; const appsToSearch = app ? [ app ] : installedApps ; const foundMethod = appsToSearch . reduce < FoundMethod | undefined > ( ( found , app ) => { if ( found ) { return found ; } const method = findAppMethodFromData ( app , intent . data ) ; if ( method ) { return { method , abi : method . abi ? [ method . abi ] : app . abi , } ; } } , undefined ) ; const { abi , method } = foundMethod || { } ; let evaluatedNotice ; if ( method && method . notice ) { try { evaluatedNotice = await radspec . evaluate ( method . notice , { abi , transaction : intent , } , { provider : provider } ) ; } catch ( err ) { console . error ( `Could not evaluate a description for given transaction data: ${ intent . data }` , err ) ; } } return { ... intent , description : evaluatedNotice } ; } param { Object } intent param { Object } wrapper return { Promise < Object > } export async function tryDescribingUpdateAppIntent ( intent , installedApps ) { const upgradeIntentParams = filterAndDecodeAppUpgradeIntents ( [ intent ] , installedApps ) [ 0 ] ; if ( Array . isArray ( upgradeIntentParams ) && upgradeIntentParams . length === 0 ) return undefined ; const { appId , appAddress } = upgradeIntentParams ; const app = installedApps . find ( ( app ) => app . address === appAddress ) ; const repo = await app ?. repo ( ) ; return { ... intent , description : `Upgrade ${ appId } app instances to v${ repo ?. lastVersion }` , } ; } export { postprocessRadspecDescription } from './postprocess' ;