Skip to content

Commit

Permalink
Merge pull request #91 from FuelLabs/mainnet/gasless
Browse files Browse the repository at this point in the history
feat: Swayfarm Gas Station Mainnet Deployment
  • Loading branch information
rishabhkeshan authored Jan 3, 2025
2 parents cb8cafb + fd2398c commit 7ac7b72
Show file tree
Hide file tree
Showing 33 changed files with 11,991 additions and 17,680 deletions.
4 changes: 2 additions & 2 deletions contract/Forc.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[[package]]
name = "core"
source = "path+from-root-7053AAA90CC5E690"
source = "path+from-root-6A8836696D55BC6E"

[[package]]
name = "farm-contract"
Expand All @@ -9,5 +9,5 @@ dependencies = ["std"]

[[package]]
name = "std"
source = "git+https://github.com/fuellabs/sway?tag=v0.66.2#31486c0b47669612acb7c64d66ecb50aea281282"
source = "git+https://github.com/fuellabs/sway?tag=v0.66.5#94a066652468b4afa3bd396dacef482ed590976b"
dependencies = ["core"]
16 changes: 9 additions & 7 deletions contract/src/abi_structs.sw
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,31 @@ 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);
#[storage(read, write), payable]
fn accelerate_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 +47,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
87 changes: 63 additions & 24 deletions contract/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ enum InvalidError {
NotEnoughTokens: u64,
NotEnoughSeeds: u64,
IncorrectAssetId: AssetId,
IncorrectAmount: u64,
}

storage {
Expand All @@ -36,9 +37,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 +75,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 +93,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 +104,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 +123,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 +146,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 All @@ -172,15 +174,52 @@ impl GameContract for Contract {
timestamp: timestamp(),
});
}
#[storage(read, write), payable]
fn accelerate_plant_seed_at_index(food_type: FoodType, index: u64, address: Identity) {
// get the sender
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);
require(
current_amount >= 1,
InvalidError::NotEnoughSeeds(current_amount),
);

let new_amount = current_amount - 1;
// update amount from player_seeds
storage.player_seeds.insert((sender, food_type), new_amount);
let current_time = timestamp();
let extra_time = 10 * 60; // 10 minutes in seconds
let accelerated_time = current_time + extra_time;
let mut vec = storage.planted_seeds.get(sender).try_read().unwrap();
let food = Food::new(food_type, Some(accelerated_time));
vec.plant_at_index(food, index);

storage.planted_seeds.insert(sender, vec);
// Check that 10 tokens were sent
let asset_id = msg_asset_id();
let amount = msg_amount();
require(amount == 10, InvalidError::IncorrectAmount(amount));

// Return the 10 tokens to the sender
transfer(sender, asset_id, amount);
log(PlantSeed {
address: sender,
food_type,
index,
timestamp: accelerated_time,
});
}

#[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 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 +255,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 +275,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 +336,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 All @@ -308,10 +347,10 @@ impl GameContract for Contract {
let food = planted_seeds.inner[index].unwrap();
let current_time = timestamp();
let planted_time = food.time_planted.unwrap();
// let one_min: u64 = 120;
let one_min: u64 = 120;
// use this for testing
let time = 0;
// let time = one_min * 5;
// let time = 0;
let time = one_min * 5;
let finish_time = planted_time + time;
if current_time >= finish_time {
true
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

0 comments on commit 7ac7b72

Please sign in to comment.