Skip to content

Commit

Permalink
Few corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
1010adigupta committed Dec 9, 2023
1 parent 19deb5e commit caf78f7
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
16 changes: 16 additions & 0 deletions weth/Cargo.toml
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]
65 changes: 65 additions & 0 deletions weth/src/lib.rs
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)
}
}

0 comments on commit caf78f7

Please sign in to comment.