Skip to content

Commit

Permalink
feat(jstzd): update
Browse files Browse the repository at this point in the history
  • Loading branch information
huancheng-trili committed Oct 3, 2024
1 parent 50f45f6 commit 0388901
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
53 changes: 38 additions & 15 deletions crates/jstzd/src/protocol/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,18 @@ impl From<BootstrapAccounts> for Value {
pub struct BootstrapContract {
script: Value,
amount_tez: u64,
// TODO: remove allow(dead_code) after this hash is being referenced in jstzd
#[allow(dead_code)]
hash: ContractKt1Hash,
hash: Option<ContractKt1Hash>,
}

impl BootstrapContract {
pub fn new(script: Value, amount: u64, hash: &str) -> anyhow::Result<Self> {
pub fn new(script: Value, amount: u64, hash: Option<&str>) -> anyhow::Result<Self> {
Ok(Self {
script,
amount_tez: amount,
hash: ContractKt1Hash::from_base58_check(hash)?,
hash: match hash {
Some(v) => Some(ContractKt1Hash::from_base58_check(v)?),
None => None,
},
})
}
}
Expand All @@ -85,6 +86,9 @@ impl From<BootstrapContracts> for Value {
"amount".to_owned(),
Value::Number(v.amount_tez.into()),
);
if let Some(v) = &v.hash {
map.insert("hash".to_owned(), Value::String(v.to_string()));
}
map
})
})
Expand Down Expand Up @@ -132,32 +136,51 @@ mod tests {
let contract = BootstrapContract::new(
Value::String("dummy-script".to_owned()),
1000,
CONTRACT_HASH,
Some(CONTRACT_HASH),
)
.unwrap();
assert_eq!(contract.amount_tez, 1000);
assert_eq!(contract.hash.to_string(), CONTRACT_HASH);
assert_eq!(contract.hash.unwrap().to_string(), CONTRACT_HASH);
assert_eq!(contract.script.as_str().unwrap(), "dummy-script");
}

#[test]
fn serde_value_from_bootstrap_contracts() {
let contracts = BootstrapContracts {
contracts: vec![BootstrapContract::new(
Value::String("dummy-script".to_owned()),
1000,
CONTRACT_HASH,
)
.unwrap()],
contracts: vec![
BootstrapContract::new(
Value::String("dummy-script".to_owned()),
1000,
Some(CONTRACT_HASH),
)
.unwrap(),
BootstrapContract::new(
Value::String("dummy-script-no-hash".to_owned()),
900,
None,
)
.unwrap(),
],
};
let value = Value::from(contracts);
let arr = value.as_array().unwrap();
assert_eq!(arr.len(), 1);
let contract = arr.last().unwrap().as_object().unwrap();
assert_eq!(arr.len(), 2);
let contract = arr.first().unwrap().as_object().unwrap();
assert_eq!(contract.get("amount").unwrap(), 1000);
assert_eq!(
contract.get("script").unwrap().as_str().unwrap(),
"dummy-script"
);
assert_eq!(
contract.get("hash").unwrap().as_str().unwrap(),
CONTRACT_HASH
);

let contract = arr.last().unwrap().as_object().unwrap();
assert_eq!(contract.get("amount").unwrap(), 900);
assert_eq!(
contract.get("script").unwrap().as_str().unwrap(),
"dummy-script-no-hash"
);
}
}
2 changes: 1 addition & 1 deletion crates/jstzd/src/protocol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ mod tests {
.set_bootstrap_contracts(vec![BootstrapContract::new(
serde_json::Value::String("test-contract".to_owned()),
900,
CONTRACT_HASH,
Some(CONTRACT_HASH),
)
.unwrap()]);
let output_path = builder.build().await.unwrap();
Expand Down

0 comments on commit 0388901

Please sign in to comment.