-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TicketItem for Stack and instruction skeleton
- Loading branch information
Showing
8 changed files
with
150 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,6 @@ bin | |
target | ||
tezos_kernel/target | ||
boot_kernel/target | ||
.env | ||
.env | ||
.bin | ||
local.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ mod lambda; | |
mod math; | ||
mod scope; | ||
mod stack; | ||
mod tickets; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// SPDX-FileCopyrightText: 2023 Baking Bad <[email protected]> | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
use tezos_michelson::michelson::data::instructions::{ | ||
Ticket, ReadTicket, SplitTicket, JoinTickets, | ||
}; | ||
|
||
use crate::{ | ||
err_mismatch, | ||
interpreter::{ | ||
ContextInterpreter, InterpreterContext, PureInterpreter, | ||
}, | ||
//pop_cast, | ||
stack::Stack, | ||
Result, types::{AddressItem, NatItem, StackItem, TicketItem, PairItem, OptionItem}, | ||
}; | ||
|
||
impl ContextInterpreter for Ticket { | ||
fn execute(&self, stack: &mut Stack, context: &mut impl InterpreterContext) -> Result<()> { | ||
let identifier = stack.pop()?; | ||
let amount = stack.pop()?; | ||
|
||
// TODO: compare amount with zero | ||
// TODO: convert StackItem identifier to Micheline | ||
// TODO: get Type for identifier | ||
// TODO: get self address | ||
// TODO: save balance info to context? | ||
|
||
//stack.push(StackItem::Ticket(TicketItem::new())); | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl PureInterpreter for ReadTicket { | ||
fn execute(&self, stack: &mut Stack) -> Result<()> { | ||
let ticket_item = stack.pop()?; | ||
let ticket = match ticket_item { | ||
StackItem::Ticket(ticket) => ticket, | ||
item => return err_mismatch!("Ticket", item) | ||
}; | ||
|
||
let source = StackItem::Address(AddressItem::new(ticket.source)); | ||
let identifier = ticket.identifier; // TODO: identifier to StackItem | ||
let amount = StackItem::Nat(NatItem::new(ticket.amount)); | ||
|
||
let pair = PairItem::from_items(vec![source, identifier, amount])?; | ||
|
||
stack.push(StackItem::Pair(pair)); | ||
stack.push(ticket_item); // return ticket back to stack | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl ContextInterpreter for SplitTicket { | ||
fn execute(&self, stack: &mut Stack, context: &mut impl InterpreterContext) -> Result<()> { | ||
let ticket = stack.pop()?; // ticket | ||
let split_pair = stack.pop()?; // pair nat nat | ||
|
||
// TODO: if n + m != ticket.amount or n == 0 or m == 0 return none | ||
stack.push(StackItem::Option(OptionItem::None())); | ||
|
||
// TODO: else return pair (ticket_n, ticket_m) | ||
stack.push(StackItem::Option(OptionItem::Some())); | ||
|
||
// TODO: update balance in context? | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
impl ContextInterpreter for JoinTickets { | ||
fn execute(&self, stack: &mut Stack, context: &mut impl InterpreterContext) -> Result<()> { | ||
let tickets = stack.pop()?; // tickets pair | ||
// TODO: get ticket_a | ||
// TODO: get ticket_b | ||
// TODO: compare sources and identifiers (and identifiers types?) | ||
|
||
// TODO: if ticket_a.source != ticket_b.source or ticket_a.identifier != ticket_b.identifier | ||
stack.push(StackItem::Option(OptionItem::None())); | ||
|
||
// TODO: OR otherwise return Some(ticket) | ||
stack.push(StackItem::Option(OptionItem::Some())); | ||
|
||
// TODO: update balance in context? | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// SPDX-FileCopyrightText: 2023 Baking Bad <[email protected]> | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
use std::fmt::Display; | ||
|
||
use ibig::UBig; | ||
use tezos_core::types::encoded::Address; | ||
use tezos_michelson::{michelson::types::{self, Type}, micheline::Micheline}; | ||
|
||
use crate::{ | ||
types::TicketItem, | ||
Result, | ||
}; | ||
|
||
impl TicketItem { | ||
pub fn new(source: Address, identifier: Micheline, identifier_type: Type, amount: UBig) -> Self{ | ||
Self { | ||
source, | ||
identifier, | ||
identifier_type, | ||
amount, | ||
} | ||
} | ||
|
||
pub fn get_type(&self) -> Result<Type> { | ||
Ok(types::ticket(self.identifier_type.clone())) | ||
} | ||
|
||
} | ||
|
||
impl Display for TicketItem { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
f.write_fmt(format_args!("({:?} {:?} {})", self.source, self.identifier, self.amount)) | ||
} | ||
} |