import { Contract , providers as ethersProvider , utils as ethersUtils , } from 'ethers' ; import { appIntent } from '../utils/intent' ; import { Abi , AragonArtifact , AragonManifest , Metadata , AppData , PathOptions , } from '../types' ; import ForwardingPath from './ForwardingPath' ; import Organization from './Organization' ; import Repo from './Repo' ; import Role from './Role' ; import { resolveArtifact , resolveManifest } from '../utils/metadata' ; import IOrganizationConnector from '../connections/IOrganizationConnector' ; export default class App { #metadata : Metadata ; readonly address : string ; readonly appId : string ; readonly codeAddress : string ; readonly contentUri ? : string ; readonly isForwarder ? : boolean ; readonly isUpgradeable ? : boolean ; readonly kernelAddress : string ; readonly name ? : string ; readonly organization : Organization ; readonly registry ? : string ; readonly registryAddress : string ; readonly repoAddress ? : string ; readonly version ? : string ; constructor ( data , metadata , organization ) { this . #metadata = metadata ; this . address = data . address ; this . appId = data . appId ; this . codeAddress = data . codeAddress ; this . contentUri = data . contentUri ; this . isForwarder = data . isForwarder ; this . isUpgradeable = data . isUpgradeable ; this . kernelAddress = data . kernelAddress ; this . name = data . name ; this . organization = organization ; this . registry = data . registry ; this . registryAddress = data . registryAddress ; this . repoAddress = data . repoAddress ; this . version = data . version ; } static async create ( data , organization ) { const artifact = await resolveArtifact ( organization . connection . ipfs , data ) ; const manifest = await resolveManifest ( organization . connection . ipfs , data ) ; return new App ( data , [ artifact , manifest ] , organization ) ; } private orgConnector ( ) { return this . organization . connection . orgConnector ; } get provider ( ) : ethersProvider . Provider { return this . organization . connection . ethersProvider ; } get artifact ( ) : AragonArtifact { return this . #metadata [ 0 ] as AragonArtifact ; } get manifest ( ) : AragonManifest { return this . #metadata [ 1 ] as AragonManifest ; } get abi ( ) : Abi { return this . artifact . abi ; } async repo ( ) { return this . orgConnector ( ) . repoForApp ( this . organization , this . address ) ; } async roles ( ) { return this . orgConnector ( ) . rolesForAddress ( this . organization , this . address ) ; } toJSON ( ) { return { ... this , organization : null , } ; } ethersContract ( ) { if ( ! this . abi ) { throw new Error ( `No ABI specified in app for ${ this . address }. Make sure the metada for the app is available` ) ; } return new Contract ( this . address , this . abi , this . provider ) ; } ethersInterface ( ) { if ( ! this . abi ) { throw new Error ( `No ABI specified in app for ${ this . address }. Make sure the metada for the app is available` ) ; } return new ethersUtils . Interface ( this . abi ) ; } param { string } methodSignature param { Array < * > } params param { Object } options return { Promise < ForwardingPath > } async intent ( methodSignature , params , options = { } ) { const sender = options . actAs || this . organization . connection . actAs ; if ( ! sender ) { throw new Error ( `No sender address specified. Use 'actAs' option or set one as default on your organization connection.` ) ; } const installedApps = await this . organization . apps ( ) ; return appIntent ( sender , this , methodSignature , params , installedApps , this . provider ) ; } }