Skip to content

Commit

Permalink
feat: add udc schema impl
Browse files Browse the repository at this point in the history
  • Loading branch information
crisdut committed Jan 15, 2024
1 parent d0565ee commit 501bd0f
Show file tree
Hide file tree
Showing 12 changed files with 700 additions and 84 deletions.
195 changes: 123 additions & 72 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,9 @@ serde_yaml = "0.9.27"

[features]
all=[]


[patch.crates-io]
aluvm = { path = "../rust-aluvm" }
rgb-core = { path = "../rgb-core" }
rgb-std = { git = "https://github.com/RGB-WG/rgb-std", branch = "v0.11" }
4 changes: 2 additions & 2 deletions examples/rgb20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ fn main() {
eprintln!("\nThe issued contract data:");
eprintln!("{}", serde_json::to_string(&contract.spec()).unwrap());

for FungibleAllocation { owner, witness, value } in allocations {
eprintln!("amount={value}, owner={owner}, witness={witness}");
for FungibleAllocation { seal, state, witness, .. } in allocations {
eprintln!("amount={}, owner={seal}, witness={witness}", state.value());
}
eprintln!("totalSupply={}", contract.total_supply());
eprintln!("created={}", contract.created().to_local().unwrap());
Expand Down
98 changes: 91 additions & 7 deletions examples/rgb21.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use amplify::confinement::SmallBlob;
use amplify::hex::FromHex;
use amplify::Wrapper;
use bp::Txid;
use rgb_schemata::{uda_rgb21, uda_schema};
use rgb_schemata::{uda_rgb21, uda_schema, udc_schema, udc_rgb21};
use rgbstd::containers::BindleContent;
use rgbstd::interface::rgb21::{Allocation, EmbeddedMedia, OwnedFraction, TokenData, TokenIndex};
use rgbstd::interface::{rgb21, ContractBuilder};
Expand All @@ -32,8 +32,7 @@ impl ResolveHeight for DumbResolver {
}
}

#[rustfmt::skip]
fn main() {
fn uda() {
let spec = DivisibleAssetSpec::new("TEST", "Test uda", Precision::Indivisible);
let terms = RicardianContract::default();
let created = Timestamp::now();
Expand All @@ -42,16 +41,16 @@ fn main() {
let beneficiary = XChain::Bitcoin(GenesisSeal::tapret_first_rand(beneficiary_txid, 1));

let fraction = OwnedFraction::from_inner(1);
let index = TokenIndex::from_inner(2);
let index = TokenIndex::from_inner(1);

let preview = EmbeddedMedia {
ty: stl::MediaType::with("text/*"),
data: SmallBlob::try_from_iter(vec![0, 0]).expect("invalid data"),
};

let token_data = TokenData { index, preview: Some(preview), ..Default::default() };

let allocation = Allocation::with(index, fraction);

let contract = ContractBuilder::testnet(
rgb21(),
uda_schema(),
Expand Down Expand Up @@ -82,8 +81,8 @@ fn main() {

let bindle = contract.bindle();
eprintln!("{bindle}");
bindle.save("examples/rgb21-simplest.contract.rgb").expect("unable to save contract");
fs::write("examples/rgb21-simplest.contract.rgba", bindle.to_string()).expect("unable to save contract");
bindle.save("examples/rgb21-uda.rgb").expect("unable to save contract");
fs::write("examples/rgb21-uda.rgba", bindle.to_string()).expect("unable to save contract");

// Let's create some stock - an in-memory stash and inventory around it:
let mut stock = Stock::default();
Expand All @@ -105,3 +104,88 @@ fn main() {
let nominal = contract.global("spec").unwrap();
eprintln!("{}", nominal[0]);
}

fn udc() {
let spec = DivisibleAssetSpec::new("TEST", "Test udc", Precision::Indivisible);
let terms = RicardianContract::default();
let created = Timestamp::now();
let beneficiary_txid =
Txid::from_hex("14295d5bb1a191cdb6286dc0944df938421e3dfcbf0811353ccac4100c2068c5").unwrap();
let beneficiary = XChain::Bitcoin(GenesisSeal::tapret_first_rand(beneficiary_txid, 1));

let fraction = OwnedFraction::from_inner(1);
let index = TokenIndex::from_inner(1);

let token_data_1 = TokenData { index, ..Default::default() };
let allocation_1 = Allocation::with(index, fraction);

let index = TokenIndex::from_inner(2);
let token_data_2 = TokenData { index, ..Default::default() };
let allocation_2 = Allocation::with(index, fraction);
let beneficiary_2 = XChain::Bitcoin(GenesisSeal::tapret_first_rand(beneficiary_txid, 2));

let contract = ContractBuilder::testnet(
rgb21(),
udc_schema(),
udc_rgb21(),
).expect("schema fails to implement RGB21 interface")

.add_global_state("tokens", token_data_1)
.expect("invalid token data")

.add_global_state("tokens", token_data_2)
.expect("invalid token data")

.add_global_state("spec", spec)
.expect("invalid nominal")

.add_global_state("created", created)
.expect("invalid creation date")

.add_global_state("terms", terms)
.expect("invalid contract text")

.add_data("assetOwner", beneficiary, allocation_1)
.expect("invalid asset blob")

.add_data("assetOwner", beneficiary_2, allocation_2)
.expect("invalid asset blob")

.issue_contract()
.expect("contract doesn't fit schema requirements");
eprintln!("{}", serde_yaml::to_string(&contract.genesis).unwrap());

let contract_id = contract.contract_id();
debug_assert_eq!(contract_id, contract.contract_id());

let bindle = contract.bindle();
eprintln!("{bindle}");
bindle.save("examples/rgb21-udc.rgb").expect("unable to save contract");
fs::write("examples/rgb21-udc.rgba", bindle.to_string()).expect("unable to save contract");

// Let's create some stock - an in-memory stash and inventory around it:
let mut stock = Stock::default();
stock.import_iface(rgb21()).unwrap();
stock.import_schema(udc_schema()).unwrap();
stock.import_iface_impl(udc_rgb21()).unwrap();

// Noe we verify our contract consignment and add it to the stock
let verified_contract = match bindle.unbindle().validate(&mut DumbResolver, true) {
Ok(consignment) => consignment,
Err(consignment) => {
panic!("can't produce valid consignment. Report: {}", consignment.validation_status().expect("status always present upon validation"));
}
};
stock.import_contract(verified_contract, &mut DumbResolver).unwrap();

// Reading contract state through the interface from the stock:
let contract = stock.contract_iface_id(contract_id, rgb21().iface_id()).unwrap();
let nominal = contract.global("spec").unwrap();
eprintln!("{}", nominal[0]);
}

#[rustfmt::skip]
fn main() {
uda();
udc();
}
4 changes: 2 additions & 2 deletions examples/rgb25.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ fn main() {
let allocations = contract.fungible("assetOwner", &FilterIncludeAll).unwrap();
eprintln!("{}", Name::from_strict_val_unchecked(&name[0]));

for FungibleAllocation { owner, witness, value } in allocations {
eprintln!("(amount={value}, owner={owner}, witness={witness})");
for FungibleAllocation { seal, state, witness, .. } in allocations {
eprintln!("amount={}, owner={seal}, witness={witness}", state.value());
}
}
Binary file added schemata/UniqueDigitalCollection-RGB21.rgb
Binary file not shown.
12 changes: 12 additions & 0 deletions schemata/UniqueDigitalCollection-RGB21.rgba
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-----BEGIN RGB INTERFACE IMPLEMENTATION-----
Id: QE7VoS-Mw3Ka3Xy-rHw1jGnp-FZTHi87Z-PeurT8Gd-f6Ybg
Mnemonic: radius-media-engine
IfaceId: 8Q9TfV-aJtgbWFw-entqAA5f-7hYUGjG4-xaAD9eo6-hmcfnU
SchemaId: 9mL5Nv-2s5vHPUL-Nvy2rzHg-57an1g9R-nErqoz4R-cDnAyE

0D?H4raMhQ`0uVqz>E~9^2wn6Fv1sxnv<UcvUzu)DQ)k&hUh!fbn@NR0#zSot#D$
;MEUwpQg_}a0fYIF9R$z^1aoj@V*mgE0MiEtV{&C-bY)}!0000r2nBRya&2<}000
0s2nKX-Yh`Y8000000iX{GVRLh3bWe9~WpV%j000015GM#!a$#<BW@T~!0000000
000

-----END RGB INTERFACE IMPLEMENTATION-----
Binary file added schemata/UniqueDigitalCollection.rgb
Binary file not shown.
Loading

0 comments on commit 501bd0f

Please sign in to comment.