Skip to content

Commit

Permalink
Fix: solve metadata for Roles + TransactionRequest#children (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xGabi authored Jul 7, 2020
1 parent 03feb33 commit 1ea7377
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 55 deletions.
2 changes: 1 addition & 1 deletion docs/api-reference/transaction-request.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ An object describing a transaction that can be signed by a library like [ethers.

| Name | Type | Description |
| :--- | :--- | :--- |
| `children` | `TransactionRequest[]` | List of the next transactions in the path. |
| `descriptionAnnotated` | `Annotation[]` | List of the Radspec description bindings with the properties `{ type: string, value: any }`. |
| `description` | `string` | Radspec description for the transaction. |
| `data` | `String` | Transaction data. |
| `from` | `String` | Address to use as default sender. |
| `to` | `String` | Target address or ENS name. |

27 changes: 3 additions & 24 deletions packages/connect-core/src/entities/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,8 @@ import {
Abi,
AppIntent,
} from '../types'
import { resolveMetadata } from '../utils/metadata'
import { resolveManifest, resolveArtifact } from '../utils/metadata'
import { ConnectorInterface } from '../connections/ConnectorInterface'
import {
getApmInternalAppInfo,
getAragonOsInternalAppInfo,
} from '../utils/overrides/index'
import { hasAppInfo } from '../utils/overrides/interfaces'

// TODO:
// [ ] (ipfs) contentUrl String The HTTP URL of the app content. Uses the IPFS HTTP provider. E.g. http://gateway.ipfs.io/ipfs/QmdLEDDfi…/ (ContentUri passing through the resolver)
Expand Down Expand Up @@ -78,24 +73,8 @@ export default class App extends CoreEntity {
data: AppData,
connector: ConnectorInterface
): Promise<App> {
let artifact: AragonArtifact
if (hasAppInfo(data.appId, 'apm')) {
artifact = getApmInternalAppInfo(data.appId)
} else if (hasAppInfo(data.appId, 'aragon')) {
artifact = getAragonOsInternalAppInfo(data.appId)
} else {
artifact = await resolveMetadata(
'artifact.json',
data.contentUri || undefined,
data.artifact
)
}

const manifest: AragonManifest = await resolveMetadata(
'manifest.json',
data.contentUri || undefined,
data.manifest
)
const artifact = await resolveArtifact(data)
const manifest = await resolveManifest(data)

const metadata: Metadata = [artifact, manifest]

Expand Down
5 changes: 5 additions & 0 deletions packages/connect-core/src/entities/Organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ export default class Organization {
return this.#address || '' // The || '' should never happen but TypeScript requires it.
}

get provider(): ethers.providers.Provider {
this.checkConnected()
return this.#provider
}

///////// APPS ///////////

async app(filters?: AppFiltersParam): Promise<App> {
Expand Down
11 changes: 3 additions & 8 deletions packages/connect-core/src/entities/Repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
Metadata,
AragonArtifactRole,
} from '../types'
import { resolveMetadata } from '../utils/metadata'
import { resolveMetadata, resolveManifest } from '../utils/metadata'
import { ConnectorInterface } from '../connections/ConnectorInterface'

export interface RepoData {
Expand Down Expand Up @@ -46,17 +46,12 @@ export default class Repo extends CoreEntity {
data: RepoData,
connector: ConnectorInterface
): Promise<Repo> {
const artifact: AragonArtifact = await resolveMetadata(
const artifact = await resolveMetadata(
'artifact.json',
data.contentUri || undefined,
data.artifact
)

const manifest: AragonManifest = await resolveMetadata(
'manifest.json',
data.contentUri || undefined,
data.manifest
)
const manifest = await resolveManifest(data)

const metadata: Metadata = [artifact, manifest]

Expand Down
12 changes: 5 additions & 7 deletions packages/connect-core/src/entities/Role.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import CoreEntity from './CoreEntity'
import Permission, { PermissionData } from './Permission'
import { AragonArtifact, Metadata } from '../types'
import { resolveMetadata } from '../utils/metadata'
import { resolveArtifact } from '../utils/metadata'
import { ConnectorInterface } from '../connections/ConnectorInterface'

export interface RoleData {
appAddress: string
appId: string
artifact?: string | null
contentUri?: string | null
hash: string
Expand All @@ -15,6 +16,7 @@ export interface RoleData {

export default class Role extends CoreEntity {
readonly appAddress!: string
readonly appId!: string
readonly description?: string
readonly hash!: string
readonly params?: string[]
Expand All @@ -31,7 +33,7 @@ export default class Role extends CoreEntity {

const { roles } = metadata[0] as AragonArtifact

const role = roles.find(role => role.bytes === data.hash)
const role = roles?.find(role => role.bytes === data.hash)

this.appAddress = data.appAddress
this.description = role?.name
Expand All @@ -48,11 +50,7 @@ export default class Role extends CoreEntity {
data: RoleData,
connector: ConnectorInterface
): Promise<Role> {
const artifact: AragonArtifact = await resolveMetadata(
'artifact.json',
data.contentUri || undefined,
data.artifact
)
const artifact = await resolveArtifact(data)

const metadata: Metadata = [artifact]

Expand Down
4 changes: 2 additions & 2 deletions packages/connect-core/src/transactions/TransactionRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ export interface Annotation {
value: any
}

// TODO: Handle children

export default class TransactionRequest {
readonly children?: TransactionRequest[]
readonly description?: string
readonly descriptionAnnotated?: Annotation[]
readonly data!: string
readonly from?: string
readonly to!: string

constructor(data: TransactionRequestData) {
this.children = data.children
this.description = data.description
this.descriptionAnnotated = data.descriptionAnnotated
this.data = data.data
Expand Down
6 changes: 3 additions & 3 deletions packages/connect-core/src/utils/descriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ export async function describeTransaction(
provider
)
}

// eslint-disable-next-line no-empty
} catch (_) {}
} catch (err) {
throw new Error(`Could not describe transaction: ${err}`)
}

return new TransactionRequest({
...transaction,
Expand Down
35 changes: 35 additions & 0 deletions packages/connect-core/src/utils/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { ethers } from 'ethers'

import { AppData } from '../entities/App'
import { RepoData } from '../entities/Repo'
import { RoleData } from '../entities/Role'
import {
getApmInternalAppInfo,
getAragonOsInternalAppInfo,
hasAppInfo,
} from './overrides/index'
import { DEFAULT_IPFS_GATEWAY } from '../params'
import { AragonArtifact, AragonManifest } from '../types'

export function parseMetadata(name: string, metadata: string): any {
let parsedMetaData
Expand Down Expand Up @@ -39,3 +48,29 @@ export async function resolveMetadata(
if (contentUri) return fetchMetadata(fileName, contentUri)
return {}
}

export async function resolveManifest(
data: AppData | RepoData
): Promise<AragonManifest> {
return resolveMetadata(
'manifest.json',
data.contentUri || undefined,
data.manifest
)
}

export async function resolveArtifact(
data: AppData | RoleData
): Promise<AragonArtifact> {
if (hasAppInfo(data.appId, 'apm')) {
return getApmInternalAppInfo(data.appId)
}
if (hasAppInfo(data.appId, 'aragon')) {
return getAragonOsInternalAppInfo(data.appId)
}
return resolveMetadata(
'artifact.json',
data.contentUri || undefined,
data.artifact
)
}
2 changes: 2 additions & 0 deletions packages/connect-core/src/utils/overrides/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ export function getAragonOsInternalAppInfo(appId: string): AragonArtifact {
export function getApmInternalAppInfo(appId: string): AragonArtifact {
return getAppInfo(appId, 'apm')
}

export { hasAppInfo, getAppInfo } from './interfaces'
16 changes: 6 additions & 10 deletions packages/connect-thegraph/src/parsers/roles.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { Role, RoleData, PermissionData } from '@aragon/connect-core'
import { QueryResult } from '../types'

async function _parseRole(
role: any,
connector: any,
contentUri: string | null,
artifact?: string | null
): Promise<Role> {
async function _parseRole(role: any, app: any, connector: any): Promise<Role> {
const grantees =
role?.grantees &&
role?.grantees.map(
Expand All @@ -33,8 +28,9 @@ async function _parseRole(
manager: role.manager,
hash: role.roleHash,
grantees,
artifact,
contentUri,
appId: app.appId,
artifact: app.version?.artifact,
contentUri: app?.contentUri,
}

return Role.create(roleData, connector)
Expand All @@ -51,7 +47,7 @@ export async function parseRole(
throw new Error('Unable to parse role.')
}

return _parseRole(role, connector, app?.contentUri, app.version?.artifact)
return _parseRole(role, app, connector)
}

export async function parseRoles(
Expand All @@ -67,7 +63,7 @@ export async function parseRoles(

return Promise.all(
roles.map(async (role: any) => {
return _parseRole(role, connector, app?.contentUri, app.version?.artifact)
return _parseRole(role, app, connector)
})
)
}

0 comments on commit 1ea7377

Please sign in to comment.