Skip to content

Commit

Permalink
merge user/dev programs flows
Browse files Browse the repository at this point in the history
  • Loading branch information
mixmix committed Jul 31, 2024
1 parent 130b4c1 commit 688e8de
Show file tree
Hide file tree
Showing 14 changed files with 115 additions and 118 deletions.
101 changes: 0 additions & 101 deletions src/flows/DeployPrograms/index.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/flows/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
export { entropyFaucet } from './entropyFaucet'
export { checkBalance } from './balance'
export { entropyRegister } from './register'
export { userPrograms } from './user-program-management'
export { devPrograms } from './DeployPrograms'
export { userPrograms, devPrograms } from './programs'
export { sign } from './sign'
export { entropyTransfer } from './entropyTransfer'
export { manageAccounts } from './manage-accounts'
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import Entropy from "@entropyxyz/sdk"
import { readFileSync } from "fs"
import inquirer from "inquirer"
import * as util from "@polkadot/util"
import { initializeEntropy } from "../../common/initializeEntropy"
import { getSelectedAccount, print } from "../../common/utils"
import { EntropyLogger } from "src/common/logger";

import { addProgram } from "./add";
import { viewPrograms } from "./view";
import { removeProgram } from "./remove";
import { addQuestions, getProgramPointerInput, verifyingKeyQuestion } from "./helpers/questions";
import { displayPrograms } from "./helpers/utils";
import { removeProgram } from "./remove";
import { initializeEntropy } from "../../common/initializeEntropy"
import { getSelectedAccount, print } from "../../common/utils"
import { EntropyLogger } from "../../common/logger";
import { EntropyTuiOptions } from "../../types"

let verifyingKey: string;

export async function userPrograms ({ accounts, selectedAccount: selectedAccountAddress }, options, logger: EntropyLogger) {
const FLOW_CONTEXT = 'USER_PROGRAMS'
export async function userPrograms ({ accounts, selectedAccount: selectedAccountAddress }, options: EntropyTuiOptions, logger: EntropyLogger) {
const FLOW_CONTEXT = 'PROGRAMS'
const { endpoint } = options
const selectedAccount = getSelectedAccount(accounts, selectedAccountAddress)

Expand Down Expand Up @@ -112,3 +116,98 @@ export async function userPrograms ({ accounts, selectedAccount: selectedAccount
}
}

// eslint-disable-next-line
export async function devPrograms ({ accounts, selectedAccount: selectedAccountAddress }, options: EntropyTuiOptions, logger: EntropyLogger) {
// const FLOW_CONTEXT = 'PROGRAMS'
const { endpoint } = options
const selectedAccount = getSelectedAccount(accounts, selectedAccountAddress)

const choices = {
"Deploy": deployProgram,
"Get Owned Programs": getOwnedPrograms,
"Exit to Main Menu": () => 'exit'
}

const actionChoice = await inquirer.prompt([
{
type: "list",
name: "action",
message: "Select your action:",
choices: Object.keys(choices)
},
])

const entropy = await initializeEntropy({
keyMaterial: selectedAccount.data,
endpoint
})

const flow = choices[actionChoice.action]
await flow(entropy, selectedAccount)
}

async function deployProgram (entropy: Entropy, account: any) {
const deployQuestions = [
{
type: "input",
name: "programPath",
message: "Please provide the path to your program:",
},
{
type: "confirm",
name: "hasConfig",
message: "Does your program have a configuration file?",
default: false,
},
]

const deployAnswers = await inquirer.prompt(deployQuestions)
const userProgram = readFileSync(deployAnswers.programPath)

let programConfig = ""

if (deployAnswers.hasConfig) {
const configAnswers = await inquirer.prompt([
{
type: "input",
name: "config",
message: "Please provide your program configuration as a JSON string:",
},
])

// Convert JSON string to bytes and then to hex
const encoder = new TextEncoder()
const byteArray = encoder.encode(configAnswers.config)
programConfig = util.u8aToHex(new Uint8Array(byteArray))
}

try {
// Deploy the program with config
const pointer = await entropy.programs.dev.deploy(
userProgram,
programConfig
)
print("Program deployed successfully with pointer:", pointer)
} catch (deployError) {
console.error("Deployment failed:", deployError)
}

print("Deploying from account:", account.address)
}

async function getOwnedPrograms (entropy: Entropy, account: any) {
const userAddress = account.address
if (!userAddress) return

try {
const fetchedPrograms = await entropy.programs.dev.get(userAddress)
if (fetchedPrograms.length) {
print("Retrieved program pointers:")
print(fetchedPrograms)
} else {
print("There are no programs to show")
}
} catch (error) {
console.error("Failed to retrieve program pointers:", error)
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/tui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { logo } from './common/ascii'
import { print } from './common/utils'
import { EntropyLogger } from './common/logger'


let shouldInit = true

// tui = text user interface
Expand All @@ -22,6 +21,7 @@ export default function tui (options: EntropyTuiOptions) {
'Register': flows.entropyRegister,
'Sign': flows.sign,
'Transfer': flows.entropyTransfer,
// TODO: design programs in TUI (merge deploy+user programs)
'Deploy Program': flows.devPrograms,
'User Programs': flows.userPrograms,
// 'Construct an Ethereum Tx': flows.ethTransaction,
Expand Down
14 changes: 7 additions & 7 deletions tests/user-program-management.test.ts → tests/programs.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import test from 'tape'
import { readFileSync } from 'node:fs'
import { promiseRunner, charlieStashSeed, setupTest } from './testing-utils'
import { AddProgramParams } from 'src/flows/user-program-management/types'
import { addProgram } from 'src/flows/user-program-management/add'
import { viewPrograms } from 'src/flows/user-program-management/view'
import { removeProgram } from 'src/flows/user-program-management/remove'
import { AddProgramParams } from 'src/flows/programs/types'
import { addProgram } from 'src/flows/programs/add'
import { viewPrograms } from 'src/flows/programs/view'
import { removeProgram } from 'src/flows/programs/remove'

const networkType = 'two-nodes'

test('User Program Management', async t => {
test('programs', async t => {
const { run, entropy } = await setupTest(t, { seed: charlieStashSeed, networkType })
await run('charlie stash register', entropy.register())
const noopProgram: any = readFileSync(
'src/programs//program_noop.wasm'
'./programs/program_noop.wasm'
)
const newPointer = await run(
'deploy',
Expand Down Expand Up @@ -53,4 +53,4 @@ test('User Program Management', async t => {
vp.equal(programs.length, 1, 'charlie has 1 program')
vp.end()
})
})
})
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/register.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test('Regsiter - Default Program', async (t) => {
test('Register - Barebones Program', async t => {
const { run, entropy } = await setupTest(t, { networkType, seed: charlieStashSeed })
const dummyProgram: any = readFileSync(
'src/programs/template_barebones.wasm'
'./programs/template_barebones.wasm'
)
const pointer = await run(
'deploy program',
Expand Down

0 comments on commit 688e8de

Please sign in to comment.