Skip to content
LogoLogo

Ether and time units

Ether amounts are wei. Points in time are timestamp. Durations such as 1days add to timestamps, and subtracting two timestamps gives a number of seconds.

units.lithe
import "std/evm";
 
storage is record {
  price : wei;
  unlockAt : timestamp;
}
 
external function configure() is {
  (* Ether units produce `wei`. *)
  storage.price := 1eth + 500gwei + 7wei;
 
  (* Time units produce durations; adding one to a timestamp gives a timestamp. *)
  storage.unlockAt := Evm.blockTimestamp() + 1days + 12hours + 30minutes;
}
 
external view function secondsLeft() : uint is {
  var left : uint := 0;
  if Evm.blockTimestamp() < storage.unlockAt then {
    (* timestamp - timestamp is a plain uint of seconds. *)
    left := storage.unlockAt - Evm.blockTimestamp();
  }
} with left
 
external pure function oneWeek() : uint is
  7days
 
(* `wei` scales by literals, and converts to `uint` explicitly. *)
external view function doublePrice() : wei is
  storage.price * 2
 
external view function priceInWei() : uint is
  uint(storage.price)
  • 1eth + 500gwei + 7wei is a wei value.
  • Evm.blockTimestamp() + 1days is a timestamp. A uint can't be stored in a timestamp field by accident.
  • Convert out explicitly with uint(price).