This page describes the different types of actions that a smart contract can perform on NEAR like transferring NEAR, calling other contracts, creating sub-accounts, and deploying contracts. It also explains how to add access keys to accounts.
Smart contracts can perform specific Actions such as transferring NEAR, or calling other contracts.
An important property of Actions is that they can be batched together when acting on the same contract. Batched actions act as a unit: they execute in the same receipt, and if any fails, then they all get reverted.
Actions can be batched only when they act on the same contract. You can batch calling two methods on a contract,
but cannot call two methods on different contracts.
Transfer NEAR β
You can send $NEAR from your contract to any other account on the network. The Gas cost for transferring $NEAR is fixed and is based on the protocolβs genesis config. Currently, it costs ~0.45 TGas.
π¦ Rust
π JavaScript
π Python
πΉ GO
Why is there no callback?
The only case where a transfer will fail is if the receiver account does not exist.
Remember that your balance is used to cover for the contractβs storage. When sending money, make sure you always leave enough to cover for future storage needs.
Function Call
Your smart contract can call methods in another contract. In the snippet below we call a method
in a deployed Hello NEAR contract, and check if everything went
right in the callback.
π¦ Rust
π JavaScript
π Python
πΉ GO
The snippet showed above is a low level way of calling other methods. We recommend make calls to other contracts as explained in the Cross-contract Calls section.
Create a Sub Account
Your contract can create direct sub accounts of itself, for example, user.near can create sub.user.near.
Accounts do NOT have control over their sub-accounts, since they have their own keys.
Sub-accounts are simply useful for organizing your accounts (e.g. dao.project.near, token.project.near).
π¦ Rust
π JavaScript
π Python
πΉ GO
Notice that in the snippet we are transferring some money to the new account for storage
When you create an account from within a contract, it has no keys by default. If you donβt explicitly add keys to it or deploy a contract on creation then it will be locked.
Creating .testnet / .near Accounts
Accounts can only create immediate sub-accounts of themselves.
If your contract wants to create a .mainnet or .testnet account, then it needs to call
the create_account method of near or testnet root contracts.
π¦ Rust
π JavaScript
π Python
πΉ GO
Deploy a Contract
When creating an account you can also batch the action of deploying a contract to it. Note that for this, you will need to pre-load the byte-code you want to deploy in your contract.
π¦ Rust
π Python
πΉ GO
If an account with a contract deployed does not have any access keys, this is known as a locked contract. When the account is locked, it cannot sign transactions therefore, actions can only be performed from within the contract code.
Add Keys
When you use actions to create a new account, the created account does not have any access keys, meaning that it cannot sign transactions (e.g. to update its contract, delete itself, transfer money).
There are two options for adding keys to the account:
add_access_key: adds a key that can only call specific methods on a specified contract.
add_full_access_key: adds a key that has full access to the account.
π¦ Rust
π JavaScript
π Python
πΉ GO
Notice that what you actually add is a βpublic keyβ. Whoever holds its private counterpart, i.e. the private-key, will be able to use the newly access key.
If an account with a contract deployed does not have any access keys, this is known as a locked contract. When the account is locked, it cannot sign transactions therefore, actions can only be performed from within the contract code.
Delete Account
There are two scenarios in which you can use the delete_account action:
- As the last action in a chain of batched actions.
- To make your smart contract delete its own account.
π¦ Rust
π JavaScript
π Python
πΉ GO
Token Loss
Do not use delete to try fund a new account. Since the account doesnβt exist the tokens will be lost.