Skip to content

Commit

Permalink
Add CLI management tool (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
dankelleher committed Sep 11, 2024
1 parent 00c94ba commit 12c0e62
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
18 changes: 12 additions & 6 deletions scripts/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ import {showData} from "./util";
import {submenuRouteToRecipient} from "./submenu/routeToRecipient";
import {submenuAllocateYield} from "./submenu/allocateYield";
import {submenuUpdateProportions} from "./submenu/updateProportions";
import {submenuUpdateDestinationAddress} from "./submenu/updateDestinationAddress";

export const showMenu = async () => {
console.log(chalk.magentaBright('\nChoose an option:'));
console.log(chalk.cyanBright('1) Refresh'));
console.log(chalk.cyanBright('2) Allocate Yield'));
console.log(chalk.cyanBright('3) Route Funds to Recipient'));
console.log(chalk.cyanBright('4) Update Proportions'));
console.log(chalk.cyanBright('5) Add Recipient'));
console.log(chalk.cyanBright('6) Remove Recipient'));
console.log(chalk.cyanBright('7) Quit'));
console.log(chalk.cyanBright('5) Update Recipient Address'));
console.log(chalk.cyanBright('6) Add Recipient'));
console.log(chalk.cyanBright('7) Remove Recipient'));
console.log(chalk.cyanBright('8) Quit'));

const choice = readlineSync.keyIn(chalk.yellow('\nEnter your choice: '), { limit: '$<1-7>' });
const choice = readlineSync.keyIn(chalk.yellow('\nEnter your choice: '), { limit: '$<1-8>' });

switch (choice) {
case '1':
Expand All @@ -32,12 +34,16 @@ export const showMenu = async () => {
await submenuUpdateProportions();
break;
case '5':
console.log(chalk.green('Adding recipient...'));

await submenuUpdateDestinationAddress();
break;
case '6':
console.log(chalk.green('Removing recipient...'));
console.log(chalk.green('Adding recipient...'));
break;
case '7':
console.log(chalk.green('Removing recipient...'));
break;
case '8':
console.log(chalk.green('Exiting...'));
process.exit(0);
break;
Expand Down
47 changes: 47 additions & 0 deletions scripts/submenu/updateDestinationAddress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import chalk from "chalk";
import {fundSenderClients, fundSenderDestinations, getFundSenderAvailableAmount, selectAmount} from "../util";
import readlineSync from "readline-sync";
import BN from "bn.js";
import {PublicKey} from "@solana/web3.js";

export const submenuUpdateDestinationAddress = async () => {
console.log(chalk.magentaBright('\nChoose a recipient:'));
fundSenderDestinations.forEach((destinationName, index) => {
console.log(chalk.cyanBright(`${index + 1}) ${destinationName}`));
});
console.log(chalk.cyanBright(`${fundSenderDestinations.length + 1}) Cancel`));

const choice = readlineSync.keyIn(chalk.yellow('\nEnter your choice: '), { limit: `$<1-${fundSenderDestinations.length}>` });

if (choice === `${fundSenderDestinations.length + 1}`) {
return;
}

const destinationName = fundSenderDestinations[parseInt(choice) - 1];
const client = fundSenderClients.find(c => c.config.destinationName === destinationName);

if (!client) throw new Error('Client not found - trigger a refresh');

// ask for address
const newDestinationAddress = readlineSync.question(chalk.yellow('Enter the new destination address: '));

// verify it is a valid Solana address
let newDestinationAddressKey: PublicKey;
try {
newDestinationAddressKey = new PublicKey(newDestinationAddress);
} catch (e) {
console.log(chalk.red('Invalid address'));
return;
}

// ask for confirmation:
console.log(chalk.yellow(`New destination address: ${newDestinationAddressKey.toBase58()}`));
const confirm = readlineSync.question(chalk.yellow('Confirm (y/n): '));

if (confirm === 'y') {
await client.updateDestinationAccount(newDestinationAddressKey, client.config.spendThreshold);
console.log(chalk.green(`Done`));
} else {
console.log(chalk.red('Update cancelled'));
}
}

0 comments on commit 12c0e62

Please sign in to comment.