Skip to content
Open
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
Binary file added .DS_Store
Binary file not shown.
Binary file added token-factory/.DS_Store
Binary file not shown.
9 changes: 9 additions & 0 deletions token-factory/.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
33 changes: 33 additions & 0 deletions token-factory/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[package]
name = "TokenFactory"
version = "0.1.0"
authors = ["[Luciano Otto Rodrigues] [https://github.com/Lucianoottor]"]
edition = "2024"

[dependencies]
ink = { version = "6.0.0-beta.1", default-features = false, features = ["unstable-hostfn"] }
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2.6", default-features = false, features = ["derive"], optional = true }
SimpleToken = { path = "SimpleToken", default-features = false, features = ["ink-as-dependency"] } # we have to pass the other contract as dependency


[lib]
path = "lib.rs"

[features]
default = ["std"]
std = [
"ink/std",
"SimpleToken/std" # enable std for the dependency
]
ink-as-dependency = []
e2e-tests = []

[package.metadata.ink-lang]
abi = "ink"

[lints.rust.unexpected_cfgs]
level = "warn"
check-cfg = [
'cfg(ink_abi, values("ink", "sol", "all"))'
]
Binary file added token-factory/SimpleToken/.DS_Store
Binary file not shown.
9 changes: 9 additions & 0 deletions token-factory/SimpleToken/.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
33 changes: 33 additions & 0 deletions token-factory/SimpleToken/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[package]
name = "SimpleToken"
version = "0.1.0"
authors = ["[Luciano Otto Rodrigues] [https://github.com/Lucianoottor]"]
edition = "2024"

[dependencies]
ink = { version = "6.0.0-beta.1", default-features = false, features = ["unstable-hostfn"] }
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2.6", default-features = false, features = ["derive"], optional = true }

[dev-dependencies]
ink_e2e = { version = "6.0.0-beta.1" }

[lib]
path = "lib.rs"

[features]
default = ["std"]
std = [
"ink/std",
]
ink-as-dependency = []
e2e-tests = []

[package.metadata.ink-lang]
abi = "ink"

[lints.rust.unexpected_cfgs]
level = "warn"
check-cfg = [
'cfg(ink_abi, values("ink", "sol", "all"))'
]
27 changes: 27 additions & 0 deletions token-factory/SimpleToken/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#![cfg_attr(not(feature = "std"), no_std, no_main)]

#[ink::contract]
pub mod SimpleToken {

use ink::storage::Mapping;


#[ink(storage)]
pub struct SimpleToken {
balances: Mapping<ink::Address, Balance>
}

impl SimpleToken {
#[ink(constructor)]
pub fn new(initial_supply: Balance) -> Self {
let mut new = Mapping::new();
new.insert(ink::env::caller(), &initial_supply);
Self { balances: new }
}

#[ink(message)]
pub fn balance_of(&self, owner: ink::Address) -> Balance {
self.balances.get(&owner).unwrap_or(0)
}
}
}
49 changes: 49 additions & 0 deletions token-factory/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#![cfg_attr(not(feature = "std"), no_std, no_main)]

// Factory contract that can deploy instances of the SimpleToken contract
// It uses cross-contract calls to instantiate new token contracts and factory pattern

#[ink::contract]
mod TokenFactory {

use ink::storage::StorageVec;
use ink::prelude::vec::Vec;
use SimpleToken::SimpleTokenRef;

#[ink(storage)]
pub struct TokenFactory {
token_code_hash: ink::H256,
children: StorageVec<ink::Address>,
}

impl TokenFactory {

#[ink(constructor)]
pub fn new(token_code_hash: ink::H256) -> Self {
Self {
token_code_hash,
children: StorageVec::new(),
}
}

// Creates a new instance of the SimpleToken contract with the initial parameter
#[ink(message)]
pub fn create_token(&mut self, initial_supply: Balance) {
let token_instance = SimpleTokenRef::new(initial_supply) // Here happens the cross-contract call, we promise to deploy a new SimpleToken and we will store its address to our mapping "children", so the factory can track the deployed tokens
.code_hash(self.token_code_hash) // We provide the code hash of the SimpleToken contract to instantiate it, ensuring that the correct contract code is used
.salt_bytes(None)
.instantiate();
let token_address = ink::ToAddr::to_addr(&token_instance);
self.children.push(&token_address);
}

#[ink(message)]
pub fn list_tokens(&self) -> Vec<ink::Address> {
let mut tokens = Vec::new();
for i in 0..self.children.len() {
tokens.push(self.children.get(i).unwrap());
}
tokens
}
}
}
Loading