Skip to content

Commit

Permalink
chore: replace while loops with macros
Browse files Browse the repository at this point in the history
  • Loading branch information
thounyy committed Sep 11, 2024
1 parent 7d4c51a commit 88d389f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
4 changes: 3 additions & 1 deletion packages/actions/sources/upgrade_policies.move
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ public fun execute_upgrade(
upgrade(executable, multisig, Issuer {})
}

// step 5: consume the receipt to commit the upgrade
// step 5: consume the ticket to upgrade

// step 6: consume the receipt to commit the upgrade
public fun confirm_upgrade(
mut executable: Executable,
multisig: &mut Multisig,
Expand Down
29 changes: 13 additions & 16 deletions packages/multisig/sources/coin_operations.move
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,18 @@ public struct Issuer has copy, drop {}
// returns the IDs to use in a following proposal, sorted by "to_split" amounts
public fun merge_and_split<T: drop>(
multisig: &mut Multisig,
mut to_merge: vector<Receiving<Coin<T>>>, // there can be only one coin if we just want to split
to_merge: vector<Receiving<Coin<T>>>, // there can be only one coin if we just want to split
to_split: vector<u64>, // there can be no amount if we just want to merge
ctx: &mut TxContext
): vector<ID> {
multisig.assert_is_member(ctx);

// receive all coins
let mut coins = vector::empty();
while (!to_merge.is_empty()) {
let item = to_merge.pop_back();
to_merge.do!(|item| {
let coin = multisig.receive(Issuer {}, item);
coins.push_back(coin);
};
to_merge.destroy_empty();
});

let coin = merge(coins, ctx);
let ids = split(multisig, coin, to_split, ctx);
Expand All @@ -44,30 +42,29 @@ public fun merge_and_split<T: drop>(
}

fun merge<T: drop>(
mut coins: vector<Coin<T>>,
coins: vector<Coin<T>>,
ctx: &mut TxContext
): Coin<T> {
let mut merged = coin::zero<T>(ctx);
while (!coins.is_empty()) {
merged.join(coins.pop_back());
};
coins.destroy_empty();
coins.do!(|coin| {
merged.join(coin);
});

merged
}

fun split<T: drop>(
multisig: &Multisig,
mut coin: Coin<T>,
mut amounts: vector<u64>,
amounts: vector<u64>,
ctx: &mut TxContext
): vector<ID> {
let mut ids = vector::empty();
while (!amounts.is_empty()) {
let split = coin.split(amounts.remove(0), ctx);
ids.push_back(object::id(&split));
let ids = amounts.map!(|amount| {
let split = coin.split(amount, ctx);
let id = object::id(&split);
transfer::public_transfer(split, multisig.addr());
};
id
});
transfer::public_transfer(coin, multisig.addr());

ids
Expand Down

0 comments on commit 88d389f

Please sign in to comment.