Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
wip
  • Loading branch information
fbielejec committed Aug 16, 2022
commit 72d77b56cc7b6cfe94d33f6f167217653f9e4d56
9 changes: 5 additions & 4 deletions contracts/back_to_the_future/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ mod back_to_the_future {

use access_control::{traits::AccessControlled, Role, ACCESS_CONTROL_PUBKEY};
use button::{
ButtonData, ButtonGame, ButtonGameEnvironment, ButtonResult, GameError, IButtonGame, Score,
ButtonData, ButtonGame, ButtonGameEnvironment, ButtonResult, GameError, IButtonGame,
};
use game_token::{BALANCE_OF_SELECTOR, TRANSFER_SELECTOR};
use game_token::{BALANCE_OF_SELECTOR, MINT_TO_SELECTOR, TRANSFER_SELECTOR};
use ink_env::Error as InkEnvError;
use ink_lang::{
codegen::{initialize_contract, EmitEvent},
Expand Down Expand Up @@ -88,7 +88,7 @@ mod back_to_the_future {
&mut self.data
}

fn score(&self, now: BlockNumber) -> Score {
fn score(&self, now: BlockNumber) -> Balance {
if let Some(last_press) = self.get().last_press {
return now - last_press;
}
Expand All @@ -108,7 +108,7 @@ mod back_to_the_future {
fn press(&mut self) -> ButtonResult<()> {
let caller = self.env().caller();
let now = Self::env().block_number();
ButtonGame::press(self, now, caller)?;
ButtonGame::press(self, TRANSFER_SELECTOR, MINT_TO_SELECTOR, now, caller, this)?;
Self::emit_event(
self.env(),
Event::ButtonPressed(ButtonPressed {
Expand Down Expand Up @@ -270,6 +270,7 @@ mod back_to_the_future {
self.data.access_control = AccountId::from(ACCESS_CONTROL_PUBKEY);
self.data.button_lifetime = button_lifetime;
self.data.game_token = game_token;
self.data.last_press = Some(now);

Self::emit_event(
Self::env(),
Expand Down
68 changes: 68 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 @@ -17,6 +17,7 @@ scale-info = { version = "2", default-features = false, features = ["derive"], o

access_control = { path = "../access_control", 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"
Expand All @@ -36,5 +37,6 @@ std = [
"ink_storage/std",
"scale-info/std",
"scale/std",
"num/std"
]
ink-as-dependency = []
112 changes: 38 additions & 74 deletions contracts/button/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ pub struct ButtonData {
/// counter for the number of presses
pub presses: u128,
/// AccountId of the PSP22 ButtonToken instance on-chain
pub game_token: AccountId,
pub reward_token: AccountId,
/// Account ID of the ticket token
pub ticket_token: AccountId,
/// access control contract
Expand Down Expand Up @@ -159,8 +159,8 @@ pub trait ButtonGame {
/// where k is the number of players that participated until the button has died
/// Can be overriden to some other custom calculation
fn pressiah_score(&self) -> Balance {
let presses = self.get().presses as f64;
(presses * f64::sqrt(presses)) as Balance
let presses = self.get().presses;
(presses * num::integer::sqrt(presses)) as Balance
}

fn is_dead(&self, now: BlockNumber) -> bool {
Expand Down Expand Up @@ -203,22 +203,22 @@ pub trait ButtonGame {
self.get().last_presser
}

fn game_token(&self) -> AccountId {
self.get().game_token
fn reward_token(&self) -> AccountId {
self.get().reward_token
}

// fn balance<E>(&self, balance_of_selector: [u8; 4], this: AccountId) -> ButtonResult<Balance>
// where
// E: Environment<AccountId = AccountId>,
// {
// let game_token = self.get().game_token;
// let balance = build_call::<E>()
// .call_type(Call::new().callee(game_token))
// .exec_input(ExecutionInput::new(Selector::new(balance_of_selector)).push_arg(this))
// .returns::<Balance>()
// .fire()?;
// Ok(balance)
// }
fn balance<E>(&self, balance_of_selector: [u8; 4], this: AccountId) -> ButtonResult<Balance>
where
E: Environment<AccountId = AccountId>,
{
let ticket_token = self.get().ticket_token;
let balance = build_call::<E>()
.call_type(Call::new().callee(ticket_token))
.exec_input(ExecutionInput::new(Selector::new(balance_of_selector)).push_arg(this))
.returns::<Balance>()
.fire()?;
Ok(balance)
}

fn transfer_tx<E>(
&self,
Expand All @@ -230,7 +230,7 @@ pub trait ButtonGame {
E: Environment<AccountId = AccountId>,
{
build_call::<E>()
.call_type(Call::new().callee(self.get().game_token))
.call_type(Call::new().callee(self.get().reward_token))
.exec_input(
ExecutionInput::new(Selector::new(transfer_selector))
.push_arg(to)
Expand All @@ -251,7 +251,7 @@ pub trait ButtonGame {
E: Environment<AccountId = AccountId>,
{
build_call::<E>()
.call_type(Call::new().callee(self.get().game_token))
.call_type(Call::new().callee(self.get().reward_token))
.exec_input(
ExecutionInput::new(Selector::new(mint_to_selector))
.push_arg(to)
Expand All @@ -276,7 +276,6 @@ pub trait ButtonGame {
)
}

// TODO : transfer ticket token
fn press<E>(
&mut self,
transfer_selector: [u8; 4],
Expand Down Expand Up @@ -311,6 +310,7 @@ pub trait ButtonGame {
state.presses = presses + 1;
state.last_presser = Some(caller);
state.last_press = Some(now);

swap(self.get_mut(), &mut state);

Ok(())
Expand All @@ -321,62 +321,31 @@ pub trait ButtonGame {
/// Erases the storage and pays award to the Pressiah
/// Can be called by any account on behalf of a player
/// Can only be called after button's deadline
fn reset<E>(
&mut self,
now: BlockNumber,
for_player: AccountId,
// balance_of_selector: [u8; 4],
transfer_selector: [u8; 4],
this: AccountId,
) -> ButtonResult<()>
fn reset<E>(&mut self, now: BlockNumber, mint_to_selector: [u8; 4]) -> ButtonResult<()>
where
E: Environment<AccountId = AccountId>,
{
let ButtonData {
presses,
last_presser,
..
} = self.get();
let ButtonData { last_presser, .. } = self.get();

if !self.is_dead(now) {
return Err(GameError::BeforeDeadline);
}

// if reward_claimed.get(&for_player).is_some() {
// return Err(GameError::AlreadyCLaimed);
// }
if let Some(pressiah) = last_presser {
let reward = self.pressiah_score();
self.mint_tx::<E>(mint_to_selector, *pressiah, reward)??;
};

// let mut total_rewards = 0;

// TODO
match last_presser {
None => Ok(()), // there weren't any players
Some(pressiah) => {
// let total_balance = self.balance::<E>(balance_of_selector, this)?;
// let pressiah_reward = total_balance / 2;
// let remaining_balance = total_balance - pressiah_reward;

// if &for_player == pressiah {
// // Pressiah gets 50% of supply
// self.transfer_tx::<E>(transfer_selector, *pressiah, pressiah_reward)??;
// total_rewards += pressiah_reward;
// }

// // NOTE: in this design the Pressiah gets *both* his/her reward *and* a reward for playing

// if let Some(score) = presses.get(&for_player) {
// // transfer reward proportional to the score
// let reward = (score as u128 * remaining_balance) / *total_scores as u128;
// self.transfer_tx::<E>(transfer_selector, for_player, reward)??;
// zero the counters in storage
let root_key = ::ink_primitives::Key::from([0x00; 32]);
let mut state = ::ink_storage::traits::pull_spread_root::<ButtonData>(&root_key);

// // pressiah is also marked as having made the claim, because his/her score was recorder
// self.get_mut().reward_claimed.insert(&for_player, &());
// total_rewards += reward;
// }
state.presses = 0;
state.last_presser = None;
state.last_press = None;
swap(self.get_mut(), &mut state);

Ok(())
}
}
Ok(())
}
}

Expand All @@ -390,10 +359,6 @@ pub trait IButtonGame {
#[ink(message)]
fn press(&mut self) -> ButtonResult<()>;

// /// Pays out the award
// #[ink(message)]
// fn claim_reward(&mut self, for_player: AccountId) -> ButtonResult<()>;

/// Returns the buttons status
#[ink(message)]
fn is_dead(&self) -> bool;
Expand All @@ -414,16 +379,15 @@ pub trait IButtonGame {

/// Returns address of the game's reward token
#[ink(message)]
fn game_token(&self) -> AccountId;
fn reward_token(&self) -> AccountId;

/// Returns address of the game's ticket token
#[ink(message)]
fn ticket_token(&self) -> AccountId;

// TODO : balance of ticket token
// /// Returns then game token balance of the game contract
// #[ink(message)]
// fn balance(&self) -> ButtonResult<Balance>;
/// Returns then number of ticket tokens in the game contract
#[ink(message)]
fn balance(&self) -> ButtonResult<Balance>;

/// Resets the game
#[ink(message)]
Expand Down
Loading