Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: add configuration to support radspec helprs and functions #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 4 additions & 73 deletions examples/nodejs/src/describe-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,81 +6,12 @@ const NETWORK = 100
async function main() {
const org = await connect(ORG_ADDRESS, 'thegraph', { network: NETWORK })

const [apps, apps2, apps3, apps4, apps5, apps6] = await Promise.all([
org.apps(),
org.apps(),
org.apps(),
org.apps(),
org.apps(),
org.apps(),
])
// const apps = await org.apps()
const script =
'0x00000001a377585abed3e943e58174b55558a2482894ce2000000064beabacc80000000000000000000000003a97704a1b25f08aa230ae53b352e2e72ef528430000000000000000000000006626528de0c75ccc7a0d24f2d24b99060f74edee00000000000000000000000000000000000000000000001b1ae4d6e2ef500000'

// const script =
// '0x00000001a377585abed3e943e58174b55558a2482894ce2000000064beabacc80000000000000000000000003a97704a1b25f08aa230ae53b352e2e72ef528430000000000000000000000006626528de0c75ccc7a0d24f2d24b99060f74edee00000000000000000000000000000000000000000000001b1ae4d6e2ef500000'
const description = await org.describeScript(script)

// const apps2 = await org.apps()
// const apps3 = await org.apps()
// const apps4 = await org.apps()
// const apps5 = await org.apps()
// const apps6 = await org.apps()

// const description = await org.describeScript(script)

console.log(
'First: ',
apps.map((app) => {
return {
name: app.name,
artifactName: app.artifact.appName,
}
})
)
console.log(
'Second: ',
apps2.map((app) => {
return {
name: app.name,
artifactName: app.artifact.appName,
}
})
)
console.log(
'Second: ',
apps3.map((app) => {
return {
name: app.name,
artifactName: app.artifact.appName,
}
})
)
console.log(
'Second: ',
apps4.map((app) => {
return {
name: app.name,
artifactName: app.artifact.appName,
}
})
)
console.log(
'Second: ',
apps5.map((app) => {
return {
name: app.name,
artifactName: app.artifact.appName,
}
})
)
console.log(
'Second: ',
apps6.map((app) => {
return {
name: app.name,
artifactName: app.artifact.appName,
}
})
)
console.log(JSON.stringify(description, null, 2))
}

main()
Expand Down
2 changes: 1 addition & 1 deletion packages/connect-core/src/entities/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export default class App {
methodSignature,
params,
installedApps,
this.provider
this.organization.connection
)
}
}
13 changes: 6 additions & 7 deletions packages/connect-core/src/entities/ForwardingPath.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Provider } from '@ethersproject/providers'

import { buildApprovePreTransactions } from '../utils/transactions'
import {
ForwardingPathData,
Expand All @@ -12,6 +10,7 @@ import ForwardingPathDescription, {
} from '../utils/descriptor/index'
import App from './App'
import Transaction from './Transaction'
import { ConnectionContext } from '..'

const normalizePreTransactions = (
preTransactions: (Transaction | TransactionData)[]
Expand All @@ -25,18 +24,18 @@ const normalizePreTransactions = (

export default class ForwardingPath {
#installedApps: App[]
#provider: Provider
#connection: ConnectionContext
readonly destination: App
readonly path: Transaction[]
readonly transactions: Transaction[]

constructor(
data: ForwardingPathData,
installedApps: App[],
provider: Provider
connection: ConnectionContext
) {
this.#installedApps = installedApps
this.#provider = provider
this.#connection = connection
this.destination = data.destination
this.path = data.path
this.transactions = data.transactions
Expand All @@ -59,7 +58,7 @@ export default class ForwardingPath {
description = await describePath(
this.path,
this.#installedApps,
this.#provider
this.#connection
)
// eslint-disable-next-line no-empty
} catch (_) {}
Expand All @@ -75,7 +74,7 @@ export default class ForwardingPath {
return buildApprovePreTransactions(
this.transactions[0],
tokenData,
this.#provider
this.#connection
)
}

Expand Down
10 changes: 3 additions & 7 deletions packages/connect-core/src/entities/Organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default class Organization {
this.connection = connection
}

get location() {
get location(): string {
return this.connection.orgLocation
}

Expand Down Expand Up @@ -169,7 +169,7 @@ export default class Organization {
const describedSteps = await describePath(
decodeForwardingPath(script),
installedApps,
this.connection.ethersProvider
this.connection
)

return new ForwardingPathDescription(describedSteps, installedApps)
Expand All @@ -179,10 +179,6 @@ export default class Organization {
async describeTransaction(
transaction: Transaction
): Promise<PostProcessDescription> {
return describeTransaction(
transaction,
await this.apps(),
this.connection.ethersProvider
)
return describeTransaction(transaction, await this.apps(), this.connection)
}
}
6 changes: 6 additions & 0 deletions packages/connect-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,11 @@ export type IpfsResolver = {
json: (cid: string, path?: string) => Promise<object>
}

export type RadspecOptions = {
userFunctions?: Record<string, string>
userHelpers?: Record<string, unknown>
}

export type ConnectionContext = {
actAs: Address | null
ethereumProvider: object | null
Expand All @@ -264,6 +269,7 @@ export type ConnectionContext = {
orgAddress: Address
orgConnector: IOrganizationConnector
orgLocation: string
radspec?: RadspecOptions
verbose: boolean
}

Expand Down
21 changes: 12 additions & 9 deletions packages/connect-core/src/utils/descriptor/describe.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
/* eslint-disable @typescript-eslint/no-use-before-define */
/* eslint-disable no-empty */
import { Provider } from '@ethersproject/providers'

import {
tryEvaluatingRadspec,
tryDescribingUpdateAppIntent,
postprocessRadspecDescription,
} from '../radspec/index'
import { StepDecoded, StepDescribed, PostProcessDescription } from '../../types'
import { ConnectionContext } from '../..'
import App from '../../entities/App'
import Transaction from '../../entities/Transaction'

export async function describeStep(
step: StepDecoded,
installedApps: App[],
provider: Provider
connection: ConnectionContext
): Promise<StepDescribed> {
let decoratedStep
// TODO: Add intent Basket support
Expand All @@ -28,7 +27,11 @@ export async function describeStep(
// Finally, if the step wasn't handled yet, evaluate via radspec normally
if (!decoratedStep) {
try {
decoratedStep = await tryEvaluatingRadspec(step, installedApps, provider)
decoratedStep = await tryEvaluatingRadspec(
step,
installedApps,
connection
)
} catch (err) {}
}

Expand All @@ -49,7 +52,7 @@ export async function describeStep(
decoratedStep.children = await describePath(
decoratedStep.children,
installedApps,
provider
connection
)
}

Expand All @@ -63,17 +66,17 @@ export async function describeStep(
export async function describePath(
path: StepDecoded[],
installedApps: App[],
provider: Provider
connection: ConnectionContext
): Promise<StepDescribed[]> {
return Promise.all(
path.map(async (step) => describeStep(step, installedApps, provider))
path.map(async (step) => describeStep(step, installedApps, connection))
)
}

export async function describeTransaction(
transaction: Transaction,
installedApps: App[],
provider: Provider
connection: ConnectionContext
): Promise<PostProcessDescription> {
if (!transaction.to) {
throw new Error(`Could not describe transaction: missing 'to'`)
Expand All @@ -87,7 +90,7 @@ export async function describeTransaction(
description = await tryEvaluatingRadspec(
transaction,
installedApps,
provider
connection
)

if (description) {
Expand Down
10 changes: 7 additions & 3 deletions packages/connect-core/src/utils/forwarding.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Contract } from '@ethersproject/contracts'
import { Provider } from '@ethersproject/providers'
import { ConnectionContext } from '..'
import { forwarderAbi } from './abis'

export const FORWARD_SIG = '0xd948d468' // function forward(bytes)
Expand Down Expand Up @@ -58,10 +58,14 @@ export function canForward(
forwarderAddress: string,
sender: string,
script: string,
provider: Provider
connection: ConnectionContext
): Promise<boolean> {
// Check if a token approval pretransaction is needed due to the forwarder requiring a fee
const forwarder = new Contract(forwarderAddress, forwarderAbi, provider)
const forwarder = new Contract(
forwarderAddress,
forwarderAbi,
connection.ethersProvider
)

return forwarder.canForward(sender, script).catch(() => false)
}
10 changes: 5 additions & 5 deletions packages/connect-core/src/utils/intent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { Address } from '@1hive/connect-types'
import { Result } from '@ethersproject/abi'
import { Provider } from '@ethersproject/providers'

import { addressesEqual } from './address'
import {
Expand All @@ -12,14 +11,15 @@ import { getForwardingPath, getACLForwardingPath } from './path/index'
import { StepDecoded } from '../types'
import App from '../entities/App'
import ForwardingPath from '../entities/ForwardingPath'
import { ConnectionContext } from '..'

export async function appIntent(
sender: Address,
destinationApp: App,
methodSignature: string,
params: any[],
installedApps: App[],
provider: Provider
connection: ConnectionContext
): Promise<ForwardingPath> {
const acl = installedApps.find((app) => app.name === 'acl')

Expand All @@ -31,7 +31,7 @@ export async function appIntent(
methodSignature,
params,
installedApps,
provider
connection
)
} catch (_) {
// emtpy path
Expand All @@ -42,7 +42,7 @@ export async function appIntent(
transactions: [],
},
installedApps,
provider
connection
)
}
}
Expand All @@ -53,7 +53,7 @@ export async function appIntent(
methodSignature,
params,
installedApps,
provider
connection
)
}

Expand Down
5 changes: 1 addition & 4 deletions packages/connect-core/src/utils/ipfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import { IpfsResolver } from '../types'
import { ErrorConnection, ErrorUnexpectedResult } from '../errors'
import { createCacheStore } from './cache-store'

export function ipfsResolver(
urlTemplate: string,
cache: number = 0
): IpfsResolver {
export function ipfsResolver(urlTemplate: string, cache = 0): IpfsResolver {
const cacheStore = cache === 0 ? null : createCacheStore<object>(cache)

return {
Expand Down
2 changes: 1 addition & 1 deletion packages/connect-core/src/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function normalizeFiltersAndCallback<C, F>(

const warned = new Map<string, boolean>()

export function warn(messages: any | any[], once: boolean = true) {
export function warn(messages: any | any[], once = true) {
if (process.env.NODE_ENV !== 'development') {
return
}
Expand Down
Loading