Search code examples
smartcontractsnearprotocol

Sub account access keys and storage staking


Lets say a contract that lives on account factory.near has a create method that creates sub accounts and deploy contracts to it like the following..

    const promise = ContractPromiseBatch.create(subaccount)
      .create_account()
      .deploy_contract(Uint8Array.wrap(changetype<ArrayBuffer>(CODE)))
      .add_full_access_key(base58.decode(Context.senderPublicKey))
      .function_call(
        "init",
        new PlaceInitArgs(Context.sender),
        Context.attachedDeposit,
        XCC_GAS
      )
  1. Does the parent account that holds the factory contract automatically have full access or function call access to the sub account?
  2. Would all sub accounts have the same contract hash since the same WASM file is being deployed onto it?
  3. What would happen if the attached deposit isn't enough to stake storage? Would the account be made without the contract deployed or would both fail?
  4. If a subaccount is deleted, would the NEAR tokens staked as storage also be returned to the specified beneficiary in the delete function call?

Solution

  • Does the parent account that holds the factory contract automatically have full access or function call access to the sub account?

    No the access control doesn't work between accounts. It is invalid to say that some account has full access to some other accounts. You have full access to some account if and only if you own a full access key (the private key) of that account.

    Would all sub accounts have the same contract hash since the same WASM file is being deployed onto it?

    Yes if they are created this way.

    What would happen if the attached deposit isn't enough to stake storage? Would the account be made without the contract deployed or would both fail?

    The actions are batched in one receipt. The execution of a receipt is atomic -- it either succeeds or the entire state touched in between is rolled back. In this case, the account creation will fail if attached deposit is not enough.

    If a subaccount is deleted, would the NEAR tokens staked as storage also be returned to the specified beneficiary in the delete function call?

    Yes