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

feat: support evm/svm gasstation #85

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
719 changes: 719 additions & 0 deletions addnewPlayerReponse.json

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions contract/src/abi_structs.sw
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@ use std::{bytes::Bytes, hash::{Hash, Hasher}};
abi GameContract {
// initialize player, mint some coins
#[storage(read, write)]
fn new_player();
fn new_player(address: Identity);

// get asset ID
fn get_asset_id() -> AssetId;

// level up farming skill
#[storage(read, write)]
fn level_up();
fn level_up(address: Identity);

// buy any amount of a certain seed
#[storage(read, write), payable]
fn buy_seeds(food_type: FoodType, amount: u64);
fn buy_seeds(food_type: FoodType, amount: u64, address: Identity);

#[storage(read, write)]
fn plant_seed_at_index(food_type: FoodType, index: u64);
fn plant_seed_at_index(food_type: FoodType, index: u64, address: Identity);

// harvest grown seeds at certain indexes
#[storage(read, write)]
fn harvest(indexes: Vec<u64>);
fn harvest(indexes: Vec<u64>, address: Identity);

// sell a harvested item
#[storage(read, write)]
fn sell_item(food_type: FoodType, amount: u64);
fn sell_item(food_type: FoodType, amount: u64, address: Identity);

#[storage(read)]
fn get_player(id: Identity) -> Option<Player>;
Expand All @@ -45,7 +45,7 @@ abi GameContract {
fn can_level_up(id: Identity) -> bool;

#[storage(read)]
fn can_harvest(index: u64) -> bool;
fn can_harvest(index: u64, address: Identity) -> bool;
}

pub struct Player {
Expand Down
37 changes: 19 additions & 18 deletions contract/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ storage {

impl GameContract for Contract {
#[storage(read, write)]
fn new_player() {
fn new_player(address: Identity) {
// get the message sender
let sender = msg_sender().unwrap();
let sender = address;
// let sender = msg_sender().unwrap();

// make sure the player doesn't already exist
require(
Expand Down Expand Up @@ -73,9 +74,11 @@ impl GameContract for Contract {
}

#[storage(read, write)]
fn level_up() {
fn level_up(address: Identity) {
// get the player with the message sender
let mut player = storage.players.get(msg_sender().unwrap()).try_read().unwrap();
let sender = address;

let mut player = storage.players.get(sender).try_read().unwrap();

// the max skill level is 10
require(player.farming_skill < 10, "skill already max");
Expand All @@ -89,8 +92,6 @@ impl GameContract for Contract {
// increase the player's skill level
player.level_up_skill();

let sender = msg_sender().unwrap();

// overwrite storage with the updated player struct
storage.players.insert(sender, player);

Expand All @@ -102,7 +103,7 @@ impl GameContract for Contract {
}

#[storage(read, write), payable]
fn buy_seeds(food_type: FoodType, amount: u64) {
fn buy_seeds(food_type: FoodType, amount: u64, address: Identity) {
let asset_id = msg_asset_id();
require(
asset_id == AssetId::default(),
Expand All @@ -121,7 +122,7 @@ impl GameContract for Contract {
// require that the amount is at least the price of the item
require(msg_amount() >= cost, InvalidError::NotEnoughTokens(amount));

let sender = msg_sender().unwrap();
let sender = address;

// check how many seeds the player currenly has
let current_amount_option = storage.player_seeds.get((sender, food_type)).try_read();
Expand All @@ -144,9 +145,9 @@ impl GameContract for Contract {
}

#[storage(read, write)]
fn plant_seed_at_index(food_type: FoodType, index: u64) {
fn plant_seed_at_index(food_type: FoodType, index: u64, address: Identity) {
// get the sender
let sender = msg_sender().unwrap();
let sender = address;
// require player has this many seeds
let current_amount_option = storage.player_seeds.get((sender, food_type)).try_read();
let current_amount = current_amount_option.unwrap_or(0);
Expand Down Expand Up @@ -174,13 +175,13 @@ impl GameContract for Contract {
}

#[storage(read, write)]
fn harvest(indexes: Vec<u64>) {
fn harvest(indexes: Vec<u64>, address: Identity) {
// use this for testing
let time = 0;
// let one_min = 120;
// let time = one_min * 5;

let sender = msg_sender().unwrap();
let sender = address;
let mut planted_seeds = storage.planted_seeds.get(sender).try_read().unwrap();
let current_time = timestamp();

Expand Down Expand Up @@ -216,8 +217,8 @@ impl GameContract for Contract {
}

#[storage(read, write)]
fn sell_item(food_type: FoodType, amount: u64) {
let sender = msg_sender().unwrap();
fn sell_item(food_type: FoodType, amount: u64, address: Identity) {
let sender = address;

// make sure they have that amount
let current_amount = storage.player_items.get((sender, food_type)).try_read().unwrap();
Expand All @@ -236,9 +237,9 @@ impl GameContract for Contract {
};

// increase the player's total_value_sold
let mut player = storage.players.get(msg_sender().unwrap()).try_read().unwrap();
let mut player = storage.players.get(sender).try_read().unwrap();
player.increase_tvs(amount_to_mint);
storage.players.insert(msg_sender().unwrap(), player);
storage.players.insert(sender, player);

// send tokens
mint_to(sender, DEFAULT_SUB_ID, amount_to_mint);
Expand Down Expand Up @@ -297,8 +298,8 @@ impl GameContract for Contract {
}

#[storage(read)]
fn can_harvest(index: u64) -> bool {
let sender = msg_sender().unwrap();
fn can_harvest(index: u64, address: Identity) -> bool {
let sender = address;
let planted_seeds_result = storage.planted_seeds.get(sender);
if planted_seeds_result.try_read().is_none() {
return false;
Expand Down
1 change: 1 addition & 0 deletions frontend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ dist-ssr
*.njsproj
*.sln
*.sw?
*.env
Loading
Loading