From 79650525285a05b5d630d1df19ba378b03dd15f2 Mon Sep 17 00:00:00 2001 From: Luciano Otto Rodrigues Date: Thu, 27 Nov 2025 01:08:06 -0300 Subject: [PATCH] First token factory commit --- .DS_Store | Bin 0 -> 10244 bytes token-factory/.DS_Store | Bin 0 -> 6148 bytes token-factory/.gitignore | 9 +++++ token-factory/Cargo.toml | 33 ++++++++++++++++++ token-factory/SimpleToken/.DS_Store | Bin 0 -> 6148 bytes token-factory/SimpleToken/.gitignore | 9 +++++ token-factory/SimpleToken/Cargo.toml | 33 ++++++++++++++++++ token-factory/SimpleToken/lib.rs | 27 +++++++++++++++ token-factory/lib.rs | 49 +++++++++++++++++++++++++++ 9 files changed, 160 insertions(+) create mode 100644 .DS_Store create mode 100644 token-factory/.DS_Store create mode 100755 token-factory/.gitignore create mode 100755 token-factory/Cargo.toml create mode 100644 token-factory/SimpleToken/.DS_Store create mode 100755 token-factory/SimpleToken/.gitignore create mode 100755 token-factory/SimpleToken/Cargo.toml create mode 100755 token-factory/SimpleToken/lib.rs create mode 100755 token-factory/lib.rs diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..9e414095b42b795763ca62d4697c979ad6fc1eb3 GIT binary patch literal 10244 zcmeHM%Wfk@6uq6tIG$u?93~M6X^%+7SRgd2Zgp3?eb1>|=iaJvl|>{v8a8)D$|ACY zi)CpIw>byD=QVRIAb$>+m8d5EKXs1ODGgjA1D7bTIm%Zkk=71eS-UCa(!pB3Es4ZWlB`irvO28U!5-n-exV1RL~ctRcC?RWTfPHVCaH({#ltKEGe(WE z^j*v7foJ)qOniYS@*F!1ec&?eGSsAnH|BFshOisp&fN!}^Cyq&Q@3BL#GdkVG$ zT$8%k?%L!#&%-}Gw);8TKz*VY6>z3<0WTXju`IMameL#+r=_kJ8ik*&D7-cYPknoc z3%y}${75n?Hplv04l12O^>4}MdZE#>8P?`7Jjc6^hhs!++y-oV(2JR9qL9L0)@<~o zjtNdbvW$hz^WdNIpq3iqf1zz_pk+~SDI4#x&Oj?r_5$o!T@wFOU@B~*&h=G)o)N1t+d7Qusvy0t}!iDOj) z(;hCy!>C_DJwqVbXVBX{JYwPC3&8e(-Nktz2d=fSZ9iE0Ea%pHvg2AZ5)SXJ-12>R zx9i%f>NYEFFL2Rkzr9q&w!Mc8H|=agV~#e|mgA!KBey11%SIP~PM}K{=WIWrHn#N| ztcUVF&Wf@#hU}#+w-#-?jayYK_LX>&Gqbg+3V&GI7T{S8b&XYvhFgb5!nOWbp2%Z4 zkx!*6C$2Snd}Ld1!jB>N>HpvL+*wVSZ#L74#ncCzr$s~!ZTmGS&GF$Jcg_Si%sr~1 zjk~9~@l-#0eFlycXDo@^UTV4UwWZ}3c zE9n1R7Ao$r+y;X2x92LqFG5=f!#2-g&;pJp9m(7_Q)vfXl&8psM{+Empxg#x&S>c% zHc^{3QfOvO#t`~#jqYyUs`qb}-X$nBhJ6DZod2nF7s_LlgC~7C^YGN%#;?gU)OQa1 zR6YWl^9g@Fj9nc2^WukAy*J|OqkcF4HS}w}o3#6bot@vKg~g@iTZKZguv+-4dX|l< zgW6!&KdSY<;HPI_^OsIHG*H zBzMf`=^bsA8{2nL?WIYU^7jexZ$U54F8rm$d|@ouWB$!-%}lmqv+8i0*@cm% z2)ZX=;SAhEX=Ws5f1by#FB~fzhH1#dLr@?n5EPgyuxOsjt0+%y{{Me!A0C1NL4j9K zfoSQtdVGX-O@1u;P2^sC8P_+sc+l?JlnTL3x8qUjc04|CJN_M9wB;V79(X?F1IlYt o;t9up{%1fPm0}-LSF-lnI;{U+Xg~@Q*8k3Kx3jW+CD#9c0aQ2mjsO4v literal 0 HcmV?d00001 diff --git a/token-factory/.DS_Store b/token-factory/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..f8b88e84f6a0e1bb4f1cd0534022d7b3f55f9209 GIT binary patch literal 6148 zcmeHK%Wl&^6upy%Bql=2f=XpkX(YB$NfcF4#m1y%!4_9#0Vvo_RIE6j$acsgisU_f z1HZtUAK`c4%p(7;4*`0?a8T zTbyrOoEqMZm?GRGqkJ=0oNnbdDM0R&X5ftJ8vRdz=_boO)z{yi>HsTRQy4jF6qn#ITpumY^WUIpA@(P-{%@^G>P)3GM>3k2Lm|`mE3^R88-4(o!bfB6 zp2szOL8V2%2yhh@d1mr6#Mhj4uAnF&3Wx$XP{1!Go&6hdg(3=w0=HWMULQ1^G4xnF zv`YsHdjtSB5w?ap|1L5|dkj6+4$%WsjtX>C<&PN3(Gk}^FZ5VDbaYbw@S*(3%AZh_ z9Ua%#Hl0-HP)boi6xdW?&o#Te|G)V2`G1onEm1%exK#?M=1Fui#v}Q?b?4!DuhsC6 qa5m1X9bQvVv0E{6c`JShw}!ap3NZ9oJ46H~KLS<;DMW#Ps=!a6KYl6z literal 0 HcmV?d00001 diff --git a/token-factory/SimpleToken/.gitignore b/token-factory/SimpleToken/.gitignore new file mode 100755 index 00000000..8de8f877 --- /dev/null +++ b/token-factory/SimpleToken/.gitignore @@ -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 diff --git a/token-factory/SimpleToken/Cargo.toml b/token-factory/SimpleToken/Cargo.toml new file mode 100755 index 00000000..04743f99 --- /dev/null +++ b/token-factory/SimpleToken/Cargo.toml @@ -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"))' +] diff --git a/token-factory/SimpleToken/lib.rs b/token-factory/SimpleToken/lib.rs new file mode 100755 index 00000000..81a566b8 --- /dev/null +++ b/token-factory/SimpleToken/lib.rs @@ -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 + } + + 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) + } + } +} diff --git a/token-factory/lib.rs b/token-factory/lib.rs new file mode 100755 index 00000000..e601c3ce --- /dev/null +++ b/token-factory/lib.rs @@ -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, + } + + 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 { + let mut tokens = Vec::new(); + for i in 0..self.children.len() { + tokens.push(self.children.get(i).unwrap()); + } + tokens + } + } +}