-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ganesh oli
committed
Jan 16, 2023
1 parent
e8fbbab
commit d02477c
Showing
6 changed files
with
292 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
[package] | ||
name = "erc20_crowd_sale" | ||
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 } | ||
# openbrush = { tag = "v2.3.0", git = "https://github.com/Supercolony-net/openbrush-contracts", default-features = false, features = ["psp22"] } | ||
openbrush = { tag = "v2.3.0", git = "https://github.com/Supercolony-net/openbrush-contracts", default-features = false, features = ["psp22"] } | ||
|
||
[lib] | ||
name = "erc20_crowd_sale" | ||
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", | ||
"openbrush/std", | ||
] | ||
ink-as-dependency = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
#![cfg_attr(not(feature = "std"), no_std)] | ||
#![feature(min_specialization)] | ||
|
||
// use ink_lang as ink; | ||
|
||
#[openbrush::contract] | ||
mod erc20_crowd_sale { | ||
|
||
use ink_env::debug_print; | ||
use ink_storage::traits::SpreadAllocate; | ||
use openbrush::{ | ||
contracts::psp22::{ | ||
Transfer, | ||
extensions::metadata::*, | ||
}, | ||
traits::{Storage, String}, | ||
}; | ||
|
||
#[ink(storage)] | ||
#[derive(Default, SpreadAllocate, Storage)] | ||
pub struct Erc20CrowdSale { | ||
#[storage_field] | ||
psp22: psp22::Data, | ||
#[storage_field] | ||
metadata: metadata::Data, | ||
owner: AccountId, | ||
buy_rate: u128, | ||
hated_account: AccountId, | ||
} | ||
|
||
#[ink(event)] | ||
pub struct TokenBuy { | ||
caller: AccountId, | ||
value: u128, | ||
tokens: u128, | ||
} | ||
|
||
impl Transfer for Erc20CrowdSale { | ||
// let's override method to reject transactions to bad account | ||
fn _before_token_transfer( | ||
&mut self, | ||
_from: Option<&AccountId>, | ||
to: Option<&AccountId>, | ||
_amount: &Balance, | ||
) -> Result<(), PSP22Error> { | ||
if to == Some(&self.hated_account) { | ||
return Err(PSP22Error::Custom(String::from("I hate this account"))); | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl PSP22 for Erc20CrowdSale {} | ||
impl PSP22Metadata for Erc20CrowdSale {} | ||
|
||
impl Erc20CrowdSale { | ||
#[ink(constructor)] | ||
pub fn new( | ||
total_supply: u128, | ||
buy_rate: u128, | ||
name: Option<String>, | ||
symbol: Option<String>, | ||
decimal: u8 | ||
) -> Self { | ||
let caller = Self::env().caller(); | ||
ink_lang::utils::initialize_contract(|instance: &mut Erc20CrowdSale| { | ||
instance.metadata.name = name; | ||
instance.metadata.symbol = symbol; | ||
instance.metadata.decimals = decimal; | ||
instance.owner = caller; | ||
instance | ||
._mint_to(caller, total_supply) | ||
.expect("Should mint"); | ||
instance.buy_rate = buy_rate; | ||
}) | ||
} | ||
|
||
#[ink(message, payable)] | ||
pub fn buy_token(&self) { | ||
let caller = self.env().caller(); | ||
let value = self.env().transferred_value(); | ||
let balance = self.env().balance(); | ||
debug_print!("my value: {}\n",value); | ||
debug_print!("contract balance: {}\n",balance); | ||
// assert!(value > 0, "Transferred value must not be 0"); | ||
let tokens = value * self.buy_rate; | ||
self.env().transfer(caller, tokens).unwrap(); | ||
self.env().transfer(self.owner, value).unwrap(); | ||
self.env().emit_event(TokenBuy{caller, value, tokens}); | ||
} | ||
|
||
#[ink(message)] | ||
pub fn set_hated_account(&mut self, hated: AccountId) { | ||
self.hated_account = hated; | ||
} | ||
|
||
#[ink(message)] | ||
pub fn get_hated_account(&self) -> AccountId { | ||
self.hated_account.clone() | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
[package] | ||
name = "tokenlist" | ||
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 = "tokenlist" | ||
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 = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 tokenlist { | ||
use ink_storage::{Mapping, traits::SpreadAllocate}; | ||
use ink_prelude::{ | ||
vec::Vec, | ||
string::String | ||
}; | ||
|
||
pub type NID = i32; | ||
|
||
#[ink(storage)] | ||
#[derive(SpreadAllocate)] | ||
pub struct Tokenlist { | ||
value: bool, | ||
name: Mapping<NID, String>, | ||
nid: NID, | ||
} | ||
|
||
impl Tokenlist { | ||
#[ink(constructor)] | ||
pub fn new(init_value: bool) -> Self { | ||
ink_lang::utils::initialize_contract(|instance: &mut Tokenlist|{ | ||
instance.value = init_value; | ||
instance.name = Mapping::default(); | ||
instance.nid = Default::default(); | ||
}) | ||
} | ||
|
||
#[ink(constructor)] | ||
pub fn default() -> Self { | ||
ink_lang::utils::initialize_contract(|_instance: &mut Tokenlist|{}); | ||
Self::new(Default::default()) | ||
} | ||
|
||
#[ink(message)] | ||
pub fn add_name(&mut self, name: String) { | ||
self.name.insert(self.nid, &name); | ||
self.nid += 1; | ||
} | ||
|
||
#[ink(message)] | ||
pub fn get_name(&self) -> Vec<String> { | ||
let mut result: Vec<String> = Vec::new(); | ||
for i in 0..self.nid { | ||
match self.name.get(i) { | ||
Some(value) => result.push(value), | ||
None => () | ||
} | ||
} | ||
result | ||
} | ||
|
||
#[ink(message)] | ||
pub fn flip(&mut self) { | ||
self.value = !self.value; | ||
} | ||
|
||
#[ink(message)] | ||
pub fn get(&self) -> bool { | ||
self.value | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
use ink_lang as ink; | ||
|
||
#[ink::test] | ||
fn default_works() { | ||
let tokenlist = Tokenlist::default(); | ||
assert_eq!(tokenlist.get(), false); | ||
} | ||
|
||
#[ink::test] | ||
fn it_works() { | ||
let mut tokenlist = Tokenlist::new(false); | ||
assert_eq!(tokenlist.get(), false); | ||
tokenlist.flip(); | ||
assert_eq!(tokenlist.get(), true); | ||
} | ||
} | ||
} |