Skip to content

Commit

Permalink
Refactor sweep into separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
Quantumplation committed Feb 20, 2025
1 parent b3b9d7c commit d1e2032
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 20 deletions.
30 changes: 30 additions & 0 deletions lib/sweep.ak
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use aiken/interval.{Interval, is_entirely_after}
use cardano/address.{Credential}
use cardano/assets.{Lovelace, lovelace_of, without_lovelace}
use cardano/transaction.{Input, Output}
use utilities.{input_sum, output_sum}

pub fn logic(
validity_range: Interval<Int>,
expiration: Int,
inputs: List<Input>,
outputs: List<Output>,
account: Credential,
treasury_donation: Option<Lovelace>,
) {
// First, check the authorization
expect is_entirely_after(validity_range, expiration)
// satisfied(sweep, extra_signatories, validity_range, withdrawals),
let input_sum = input_sum(inputs, account)
let output_sum = output_sum(outputs, account)

// Any ADA from the inputs that *isn't* retained in the outputs, must be donated to the treasury
expect Some(donation) = treasury_donation
expect donation >= lovelace_of(input_sum) - lovelace_of(output_sum)

// And any native assets must be retained at the script address
expect or {
assets.is_zero(without_lovelace(input_sum)),
without_lovelace(input_sum) == without_lovelace(output_sum),
}
}
32 changes: 12 additions & 20 deletions validators/treasury.ak
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use aiken/collection/list
use aiken/collection/pairs
use aiken/interval.{is_entirely_after}
use cardano/address.{Credential}
use cardano/assets.{lovelace_of, without_lovelace}
use cardano/assets.{lovelace_of}
use cardano/transaction.{OutputReference, Transaction, find_input}
use sweep
use types.{Configuration, SpendRedeemer, Sweep}
use utilities.{input_sum, output_sum}
use utilities.{output_sum}

validator treasury(config: Configuration) {
spend(_d, redeemer: SpendRedeemer, utxo: OutputReference, self: Transaction) {
Expand All @@ -19,23 +19,15 @@ validator treasury(config: Configuration) {

when redeemer is {
// Funds can be swept back to the treasury if signed by a threshold of the auditing parties, or after an expiration
Sweep -> {
// First, check the authorization
expect is_entirely_after(validity_range, expiration)
// satisfied(sweep, extra_signatories, validity_range, withdrawals),
let input_sum = input_sum(self.inputs, account)
let output_sum = output_sum(self.outputs, account)

// Any ADA from the inputs that *isn't* retained in the outputs, must be donated to the treasury
expect Some(donation) = self.treasury_donation
expect donation >= lovelace_of(input_sum) - lovelace_of(output_sum)

// And any native assets must be retained at the script address
expect or {
assets.is_zero(without_lovelace(input_sum)),
without_lovelace(input_sum) == without_lovelace(output_sum),
}
}
Sweep ->
sweep.logic(
validity_range,
expiration,
self.inputs,
self.outputs,
account,
self.treasury_donation,
)
_ -> fail
}

Expand Down

0 comments on commit d1e2032

Please sign in to comment.