Transfers & Actions
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
.
- 🌐 JavaScript
- 🦀 Rust
- 🐍 Python
- 🐹 GO
import { NearBindgen, NearPromise, call } from 'near-sdk-js'
import { AccountId } from 'near-sdk-js/lib/types'
@NearBindgen({})
class Contract{
@call({})
transfer({ to, amount }: { to: AccountId, amount: bigint }) {
return NearPromise.new(to).transfer(amount);
}
}
use near_sdk::{near, AccountId, Promise, NearToken};
#[near(contract_state)]
#[derive(Default)]
pub struct Contract { }
#[near]
impl Contract {
pub fn transfer(&self, to: AccountId, amount: NearToken){
Promise::new(to).transfer(amount);
}
}
from near_sdk_py import call, Contract
from near_sdk_py.promises import Promise
class ExampleContract(Contract):
@call
def transfer(self, to, amount):
"""Transfers NEAR to another account"""
# Create a promise to transfer NEAR
return Promise.create_batch(to).transfer(amount)
package main
import (
contractBuilder "github.com/vlmoon99/near-sdk-go/contract"
promiseBuilder "github.com/vlmoon99/near-sdk-go/promise"
"github.com/vlmoon99/near-sdk-go/types"
)
//go:export ExampleTransferToken
func ExampleTransferToken() {
contractBuilder.HandleClientJSONInput(func(input *contractBuilder.ContractInput) error {
to, err := input.JSON.GetString("to")
if err != nil {
return err
}
rawAmount, err := input.JSON.GetString("amount")
if err != nil {
return err
}
amount, _ := types.U128FromString(rawAmount)
promiseBuilder.CreateBatch(to).
Transfer(amount)
return nil
})
}
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.
- 🌐 JavaScript
- 🦀 Rust
- 🐍 Python