Skip to content

Commit

Permalink
Merge pull request #256 from diba-io/HT/minor-improvements
Browse files Browse the repository at this point in the history
Turn some unwraps into ? to better propagate errors instead of panics.
  • Loading branch information
cryptoquick authored Jul 10, 2023
2 parents baf434d + 5d27de8 commit d59bae6
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 67 deletions.
5 changes: 1 addition & 4 deletions src/bitcoin/psbt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ pub async fn sign_psbt(
blockchain.broadcast(&tx).await?;

let txid = tx.txid();
let tx = blockchain
.get_tx(&txid)
.await
.expect("tx that was just broadcasted now exists");
let tx = blockchain.get_tx(&txid).await?;

let mut sent = 0;
let mut received = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub const NOSTR_PATH: &str = "m/44h/1237h/0h";

// Magic number for versioning descriptors
pub const DIBA_DESCRIPTOR_VERSION: u8 = 0;
pub const DIBA_MAGIC_NO: [u8; 4] = [b'D', b'I', b'B', b'A'];
pub const DIBA_MAGIC_NO: [u8; 4] = *b"DIBA";
pub const DIBA_DESCRIPTOR: [u8; 5] = [
DIBA_MAGIC_NO[0],
DIBA_MAGIC_NO[1],
Expand Down
19 changes: 8 additions & 11 deletions src/rgb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,10 +426,7 @@ pub async fn transfer_asset(sk: &str, request: RgbTransferRequest) -> Result<Rgb
let psbt_hex = psbt.to_string();
let consig = RgbTransferResponse {
consig_id: transfer.bindle_id().to_string(),
consig: transfer
.to_strict_serialized::<U16>()
.expect("invalid transfer serialization")
.to_hex(),
consig: transfer.to_strict_serialized::<U16>()?.to_hex(),
psbt: psbt_hex,
commit: commit_hex,
};
Expand Down Expand Up @@ -529,7 +526,7 @@ pub async fn list_contracts(sk: &str) -> Result<ContractsResponse> {

let mut contracts = vec![];

for contract_id in stock.contract_ids().expect("invalid contracts state") {
for contract_id in stock.contract_ids()? {
let resp = extract_contract_by_id(contract_id, &mut stock, &mut resolver, &mut wallet)?;
contracts.push(resp);
}
Expand All @@ -548,10 +545,10 @@ pub async fn list_interfaces(sk: &str) -> Result<InterfacesResponse> {
let stock = retrieve_stock(sk, ASSETS_STOCK).await?;

let mut interfaces = vec![];
for schema_id in stock.schema_ids().expect("invalid schemas state") {
let schema = stock.schema(schema_id).expect("invalid schemas state");
for schema_id in stock.schema_ids()? {
let schema = stock.schema(schema_id)?;
for (iface_id, iimpl) in schema.clone().iimpls.into_iter() {
let face = stock.iface_by_id(iface_id).expect("invalid iface state");
let face = stock.iface_by_id(iface_id)?;

let item = InterfaceDetail {
name: face.name.to_string(),
Expand All @@ -569,11 +566,11 @@ pub async fn list_schemas(sk: &str) -> Result<SchemasResponse> {
let stock = retrieve_stock(sk, ASSETS_STOCK).await?;

let mut schemas = vec![];
for schema_id in stock.schema_ids().expect("invalid schemas state") {
let schema = stock.schema(schema_id).expect("invalid schemas state");
for schema_id in stock.schema_ids()? {
let schema = stock.schema(schema_id)?;
let mut ifaces = vec![];
for (iface_id, _) in schema.clone().iimpls.into_iter() {
let face = stock.iface_by_id(iface_id).expect("invalid iface state");
let face = stock.iface_by_id(iface_id)?;
ifaces.push(face.name.to_string());
}
schemas.push(SchemaDetail {
Expand Down
45 changes: 14 additions & 31 deletions src/rgb/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,9 @@ where
.expect("invalid contract data")
.to_base32(),
bech32::Variant::Bech32m,
)
.expect("invalid contract data");
let contract_strict = contract_bindle
.to_strict_serialized::<0xFFFFFF>()
.expect("invalid contract data")
.to_hex();
)?;

let contract_strict = contract_bindle.to_strict_serialized::<0xFFFFFF>()?.to_hex();

let contract_iface = stock
.contract_iface(contract_bindle.contract_id(), iface_id.to_owned())
Expand Down Expand Up @@ -148,37 +145,24 @@ where
for (index, (_, global_assign)) in contract_genesis.genesis.assignments.iter().enumerate() {
let idx = index as u16;
if global_assign.is_fungible() {
if let Some(reveal) = global_assign
.as_fungible_state_at(idx)
.expect("fail retrieve fungible data")
{
if let Some(reveal) = global_assign.as_fungible_state_at(idx)? {
supply += reveal.value.as_u64();
}
} else if global_assign.is_structured()
&& global_assign
.as_structured_state_at(idx)
.expect("fail retrieve structured data")
.is_some()
&& global_assign.as_structured_state_at(idx)?.is_some()
{
supply += 1;
}
}

let genesis = contract_genesis.genesis.clone();
let genesis_strict = genesis
.to_strict_serialized::<0xFFFFFF>()
.expect("invalid genesis data")
.to_hex();
let genesis_strict = genesis.to_strict_serialized::<0xFFFFFF>()?.to_hex();

let genesis_legacy = encode(
"rgb",
genesis
.to_strict_serialized::<0xFFFFFF>()
.expect("invalid contract data")
.to_base32(),
genesis.to_strict_serialized::<0xFFFFFF>()?.to_base32(),
bech32::Variant::Bech32m,
)
.expect("invalid contract data");
)?;

let genesis_formats = GenesisFormats {
legacy: genesis_legacy,
Expand All @@ -190,7 +174,10 @@ where
let mut meta = none!();
let ty: FieldName = FieldName::from("tokens");
if contract_iface.global(ty.clone()).is_ok() {
let type_id = contract_iface.iface.global_type(&ty).expect("");
let type_id = contract_iface
.iface
.global_type(&ty)
.expect("no global type id");

let type_schema = contract_iface
.state
Expand All @@ -217,16 +204,12 @@ where
if let Some(preview) = token_data.preview {
media = MediaInfo {
ty: preview.ty.to_string(),
source: String::from_utf8(preview.data.to_inner()).expect("invalid data"),
source: String::from_utf8(preview.data.to_inner())?,
};
}

let single = ContractMetadata::UDA(UDADetail {
token_index: token_data
.index
.to_string()
.parse()
.expect("invalid token_index"),
token_index: token_data.index.to_string().parse()?,
ticker: ticker.clone(),
name: name.clone(),
description: description.clone(),
Expand Down
31 changes: 11 additions & 20 deletions src/rgb/issue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,12 @@ where
T: ResolveHeight + ResolveTx,
T::Error: 'static,
{
let iface_name = match TypeName::from_str(iface) {
Ok(name) => name,
_ => return Err(IssueError::Forge(BuilderError::InterfaceMismatch)),
};
let iface_name = TypeName::from_str(iface)
.map_err(|_| IssueError::Forge(BuilderError::InterfaceMismatch))?;

let binding = stock.to_owned();
let iface = match binding.iface_by_name(&iface_name) {
Ok(name) => name,
_ => return Err(IssueError::Forge(BuilderError::InterfaceMismatch)),
};
let iface = stock
.iface_by_name(&iface_name)
.map_err(|_| IssueError::Forge(BuilderError::InterfaceMismatch))?;

if ticker.len() < 3 || ticker.len() > 8 || ticker.chars().any(|c| c < 'A' && c > 'Z') {
return Err(IssueError::InvalidTicker("Ticker must be between 3 and 8 chars, contain no spaces and consist only of capital letters".to_string()));
Expand All @@ -82,21 +78,16 @@ where
_ => return Err(IssueError::ContractNotfound(iface.name.to_string())),
};

let resp = match contract_issued {
Ok(resp) => resp,
Err(err) => return Err(IssueError::Forge(err)),
};
let resp = contract_issued.map_err(IssueError::Forge)?;
let contract_id = resp.contract_id().to_string();

let resp = match resp.clone().validate(resolver) {
Ok(resp) => resp,
Err(_err) => return Err(IssueError::ContractInvalid(resp.contract_id().to_string())),
};
let resp = resp
.validate(resolver)
.map_err(|_| IssueError::ContractInvalid(contract_id.clone()))?;

stock
.import_contract(resp.clone(), resolver)
.or(Err(IssueError::ImportContract(
resp.contract_id().to_string(),
)))?;
.or(Err(IssueError::ImportContract(contract_id)))?;

Ok(resp)
}
Expand Down

0 comments on commit d59bae6

Please sign in to comment.