Skip to content
This repository was archived by the owner on Oct 4, 2024. It is now read-only.

Commit 5d5ef5e

Browse files
committed
refactor: check if beneficiary account exists, require confirmation
1 parent 2c4d469 commit 5d5ef5e

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

bin/near-cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const viewAccount = {
3232

3333
const deleteAccount = {
3434
command: 'delete <accountId> <beneficiaryId>',
35-
desc: 'delete an account and transfer funds to beneficiary account.',
35+
desc: 'delete an account and transfer Near tokens to beneficiary account.',
3636
builder: (yargs) => yargs
3737
.option('accountId', {
3838
desc: 'Account to delete',

index.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ const checkExistingContract = async function(prevCodeHash) {
4545
return true;
4646
};
4747

48+
const confirmDelete = function () {
49+
return askYesNoQuestion(
50+
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) }}`,
51+
false);
52+
}
53+
4854
exports.deploy = async function (options) {
4955
await checkCredentials(options.accountId, options.networkId, options.keyStore);
5056

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

216222
exports.deleteAccount = async function (options) {
217223
await checkCredentials(options.accountId, options.networkId, options.keyStore);
218-
console.log(
219-
`Deleting account. Account id: ${options.accountId}, node: ${options.nodeUrl}, helper: ${options.helperUrl}, beneficiary: ${options.beneficiaryId}`);
220224
const near = await connect(options);
221-
const account = await near.account(options.accountId);
222-
const result = await account.deleteAccount(options.beneficiaryId);
223-
inspectResponse.prettyPrintResponse(result, options);
224-
console.log(`Account ${options.accountId} for network "${options.networkId}" was deleted.`);
225+
const beneficiaryAccount = await near.account(options.beneficiaryId);
226+
// beneficiary account does not exist if there are no access keys
227+
if (!(await beneficiaryAccount.getAccessKeys()).length) {
228+
console.log("Beneficiary account does not exist, please create the account to transfer Near tokens.");
229+
return;
230+
}
231+
232+
if (await confirmDelete()) {
233+
const account = await near.account(options.accountId);
234+
console.log(
235+
`Deleting account. Account id: ${options.accountId}, node: ${options.nodeUrl}, helper: ${options.helperUrl}, beneficiary: ${options.beneficiaryId}`);
236+
const result = await account.deleteAccount(options.beneficiaryId);
237+
inspectResponse.prettyPrintResponse(result, options);
238+
console.log(`Account ${options.accountId} for network "${options.networkId}" was deleted.`);
239+
}
240+
else {
241+
console.log(chalk`{bold.white Deletion of account with account id: {bold.blue ${options.accountId} } was {bold.red canceled}}`);
242+
}
225243
};
226244

227245
exports.keys = async function (options) {

0 commit comments

Comments
 (0)