Skip to content

Commit

Permalink
iface: add convenience methods to Rgb20 wrapper for retrieving supply…
Browse files Browse the repository at this point in the history
… info
  • Loading branch information
dr-orlovsky committed Jun 14, 2023
1 parent bedcb53 commit 882ad6b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ wasm-bindgen-test = "0.3"
features = [ "all" ]

[patch.crates-io]
strict_types = { git = "https://github.com/strict-types/strict-types", branch = "develop" }
strict_types = { git = "https://github.com/strict-types/strict-types" }
commit_verify = { git = "https://github.com/LNP-BP/client_side_validation" }
bp-core = { git = "https://github.com/BP-WG/bp-core" }
aluvm = { git = "https://github.com/AluVM/rust-aluvm" }
Expand Down
52 changes: 51 additions & 1 deletion std/src/interface/rgb20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::iter::Sum;

use amplify::confinement::SmallOrdSet;
use bp::bc::stl::bitcoin_stl;
use strict_encoding::{StrictDeserialize, StrictSerialize};
use strict_types::{CompileError, LibBuilder, TypeLib};
use strict_types::{CompileError, LibBuilder, StrictVal, TypeLib};

use super::{
AssignIface, GenesisIface, GlobalIface, Iface, OwnedIface, Req, TransitionIface, VerNo,
Expand All @@ -48,6 +50,25 @@ pub const LIB_ID_RGB20: &str = "ethnic_raja_gloria_9tSQiAn1aGijb2F892JxTqcHDgmri
)]
pub struct Amount(u64);

impl StrictSerialize for Amount {}
impl StrictDeserialize for Amount {}

impl Amount {
pub fn zero() -> Self { Amount(0) }

pub fn one() -> Self { Amount(1) }

pub fn from_strict_val_unchecked(value: &StrictVal) -> Self {
value.unwrap_uint::<u64>().into()
}
}

impl Sum for Amount {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
iter.fold(Amount::zero(), |acc, i| acc + i)
}
}

#[derive(Clone, Eq, PartialEq, Hash, Debug, Default)]
#[derive(StrictType, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB20)]
Expand Down Expand Up @@ -296,6 +317,35 @@ impl Rgb20 {
.expect("RGB20 interface requires global `spec`")[0];
DivisibleAssetSpec::from_strict_val_unchecked(strict_val)
}

pub fn total_issued_supply(&self) -> Amount {
self.0
.global("issuedSupply")
.expect("RGB20 interface requires global `issuedSupply`")
.iter()
.map(Amount::from_strict_val_unchecked)
.sum()
}

pub fn total_burned_supply(&self) -> Amount {
self.0
.global("burnedSupply")
.unwrap_or_default()
.iter()
.map(Amount::from_strict_val_unchecked)
.sum()
}

pub fn total_replaced_supply(&self) -> Amount {
self.0
.global("replacedSupply")
.unwrap_or_default()
.iter()
.map(Amount::from_strict_val_unchecked)
.sum()
}

pub fn total_supply(&self) -> Amount { self.total_issued_supply() - self.total_burned_supply() }
}

#[cfg(test)]
Expand Down

0 comments on commit 882ad6b

Please sign in to comment.