Skip to content

Commit

Permalink
Encode claims in tag
Browse files Browse the repository at this point in the history
  • Loading branch information
casey committed Mar 2, 2024
1 parent 3f59cf8 commit 80be33b
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 96 deletions.
109 changes: 49 additions & 60 deletions src/index/updater/rune_updater.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use {
super::*,
crate::runes::{varint, Edict, Runestone, CLAIM_BIT},
crate::runes::{varint, Edict, Runestone},
};

fn claim(id: u128) -> Option<u128> {
(id & CLAIM_BIT != 0).then_some(id ^ CLAIM_BIT)
struct Claim {
id: u128,
limit: u128,
}

struct Allocation {
Expand Down Expand Up @@ -144,39 +145,23 @@ impl<'a, 'db, 'tx> RuneUpdater<'a, 'db, 'tx> {
None => None,
};

if !burn {
let mut mintable: HashMap<u128, u128> = HashMap::new();

let mut claims = runestone
.edicts
.iter()
.filter_map(|edict| claim(edict.id))
.collect::<Vec<u128>>();
claims.sort();
claims.dedup();
for id in claims {
if let Ok(key) = RuneId::try_from(id) {
if let Some(entry) = self.id_to_entry.get(&key.store())? {
let entry = RuneEntry::load(entry.value());
if let Some(mint) = entry.mint {
if let Some(end) = mint.end {
if self.height >= end {
continue;
}
}
if let Some(deadline) = mint.deadline {
if self.timestamp >= deadline {
continue;
}
}
mintable.insert(id, mint.limit.unwrap_or(runes::MAX_LIMIT));
}
}
}
}
if let Some(claim) = runestone
.claim
.and_then(|id| self.claim(id).transpose())
.transpose()?
{
*unallocated.entry(claim.id).or_default() += claim.limit;

let limits = mintable.clone();
let update = self
.updates
.entry(RuneId::try_from(claim.id).unwrap())
.or_default();

update.mints += 1;
update.supply += claim.limit;
}

if !burn {
for Edict { id, amount, output } in runestone.edicts {
let Ok(output) = usize::try_from(output) else {
continue;
Expand All @@ -196,11 +181,6 @@ impl<'a, 'db, 'tx> RuneUpdater<'a, 'db, 'tx> {
Some(Allocation { balance, id, .. }) => (balance, *id),
None => continue,
}
} else if let Some(claim) = claim(id) {
match mintable.get_mut(&claim) {
Some(balance) => (balance, claim),
None => continue,
}
} else {
// Get the unallocated balance of the given ID
match unallocated.get_mut(&id) {
Expand Down Expand Up @@ -256,19 +236,6 @@ impl<'a, 'db, 'tx> RuneUpdater<'a, 'db, 'tx> {
allocate(balance, amount, output);
}
}

// increment entries with minted runes
for (id, amount) in mintable {
let minted = limits[&id] - amount;
if minted > 0 {
let update = self
.updates
.entry(RuneId::try_from(id).unwrap())
.or_default();
update.mints += 1;
update.supply += minted;
}
}
}

if let Some(Allocation {
Expand Down Expand Up @@ -410,15 +377,37 @@ impl<'a, 'db, 'tx> RuneUpdater<'a, 'db, 'tx> {

Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;
fn claim(&self, id: u128) -> Result<Option<Claim>> {
let Ok(key) = RuneId::try_from(id) else {
return Ok(None);
};

let Some(entry) = self.id_to_entry.get(&key.store())? else {
return Ok(None);
};

let entry = RuneEntry::load(entry.value());

let Some(mint) = entry.mint else {
return Ok(None);
};

if let Some(end) = mint.end {
if self.height >= end {
return Ok(None);
}
}

if let Some(deadline) = mint.deadline {
if self.timestamp >= deadline {
return Ok(None);
}
}

#[test]
fn claim_from_id() {
assert_eq!(claim(1), None);
assert_eq!(claim(1 | CLAIM_BIT), Some(1));
Ok(Some(Claim {
id,
limit: mint.limit.unwrap_or(runes::MAX_LIMIT),
}))
}
}
Loading

0 comments on commit 80be33b

Please sign in to comment.