Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A0-1238: Put tickets into marketplace when button dies #630

Merged
merged 6 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions contracts/button/Cargo.lock

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

2 changes: 2 additions & 0 deletions contracts/button/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ scale-info = { version = "2", default-features = false, features = ["derive"], o
access_control = { path = "../access_control", default-features = false, features = ["ink-as-dependency"] }
game_token = { path = "../game_token", default-features = false, features = ["ink-as-dependency"] }
ticket_token = { path = "../ticket_token", default-features = false, features = ["ink-as-dependency"] }
marketplace = { path = "../marketplace", default-features = false, features = ["ink-as-dependency"] }
openbrush = { git = "https://github.com/Supercolony-net/openbrush-contracts.git", rev = "8a20f95", default-features = false, features = ["psp22"] }

[lib]
Expand All @@ -42,6 +43,7 @@ std = [
"access_control/std",
"game_token/std",
"ticket_token/std",
"marketplace/std",
"openbrush/std",
]
ink-as-dependency = []
119 changes: 98 additions & 21 deletions contracts/button/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ mod button_game {
use ink_lang::{codegen::EmitEvent, reflect::ContractEventBase};
use ink_prelude::{format, vec};
use ink_storage::traits::{PackedLayout, SpreadLayout};
use marketplace::RESET_SELECTOR as MARKETPLACE_RESET_SELECTOR;
use openbrush::contracts::psp22::PSP22Error;
use scale::{Decode, Encode};
use ticket_token::TRANSFER_FROM_SELECTOR;
use ticket_token::{BALANCE_OF_SELECTOR, TRANSFER_FROM_SELECTOR, TRANSFER_SELECTOR};

use crate::errors::GameError;

Expand Down Expand Up @@ -89,6 +90,8 @@ mod button_game {
pub ticket_token: AccountId,
/// access control contract
pub access_control: AccountId,
/// ticket marketplace contract
pub marketplace: AccountId,
/// scoring strategy
pub scoring: Scoring,
}
Expand All @@ -102,6 +105,7 @@ mod button_game {
pub fn new(
ticket_token: AccountId,
reward_token: AccountId,
marketplace: AccountId,
button_lifetime: BlockNumber,
scoring: Scoring,
) -> Self {
Expand All @@ -113,7 +117,13 @@ mod button_game {
let access_control = AccountId::from(ACCESS_CONTROL_PUBKEY);

match ButtonGame::check_role(&access_control, &caller, required_role) {
Ok(_) => Self::init(ticket_token, reward_token, button_lifetime, scoring),
Ok(_) => Self::init(
ticket_token,
reward_token,
marketplace,
button_lifetime,
scoring,
),
Err(why) => panic!("Could not initialize the contract {:?}", why),
}
}
Expand Down Expand Up @@ -157,6 +167,12 @@ mod button_game {
self.ticket_token
}

/// Returns the address of the marketplace for exchanging this game's rewards for tickets.
#[ink(message)]
pub fn marketplace(&self) -> AccountId {
self.marketplace
}

/// Returns own code hash
#[ink(message)]
pub fn code_hash(&self) -> ButtonResult<Hash> {
Expand Down Expand Up @@ -212,25 +228,10 @@ mod button_game {
/// Can only be called after button's deadline
#[ink(message)]
pub fn reset(&mut self) -> ButtonResult<()> {
if !self.is_dead() {
return Err(GameError::BeforeDeadline);
}

let now = self.env().block_number();

// reward the Pressiah
if let Some(pressiah) = self.last_presser {
let reward = self.pressiah_score();
self.mint_reward(pressiah, reward)??;
};

self.presses = 0;
self.last_presser = None;
self.last_press = now;
self.total_rewards = 0;

Self::emit_event(self.env(), Event::GameReset(GameReset { when: now }));
Ok(())
self.ensure_dead()?;
self.reward_pressiah()?;
self.reset_state();
self.dump_tickets_in_marketplace()
}

/// Sets new access control contract address
Expand Down Expand Up @@ -264,6 +265,7 @@ mod button_game {
fn init(
ticket_token: AccountId,
reward_token: AccountId,
marketplace: AccountId,
button_lifetime: BlockNumber,
scoring: Scoring,
) -> Self {
Expand All @@ -275,6 +277,7 @@ mod button_game {
button_lifetime,
reward_token,
ticket_token,
marketplace,
last_press: now,
scoring,
last_presser: None,
Expand All @@ -295,6 +298,80 @@ mod button_game {
contract
}

fn reset_state(&mut self) {
let now = self.env().block_number();

self.presses = 0;
self.last_presser = None;
self.last_press = now;
self.total_rewards = 0;

Self::emit_event(self.env(), Event::GameReset(GameReset { when: now }));
}

fn reward_pressiah(&self) -> ButtonResult<()> {
if let Some(pressiah) = self.last_presser {
let reward = self.pressiah_score();
self.mint_reward(pressiah, reward)??;
};

Ok(())
kostekIV marked this conversation as resolved.
Show resolved Hide resolved
}

fn ensure_dead(&self) -> ButtonResult<()> {
if !self.is_dead() {
return Err(GameError::BeforeDeadline);
} else {
Ok(())
}
}

fn dump_tickets_in_marketplace(&self) -> ButtonResult<()> {
obrok marked this conversation as resolved.
Show resolved Hide resolved
self.transfer_tickets_to_marketplace()?;
self.reset_marketplace()
}

fn transfer_tickets_to_marketplace(&self) -> ButtonResult<()> {
build_call::<DefaultEnvironment>()
.call_type(Call::new().callee(self.ticket_token))
.exec_input(
ExecutionInput::new(Selector::new(TRANSFER_SELECTOR))
.push_arg(self.marketplace)
.push_arg(self.held_tickets()?)
.push_arg(vec![0x0]),
)
.call_flags(CallFlags::default().set_allow_reentry(true))
.returns::<Result<(), PSP22Error>>()
.fire()??;

Ok(())
}

fn held_tickets(&self) -> ButtonResult<Balance> {
let result = build_call::<DefaultEnvironment>()
.call_type(Call::new().callee(self.ticket_token))
.exec_input(
ExecutionInput::new(Selector::new(BALANCE_OF_SELECTOR))
.push_arg(self.env().account_id()),
)
.returns::<Balance>()
.fire()?;

Ok(result)
}

fn reset_marketplace(&self) -> ButtonResult<()> {
build_call::<DefaultEnvironment>()
.call_type(Call::new().callee(self.marketplace))
.exec_input(ExecutionInput::new(Selector::new(
MARKETPLACE_RESET_SELECTOR,
)))
.returns::<Result<(), PSP22Error>>()
.fire()??;

Ok(())
}

fn check_role(
access_control: &AccountId,
account: &AccountId,
Expand Down
4 changes: 3 additions & 1 deletion contracts/marketplace/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

use ink_lang as ink;

pub const RESET_SELECTOR: [u8; 4] = [0x00, 0x00, 0x00, 0x01];

#[ink::contract]
pub mod marketplace {
use access_control::{traits::AccessControlled, Role, ACCESS_CONTROL_PUBKEY};
Expand Down Expand Up @@ -225,7 +227,7 @@ pub mod marketplace {
/// Note that this will keep the average estimate from previous auctions.
///
/// Requires `Role::Admin`.
#[ink(message)]
#[ink(message, selector = 0x00000001)]
pub fn reset(&mut self) -> Result<(), Error> {
Self::ensure_role(self.admin())?;

Expand Down
20 changes: 10 additions & 10 deletions contracts/scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,14 @@ function deploy_button_game {
local game_type=$2
local ticket_token=$3
local game_token=$4
local salt=$5
local marketplace=$5
local salt=$6

# --- CREATE AN INSTANCE OF THE CONTRACT

cd "$CONTRACTS_PATH"/button

local contract_address=$(cargo contract instantiate --url "$NODE" --constructor new --args "$ticket_token" "$game_token" "$LIFETIME" "$game_type" --suri "$AUTHORITY_SEED" --salt "$salt")
local contract_address=$(cargo contract instantiate --url "$NODE" --constructor new --args "$ticket_token" "$game_token" "$marketplace" "$LIFETIME" "$game_type" --suri "$AUTHORITY_SEED" --salt "$salt")
local contract_address=$(echo "$contract_address" | grep Contract | tail -1 | cut -c 15-)

echo "$game_type contract instance address: $contract_address"
Expand All @@ -115,6 +116,7 @@ function deploy_button_game {
cd "$CONTRACTS_PATH"/access_control

cargo contract call --url "$NODE" --contract "$ACCESS_CONTROL" --message grant_role --args "$AUTHORITY" 'Owner('"$contract_address"')' --suri "$AUTHORITY_SEED"
cargo contract call --url "$NODE" --contract "$ACCESS_CONTROL" --message grant_role --args "$contract_address" 'Admin('"$marketplace"')' --suri "$AUTHORITY_SEED"

eval "$__resultvar='$contract_address'"
}
Expand All @@ -126,7 +128,6 @@ function deploy_marketplace {
local salt=$4
local ticket_token=$5
local game_token=$6
local game=$7

# --- CREATE AN INSTANCE OF THE CONTRACT

Expand All @@ -151,7 +152,6 @@ function deploy_marketplace {

cargo contract call --url "$NODE" --contract "$ACCESS_CONTROL" --message grant_role --args "$AUTHORITY" 'Owner('"$contract_address"')' --suri "$AUTHORITY_SEED"
cargo contract call --url "$NODE" --contract "$ACCESS_CONTROL" --message grant_role --args "$AUTHORITY" 'Admin('"$contract_address"')' --suri "$AUTHORITY_SEED"
cargo contract call --url "$NODE" --contract "$ACCESS_CONTROL" --message grant_role --args "$game" 'Admin('"$contract_address"')' --suri "$AUTHORITY_SEED"

eval "$__resultvar='$contract_address'"
}
Expand Down Expand Up @@ -219,8 +219,8 @@ echo "Early Bird Special"
salt="0x4561726C79426972645370656369616C"
deploy_ticket_token EARLY_BIRD_SPECIAL_TICKET early_bird_special_ticket EBST $salt
deploy_game_token EARLY_BIRD_SPECIAL_TOKEN early_bird_special EBS $salt
deploy_button_game EARLY_BIRD_SPECIAL EarlyBirdSpecial $EARLY_BIRD_SPECIAL_TICKET $EARLY_BIRD_SPECIAL_TOKEN $salt
deploy_marketplace EARLY_BIRD_SPECIAL_MARKETPLACE "$MARKETPLACE_CODE_HASH" early_bird_special "$salt" "$EARLY_BIRD_SPECIAL_TICKET" "$EARLY_BIRD_SPECIAL_TOKEN" "$EARLY_BIRD_SPECIAL"
deploy_marketplace EARLY_BIRD_SPECIAL_MARKETPLACE "$MARKETPLACE_CODE_HASH" early_bird_special "$salt" "$EARLY_BIRD_SPECIAL_TICKET" "$EARLY_BIRD_SPECIAL_TOKEN"
deploy_button_game EARLY_BIRD_SPECIAL EarlyBirdSpecial $EARLY_BIRD_SPECIAL_TICKET $EARLY_BIRD_SPECIAL_TOKEN $EARLY_BIRD_SPECIAL_MARKETPLACE $salt

#
# --- BACK_TO_THE_FUTURE GAME
Expand All @@ -230,8 +230,8 @@ echo "Back To The Future"
salt="0x4261636B546F546865467574757265"
deploy_ticket_token BACK_TO_THE_FUTURE_TICKET back_to_the_future_ticket BTFT $salt
deploy_game_token BACK_TO_THE_FUTURE_TOKEN back_to_the_future BTF $salt
deploy_button_game BACK_TO_THE_FUTURE BackToTheFuture $BACK_TO_THE_FUTURE_TICKET $BACK_TO_THE_FUTURE_TOKEN $salt
deploy_marketplace BACK_TO_THE_FUTURE_MARKETPLACE "$MARKETPLACE_CODE_HASH" back_to_the_future "$salt" "$BACK_TO_THE_FUTURE_TICKET" "$BACK_TO_THE_FUTURE_TOKEN" "$BACK_TO_THE_FUTURE"
deploy_marketplace BACK_TO_THE_FUTURE_MARKETPLACE "$MARKETPLACE_CODE_HASH" back_to_the_future "$salt" "$BACK_TO_THE_FUTURE_TICKET" "$BACK_TO_THE_FUTURE_TOKEN"
deploy_button_game BACK_TO_THE_FUTURE BackToTheFuture $BACK_TO_THE_FUTURE_TICKET $BACK_TO_THE_FUTURE_TOKEN $BACK_TO_THE_FUTURE_MARKETPLACE $salt

#
# --- THE_PRESSIAH_COMETH GAME
Expand All @@ -241,8 +241,8 @@ echo "The Pressiah Cometh"
salt="0x7468655F70726573736961685F636F6D657468"
deploy_ticket_token THE_PRESSIAH_COMETH_TICKET the_pressiah_cometh_ticket TPCT $salt
deploy_game_token THE_PRESSIAH_COMETH_TOKEN the_pressiah_cometh TPC $salt
deploy_button_game THE_PRESSIAH_COMETH ThePressiahCometh $THE_PRESSIAH_COMETH_TICKET $THE_PRESSIAH_COMETH_TOKEN $salt
deploy_marketplace THE_PRESSIAH_COMETH_MARKETPLACE "$MARKETPLACE_CODE_HASH" the_pressiah_cometh "$salt" "$THE_PRESSIAH_COMETH_TICKET" "$THE_PRESSIAH_COMETH_TOKEN" "$THE_PRESSIAH_COMETH"
deploy_marketplace THE_PRESSIAH_COMETH_MARKETPLACE "$MARKETPLACE_CODE_HASH" the_pressiah_cometh "$salt" "$THE_PRESSIAH_COMETH_TICKET" "$THE_PRESSIAH_COMETH_TOKEN"
deploy_button_game THE_PRESSIAH_COMETH ThePressiahCometh $THE_PRESSIAH_COMETH_TICKET $THE_PRESSIAH_COMETH_TOKEN $THE_PRESSIAH_COMETH_MARKETPLACE $salt

# spit adresses to a JSON file
cd "$CONTRACTS_PATH"
Expand Down
21 changes: 1 addition & 20 deletions contracts/ticket_token/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub mod ticket_token {
use ink_prelude::{format, string::String};
use ink_storage::traits::SpreadAllocate;
use openbrush::{
contracts::psp22::{extensions::metadata::*, Internal, Transfer},
contracts::psp22::{extensions::metadata::*, Internal},
traits::Storage,
};

Expand All @@ -37,25 +37,6 @@ pub mod ticket_token {

impl PSP22Metadata for TicketToken {}

impl Transfer for TicketToken {
fn _before_token_transfer(
&mut self,
from: Option<&AccountId>,
to: Option<&AccountId>,
amount: &Balance,
) -> core::result::Result<(), PSP22Error> {
// if from is None this is an initial mint in the constructor
// and we don't want to enforce it there
if from.is_some() && to.is_some() && !amount.eq(&1u128) {
return Err(PSP22Error::Custom(String::from(
"Only single ticket can be transferred at once",
)));
}

Ok(())
}
}

impl Internal for TicketToken {
fn _emit_transfer_event(
&self,
Expand Down