Skip to content

Commit

Permalink
wip - template
Browse files Browse the repository at this point in the history
  • Loading branch information
frankiebee committed Sep 25, 2024
1 parent 7cd5072 commit 33f117d
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/account/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ export class EntropyAccount extends EntropyBase {

await wasmGlobalsReady()
const keyring = new Keyring({ seed, path, debug: true })
const fullAccount = keyring.getAccount()
const data = keyring.getAccount()
// TODO: sdk should create account on constructor
const { admin } = keyring.getAccount()
const { admin } = data

const data = fullAccount
delete admin.pair
// const encryptedData = password ? passwordFlow.encrypt(data, password) : data

Expand Down Expand Up @@ -99,6 +98,12 @@ export class EntropyAccount extends EntropyBase {

/* PRIVATE */

/*
WARNIG: this function needs to be removed in next core release [0.3.0]!
*/

private async pruneRegistration () {
return new Promise((resolve, reject) => {
this.entropy.substrate.tx.registry.pruneRegistration()
Expand Down
2 changes: 1 addition & 1 deletion src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export function getSync (configPath = CONFIG_PATH) {

export async function set (config: EntropyConfig, configPath = CONFIG_PATH) {
assertConfigPath(configPath)

console.log('config spy: ', config)
await mkdirp(dirname(configPath))
await writeFile(configPath, serialize(config))
}
Expand Down
42 changes: 42 additions & 0 deletions src/template/command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Command } from "commander"
import { EntropyTemplate } from "./main"
import { cliWrite, accountOption, endpointOption, loadEntropy, passwordOption } from "../common/utils-cli";


const command = {
name: 'transfer'
alias: ['']

Check failure on line 8 in src/template/command.ts

View workflow job for this annotation

GitHub Actions / Build, test, and lint

',' expected.
description: 'Transfer funds between two Entropy accounts.'

Check failure on line 9 in src/template/command.ts

View workflow job for this annotation

GitHub Actions / Build, test, and lint

',' expected.
// should be order
args: [

Check failure on line 11 in src/template/command.ts

View workflow job for this annotation

GitHub Actions / Build, test, and lint

',' expected.
{name: 'destination', description: 'Account address funds will be sent to'},
{name: 'ammount', description: 'Amount of funds to be moved'},
],
optionFlags: [
endpointOption
],

}


export function entropyTeplateCommand () {
const transferCommand = new Command('tranfer')
transferCommand
.description(command.description) // TODO: name the output
.argument('destination', 'Account address funds will be sent to')
.argument('amount', 'Amount of funds to be moved')
.addOption(passwordOption('Password for the source account (if required)'))
.addOption(endpointOption())
.addOption(accountOption())
.action(async (destination, amount, opts) => {
// load entropy
const entropy = await loadEntropy(opts.account, opts.endpoint)
// create service
const transferService = new EntropyTransfer(entropy, opts.endpoint)
// use sevice
await transferService.transfer(destination, amount)
// cliWrite(??) // TODO: write the output
process.exit(0)
})
return transferCommand
}
12 changes: 12 additions & 0 deletions src/template/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const PROMPT_CONTENT = {
amount: {
name: 'amount',
message: 'Input amount to transfer:',
default: '1',
invalidError: 'Please enter a value greater than 0',
},
recipientAddress: {
name: 'recipientAddress',
message: `Input recipient's address:`,
},
}
16 changes: 16 additions & 0 deletions src/template/interaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import inquirer from "inquirer"
import { print } from "../common/utils"
import { EntropyTransfer } from "./main"
import { transferInputQuestions } from "./utils"
import { setupProgress } from "src/common/progress"

export async function entropyTransfer (entropy, endpoint) {
const progressTracker = setupProgress('Transferring Funds')
const transferService = new EntropyTransfer(entropy, endpoint)
const { amount, recipientAddress } = await inquirer.prompt(transferInputQuestions)
await transferService.transfer(recipientAddress, amount, progressTracker)
print('')
print(`Transaction successful: Sent ${amount} to ${recipientAddress}`)
print('')
print('Press enter to return to main menu')
}
33 changes: 33 additions & 0 deletions src/template/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import Entropy from "@entropyxyz/sdk";
import { EntropyBase } from "../common/entropy-base";

/*
This provides core functions that should be unit tested
consumed by both ./command.ts and ./interaction.ts
should follow a pattern of take in arguments return result with little side effects
in between NO PRINTING!
atempt strict typeing
*/


// this is for logger context incase something fails and a user can provide a report
const FLOW_CONTEXT = 'ENTROPY_TEMPLATE'

export class EntropyTemplate extends EntropyBase {
constructor (entropy: Entropy, endpoint: string) {
super({ entropy, endpoint, flowContext: FLOW_CONTEXT })
}

async mainExcutableFunction () {
// write code requireing the use of entropy!
}

static async classMethod () {
// write stateless one-offs
}
}
6 changes: 6 additions & 0 deletions src/template/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @ts-ignore
/*
RECORD TYPES HERE
*/
27 changes: 27 additions & 0 deletions src/template/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { TRANSFER_CONTENT } from "./constants";

function validateAmount (amount: string | number) {
if (isNaN(amount as number) || parseInt(amount as string) <= 0) {
return TRANSFER_CONTENT.amount.invalidError
}
return true
}

const amountQuestion = {
type: 'input',
name: TRANSFER_CONTENT.amount.name,
message: TRANSFER_CONTENT.amount.message,
default: TRANSFER_CONTENT.amount.default,
validate: validateAmount
}

const recipientAddressQuestion = {
type: 'input',
name: TRANSFER_CONTENT.recipientAddress.name,
message: TRANSFER_CONTENT.recipientAddress.message,
}

export const transferInputQuestions = [
amountQuestion,
recipientAddressQuestion
]

0 comments on commit 33f117d

Please sign in to comment.