Calling other smart functions
Smart functions can call other smart functions with the SmartFunction.call()
method, which returns a promise that resolves to a Jstz Response
object. Here is an example of calling another contract:
const targetContract: Address = "KT1L8ZzGDzaXZSTmzHkoF2azQRf7dCAfxtqx";
const response = await SmartFunction.call(
new Request(`tezos://${targetContract}`, {
method: "POST",
body: JSON.stringify({ message: "hello" }),
}),
);
console.log(await response.json());
The URL for the Request
object must be tezos://
followed by the address of a Jstz smart function. Smart functions cannot call external APIs or Tezos smart contracts directly. You can set the method in the Request
object but you cannot set the Referer
header because it automatically becomes the address of the smart function.
Errors
Failed calls to smart functions immediately revert all calls that came from that transaction and any side effects they may have had. For example, assume that a user calls smart function A, which causes function A to call smart function B. If smart function B throws an uncaught error, all of the calls are reverted, including the original call to function A.
When a chain of calls to smart functions fails in this way, these things happen:
- The storage of each smart function in the chain is set back to what it was before the initial request, like a database rollback.
- The XTZ balance of each smart function is restored to what it was before the initial request.
- Any further requests generated by the smart functions are cancelled.
You can catch errors in smart functions with try/catch blocks just like in ordinary JavaScript/TypeScript.