-
Notifications
You must be signed in to change notification settings - Fork 0
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
19deb5e
commit caf78f7
Showing
2 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
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,16 @@ | ||
[package] | ||
name = "stylus-hello-world-minimal" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
stylus-sdk = "0.4.1" | ||
wee_alloc = "0.4.5" | ||
|
||
[features] | ||
export-abi = ["stylus-sdk/export-abi"] | ||
|
||
[lib] | ||
crate-type = ["lib", "cdylib"] | ||
|
||
[workspace] |
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,65 @@ | ||
#![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::*}; | ||
|
||
#[global_allocator] | ||
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::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) | ||
} | ||
} |