Skip to content
LogoLogo

Ether wallet

A minimal wallet: deposits are open to anyone, and withdrawals are limited to the owner.

ether-wallet.lithe
(* Anyone can send ether in. Only the owner can take it out. *)
 
import "std/address";
import "std/evm";
 
error NotOwner;
error AlreadyInitialized;
error Insufficient of wei;
 
event Deposit of record {
  indexed from : address;
  amount : wei;
}
 
storage is record {
  owner : address;
}
 
external function init()
  raises AlreadyInitialized
is {
  assert(storage.owner = Address.zero, AlreadyInitialized);
  storage.owner := Evm.sender();
}
 
external payable function deposit() is {
  emit Deposit { from = Evm.sender(); amount = Evm.value(); };
}
 
external function withdraw(const amount : wei)
  raises NotOwner | Insufficient
is {
  assert(Evm.sender() = storage.owner, NotOwner);
  assert(Evm.balance(Evm.self()) >= amount, Insufficient(Evm.balance(Evm.self())));
  defer Evm.transfer(storage.owner, amount);
}
 
external view function balance() : wei is
  Evm.balance(Evm.self())