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

Imp upload command #23

Merged
merged 12 commits into from
Dec 8, 2023
2 changes: 2 additions & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.phat
dist
3 changes: 2 additions & 1 deletion examples/batch-http-request/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"@phala/pink-env": "^1.0.11"
},
"devDependencies": {
"typescript": "^5.2.2"
"typescript": "^5.2.2",
"@phala/fn": "file:../../"
}
}
3 changes: 2 additions & 1 deletion examples/http-request/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"@phala/pink-env": "^1.0.11"
},
"devDependencies": {
"typescript": "^5.2.2"
"typescript": "^5.2.2",
"@phala/fn": "file:../../"
}
}
3 changes: 2 additions & 1 deletion examples/invoke-contract/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"@phala/pink-env": "^1.0.11"
},
"devDependencies": {
"typescript": "^5.2.2"
"typescript": "^5.2.2",
"@phala/fn": "file:../../"
}
}
3 changes: 2 additions & 1 deletion examples/randomness-fullfillrandomwords/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"@phala/pink-env": "^1.0.11"
},
"devDependencies": {
"typescript": "^5.2.2"
"typescript": "^5.2.2",
"@phala/fn": "file:../../"
}
}
3 changes: 2 additions & 1 deletion examples/randomness-randrange/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"@phala/pink-env": "^1.0.11"
},
"devDependencies": {
"typescript": "^5.2.2"
"typescript": "^5.2.2",
"@phala/fn": "file:../../"
}
}
3 changes: 2 additions & 1 deletion examples/scale-codec/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"@scale-codec/core": "^2.0.1"
},
"devDependencies": {
"typescript": "^5.2.2"
"typescript": "^5.2.2",
"@phala/fn": "file:../../"
}
}
1 change: 0 additions & 1 deletion examples/viem-abi-codec/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export default function main() {
],
encodedData,
)
console.info(values)

return values[0]
}
3 changes: 2 additions & 1 deletion examples/viem-abi-codec/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"viem": "^1.18.0"
},
"devDependencies": {
"typescript": "^5.2.2"
"typescript": "^5.2.2",
"@phala/fn": "file:../../"
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"ethers": "^6.7.1",
"filesize": "^10.0.12",
"inquirer": "8",
"ora": "5",
"quickjs-emscripten": "^0.23.0",
"quickjs-emscripten-sync": "^1.5.2",
"simple-git": "^3.19.1",
Expand Down
28 changes: 21 additions & 7 deletions src/commands/build.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { lstatSync, readFileSync, existsSync } from 'node:fs'
import upath from 'upath'
import { Args, Command, Flags, ux } from '@oclif/core'
import chalk from 'chalk'
import { Args, Flags } from '@oclif/core'

import { resolveToAbsolutePath } from '../lib/utils'
import { runWebpack, printFileSizesAfterBuild } from '../lib/runWebpack'
import { formatWebpackMessages } from '../lib/formatWebpackMessages'
import BaseCommand from '../lib/BaseCommand'

export default class Build extends Command {
export default class Build extends BaseCommand {
static description = 'Build a production bundle of the function script'

static args = {
Expand Down Expand Up @@ -88,7 +89,7 @@ export default class Build extends Command {
}

if (!flags.silent) {
ux.action.start('Creating an optimized build')
this.action.start('Creating an optimized build')
}

try {
Expand All @@ -102,12 +103,25 @@ export default class Build extends Command {
isDev,
})
if (!flags.silent) {
ux.action.stop()
printFileSizesAfterBuild(stats)
this.action.stop()
const json = stats.toJson({
all: false,
warnings: true,
assets: true,
outputPath: true
})
const messages = formatWebpackMessages(json)
if (messages.warnings && messages.warnings.length) {
this.action.warn('Compiled with warnings.')
this.log(messages.warnings.join('\n\n'))
} else {
this.action.succeed('Compiled successfully.')
}
printFileSizesAfterBuild(json)
}
} catch (error: any) {
if (!flags.silent) {
ux.action.stop(chalk.red('Failed to compile.\n'))
this.action.fail('Failed to compile.')
}
return this.error(error)
}
Expand Down
26 changes: 11 additions & 15 deletions src/commands/update.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import fs from 'node:fs'
import { ux, Flags } from '@oclif/core'
import { Flags } from '@oclif/core'
import type { Result, Struct, u16, Text, Bool } from '@polkadot/types'
import {
PinkContractPromise,
} from '@phala/sdk'
import inquirer from 'inquirer'

import PhatCommandBase, { type ParsedFlags } from '../lib/PhatCommandBase'
import PhatBaseCommand, { type ParsedFlags } from '../lib/PhatBaseCommand'

interface WorkflowCodec extends Struct {
id: u16
Expand All @@ -15,15 +15,15 @@ interface WorkflowCodec extends Struct {
commandline: Text
}

export default class Update extends PhatCommandBase {
export default class Update extends PhatBaseCommand {
static description = 'Update Phat Contract JS'

static args = {
...PhatCommandBase.args
...PhatBaseCommand.args
}

static flags = {
...PhatCommandBase.flags,
...PhatBaseCommand.flags,
workflowId: Flags.integer({
description: 'Workflow ID',
required: false,
Expand All @@ -42,27 +42,24 @@ export default class Update extends PhatCommandBase {

// Step 1: Connect to the endpoint.
const endpoint = this.getEndpoint()
ux.action.start(`Connecting to the endpoint: ${endpoint}`)
const [apiPromise, registry, cert] = await this.connect({
endpoint,
pair,
})
ux.action.stop()

// Step 2: Query the brick profile contract id.
ux.action.start('Querying your Brick Profile contract ID')
this.action.start('Querying your Brick Profile contract ID')
const brickProfileContractId = await this.getBrickProfileContractId({
endpoint,
registry,
apiPromise,
pair,
cert,
})
ux.action.stop()
this.log(`Your Brick Profile contract ID: ${brickProfileContractId}`)
this.action.succeed(`Your Brick Profile contract ID: ${brickProfileContractId}`)

// Step 3: Check current user workflow settings.
ux.action.start('Checking your workflow settings')
this.action.start('Checking your workflow settings')
const brickProfileAbi = await this.loadAbiByContractId(
registry,
brickProfileContractId
Expand Down Expand Up @@ -92,10 +89,10 @@ export default class Update extends PhatCommandBase {
}\nActual: ${rollupAbi.info.source.wasmHash.toHex()}\n`
)
}
ux.action.stop()
this.action.stop()

// Step 4: Update the JS.
ux.action.start('Updating')
this.action.start('Updating')
const actionOffchainRollupContractId = actions[0].config.callee
const rollupContractKey = await registry.getContractKeyOrFail(
actionOffchainRollupContractId
Expand All @@ -111,8 +108,7 @@ export default class Update extends PhatCommandBase {
{ cert, address: pair.address, pair },
fs.readFileSync(buildScriptPath, 'utf8'),
)
ux.action.stop()
this.log(
this.action.succeed(
`The JavaScript code for workflow ${workflowId} has been updated.`
)
this.exit(0)
Expand Down
49 changes: 27 additions & 22 deletions src/commands/upload.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import fs from 'node:fs'
import { ux } from '@oclif/core'
import type { u16 } from '@polkadot/types'
import {
PinkContractPromise,
PinkBlueprintPromise,
} from '@phala/sdk'

import PhatCommandBase from '../lib/PhatCommandBase'
import PhatBaseCommand from '../lib/PhatBaseCommand'
import type { BrickProfileContract } from '../lib/PhatBaseCommand'

export default class Upload extends PhatCommandBase {
export default class Upload extends PhatBaseCommand {
static description = 'Upload JS to Phat Contract'

static args = {
...PhatCommandBase.args
...PhatBaseCommand.args
}

static flags = {
...PhatCommandBase.flags
...PhatBaseCommand.flags
}

public async run(): Promise<void> {
Expand All @@ -32,35 +32,32 @@ export default class Upload extends PhatCommandBase {

// Step 1: Connect to the endpoint.
const endpoint = this.getEndpoint()
ux.action.start(`Connecting to the endpoint: ${endpoint}`)
const [apiPromise, registry, cert] = await this.connect({
endpoint,
pair,
})
ux.action.stop()

// Step 2: Query the brick profile contract id.
ux.action.start('Querying your Brick Profile contract ID')
this.action.start('Querying your Brick Profile contract ID')
const brickProfileContractId = await this.getBrickProfileContractId({
endpoint,
registry,
apiPromise,
pair,
cert,
})
ux.action.stop()
this.log(`Your Brick Profile contract ID: ${brickProfileContractId}`)
this.action.succeed(`Your Brick Profile contract ID: ${brickProfileContractId}`)

// Step 3: Instantiating the ActionOffchainRollup contract.
ux.action.start('Instantiating the ActionOffchainRollup contract')
this.action.start('Instantiating the ActionOffchainRollup contract')
const brickProfileAbi = await this.loadAbiByContractId(
registry,
brickProfileContractId
)
const brickProfileContractKey = await registry.getContractKeyOrFail(
brickProfileContractId
)
const brickProfile = new PinkContractPromise(
const brickProfile: BrickProfileContract = new PinkContractPromise(
apiPromise,
registry,
brickProfileAbi,
Expand All @@ -85,14 +82,19 @@ export default class Upload extends PhatCommandBase {
)
await result.waitFinalized()
const contractPromise = result.contract
ux.action.stop()
this.log(
'The ActionOffchainRollup contract has been instantiated:',
contractPromise.address.toHex()
this.action.succeed(
`The ActionOffchainRollup contract has been instantiated: ${contractPromise.address.toHex()}`,
)

// Step 4: Setting up the actions.
ux.action.start('Setting up the actions')
// Step 4: Select an external account.
const externalAccountId = await this.promptEvmAccountId({
contract: brickProfile,
cert,
})
pacoyang marked this conversation as resolved.
Show resolved Hide resolved


// Step 5: Checking your settings.
this.action.start('Checking your settings')
const { output: attestorQuery } =
await contractPromise.query.getAttestAddress(cert.address, { cert })
const attestor = attestorQuery.asOk.toHex()
Expand Down Expand Up @@ -122,17 +124,20 @@ export default class Upload extends PhatCommandBase {
{ cert }
)
const num = numberQuery.asOk.toNumber()
this.action.succeed()

const projectName = await this.promptProjectName(`My Phat Contract ${numberQuery.asOk.toNumber()}`)

const externalAccountId = 0
// Step 6: Setting up the actions.
this.action.start('Setting up the actions')
const result2 = await brickProfile.send.addWorkflowAndAuthorize(
{ cert, address: pair.address, pair },
`My Phat Contract ${numberQuery.asOk.toNumber()}`,
projectName,
JSON.stringify(actions),
externalAccountId
)
await result2.waitFinalized()
ux.action.stop()
this.log(
this.action.succeed(
`🎉 Your workflow has been added, you can check it out here: https://bricks.phala.network/workflows/${brickProfileContractId}/${num}`
)
this.log('Your Attestor address:', attestor)
Expand Down
36 changes: 36 additions & 0 deletions src/lib/BaseCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Command } from '@oclif/core'
import chalk from 'chalk'
import ora from 'ora'

abstract class BaseCommand extends Command {
public spinner = ora()

get action() {
return {
start: (message?: string) => {
this.spinner.stop()
this.spinner.start(message)
},
stop: () => this.spinner.stop(),
succeed: (message?: string) =>
this.spinner.stopAndPersist({
symbol: chalk.green('✓'),
text: message,
}),
fail: (message?: string) => {
this.spinner.stopAndPersist({
symbol: chalk.red('✗'),
text: message,
})
},
warn: (message?: string) => {
this.spinner.stopAndPersist({
symbol: chalk.yellow('!'),
text: message,
})
},
}
}
}

export default BaseCommand
Loading
Loading