Skip to content

Commit

Permalink
One contract to rule them all
Browse files Browse the repository at this point in the history
  • Loading branch information
h4nsu committed Aug 17, 2022
1 parent a52e372 commit e40f937
Show file tree
Hide file tree
Showing 4 changed files with 421 additions and 425 deletions.
70 changes: 0 additions & 70 deletions contracts/button/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions contracts/button/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ edition = "2021"
ink_env = { version = "~3.3.0", default-features = false }
ink_lang = { version = "~3.3.0", default-features = false }
ink_metadata = { version = "~3.3.0", default-features = false, features = ["derive"], optional = true }
ink_prelude = { version = "~3.3.0", default-features = false }
ink_primitives = { version = "~3.3.0", default-features = false }
ink_storage = { version = "~3.3.0", default-features = false }

Expand All @@ -19,22 +18,20 @@ access_control = { path = "../access_control", default-features = false, feature
game_token = { path = "../game_token", default-features = false, features = ["ink-as-dependency"] }
ticket_token = { path = "../ticket_token", default-features = false, features = ["ink-as-dependency"] }
openbrush = { git = "https://github.com/Supercolony-net/openbrush-contracts.git", rev = "8a20f95", default-features = false, features = ["psp22"] }
num = {version = "0.4.0", default-features = false }

[lib]
name = "button"
path = "lib.rs"
crate-type = [
"cdylib",
"rlib",
]

[features]
default = ["std"]
std = [
"ink_env/std",
"ink_lang/std",
"ink_metadata/std",
"ink_prelude/std",
"ink_primitives/std",
"ink_storage/std",
"scale-info/std",
Expand Down
86 changes: 86 additions & 0 deletions contracts/button/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use ink_env::Error as InkEnvError;
use openbrush::contracts::psp22::PSP22Error;

/// GameError types
#[derive(Debug, PartialEq, Eq, scale::Encode, scale::Decode)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub enum GameError {
/// Returned if reset is called before the deadline
BeforeDeadline,
/// Returned if button is pressed after the deadline
AfterDeadline,
/// Returned if a call is made from an account with missing access control privileges
MissingRole(String),
/// Returned if a call to another contract has failed
ContractCall(String),
}

impl From<PSP22Error> for GameError {
fn from(e: PSP22Error) -> Self {
match e {
PSP22Error::Custom(message) => GameError::ContractCall(message),
PSP22Error::InsufficientBalance => {
GameError::ContractCall(String::from("PSP22::InsufficientBalance"))
}
PSP22Error::InsufficientAllowance => {
GameError::ContractCall(String::from("PSP22::InsufficientAllowance"))
}
PSP22Error::ZeroRecipientAddress => {
GameError::ContractCall(String::from("PSP22::ZeroRecipientAddress"))
}
PSP22Error::ZeroSenderAddress => {
GameError::ContractCall(String::from("PSP22::ZeroSenderAddress"))
}
PSP22Error::SafeTransferCheckFailed(message) => {
GameError::ContractCall(format!("PSP22::SafeTransferCheckFailed({})", message))
}
}
}
}

impl From<InkEnvError> for GameError {
fn from(e: InkEnvError) -> Self {
match e {
InkEnvError::Decode(_e) => {
GameError::ContractCall(String::from("Contract call failed due to Decode error"))
}
InkEnvError::CalleeTrapped => GameError::ContractCall(String::from(
"Contract call failed due to CalleeTrapped error",
)),
InkEnvError::CalleeReverted => GameError::ContractCall(String::from(
"Contract call failed due to CalleeReverted error",
)),
InkEnvError::KeyNotFound => GameError::ContractCall(String::from(
"Contract call failed due to KeyNotFound error",
)),
InkEnvError::_BelowSubsistenceThreshold => GameError::ContractCall(String::from(
"Contract call failed due to _BelowSubsistenceThreshold error",
)),
InkEnvError::TransferFailed => GameError::ContractCall(String::from(
"Contract call failed due to TransferFailed error",
)),
InkEnvError::_EndowmentTooLow => GameError::ContractCall(String::from(
"Contract call failed due to _EndowmentTooLow error",
)),
InkEnvError::CodeNotFound => GameError::ContractCall(String::from(
"Contract call failed due to CodeNotFound error",
)),
InkEnvError::NotCallable => GameError::ContractCall(String::from(
"Contract call failed due to NotCallable error",
)),
InkEnvError::Unknown => {
GameError::ContractCall(String::from("Contract call failed due to Unknown error"))
}
InkEnvError::LoggingDisabled => GameError::ContractCall(String::from(
"Contract call failed due to LoggingDisabled error",
)),
InkEnvError::EcdsaRecoveryFailed => GameError::ContractCall(String::from(
"Contract call failed due to EcdsaRecoveryFailed error",
)),
#[cfg(any(feature = "std", test, doc))]
InkEnvError::OffChain(_e) => {
GameError::ContractCall(String::from("Contract call failed due to OffChain error"))
}
}
}
}
Loading

0 comments on commit e40f937

Please sign in to comment.