Contract is reverting.
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
Thanks for the report, @AbuTuraab!
Was this on a local node on your machine or on a testnet?
@AlexD10S I tried reproducing with cargo-contract on a local ink-node and couldn't. It works fine there:
cargo contract build
cargo contract instantiate --manifest-path Cargo.toml --constructor new --suri //Alice --args 12 -x --salt 20
cargo contract call --suri //Alice --manifest-path=./Cargo.toml --message order_pizza --contract 0x771b17d0b82cfa1c7be2e5ba86da2622e69dc5f5 --args 1 -x
My suspicion is that the account is perhaps not mapped. Do you know if the UI would display something to the user in that case?
I am running them on passethub testnet, the account is mapped.
Earlier, i used an account that wasnt mapped and it gave me the response of not being mapped, so i mapped the account and it works fine, now, i am getting the error
Can you try using pop cli, that will give more info on what could be going wrong
Install using: cargo install --git https://github.com/r0gue-io/pop-cli.git --branch v6.0.0-alpha.4 --locked
hey @Daanvdplas I have used pop cli, i think its the same.
but after some time, the contract works fine again, does Paseo and passethub usually have intermittent downtimes?
Hmm interesting, let us know if it happens again!
@Daanvdplas yeah it is still happening.
Can you share your repo so that I can try it out myself?