Skip to content

Commit

Permalink
refactor: check if beneficiary account exists, require confirmation
Browse files Browse the repository at this point in the history
  • Loading branch information
judith-Near committed Jun 27, 2022
1 parent 2c4d469 commit 5d5ef5e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
2 changes: 1 addition & 1 deletion bin/near-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const viewAccount = {

const deleteAccount = {
command: 'delete <accountId> <beneficiaryId>',
desc: 'delete an account and transfer funds to beneficiary account.',
desc: 'delete an account and transfer Near tokens to beneficiary account.',
builder: (yargs) => yargs
.option('accountId', {
desc: 'Account to delete',
Expand Down
30 changes: 24 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ const checkExistingContract = async function(prevCodeHash) {
return true;
};

const confirmDelete = function () {
return askYesNoQuestion(
chalk`{bold.white This method will delete your account. Beneficiary account must exist in order to transfer all Near tokens. Make sure to send all fungible tokens or NFTs that you own to the beneficiary account prior to deleting, as this method will only transfer NEAR tokens. Do you want to proceed? {bold.green (y/n) }}`,
false);
}

exports.deploy = async function (options) {
await checkCredentials(options.accountId, options.networkId, options.keyStore);

Expand Down Expand Up @@ -215,13 +221,25 @@ exports.viewAccount = async function (options) {

exports.deleteAccount = async function (options) {
await checkCredentials(options.accountId, options.networkId, options.keyStore);
console.log(
`Deleting account. Account id: ${options.accountId}, node: ${options.nodeUrl}, helper: ${options.helperUrl}, beneficiary: ${options.beneficiaryId}`);
const near = await connect(options);
const account = await near.account(options.accountId);
const result = await account.deleteAccount(options.beneficiaryId);
inspectResponse.prettyPrintResponse(result, options);
console.log(`Account ${options.accountId} for network "${options.networkId}" was deleted.`);
const beneficiaryAccount = await near.account(options.beneficiaryId);
// beneficiary account does not exist if there are no access keys
if (!(await beneficiaryAccount.getAccessKeys()).length) {
console.log("Beneficiary account does not exist, please create the account to transfer Near tokens.");
return;
}

if (await confirmDelete()) {
const account = await near.account(options.accountId);
console.log(
`Deleting account. Account id: ${options.accountId}, node: ${options.nodeUrl}, helper: ${options.helperUrl}, beneficiary: ${options.beneficiaryId}`);
const result = await account.deleteAccount(options.beneficiaryId);
inspectResponse.prettyPrintResponse(result, options);
console.log(`Account ${options.accountId} for network "${options.networkId}" was deleted.`);
}
else {
console.log(chalk`{bold.white Deletion of account with account id: {bold.blue ${options.accountId} } was {bold.red canceled}}`);
}
};

exports.keys = async function (options) {
Expand Down

0 comments on commit 5d5ef5e

Please sign in to comment.