Skip to content

Commit

Permalink
feat(fa-deposit): handle fa deposit execution in proto
Browse files Browse the repository at this point in the history
  • Loading branch information
zcabter committed Aug 19, 2024
1 parent 8fb97fb commit 5a0dbe9
Show file tree
Hide file tree
Showing 8 changed files with 496 additions and 19 deletions.
3 changes: 1 addition & 2 deletions crates/jstz_kernel/src/inbox.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use jstz_crypto::public_key_hash::PublicKeyHash;
use jstz_proto::operation::{external::Deposit, ExternalOperation, SignedOperation};
use num_traits::ToPrimitive;
use serde::{Deserialize, Serialize};
use tezos_crypto_rs::hash::ContractKt1Hash;
use tezos_smart_rollup::inbox::ExternalMessageFrame;
use tezos_smart_rollup::michelson::ticket::FA2_1Ticket;
Expand All @@ -18,7 +17,7 @@ use tezos_smart_rollup::{
pub type ExternalMessage = SignedOperation;
pub type InternalMessage = ExternalOperation;

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq)]
pub enum Message {
External(ExternalMessage),
Internal(InternalMessage),
Expand Down
22 changes: 17 additions & 5 deletions crates/jstz_mock/src/mock.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::io::empty;

use jstz_core::{host::HostRuntime, kv::Storage};
use jstz_crypto::hash::Blake2b;
use tezos_crypto_rs::hash::ContractKt1Hash;
use tezos_smart_rollup::{
michelson::{
ticket::{FA2_1Ticket, Ticket},
ticket::{FA2_1Ticket, Ticket, TicketHash, UnitTicket},
MichelsonBytes, MichelsonContract, MichelsonNat, MichelsonOption, MichelsonPair,
MichelsonUnit,
},
storage::path::RefPath,
types::{Contract, PublicKeyHash, SmartRollupAddress},
Expand Down Expand Up @@ -118,7 +118,19 @@ pub fn account1() -> jstz_crypto::public_key_hash::PublicKeyHash {
.unwrap()
}

pub fn ticket_hash1() -> Blake2b {
let data = vec![b'0', b'0', b'0'];
Blake2b::from(&data)
pub fn account2() -> jstz_crypto::public_key_hash::PublicKeyHash {
jstz_crypto::public_key_hash::PublicKeyHash::from_base58(
"tz1QcqnzZ8pa6VuE4MSeMjsJkiW94wNrPbgX",
)
.unwrap()
}

pub fn ticket_hash1() -> TicketHash {
let ticket = UnitTicket::new(
Contract::from_b58check("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx").unwrap(),
MichelsonUnit,
10,
)
.unwrap();
ticket.hash().unwrap()
}
18 changes: 9 additions & 9 deletions crates/jstz_proto/src/context/ticket_table.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::error::Result;
use derive_more::{Display, Error, From};
use jstz_core::kv::{Entry, Transaction};
use jstz_crypto::{hash::Blake2b, public_key_hash::PublicKeyHash};
use jstz_crypto::public_key_hash::PublicKeyHash;
use tezos_smart_rollup::{
host::Runtime,
michelson::ticket::TicketHash,
storage::path::{self, OwnedPath, RefPath},
};

Expand All @@ -21,9 +22,8 @@ const TICKET_TABLE_PATH: RefPath = RefPath::assert_from(b"/ticket_table");
pub struct TicketTable;

impl TicketTable {
fn path(ticket_hash: &Blake2b, owner: &PublicKeyHash) -> Result<OwnedPath> {
let ticket_hash_path =
OwnedPath::try_from(format!("/{}", ticket_hash.to_string()))?;
fn path(ticket_hash: &TicketHash, owner: &PublicKeyHash) -> Result<OwnedPath> {
let ticket_hash_path = OwnedPath::try_from(format!("/{}", ticket_hash))?;
let owner_path = OwnedPath::try_from(format!("/{}", owner))?;

Ok(path::concat(
Expand All @@ -36,7 +36,7 @@ impl TicketTable {
rt: &mut impl Runtime,
tx: &mut Transaction,
owner: &PublicKeyHash,
ticket_hash: &Blake2b,
ticket_hash: &TicketHash,
) -> Result<Amount> {
let path = Self::path(ticket_hash, owner)?;
let result = tx.get::<Amount>(rt, path)?;
Expand All @@ -50,7 +50,7 @@ impl TicketTable {
rt: &mut impl Runtime,
tx: &mut Transaction,
owner: &PublicKeyHash,
ticket_hash: &Blake2b,
ticket_hash: &TicketHash,
amount: Amount, // TODO: check if its the correct size
) -> Result<Amount> {
let path = Self::path(ticket_hash, owner)?;
Expand All @@ -74,7 +74,7 @@ impl TicketTable {
rt: &mut impl Runtime,
tx: &mut Transaction,
owner: &PublicKeyHash,
ticket_hash: &Blake2b,
ticket_hash: &TicketHash,
amount: u64,
) -> Result<Amount> {
let path = Self::path(ticket_hash, owner)?;
Expand Down Expand Up @@ -104,8 +104,8 @@ mod test {
let ticket_hash = mock::ticket_hash1();
let owner = mock::account1();
let result = TicketTable::path(&ticket_hash, &owner).unwrap();
let expectecd = "/ticket_table/4f3b771750d60ed12c38f5f80683fb53b37e3da02dd7381454add8f1dbd2ee60/tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx";
assert_eq!(expectecd, result.to_string());
let expected = "/ticket_table/4db276d5f50bc2ad959b0f08bb34fbdf4fbe4bf95a689ffb9e922038430840d7/tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx";
assert_eq!(expected, result.to_string());
}

#[test]
Expand Down
8 changes: 7 additions & 1 deletion crates/jstz_proto/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use boa_engine::{JsError, JsNativeError};
use derive_more::{Display, Error, From};

use crate::context::ticket_table;
use crate::{context::ticket_table, executor::fa_deposit};

#[derive(Display, Debug, Error, From)]
pub enum Error {
Expand All @@ -20,6 +20,9 @@ pub enum Error {
TicketTableError {
source: ticket_table::TicketTableError,
},
FaDepositError {
source: fa_deposit::FaDepositError,
},
}
pub type Result<T> = std::result::Result<T, Error>;

Expand Down Expand Up @@ -51,6 +54,9 @@ impl From<Error> for JsError {
Error::TicketTableError { source } => JsNativeError::eval()
.with_message(format!("TicketTableError: {}", source))
.into(),
Error::FaDepositError { source } => JsNativeError::eval()
.with_message(format!("FaDepositError: {}", source))
.into(),
}
}
}
Expand Down
Loading

0 comments on commit 5a0dbe9

Please sign in to comment.