diff --git a/examples/erc20/.cargo/config b/examples/erc20/.cargo/config index 3b7ee2e..8c46df4 100644 --- a/examples/erc20/.cargo/config +++ b/examples/erc20/.cargo/config @@ -1,7 +1,16 @@ -[build] -target = "wasm32-unknown-unknown" - [target.wasm32-unknown-unknown] rustflags = [ "-C", "link-arg=-zstack-size=32768", ] + +[target.aarch64-apple-darwin] +rustflags = [ +"-C", "link-arg=-undefined", +"-C", "link-arg=dynamic_lookup", +] + +[target.x86_64-apple-darwin] +rustflags = [ +"-C", "link-arg=-undefined", +"-C", "link-arg=dynamic_lookup", +] diff --git a/examples/erc20/Cargo.lock b/examples/erc20/Cargo.lock index 7988a62..8538f33 100644 --- a/examples/erc20/Cargo.lock +++ b/examples/erc20/Cargo.lock @@ -19,7 +19,7 @@ checksum = "e416903084d3392ebd32d94735c395d6709415b76c7728e594d3f996f2b03e65" dependencies = [ "alloy-rlp", "bytes", - "cfg-if", + "cfg-if 1.0.0", "const-hex", "derive_more", "hex-literal", @@ -137,6 +137,12 @@ dependencies = [ "libc", ] +[[package]] +name = "cfg-if" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" + [[package]] name = "cfg-if" version = "1.0.0" @@ -149,7 +155,7 @@ version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08849ed393c907c90016652a01465a12d86361cd38ad2a7de026c56a520cc259" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "cpufeatures", "hex", "serde", @@ -294,7 +300,7 @@ version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "libc", "wasi", ] @@ -368,11 +374,18 @@ version = "2.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5486aed0026218e61b8a01d5fbd5a0a134649abb71a0e53b7bc088529dced86e" +[[package]] +name = "memory_units" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3" + [[package]] name = "mini-alloc" version = "0.4.2" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", + "wee_alloc", ] [[package]] @@ -623,7 +636,7 @@ version = "0.4.2" dependencies = [ "alloy-primitives", "alloy-sol-types", - "cfg-if", + "cfg-if 1.0.0", "convert_case 0.6.0", "lazy_static", "proc-macro2", @@ -640,7 +653,7 @@ version = "0.4.2" dependencies = [ "alloy-primitives", "alloy-sol-types", - "cfg-if", + "cfg-if 1.0.0", "derivative", "fnv", "hex", @@ -689,7 +702,7 @@ version = "3.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cb94d2f3cc536af71caac6b6fcebf65860b347e7ce0cc9ebe8f70d3e521054ef" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "fastrand", "redox_syscall", "rustix", @@ -756,6 +769,40 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "wee_alloc" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbb3b5a6b2bb17cb6ad44a2e68a43e8d2722c997da10e928665c72ec6c0a0b8e" +dependencies = [ + "cfg-if 0.1.10", + "libc", + "memory_units", + "winapi", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + [[package]] name = "windows-sys" version = "0.48.0" diff --git a/examples/erc20/Cargo.toml b/examples/erc20/Cargo.toml index b55e876..4651df0 100644 --- a/examples/erc20/Cargo.toml +++ b/examples/erc20/Cargo.toml @@ -11,6 +11,14 @@ mini-alloc = { path = "../../mini-alloc" } [features] export-abi = ["stylus-sdk/export-abi"] +debug = ["stylus-sdk/debug"] + +[[bin]] +name = "erc20" +path = "src/main.rs" + +[lib] +crate-type = ["lib", "cdylib"] [profile.release] codegen-units = 1 diff --git a/examples/erc20/src/lib.rs b/examples/erc20/src/lib.rs new file mode 100644 index 0000000..34aaa7a --- /dev/null +++ b/examples/erc20/src/lib.rs @@ -0,0 +1,66 @@ +#![cfg_attr(not(feature = "export-abi"), no_main, no_std)] +extern crate alloc; + +use crate::erc20::{Erc20, Erc20Params}; +use alloc::{string::String, vec::Vec}; +use stylus_sdk::{alloy_primitives::U256, call, msg, prelude::*}; + +#[cfg(target_arch = "wasm32")] +#[global_allocator] +static ALLOC: mini_alloc::MiniAlloc = mini_alloc::MiniAlloc::INIT; + +mod erc20; + +struct WethParams; + +/// Immutable definitions +impl Erc20Params for WethParams { + const NAME: &'static str = "Wrapped Ether Example"; + const SYMBOL: &'static str = "WETH"; + const DECIMALS: u8 = 18; +} + +// The contract +sol_storage! { + #[entrypoint] // Makes Weth the entrypoint + struct Weth { + #[borrow] // Allows erc20 to access Weth's storage and make calls + Erc20 erc20; + } +} + +// Another contract we'd like to call +sol_interface! { + interface IMath { + function sum(uint256[] values) pure returns (string, uint256); + } +} + +#[external] +#[inherit(Erc20)] +impl Weth { + #[payable] + pub fn deposit(&mut self) -> Result<(), Vec> { + self.erc20.mint(msg::sender(), msg::value()); + Ok(()) + } + + pub fn withdraw(&mut self, amount: U256) -> Result<(), Vec> { + self.erc20.burn(msg::sender(), amount)?; + + // send the user their funds + call::transfer_eth(msg::sender(), amount) + } + + // sums numbers + pub fn sum(values: Vec) -> Result<(String, U256), Vec> { + Ok(("sum".into(), values.iter().sum())) + } + + // calls the sum() method from the interface + pub fn sum_with_helper(&self, helper: IMath, values: Vec) -> Result> { + let (text, sum) = helper.sum(self, values)?; + assert_eq!(&text, "sum"); + Ok(sum) + } +} diff --git a/examples/erc20/src/main.rs b/examples/erc20/src/main.rs index 34aaa7a..a909e93 100644 --- a/examples/erc20/src/main.rs +++ b/examples/erc20/src/main.rs @@ -1,66 +1,6 @@ -#![cfg_attr(not(feature = "export-abi"), no_main, no_std)] -extern crate alloc; +#![cfg_attr(not(feature = "export-abi"), no_main)] -use crate::erc20::{Erc20, Erc20Params}; -use alloc::{string::String, vec::Vec}; -use stylus_sdk::{alloy_primitives::U256, call, msg, prelude::*}; - -#[cfg(target_arch = "wasm32")] -#[global_allocator] -static ALLOC: mini_alloc::MiniAlloc = mini_alloc::MiniAlloc::INIT; - -mod erc20; - -struct WethParams; - -/// Immutable definitions -impl Erc20Params for WethParams { - const NAME: &'static str = "Wrapped Ether Example"; - const SYMBOL: &'static str = "WETH"; - const DECIMALS: u8 = 18; -} - -// The contract -sol_storage! { - #[entrypoint] // Makes Weth the entrypoint - struct Weth { - #[borrow] // Allows erc20 to access Weth's storage and make calls - Erc20 erc20; - } -} - -// Another contract we'd like to call -sol_interface! { - interface IMath { - function sum(uint256[] values) pure returns (string, uint256); - } -} - -#[external] -#[inherit(Erc20)] -impl Weth { - #[payable] - pub fn deposit(&mut self) -> Result<(), Vec> { - self.erc20.mint(msg::sender(), msg::value()); - Ok(()) - } - - pub fn withdraw(&mut self, amount: U256) -> Result<(), Vec> { - self.erc20.burn(msg::sender(), amount)?; - - // send the user their funds - call::transfer_eth(msg::sender(), amount) - } - - // sums numbers - pub fn sum(values: Vec) -> Result<(String, U256), Vec> { - Ok(("sum".into(), values.iter().sum())) - } - - // calls the sum() method from the interface - pub fn sum_with_helper(&self, helper: IMath, values: Vec) -> Result> { - let (text, sum) = helper.sum(self, values)?; - assert_eq!(&text, "sum"); - Ok(sum) - } +#[cfg(feature = "export-abi")] +fn main() { + erc20::main(); } diff --git a/stylus-sdk/src/abi/export.rs b/stylus-sdk/src/abi/export.rs index c81c6c5..4b12141 100644 --- a/stylus-sdk/src/abi/export.rs +++ b/stylus-sdk/src/abi/export.rs @@ -40,6 +40,7 @@ pub fn print_abi() { println!(" * For more information, please see [The Stylus SDK](https://github.com/OffchainLabs/stylus-sdk-rs)."); println!(" */"); println!(); + println!("pragma solidity ^0.8.23;"); print!("{}", AbiPrinter::(PhantomData)); }