Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ERC20 Example + Eliminate Solc Warnings on Exports #101

Open
wants to merge 6 commits into
base: stylus
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 19 additions & 3 deletions examples/erc20/.cargo/config
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
[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",
]

[profile.release]
codegen-units = 1
strip = true
lto = true
panic = "abort"
opt-level = "s"
61 changes: 54 additions & 7 deletions examples/erc20/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions examples/erc20/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ mini-alloc = { path = "../../mini-alloc" }

[features]
export-abi = ["stylus-sdk/export-abi"]
debug = ["stylus-sdk/debug"]

[profile.release]
codegen-units = 1
strip = true
lto = true
panic = "abort"
opt-level = "s"
[[bin]]
name = "erc20"
path = "src/main.rs"

[lib]
crate-type = ["lib", "cdylib"]

[workspace]
[workspace]
66 changes: 66 additions & 0 deletions examples/erc20/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<WethParams> erc20;
}
}

// Another contract we'd like to call
sol_interface! {
interface IMath {
function sum(uint256[] values) pure returns (string, uint256);
}
}

#[external]
#[inherit(Erc20<WethParams>)]
impl Weth {
#[payable]
pub fn deposit(&mut self) -> Result<(), Vec<u8>> {
self.erc20.mint(msg::sender(), msg::value());
Ok(())
}

pub fn withdraw(&mut self, amount: U256) -> Result<(), Vec<u8>> {
self.erc20.burn(msg::sender(), amount)?;

// send the user their funds
call::transfer_eth(msg::sender(), amount)
}

// sums numbers
pub fn sum(values: Vec<U256>) -> Result<(String, U256), Vec<u8>> {
Ok(("sum".into(), values.iter().sum()))
}

// calls the sum() method from the interface
pub fn sum_with_helper(&self, helper: IMath, values: Vec<U256>) -> Result<U256, Vec<u8>> {
let (text, sum) = helper.sum(self, values)?;
assert_eq!(&text, "sum");
Ok(sum)
}
}
68 changes: 4 additions & 64 deletions examples/erc20/src/main.rs
Original file line number Diff line number Diff line change
@@ -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<WethParams> erc20;
}
}

// Another contract we'd like to call
sol_interface! {
interface IMath {
function sum(uint256[] values) pure returns (string, uint256);
}
}

#[external]
#[inherit(Erc20<WethParams>)]
impl Weth {
#[payable]
pub fn deposit(&mut self) -> Result<(), Vec<u8>> {
self.erc20.mint(msg::sender(), msg::value());
Ok(())
}

pub fn withdraw(&mut self, amount: U256) -> Result<(), Vec<u8>> {
self.erc20.burn(msg::sender(), amount)?;

// send the user their funds
call::transfer_eth(msg::sender(), amount)
}

// sums numbers
pub fn sum(values: Vec<U256>) -> Result<(String, U256), Vec<u8>> {
Ok(("sum".into(), values.iter().sum()))
}

// calls the sum() method from the interface
pub fn sum_with_helper(&self, helper: IMath, values: Vec<U256>) -> Result<U256, Vec<u8>> {
let (text, sum) = helper.sum(self, values)?;
assert_eq!(&text, "sum");
Ok(sum)
}
#[cfg(feature = "export-abi")]
fn main() {
erc20::main();
}
2 changes: 2 additions & 0 deletions stylus-sdk/src/abi/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ pub fn print_abi<T: GenerateAbi>() {
println!(" * For more information, please see [The Stylus SDK](https://github.com/OffchainLabs/stylus-sdk-rs).");
println!(" */");
println!();
println!("// SPDX-License-Identifier: TODO");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should either parse the toml for this or drop the line imo. Otherwise you're letting the consumer of the crate decide the license of the abi in a way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, will drop this line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do some investigation into parsing the toml to get rid of the pesky warning and use the right license here

rauljordan marked this conversation as resolved.
Show resolved Hide resolved
println!("pragma solidity ^0.8.23;");
print!("{}", AbiPrinter::<T>(PhantomData));
}

Expand Down
Loading