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,Noneotherwise.If given, the
entrypointparameter must be named and be a constant string. For example,sp.contract(sp.nat, sp.address("KT1xxxx"), entrypoint="approve")If the
entrypointparameter is absent, the entrypoint is taken from the address, as inKT1AafHA1C1vk959wvHWBispY9Y2f3fxBUUo%approve. If the address doesn't contain an entrypoint, the entrypoint isdefault. 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.transferfunction, as in this example:smartpyaddress = sp.address("KT1R2LTg3mQoLvHtUjo2xSi7RMBUJ1sJkDiD") contract_opt = sp.contract(sp.int, address, entrypoint="increment") match contract_opt: case Some(contract): sp.transfer(sp.int(5), sp.mutez(0), contract) case 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_nameargument 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 typesp.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").