Skip to content
On this page

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 entrypoint) templates.

Literal โ€‹

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

Example โ€‹

python
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 โ€‹

python
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 โ€‹

python
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 โ€‹

python
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 โ€‹

python
address = sp.source

More information available here.

Michelson SOURCE

Operations โ€‹

Get entrypoint from current contract โ€‹

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

Example โ€‹

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

If entry_point is empty, then the current entrypoint 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 โ€‹

python
address = sp.to_address(contract)
Michelson ADDRESS

Get entrypoint 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 โ€‹

python
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 โ€‹

python
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 entrypoint specified by the string entry_point of the contract. t must match the entrypoint's expected parameter type. Otherwise, it returns sp.none.

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

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 โ€‹

python
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 โ€‹

python
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 โ€‹

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

Abbreviation for:

python
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 โ€‹

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

See reference Create Contract template.

Michelson CREATE_CONTRACT