Skip to content

Commit

Permalink
Implement splits (ordinals#2530)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Oct 17, 2023
1 parent 5f1100e commit e1aceb2
Show file tree
Hide file tree
Showing 2 changed files with 1,187 additions and 12 deletions.
55 changes: 43 additions & 12 deletions src/index/updater/rune_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,12 @@ impl<'a, 'db, 'tx> RuneUpdater<'a, 'db, 'tx> {

if !burn {
for Edict { id, amount, output } in runestone.edicts {
let Ok(output) = usize::try_from(output) else {
continue;
};

// Skip edicts not referring to valid outputs
if output >= tx.output.len() as u128 {
if output > tx.output.len() {
continue;
}

Expand All @@ -119,19 +123,46 @@ impl<'a, 'db, 'tx> RuneUpdater<'a, 'db, 'tx> {
}
};

// Get the allocatable amount
let amount = if amount == 0 {
*balance
} else {
amount.min(*balance)
let mut allocate = |balance: &mut u128, amount: u128, output: usize| {
if amount > 0 {
*balance -= amount;
*allocated[output].entry(id).or_default() += amount;
}
};

// If the amount to be allocated is greater than zero,
// deduct it from the remaining balance, and increment
// the allocated entry.
if amount > 0 {
*balance -= amount;
*allocated[output as usize].entry(id).or_default() += amount;
if output == tx.output.len() {
// find non-OP_RETURN outputs
let destinations = tx
.output
.iter()
.enumerate()
.filter_map(|(output, tx_out)| {
(!tx_out.script_pubkey.is_op_return()).then_some(output)
})
.collect::<Vec<usize>>();

if amount == 0 {
// if amount is zero, divide balance between eligible outputs
let amount = *balance / destinations.len() as u128;

for output in destinations {
allocate(balance, amount, output);
}
} else {
// if amount is non-zero, distribute amount to eligible outputs
for output in destinations {
allocate(balance, amount.min(*balance), output);
}
}
} else {
// Get the allocatable amount
let amount = if amount == 0 {
*balance
} else {
amount.min(*balance)
};

allocate(balance, amount, output);
}
}
}
Expand Down
Loading

0 comments on commit e1aceb2

Please sign in to comment.