-
Notifications
You must be signed in to change notification settings - Fork 481
Open
Description
here is my code for the deployed contract, when i try to run the order_pizza function, i get the response that follows the code block.
#![cfg_attr(not(feature = "std"), no_std, no_main)]
#[ink::contract]
mod pizza_limited {
use ink::prelude::string::String;
use ink::prelude::vec::Vec;
use ink::storage::Mapping;
use ink::prelude::format;
use ink::scale::{Decode, Encode};
use ink::H160;
#[ink(storage)]
pub struct PizzaLimited {
daily_supply: u32,
orders_per_user: Mapping<H160, u32>,
remaining_supply: u32,
max_order_per_user: u32,
last_reset: Timestamp,
total_orders: u32,
}
#[derive(Debug, PartialEq, Eq)]
#[ink::scale_derive(Encode, Decode, TypeInfo)]
pub enum PizzaError {
DailySupplyReached,
MaxOrderPerUserReached,
CannotOrderZeroPizzas,
}
#[ink(event)]
pub struct PizzaOrdered {
caller: Option<H160>,
quantity_ordered: u32,
}
pub type Result<T> = core::result::Result<T, PizzaError>;
impl PizzaLimited {
#[ink(constructor)]
pub fn new(
max_order_per_user: u32,
) -> Self {
let daily_supply = 50;
Self {
daily_supply,
max_order_per_user,
orders_per_user: Mapping::default(),
remaining_supply: daily_supply,
last_reset: Self::env().block_timestamp(),
total_orders: 0,
}
}
#[ink(message)]
pub fn get_remaining_supply(&self) -> u32 {
self.daily_supply
}
#[ink(message)]
pub fn get_daily_supply(&self) -> u32 {
self.remaining_supply
}
#[ink(message)]
pub fn get_last_reset(&self) -> Timestamp{
self.last_reset
}
#[ink(message)]
pub fn order_pizza(
&mut self,
quantity_ordered: u32,
) -> Result<u32> {
let caller: H160 = self.env().caller();
if quantity_ordered == 0 {
return Err(PizzaError::CannotOrderZeroPizzas);
}
// Check remaining supply
if self.remaining_supply < quantity_ordered {
return Err(PizzaError::DailySupplyReached);
}
// Check current orders by user
let user_orders = self.orders_per_user.get(&caller).unwrap_or(0);
if user_orders + quantity_ordered > self.max_order_per_user {
return Err(PizzaError::MaxOrderPerUserReached);
}
// Update state
self.remaining_supply -= quantity_ordered;
self.orders_per_user.insert(caller, &(user_orders + quantity_ordered));
self.total_orders += quantity_ordered;
// Emit events
self.env().emit_event(PizzaOrdered {
caller: Some(caller),
quantity_ordered,
});
Ok(user_orders + quantity_ordered)
}
#[ink(message)]
pub fn reset_supply(&mut self) -> u32 {
self.remaining_supply = self.daily_supply;
// self.last_reset = Self::env().block_timestamp();
self.remaining_supply
}
}
#[cfg(test)]
mod tests {
/// Imports all the definitions from the outer scope so we can use them here.
use super::*;
#[ink::test]
fn it_works() {
let pizza_limited = PizzaLimited::new(50);
assert_eq!(pizza_limited.get_daily_supply(), 50);
}
#[ink::test]
fn get_remaining_supply_works() {
let mut pizza_limited = PizzaLimited::new(50);
assert_eq!(pizza_limited.get_remaining_supply(), 50);
}
#[ink::test]
fn get_daily_supply_works() {
let pizza_limited = PizzaLimited::new(50);
assert_eq!(pizza_limited.get_daily_supply(), 50);
}
#[ink::test]
fn get_last_reset_works() {
let mut pizza_limited = PizzaLimited::new(50);
assert_eq!(pizza_limited.get_last_reset(), pizza_limited.last_reset);
}
}
}
Steps to reproduce:
- cargo contract build --release
- upload and instantiate the contract on the ui.
- try to call the order_pizza function
Metadata
Metadata
Assignees
Labels
No labels
Type
Projects
Status
Backlog