diff --git a/scripts/prompt.ts b/scripts/prompt.ts index 1e15dc7..3bc2998 100644 --- a/scripts/prompt.ts +++ b/scripts/prompt.ts @@ -4,6 +4,7 @@ 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:')); @@ -11,11 +12,12 @@ export const showMenu = async () => { 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': @@ -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; diff --git a/scripts/submenu/updateDestinationAddress.ts b/scripts/submenu/updateDestinationAddress.ts new file mode 100644 index 0000000..5daab05 --- /dev/null +++ b/scripts/submenu/updateDestinationAddress.ts @@ -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')); + } +} \ No newline at end of file