Skip to content

Commit

Permalink
Merge branch 'master' into runes-etched-in-block
Browse files Browse the repository at this point in the history
  • Loading branch information
lugondev committed Mar 26, 2024
2 parents 65a7e82 + 4533fde commit b00852b
Show file tree
Hide file tree
Showing 47 changed files with 2,062 additions and 1,446 deletions.
322 changes: 161 additions & 161 deletions src/index.rs

Large diffs are not rendered by default.

212 changes: 195 additions & 17 deletions src/index/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,34 +34,51 @@ pub struct RuneEntry {
pub divisibility: u8,
pub etching: Txid,
pub mint: Option<MintEntry>,
pub mints: u64,
pub mints: u128,
pub number: u64,
pub premine: u128,
pub spaced_rune: SpacedRune,
pub supply: u128,
pub symbol: Option<char>,
pub timestamp: u32,
}

impl RuneEntry {
pub fn mintable(&self, block_height: Height, block_time: u32) -> Result<u128, MintError> {
let Some(mint) = self.mint else {
return Err(MintError::Unmintable(self.spaced_rune.rune));
return Err(MintError::Unmintable);
};

if let Some(end) = mint.end {
if block_height.0 >= end {
return Err(MintError::End((self.spaced_rune.rune, end)));
return Err(MintError::End(end));
}
}

if let Some(deadline) = mint.deadline {
if block_time >= deadline {
return Err(MintError::Deadline((self.spaced_rune.rune, deadline)));
return Err(MintError::Deadline(deadline));
}
}

Ok(mint.limit.unwrap_or(runes::MAX_LIMIT))
let cap = mint.cap.unwrap_or_default();

if self.mints >= cap {
return Err(MintError::Cap(cap));
}

Ok(mint.limit.unwrap_or_default())
}

pub fn supply(&self) -> u128 {
self.premine + self.mints * self.mint.and_then(|mint| mint.limit).unwrap_or_default()
}

pub fn pile(&self, amount: u128) -> Pile {
Pile {
amount,
divisibility: self.divisibility,
symbol: self.symbol,
}
}
}

Expand All @@ -70,23 +87,24 @@ pub(super) type RuneEntryValue = (
u8, // divisibility
(u128, u128), // etching
Option<MintEntryValue>, // mint parameters
u64, // mints
u128, // mints
u64, // number
u128, // premine
(u128, u32), // spaced rune
u128, // supply
Option<char>, // symbol
u32, // timestamp
);

#[derive(Debug, PartialEq, Copy, Clone, Serialize, Deserialize, Default)]
pub struct MintEntry {
pub cap: Option<u128>, // mint cap
pub deadline: Option<u32>, // unix timestamp
pub end: Option<u32>, // block height
pub limit: Option<u128>, // claim amount
}

type MintEntryValue = (
Option<u128>, // cap
Option<u32>, // deadline
Option<u32>, // end
Option<u128>, // limit
Expand All @@ -103,7 +121,6 @@ impl Default for RuneEntry {
number: 0,
premine: 0,
spaced_rune: SpacedRune::default(),
supply: 0,
symbol: None,
timestamp: 0,
}
Expand All @@ -123,7 +140,6 @@ impl Entry for RuneEntry {
number,
premine,
(rune, spacers),
supply,
symbol,
timestamp,
): RuneEntryValue,
Expand All @@ -141,7 +157,8 @@ impl Entry for RuneEntry {
high[14], high[15],
])
},
mint: mint.map(|(deadline, end, limit)| MintEntry {
mint: mint.map(|(cap, deadline, end, limit)| MintEntry {
cap,
deadline,
end,
limit,
Expand All @@ -153,7 +170,6 @@ impl Entry for RuneEntry {
rune: Rune(rune),
spacers,
},
supply,
symbol,
timestamp,
}
Expand All @@ -178,16 +194,16 @@ impl Entry for RuneEntry {
},
self.mint.map(
|MintEntry {
cap,
deadline,
end,
limit,
}| (deadline, end, limit),
}| (cap, deadline, end, limit),
),
self.mints,
self.number,
self.premine,
(self.spaced_rune.rune.0, self.spaced_rune.spacers),
self.supply,
self.symbol,
self.timestamp,
)
Expand Down Expand Up @@ -492,6 +508,7 @@ mod tests {
0x1E, 0x1F,
]),
mint: Some(MintEntry {
cap: Some(1),
deadline: Some(2),
end: Some(4),
limit: Some(5),
Expand All @@ -503,7 +520,6 @@ mod tests {
rune: Rune(7),
spacers: 8,
},
supply: 9,
symbol: Some('a'),
timestamp: 10,
};
Expand All @@ -515,12 +531,11 @@ mod tests {
0x0F0E0D0C0B0A09080706050403020100,
0x1F1E1D1C1B1A19181716151413121110,
),
Some((Some(2), Some(4), Some(5))),
Some((Some(1), Some(2), Some(4), Some(5))),
11,
6,
12,
(7, 8),
9,
Some('a'),
10,
);
Expand Down Expand Up @@ -550,4 +565,167 @@ mod tests {

assert_eq!(actual, expected);
}

#[test]
fn mintable() {
assert_eq!(
RuneEntry::default().mintable(Height(0), 0),
Err(MintError::Unmintable)
);

assert_eq!(
RuneEntry {
mint: Some(MintEntry {
end: Some(1),
limit: Some(1000),
cap: Some(1),
..default()
}),
..default()
}
.mintable(Height(0), 0),
Ok(1000),
);

assert_eq!(
RuneEntry {
mint: Some(MintEntry {
end: Some(1),
limit: Some(1000),
cap: Some(1),
..default()
}),
..default()
}
.mintable(Height(1), 0),
Err(MintError::End(1)),
);

assert_eq!(
RuneEntry {
mint: Some(MintEntry {
deadline: Some(1),
limit: Some(1000),
cap: Some(1),
..default()
}),
..default()
}
.mintable(Height(0), 0),
Ok(1000),
);

assert_eq!(
RuneEntry {
mint: Some(MintEntry {
deadline: Some(1),
limit: Some(1000),
cap: Some(1),
..default()
}),
..default()
}
.mintable(Height(0), 1),
Err(MintError::Deadline(1)),
);

assert_eq!(
RuneEntry {
mint: Some(MintEntry {
cap: Some(1),
limit: Some(1000),
..default()
}),
mints: 0,
..default()
}
.mintable(Height(0), 0),
Ok(1000),
);

assert_eq!(
RuneEntry {
mint: Some(MintEntry {
cap: Some(1),
limit: Some(1000),
..default()
}),
mints: 1,
..default()
}
.mintable(Height(0), 0),
Err(MintError::Cap(1)),
);

assert_eq!(
RuneEntry {
mint: Some(MintEntry {
cap: None,
limit: Some(1000),
..default()
}),
mints: 0,
..default()
}
.mintable(Height(0), 0),
Err(MintError::Cap(0)),
);
}

#[test]
fn supply() {
assert_eq!(
RuneEntry {
mint: Some(MintEntry {
limit: Some(1000),
..default()
}),
mints: 0,
..default()
}
.supply(),
0
);

assert_eq!(
RuneEntry {
mint: Some(MintEntry {
limit: Some(1000),
..default()
}),
mints: 1,
..default()
}
.supply(),
1000
);

assert_eq!(
RuneEntry {
mint: Some(MintEntry {
limit: Some(1000),
..default()
}),
mints: 0,
premine: 1,
..default()
}
.supply(),
1
);

assert_eq!(
RuneEntry {
mint: Some(MintEntry {
limit: Some(1000),
..default()
}),
mints: 1,
premine: 1,
..default()
}
.supply(),
1001
);
}
}
6 changes: 3 additions & 3 deletions src/index/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl Context {
for (id, entry) in runes {
pretty_assert_eq!(
outstanding.get(id).copied().unwrap_or_default(),
entry.supply - entry.burned
entry.supply() - entry.burned
);
}
}
Expand All @@ -160,7 +160,7 @@ impl Context {
self.rpc_server.broadcast_tx(TransactionTemplate {
inputs: &[(block_count, 0, 0, Witness::new())],
p2tr: true,
..Default::default()
..default()
});

self.mine_blocks(RUNE_COMMIT_INTERVAL.into());
Expand Down Expand Up @@ -191,7 +191,7 @@ impl Context {
inputs: &[(block_count + 1, 1, 0, witness)],
op_return: Some(runestone.encipher()),
outputs,
..Default::default()
..default()
});

self.mine_blocks(1);
Expand Down
Loading

0 comments on commit b00852b

Please sign in to comment.