Delete a contract using using delete_account method from class ContractPromiseBatch near-sdk-core

Viewed 192

TLDR: What is the correct way to do a cross-contract call from parent account sub.myacc.testnet to delete a sub-account one.sub.myacc.testnet using delete_account on ContractPromiseBatch from near-sdk-core without getting permission error

Full Explanation: From the command line I make sure I am logged in: near login account myacc.testnet

I created the following subaccount to where I deploy the factory contract:

near create-account sub.myacc.testnet --masterAccount myacc.testnet

I then deploy my factory contract using: near deploy

Using near-sdk-core, this factory contract has a function that creates, deploys a contract and adds a full_access_key using ContractPromiseBatch

Like this:

 ContractPromiseBatch
  .create("one.sub.myacc.testnet")
  .create_account()
  .transfer(u128.from(100))
  .add_full_access_key(base58.decode(context.senderPublicKey))
  .deploy_contract(code)

I then make sure all accounts have the right keys with near keys <accountId>

near keys sub.myacc.testnet

[
  {
    access_key: { nonce: 84008729000009, permission: 'FullAccess' },
    public_key: 'ed25519:gu5xxxxxxxxxxxx'
  }
]

near keys one.sub.myacc.testnet

[
  {
    access_key: { nonce: 84009033000000, permission: 'FullAccess' },
    public_key: 'ed25519:gu5xxxxxxxxxxxx'
  }
]

Both keys match.

I then add the key to our main account to make sure all accounts have same access:

near add-key myacc.testnet gu5xxxxxx

If I attempt to delete the contract one.sub.myacc.testnet with the following command: near delete one.sub.myacc.testnet <beneficiary>

I get the following error: Unable to find [ testnet ] credentials for [ one.sub.myacc.testnet ]...

If I check ~/.near-credentials/testnet I see that I do indeed have myacc.testnet.json file with the key we added but I don't see either sub.myacc.testnet.json or one.sub.myacc.testnet.json

So I manually added: one.sub.myacc.testnet.json and copy the key

This will work: Deleting account. Account id: one.sub.myacc.testnet

Error: Now, I have a function in the factory contract where I try to use delete_account method and sign it with either sub.myacc.testnet, one.sub.myacc.testnet, or myacc.testent

ContractPromiseBatch.create(one.sub.myacc.testent) 
    .delete_account(one.sub.myacc.testent)

I get the following error: Error: Actor sub.myacc.testent doesn't have permission to account one.sub.myacc.testnet to complete the action

From the docs: Subaccounts work exactly like standalone accounts, meaning that after creation, the parent account no longer has any special control or access to the subaccount.

I would think that the parent account could control the subaccount if it has the full access key? If so, I am missing a step or a way to call ContractPromiseBatch with the correct full_access_key but can't find an example of how this should be done.

Thanks in advance,

2 Answers

Error: Actor sub.myacc.testent doesn't have permission to account one.sub.myacc.testnet to complete the action

Hey, I hope this is just bad redaction, but if that's not the case, maybe your problem is testent instead of testnet.

Accounts have no power over their sub-accounts. In practice, they work as completely different and independent accounts, the only benefit of sub-accounts is that they allow you to better organize your accounts.

With that being said, the sub account is the only one that can delete itself. However, you could add a method to your subaccount that only the parent account can call that makes the subaccount delete itself (reference).

Related