Skip to content

Commit

Permalink
simple bank contract
Browse files Browse the repository at this point in the history
  • Loading branch information
Ganesh oli committed Dec 6, 2022
1 parent f628acd commit 62ae13a
Show file tree
Hide file tree
Showing 7 changed files with 253 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lotterycontract/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Ignore build artifacts from the local tests sub-crate.
/target/

# Ignore backup files creates by cargo fmt.
**/*.rs.bk

# Remove Cargo.lock when creating an executable, leave it for libraries
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
Cargo.lock
36 changes: 36 additions & 0 deletions lotterycontract/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[package]
name = "lotterycontract"
version = "0.1.0"
authors = ["[your_name] <[your_email]>"]
edition = "2021"

[dependencies]
ink_primitives = { version = "3.3", default-features = false }
ink_metadata = { version = "3.3", default-features = false, features = ["derive"], optional = true }
ink_env = { version = "3.3", default-features = false }
ink_storage = { version = "3.3", default-features = false }
ink_lang = { version = "3.3", default-features = false }
ink_prelude = { version = "3.3", default-features = false}
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2", default-features = false, features = ["derive"], optional = true }

[lib]
name = "lotterycontract"
path = "lib.rs"
crate-type = [
# Used for normal contract Wasm blobs.
"cdylib",
]

[features]
default = ["std"]
std = [
"ink_metadata/std",
"ink_env/std",
"ink_storage/std",
"ink_primitives/std",
"scale/std",
"scale-info/std",
"ink_prelude/std"
]
ink-as-dependency = []
88 changes: 88 additions & 0 deletions lotterycontract/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#![cfg_attr(not(feature = "std"), no_std)]

use ink_lang as ink;

#[ink::contract]
mod lotterycontract {
use ink_env::AccountId;
use ink_storage::{Mapping, traits::{SpreadAllocate, SpreadLayout, PackedLayout, StorageLayout, PackedAllocate}};
use scale::{Encode, Decode};
use ink_prelude::vec::Vec;
// use ink_primitives::Key;

#[derive(
Encode,
Decode,
SpreadLayout,
PackedLayout,
SpreadAllocate,
Default,
)]
#[cfg_attr(
feature = "std",
derive(Debug, PartialEq, Eq, scale_info::TypeInfo, StorageLayout)
)]
pub struct Lottery {
lottery_name: Vec<u8>,
lottery_manager: AccountId,
}

// impl ink_storage::traits::PackedAllocate for Lottery {
// fn allocate_packed(&mut self, at: &Key){
// PackedAllocate::allocate_packed(&mut *self, at)
// }
// }

pub type LotteryId = u32;

#[ink(storage)]
#[derive(SpreadAllocate)]
pub struct Lotterycontract {
my_lottery: Mapping<LotteryId, Lottery>,
next_lottery_id: u32,
is_lottery_live: bool,
lottery_bag: Vec<AccountId>
}

impl Lotterycontract {
#[ink(constructor)]
pub fn new() -> Self {
ink_lang::utils::initialize_contract(|_instance: &mut Self|{
_instance.my_lottery = Mapping::default();
_instance.is_lottery_live = false;
_instance.lottery_bag = Default::default();
})
}

#[ink(message)]
pub fn create_lottery(&mut self, lottery_name: Vec<u8>) {
let lottery_manager = self.env().caller();
let lottery = Lottery {
lottery_name,
lottery_manager
};

let lottery_id = self.next_lottery_id();
self.my_lottery.insert(lottery_id, &lottery);
self.is_lottery_live = true;
}

#[ink(message)]
pub fn participate(&mut self, player: AccountId) {
self.lottery_bag.push(player)
}

#[ink(message)]
pub fn declare_winner(&mut self) -> AccountId {
let winner = self.lottery_bag.get(self.lottery_bag.len() % 2).unwrap();
self.is_lottery_live = false;
winner
}

fn next_lottery_id(&mut self) -> LotteryId {
let id = self.next_lottery_id;
self.next_lottery_id += 1;
id
}
}
}
5 changes: 5 additions & 0 deletions psp34_housetoken/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,10 @@ pub mod my_psp34_mintable {
let account_id = self.env().account_id();
self.mint(account_id, Id::U8(house_id)).unwrap();
}

#[ink(message)]
pub fn get_house(&self, house_id: u8) -> Option<House> {
self.house.get(&house_id)
}
}
}
9 changes: 9 additions & 0 deletions simplebank/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Ignore build artifacts from the local tests sub-crate.
/target/

# Ignore backup files creates by cargo fmt.
**/*.rs.bk

# Remove Cargo.lock when creating an executable, leave it for libraries
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
Cargo.lock
35 changes: 35 additions & 0 deletions simplebank/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[package]
name = "simplebank"
version = "0.1.0"
authors = ["[your_name] <[your_email]>"]
edition = "2021"

[dependencies]
ink_primitives = { version = "3.3", default-features = false }
ink_metadata = { version = "3.3", default-features = false, features = ["derive"], optional = true }
ink_env = { version = "3.3", default-features = false }
ink_storage = { version = "3.3", default-features = false }
ink_lang = { version = "3.3", default-features = false }

scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2", default-features = false, features = ["derive"], optional = true }

[lib]
name = "simplebank"
path = "lib.rs"
crate-type = [
# Used for normal contract Wasm blobs.
"cdylib",
]

[features]
default = ["std"]
std = [
"ink_metadata/std",
"ink_env/std",
"ink_storage/std",
"ink_primitives/std",
"scale/std",
"scale-info/std",
]
ink-as-dependency = []
71 changes: 71 additions & 0 deletions simplebank/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#![cfg_attr(not(feature = "std"), no_std)]

use ink_lang as ink;

#[ink::contract]
mod simplebank {
use ink_storage::{Mapping, traits::{SpreadAllocate}};


#[ink(storage)]
#[derive(SpreadAllocate)]
pub struct Simplebank {
client_count: u8,
balance: Mapping<AccountId, u128>,
owner: AccountId,
}

#[ink(event)]
pub struct DepositMade {
account_address: AccountId,
balance: u128,
}

impl Simplebank {
#[ink(constructor)]
pub fn new() -> Self {
// assert!(Self::env().balance() == 30);
let caller = Self::env().caller();

ink_lang::utils::initialize_contract(|_instance: &mut Simplebank|{
_instance.client_count = Default::default();
_instance.balance = Default::default();
_instance.owner = caller;
})
}

#[ink(message, payable)]
pub fn deposit(&mut self) {
if self.client_count < 3 {
self.client_count += 1;
self.balance.insert(self.env().caller(), &10);
} else {
self.balance.insert(self.env().caller(), &self.env().balance());
}
}

#[ink(message, payable)]
pub fn withdraw(&mut self, withdraw_amount: u128) {
let caller = self.env().caller();
let mut balance = self.balance.get(caller).unwrap_or_default();
if withdraw_amount <= balance {
balance -= withdraw_amount;
self.env().transfer(caller, withdraw_amount).unwrap_or_default();
}
}

#[ink(message)]
pub fn balance(&self) -> Option<u128>{
let caller = self.env().caller();
self.balance.get(caller)
}

#[ink(message)]
pub fn deposits_balance(&self) -> u128 {
self.env().balance()
}


}

}

0 comments on commit 62ae13a

Please sign in to comment.