-
Notifications
You must be signed in to change notification settings - Fork 78
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
1 parent
b1f3971
commit 16c61ea
Showing
12 changed files
with
1,542 additions
and
244 deletions.
There are no files selected for viewing
File renamed without changes.
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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,59 @@ | ||
// Only run this as a WASM if the export-abi feature is not set. | ||
#![cfg_attr(not(any(feature = "export-abi", test)), no_main)] | ||
extern crate alloc; | ||
|
||
// Modules and imports | ||
mod erc20; | ||
|
||
use alloy_primitives::{Address, U256}; | ||
use stylus_sdk::{ | ||
msg, | ||
prelude::* | ||
}; | ||
use crate::erc20::{Erc20, Erc20Params, Erc20Error}; | ||
|
||
/// Initializes a custom, global allocator for Rust programs compiled to WASM. | ||
#[global_allocator] | ||
static ALLOC: mini_alloc::MiniAlloc = mini_alloc::MiniAlloc::INIT; | ||
|
||
/// Immutable definitions | ||
struct StylusTestTokenParams; | ||
impl Erc20Params for StylusTestTokenParams { | ||
const NAME: &'static str = "StylusTestToken"; | ||
const SYMBOL: &'static str = "STTK"; | ||
const DECIMALS: u8 = 18; | ||
} | ||
|
||
// Define the entrypoint as a Solidity storage object. The sol_storage! macro | ||
// will generate Rust-equivalent structs with all fields mapped to Solidity-equivalent | ||
// storage slots and types. | ||
sol_storage! { | ||
#[entrypoint] | ||
struct StylusTestToken { | ||
// Allows erc20 to access StylusTestToken's storage and make calls | ||
#[borrow] | ||
Erc20<StylusTestTokenParams> erc20; | ||
} | ||
} | ||
|
||
#[external] | ||
#[inherit(Erc20<StylusTestTokenParams>)] | ||
impl StylusTestToken { | ||
/// Mints tokens | ||
pub fn mint(&mut self, value: U256) -> Result<(), Erc20Error> { | ||
self.erc20.mint(msg::sender(), value)?; | ||
Ok(()) | ||
} | ||
|
||
/// Mints tokens to another address | ||
pub fn mint_to(&mut self, to: Address, value: U256) -> Result<(), Erc20Error> { | ||
self.erc20.mint(to, value)?; | ||
Ok(()) | ||
} | ||
|
||
/// Burns tokens | ||
pub fn burn(&mut self, value: U256) -> Result<(), Erc20Error> { | ||
self.erc20.burn(msg::sender(), value)?; | ||
Ok(()) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,65 +1,4 @@ | ||
#![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) { | ||
self.erc20.mint(msg::sender(), msg::value()); | ||
} | ||
|
||
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>) -> (String, U256) { | ||
("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::print_abi("MIT-OR-APACHE-2.0", "pragma solidity ^0.8.23;"); | ||
} |
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,7 @@ | ||
[build] | ||
target = "wasm32-unknown-unknown" | ||
|
||
[target.wasm32-unknown-unknown] | ||
rustflags = [ | ||
"-C", "link-arg=-zstack-size=32768", | ||
] |
Oops, something went wrong.