Contracts and addresses โ
Following Michelson, there are two ways to point to other contracts in SmartPy:
- typed sp.TContract(
t
) for contracts with an entrypoint of typet
; - and untyped sp.TAddress.
The corresponding types in Michelson are
Michelson contractand
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 โ
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
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.
The sender address โ
sp.sender
The address that called the current entry point.
Example โ
address = sp.sender
More information available here.
Michelson SENDERThe 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 SOURCEOperations โ
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 โ
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 โ
address = sp.to_address(contract)
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 โ
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 returnssp.some(c)
, wherec
is a contract handle of type TContract(t
), ifaddress
, of type TAddress, points to a contract that expects a parameter of typet
. Otherwise it returnssp.none
.When
entry_point
is not empty, it returns the specific entrypoint specified by the stringentry_point
of the contract.t
must match the entrypoint's expected parameter type. Otherwise, it returnssp.none
.
Due to restrictions of Michelson, it only works properly for contracts with multiple entrypoints.
Michelson CONTRACTCast 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_ACCOUNTTransfer โ
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))
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