Skip to content

Commit

Permalink
Merge pull request #36 from RGB-WG/ifaces
Browse files Browse the repository at this point in the history
Update for the refactored interfaces
  • Loading branch information
dr-orlovsky authored Mar 29, 2024
2 parents 3294005 + c7ed419 commit 3464d7d
Show file tree
Hide file tree
Showing 28 changed files with 1,282 additions and 1,315 deletions.
4 changes: 2 additions & 2 deletions 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 @@ -33,4 +33,4 @@ all = ["log"]
log = ["aluvm/log"]

[patch.crates-io]
rgb-std = { git = "https://github.com/RGB-WG/rgb-std", branch = "master" }
rgb-std = { git = "https://github.com/RGB-WG/rgb-std", branch = "iface" }
1 change: 0 additions & 1 deletion examples/rgb20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,4 @@ fn main() {
eprintln!("amount={state}, owner={seal}, witness={witness}");
}
eprintln!("totalSupply={}", contract.total_supply());
eprintln!("created={}", contract.created().to_local().unwrap());
}
35 changes: 21 additions & 14 deletions examples/rgb21.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ use bp::Txid;
use rgb_schemata::{uda_rgb21, uda_schema};
use rgbstd::containers::FileContent;
use rgbstd::interface::rgb21::{Allocation, EmbeddedMedia, OwnedFraction, TokenData, TokenIndex};
use rgbstd::interface::{rgb21, ContractBuilder};
use rgbstd::interface::{ContractBuilder, Rgb21, IfaceClass, IfaceWrapper};
use rgbstd::invoice::Precision;
use rgbstd::persistence::{Inventory, Stock};
use rgbstd::resolvers::ResolveHeight;
use rgbstd::stl::{self, DivisibleAssetSpec, RicardianContract, Timestamp};
use rgbstd::stl::{AssetSpec, AssetTerms, Attachment, MediaType, RicardianContract};
use rgbstd::validation::{ResolveWitness, WitnessResolverError};
use rgbstd::{GenesisSeal, WitnessAnchor, WitnessId, XAnchor, XChain, XPubWitness};
use sha2::{Digest, Sha256};
use strict_encoding::StrictDumb;

struct DumbResolver;
Expand All @@ -35,26 +36,35 @@ impl ResolveHeight for DumbResolver {

#[rustfmt::skip]
fn main() {
let spec = DivisibleAssetSpec::new("TEST", "Test uda", Precision::Indivisible);
let terms = RicardianContract::default();
let created = Timestamp::now();
let spec = AssetSpec::new("TEST", "Test uda", Precision::Indivisible);
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(2);

let file_bytes = std::fs::read("README.md").unwrap();
let mut hasher = Sha256::new();
hasher.update(file_bytes);
let file_hash = hasher.finalize();
let terms = AssetTerms {
text: RicardianContract::default(),
media: Some(Attachment {
ty: MediaType::with("text/*"),
digest: file_hash.into(),
}),
};
let preview = EmbeddedMedia {
ty: stl::MediaType::with("text/*"),
ty: MediaType::with("image/*"),
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(),
Rgb21::iface(),
uda_schema(),
uda_rgb21(),
).expect("schema fails to implement RGB21 interface")
Expand All @@ -65,9 +75,6 @@ fn main() {
.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")

Expand All @@ -82,12 +89,12 @@ fn main() {
debug_assert_eq!(contract_id, contract.contract_id());

eprintln!("{contract}");
contract.save_file("examples/rgb21-simplest.contract.rgb").expect("unable to save contract");
fs::write("examples/rgb21-simplest.contract.rgba", contract.to_ascii_armored_string()).expect("unable to save contract");
contract.save_file("examples/rgb21-simplest.rgb").expect("unable to save contract");
fs::write("examples/rgb21-simplest.rgba", contract.to_ascii_armored_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_iface(Rgb21::iface()).unwrap();
stock.import_schema(uda_schema()).unwrap();
stock.import_iface_impl(uda_rgb21()).unwrap();

Expand All @@ -101,7 +108,7 @@ fn main() {
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 contract = stock.contract_iface_id(contract_id, Rgb21::IFACE_ID).unwrap();
let nominal = contract.global("spec").unwrap();
eprintln!("{}", nominal[0]);
}
37 changes: 15 additions & 22 deletions examples/rgb25.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ use std::fs;
use amplify::hex::FromHex;
use bp::Txid;
use rgb_schemata::{cfa_rgb25, cfa_schema};
use rgbstd::interface::{rgb25, ContractBuilder, FilterIncludeAll, FungibleAllocation};
use rgbstd::interface::{ContractBuilder, FilterIncludeAll, FungibleAllocation, Rgb25, IfaceClass, IfaceWrapper};
use rgbstd::invoice::{Amount, Precision};
use rgbstd::persistence::{Inventory, Stock};
use rgbstd::resolvers::ResolveHeight;
use rgbstd::stl::{
Attachment, ContractData, Details, MediaType, Name, RicardianContract, Timestamp,
};
use rgbstd::stl::{AssetTerms, Attachment, Details, MediaType, Name, RicardianContract};
use rgbstd::validation::{ResolveWitness, WitnessResolverError};
use rgbstd::{GenesisSeal, WitnessAnchor, WitnessId, XAnchor, XChain, XPubWitness};
use rgbstd::containers::FileContent;
Expand All @@ -37,28 +35,26 @@ fn main() {
let name = Name::try_from("Test asset").unwrap();
let details = Details::try_from("details with ℧nicode characters").unwrap();
let precision = Precision::CentiMicro;
let terms = RicardianContract::default();

let file_bytes = std::fs::read("README.md").unwrap();
let mut hasher = Sha256::new();
hasher.update(file_bytes);
let file_hash = hasher.finalize();
let media = Some(Attachment {
ty: MediaType::with("text/*"),
digest: file_hash.into(),
});
let contract_data = ContractData {
terms,
media,
let terms = AssetTerms {
text: RicardianContract::default(),
media: Some(Attachment {
ty: MediaType::with("text/*"),
digest: file_hash.into(),
}),
};
let created = Timestamp::now();
let beneficiary_txid =
Txid::from_hex("14295d5bb1a191cdb6286dc0944df938421e3dfcbf0811353ccac4100c2068c5").unwrap();
let beneficiary = XChain::Bitcoin(GenesisSeal::tapret_first_rand(beneficiary_txid, 1));

const ISSUE: u64 = 1_000_000_000_000;

let contract = ContractBuilder::testnet(
rgb25(),
Rgb25::iface(),
cfa_schema(),
cfa_rgb25()
).expect("schema fails to implement RGB25 interface")
Expand All @@ -72,13 +68,10 @@ fn main() {
.add_global_state("precision", precision)
.expect("invalid precision")

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

.add_global_state("issuedSupply", Amount::from(ISSUE))
.expect("invalid issuedSupply")

.add_global_state("data", contract_data)
.add_global_state("terms", terms)
.expect("invalid contract data")

.add_fungible_state("assetOwner", beneficiary, ISSUE)
Expand All @@ -91,12 +84,12 @@ fn main() {
debug_assert_eq!(contract_id, contract.contract_id());

eprintln!("{contract}");
contract.save_file("examples/rgb25-simplest.contract.rgb").expect("unable to save contract");
fs::write("examples/rgb25-simplest.contract.rgba", contract.to_string()).expect("unable to save contract");
contract.save_file("examples/rgb25-simplest.rgb").expect("unable to save contract");
fs::write("examples/rgb25-simplest.rgba", contract.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(rgb25()).unwrap();
stock.import_iface(Rgb25::iface()).unwrap();
stock.import_schema(cfa_schema()).unwrap();
stock.import_iface_impl(cfa_rgb25()).unwrap();

Expand All @@ -110,7 +103,7 @@ fn main() {
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, rgb25().iface_id()).unwrap();
let contract = stock.contract_iface_id(contract_id, Rgb25::IFACE_ID).unwrap();
let name = contract.global("name").unwrap();
let allocations = contract.fungible("assetOwner", &FilterIncludeAll).unwrap();
eprintln!("{}", Name::from_strict_val_unchecked(&name[0]));
Expand Down
Binary file modified interfaces/RGB20.rgb
Binary file not shown.
Loading

0 comments on commit 3464d7d

Please sign in to comment.