Skip to content
LogoLogo

Templates and choices

use weld::{Party, Asset, Tx, Verdict, Effects, template, choice};
 
#[weld::template]
struct Escrow {
    #[signatory] depositor: Party,
    #[signatory] beneficiary: Party,
    #[observer]  agent: Party,
    asset: Asset,
    amount: u128,
    expiry_bucket: Field,
}
 
#[weld::choices]
impl Escrow {
    #[choice(controllers = [self.depositor, self.beneficiary], consuming)]
    fn release(self, tx: Tx) -> (Verdict, Effects) {
        let v = Verdict::require(tx.time_bucket() < self.expiry_bucket, "escrow/expired");
        (v, Effects::new().pay_from_bound(self.beneficiary, self.asset, self.amount))
    }
 
    #[choice(controllers = [self.depositor], consuming)]
    fn refund(self, tx: Tx) -> (Verdict, Effects) {
        let v = Verdict::require(tx.time_bucket() >= self.expiry_bucket, "escrow/not-expired");
        (v, Effects::new().pay_from_bound(self.depositor, self.asset, self.amount))
    }
 
    #[choice(controllers = [self.agent], non_consuming)]
    fn note_inspection(self, tx: Tx) -> (Verdict, Effects) {
        (Verdict::ok(), Effects::none())            // compiles to consume-and-recreate
    }
}

Effects is the typed description of what the method creates: records (create(T { .. })), party notes (pay(party, asset, amount)), bound notes (bind(record, asset, amount)), payments out of consumed bound notes (pay_from_bound), flows (flow_out(edge, asset, amount), flow_in(edge, asset, amount)), and disclosures. The framework turns it into created commitments, conservation witnesses, and envelopes. A method never sees a hash, a nullifier, or a signature.