Skip to content
Julian Konchunas edited this page Jul 1, 2020 · 1 revision

A router is a concept in Ligo that allows you to make foreign calls to any method specified in the contract. In Solidity, It is easy to call any method of a contract from outside. Just get the name from ABI, properly arrange arguments and you are good to go. However, in LIGO you can only call the main method. To work around the problem is a "router", namely: a special structure that allows users to pass the method name and its arguments straight into the main method. It will take care of the branching, make a specified call and return its value. Usually, developers of the contract do it manually, deciding which methods are accessible from the main entrypoint.

Consider the following Solidity code:

pragma solidity ^0.4.16;

contract eee {
    function method1(int y) public {
      int x = y + 1;
    }

    function method2(int y) public {
      int x = y + 2;
    }
}

Our compiler will automatically generate the following stubs for you:

type method1_args is unit;
type method2_args is unit;

type router_enum is
  | Method1 of method1_args
  | Method2 of method2_args;

function main (const action : router_enum; const self : state) : (list(operation) * state) is
  (case action of
  | Method1(match_action) -> method1(self)
  | Method2(match_action) -> method2(self)
  end);

Every method is wrapped into enum and given struct payload. Now, to call this method from the outside world (say, foreign contract) you have to supply Method1(<int_value>) to the contract. It will pass wrapped argument to the case shoulder. Also Ligo will compile this into special Michelson declaration called entrypoint. Names of router enum will be available as entrypoints for you to be used in blockexplorers and such.

Clone this wiki locally