Skip to content
On this page

Contracts

Within SmartPy modules, contracts are represented with the type sp.contract and are constructed with the sp.contract(type, address, entrypoint) function. For example, to call a contract from an entrypoint in another contract, you create an sp.contract type to represent the contract to call.

INFO

The sp.contract type is different from the contract objects that you create in test scenarios. The sp.contract type is used in entrypoint code to call other contracts from a deployed smart contract, not in test scenarios.

sp.contract(type, address) → sp.option[sp.contract[t]]
sp.contract(type, address, entrypoint) → sp.option[sp.contract[t]]

Returns a sp.Some(sp.contract[t]) if this contract exists, None otherwise.

If given, the entrypoint parameter must be named and be a constant string. For example, sp.contract(sp.nat, sp.address("KT1xxxx"), entrypoint="approve")

If the entrypoint parameter is absent, the entrypoint is taken from the address, as in KT1AafHA1C1vk959wvHWBispY9Y2f3fxBUUo%approve. If the address doesn't contain an entrypoint, the entrypoint is default. For more information about the default entrypoint, see Implementation details: the default entrypoint on docs.tezos.com.

Then you can call the contract with the sp.transfer function, as in this example:

smartpy
address = sp.address("KT1R2LTg3mQoLvHtUjo2xSi7RMBUJ1sJkDiD")
contract_opt = sp.contract(sp.int, address, entrypoint = "increment")
with sp.match(contract_opt):
  with sp.case.Some as contract:
      sp.transfer(sp.int(5), sp.mutez(0), contract)
  with None:
      sp.trace("Failed to find contract")

For more information about calling contracts from contracts, see Operations.

sp.self_entrypoint(entrypoint_name) → sp.contract[t]

Returns an sp.contract[t] type corresponding to the contract.

The entrypoint_name argument must be a constant string, not an expression.

sp.to_address(c: sp.contract[t]) → sp.address

sp.to_address(c) returns the address of a value of type sp.contract.

If the sp.contract[t] designates a specific entrypoint, the entrypoint is included in the address. For example, this address includes the "approve" entrypoint: sp.address("KT1AafHA1C1vk959wvHWBispY9Y2f3fxBUUo%approve").