Skip to main content

Contracts and Addresses

Following Michelson, there are two ways to point to other contracts in SmartPy:

The corresponding types in Michelson are Michelson contract and Michelson address.

See Inter-Contract Calls (simple), On Chain Contract Calls - Collatz (advanced) and FA1.2 (applied, in the balance_of entry point) templates.

Literal​

sp.address(<address>)
Create a literal of type sp.TAddress.

Example​

address = sp.address("tz1aTgF2c3vyrk2Mko1yzkJQGAnqUeDapxxm");
address2 = sp.address("KT1Tezooo4zzSmartPyzzSTATiCzzzyPVdv3");

Global properties​

The current contract​

sp.self
Get the current contract of type sp.TContract(t) for some type t.

Example​

contract = sp.self
Michelson SELF

The address of the current contract​

sp.self_address
This is the proper way to get a contract's own address.

Example​

address = sp.self_address

In tests, a contract's address is accessible through the <contract>.address field.

Michelson SELF_ADDRESS

The sender address​

sp.sender
The address that called the current entry point.

Example​

address = sp.sender

More information available here.

Michelson SENDER

The source address​

sp.source
The address that initiated the current transaction.
It may or may not be equal to sp.sender.

Example​

address = sp.source

More information available here.

Michelson SOURCE

Operations​

Get entry point from current contract​

sp.self_entry_point(entry_point = '<entry_point_name>')
Get an entry point from the current contract, where the optional entry_point argument contains the entry point's name. The returned contract is of type sp.TContract(t) where t is the type of the entry point's parameters.

Example​

contract = sp.self_entry_point(entry_point = '<entry_point_name>')

If entry_point is empty, then the current entry point is used.

Convert contract to address​

sp.to_address(<contract>)
Compute the address of type sp.TAddress, from a contract of type sp.TContract(t) for some type t.

Example​

address = sp.to_address(contract)
Michelson ADDRESS

Get entry point address from current contract​

sp.self_entry_point_address(entry_point = '<entry_point_name>')
It is an alias for sp.to_address(sp.self_entry_point(entry_point)).

This is the proper way to get a contract's own address of an entry point.

Example​

address = sp.self_entry_point_address(entry_point = '<entry_point_name>')

Cast an address to a typed contract​

sp.contract(t, address, entry_point = '<entry_point_name>')
Cast an address of type sp.TAddress to an optional typed contract of type t. It returns an expression of type sp.TOption(sp.TContract(t)).

Example​

contract = sp.contract(sp.TNat, sp.address("KT1Tezooo1zzSmartPyzzSTATiCzzzyfC8eF"), "an_entrypoint").open_some("INTERFACE_MISMATCH")
  • When optional parameter entry_point is empty or unspecified, it returns sp.some(c), where c is a contract handle of type TContract(t), if address, of type TAddress, points to a contract that expects a parameter of type t. Otherwise it returns sp.none.

  • When entry_point is not empty, it returns the specific entry point specified by the string entry_point of the contract. t must match the entry point's expected parameter type. Otherwise, it returns sp.none.

Due to restrictions of Michelson, it only works properly for contracts with multiple entry points.

Michelson CONTRACT

Cast key_hash to a typed contract​

sp.implicit_account(<key_hash>)
Implicit accounts are contracts which always have type sp.TUnit.

The instruction above converts a value of type sp.TKeyHash into sp.TContract(sp.TUnit).

Example​

contract = sp.implicit_account(key_hash)

See Key Hash for description.

Michelson IMPLICIT_ACCOUNT

Transfer​

sp.transfer(arg, amount, destination)
Call the destination contract with argument arg while sending the specified amount to it. Note that destination must be of type sp.TContract(t). The type of arg must be t, i.e. the argument sent to the destination must be consistent with what it expects.

Example​

sp.transfer(arg, amount, destination)

sp.send(destination, amount, message = None)
Send the specified amount to the destination contract. Will fail with optional error message if destination (of type sp.TAddress) does not resolve to a contract that expects a sp.TUnit argument (e.g. an account that does not result in any actions).

Example​

sp.send(destination, amount, message = None)

Abbreviation for:

sp.transfer(sp.unit, amount, sp.contract(sp.TUnit, destination).open_some(message = message))
Michelson TRANSFER_TOKENS

Create a new contract​

sp.create_contract(contract, storage = None, amount = sp.tez(0), baker = None)
Create a new contract from stored SmartPy contract with optional storage, amount and baker.

Example​

sp.create_contract(contract, storage = "abc", amount = sp.tez(2))

See reference Create Contract template.

Michelson CREATE_CONTRACT