Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement splits #2530

Merged
merged 10 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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