Skip to content
LogoLogo

Interfaces and queries

An interface describes another contract as function headers. view methods are read-only and can be called immediately with query, which compiles to STATICCALL.

interfaces.lithe
(* An interface types another contract. `view` methods are read-only and run
   now, through STATICCALL. Other methods change state and can only be deferred. *)
 
interface Oracle is {
  view function price(asset : address) : uint;
  view function decimals() : uint8;
}
 
error StalePrice of uint;
 
storage is record {
  oracle : Oracle;
  lastPrice : uint;
}
 
(* An interface value is an address in the ABI. *)
external function setOracle(const oracle : Oracle) is {
  storage.oracle := oracle;
}
 
external function refresh(const asset : address)
  raises StalePrice
is {
  const price : uint := query storage.oracle.price(asset);
  assert(price > 0, StalePrice(price));
  storage.lastPrice := price;
}
 
external view function quote(const asset : address; const amount : uint) : uint is
  amount * query storage.oracle.price(asset)
  • query storage.oracle.price(asset) returns a uint right away. The EVM guarantees the oracle can't change state during the call.
  • quote is an external view that queries. Queries are allowed in views, but not in pure functions.