Skip to content

Commit

Permalink
refactor(jstz_kernel): mirgrate to new_address
Browse files Browse the repository at this point in the history
  • Loading branch information
ryutamago committed Jan 7, 2025
1 parent 67fac47 commit 3502133
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 27 deletions.
33 changes: 19 additions & 14 deletions crates/jstz_kernel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ mod test {
};
use jstz_proto::context::{
account::{Account, ParsedCode},
new_account::NewAddress,
ticket_table::TicketTable,
};
use tezos_smart_rollup::types::{Contract, PublicKeyHash};
Expand Down Expand Up @@ -137,13 +138,15 @@ mod test {
TicketTable::get_balance(host.rt(), tx, &proxy, &ticket_hash)
.unwrap();
assert_eq!(300, proxy_balance);
let receiver_balance = TicketTable::get_balance(
host.rt(),
tx,
&try_parse_contract(&deposit.receiver).unwrap(),
&ticket_hash,
)
.unwrap();
// TODO: use new address after jstz-proto is updated
// https://linear.app/tezos/issue/JSTZ-261/use-newaddress-for-jstz-proto
let owner = match try_parse_contract(&deposit.receiver).unwrap() {
NewAddress::User(pkh) => pkh,
NewAddress::SmartFunction(_) => panic!("Unexpected owner"),
};
let receiver_balance =
TicketTable::get_balance(host.rt(), tx, &owner, &ticket_hash)
.unwrap();
assert_eq!(0, receiver_balance);
}
_ => panic!("Unexpected receiver"),
Expand All @@ -167,13 +170,15 @@ mod test {
TicketTable::get_balance(host.rt(), &mut tx, &proxy, &ticket_hash)
.unwrap();
assert_eq!(0, proxy_balance);
let receiver_balance = TicketTable::get_balance(
host.rt(),
&mut tx,
&try_parse_contract(&deposit.receiver).unwrap(),
&ticket_hash,
)
.unwrap();
// TODO: use new address after jstz-proto is updated
// https://linear.app/tezos/issue/JSTZ-261/use-newaddress-for-jstz-proto
let owner = match try_parse_contract(&deposit.receiver).unwrap() {
NewAddress::User(pkh) => pkh,
NewAddress::SmartFunction(_) => panic!("Unexpected owner"),
};
let receiver_balance =
TicketTable::get_balance(host.rt(), &mut tx, &owner, &ticket_hash)
.unwrap();
assert_eq!(300, receiver_balance);
}
_ => panic!("Unexpected receiver"),
Expand Down
64 changes: 52 additions & 12 deletions crates/jstz_kernel/src/parsing.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
use jstz_crypto::hash::Hash;
use jstz_crypto::public_key_hash::PublicKeyHash;
use jstz_crypto::smart_function_hash::SmartFunctionHash;
use jstz_proto::context::account::Address;
use jstz_proto::operation::external::FaDeposit;
use jstz_proto::{context::account::Address, Result};
use jstz_proto::{context::new_account::NewAddress, Result};
use num_traits::ToPrimitive;
use tezos_smart_rollup::michelson::{ticket::FA2_1Ticket, MichelsonContract};
use tezos_smart_rollup::types::{Contract, PublicKeyHash as TezosPublicKeyHash};

pub fn try_parse_contract(contract: &Contract) -> Result<Address> {
pub fn try_parse_contract(contract: &Contract) -> Result<NewAddress> {
match contract {
Contract::Implicit(TezosPublicKeyHash::Ed25519(tz1)) => {
Ok(PublicKeyHash::Tz1(tz1.clone()))
Ok(NewAddress::User(PublicKeyHash::Tz1(tz1.clone())))
}
Contract::Originated(contract_kt1_hash) => Ok(NewAddress::SmartFunction(
SmartFunctionHash::Kt1(contract_kt1_hash.clone()),
)),
_ => Err(jstz_proto::Error::InvalidAddress),
}
}
Expand All @@ -20,15 +26,22 @@ pub fn try_parse_fa_deposit(
receiver: MichelsonContract,
proxy_contract: Option<MichelsonContract>,
) -> Result<FaDeposit> {
// TODO: use NewAddress after jstz-proto is updated
// https://linear.app/tezos/issue/JSTZ-261/use-newaddress-for-jstz-proto
let receiver = try_parse_contract(&receiver.0)?;
let receiver = Address::from_base58(&receiver.to_base58())?;
let proxy_smart_function = (proxy_contract)
.map(|c| try_parse_contract(&c.0))
.transpose()?;
let proxy_smart_function = proxy_smart_function
.map(|p| Address::from_base58(&p.to_base58()))
.transpose()?;
let amount = ticket
.amount()
.to_u64()
.ok_or(jstz_proto::Error::TicketAmountTooLarge)?;
let ticket_hash = ticket.hash()?;

Ok(FaDeposit {
inbox_id,
amount,
Expand All @@ -41,7 +54,7 @@ pub fn try_parse_fa_deposit(
#[cfg(test)]
mod test {

use jstz_crypto::hash::Hash;
use jstz_crypto::{hash::Hash, smart_function_hash::SmartFunctionHash};
use jstz_proto::operation::external::FaDeposit;
use tezos_smart_rollup::{
michelson::{
Expand All @@ -60,6 +73,10 @@ mod test {
MichelsonContract(Contract::from_b58check(&pkh.to_base58()).unwrap())
}

fn jstz_sfh_to_michelson(sfh: &SmartFunctionHash) -> MichelsonContract {
MichelsonContract(Contract::from_b58check(&sfh.to_base58()).unwrap())
}

#[test]
fn try_parse_fa_deposit_should_pass() {
let amount = 10;
Expand Down Expand Up @@ -89,6 +106,28 @@ mod test {
assert_eq!(expected, fa_deposit)
}

//TODO: this should pass after jstz-proto is updated
// https://linear.app/tezos/issue/JSTZ-261/use-newaddress-for-jstz-proto
#[test]
fn try_parse_fa_deposit_should_fail_for_smart_function() {
let amount = 10;
let ticket: FA2_1Ticket = Ticket::new(
Contract::from_b58check("KT1NgXQ6Mwu3XKFDcKdYFS6dkkY3iNKdBKEc").unwrap(),
MichelsonPair(
MichelsonNat::from(100_u32),
MichelsonOption(Some(MichelsonBytes(b"12345".to_vec()))),
),
amount,
)
.unwrap();
let receiver = jstz_sfh_to_michelson(&jstz_mock::sf_account1());
let proxy_contract = Some(jstz_sfh_to_michelson(&jstz_mock::sf_account1()));
let inbox_id = 41717;

let fa_deposit = try_parse_fa_deposit(inbox_id, ticket, receiver, proxy_contract);
assert!(fa_deposit.is_err());
}

#[test]
fn try_parse_contract_tz1_should_pass() {
let value = try_parse_contract(
Expand All @@ -98,6 +137,15 @@ mod test {
assert_eq!("tz1ha7kscNYSgJ76k5gZD8mhBueCv3gqfMsA", value.to_base58());
}

#[test]
fn try_parse_contract_kt1_should_pass() {
let value = try_parse_contract(
&Contract::from_b58check("KT1EfTusMLoeCAAGd9MZJn5yKzFr6kJU5U91").unwrap(),
)
.expect("Expected to be parsable");
assert_eq!("KT1EfTusMLoeCAAGd9MZJn5yKzFr6kJU5U91", value.to_base58());
}

#[test]
fn try_parse_contract_tz2_should_fail() {
try_parse_contract(
Expand All @@ -121,12 +169,4 @@ mod test {
)
.expect_err("Expected to fail");
}

#[test]
fn try_parse_contract_kt1_should_fail() {
try_parse_contract(
&Contract::from_b58check("KT1EfTusMLoeCAAGd9MZJn5yKzFr6kJU5U91").unwrap(),
)
.expect_err("Expected to fail");
}
}
6 changes: 5 additions & 1 deletion crates/jstz_mock/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use jstz_crypto::hash::Hash;
use jstz_crypto::{hash::Hash, smart_function_hash::SmartFunctionHash};
use tezos_crypto_rs::hash::ContractKt1Hash;
use tezos_smart_rollup::{
michelson::{
Expand Down Expand Up @@ -42,6 +42,10 @@ pub fn kt1_account1() -> ContractKt1Hash {
ContractKt1Hash::try_from("KT1QgfSE4C1dX9UqrPAXjUaFQ36F9eB4nNkV").unwrap()
}

pub fn sf_account1() -> SmartFunctionHash {
SmartFunctionHash::from_base58("KT1QgfSE4C1dX9UqrPAXjUaFQ36F9eB4nNkV").unwrap()
}

pub fn ticket_hash1() -> TicketHash {
let ticket = UnitTicket::new(
Contract::from_b58check("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx").unwrap(),
Expand Down

0 comments on commit 3502133

Please sign in to comment.