0

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:

  1. compile and originate the receiver contract
  2. put the newly originated receiver contract address in the broadcaster contract
  3. 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 source code can be found at https://github.com/caaatisgood/ligo-inter-contract-invocation as well as the deployment script here.

2

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.