I tried to interact with other contracts but failed because Tezos.get_contract_opt
always returns None
. Here's the demo code (ligo v0.60.0):
receiver.jsligo - A contract that the "broadcaster" contract will call. It has a nat
var which will be increased by 1 whenever it's called.
type Storage = {
signal_received: nat,
};
type Parameter =
| ["Ping"];
type Return = [list<operation>, Storage];
const main = (ep: Parameter, store: Storage): Return =>
match(ep, {
Ping: (_p) => [
list([]),
{
...store,
signal_received: store.signal_received + (1 as nat)
}
],
});
broadcaster.jsligo - A contract that will call the "receiver" contract
type Storage = unit;
type Parameter =
| ["Ping_receiver"];
type Return = [list<operation>, Storage];
const ping_receiver = (_p: unit, store: Storage) => {
// [[[ put the receiver contract address here ]]]
const addr = "KT1EjK4pKLeVYdhSx1LNKj6j9jGMxn8e4mGs" as address;
const receiverContract =
match(Tezos.get_contract_opt(addr), {
Some: contract => contract,
None: () => failwith("Receiver contract not found.")
});
const op = Tezos.transaction(
["ping"],
0 as tez,
receiverContract
);
return [list([op]), store]
};
const main = (ep: Parameter, store: Storage): Return =>
match(ep, {
Ping_receiver: (p: unit) => ping_receiver(p, store),
});
I used taquito
to originate the contracts. The overall flow was:
- compile and originate the receiver contract
- put the newly originated receiver contract address in the broadcaster contract
- compile and originate the broadcaster contract
I ran into the "Receiver contract not found." error when trying to test out the broadcaster contract on Better Call Dev. But it works fine when interacting with the receiver contract directly.
- The receiver contract (Ghostnet)
- The broadcaster contract (Ghostnet)
The source code can be found at https://github.com/caaatisgood/ligo-inter-contract-invocation as well as the deployment script here.
unit
, while I'm expecting a contract that has string as parameter in the broadcaster contract. Addressed in github.com/caaatisgood/ligo-inter-contract-invocation/commit/…