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

feat(example): basic implementation of ERC20 #228

Merged
merged 22 commits into from
Nov 6, 2024
Merged
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
chore(erc20): adapt to new constructor
  • Loading branch information
clearloop committed Sep 26, 2024
commit 9ac36b9dc982faf6845d14df3548cca6dc3f11d7
60 changes: 35 additions & 25 deletions examples/erc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,16 @@ extern crate zink;
use zink::Storage;

#[zink::storage(u32)]
pub struct Name;
struct Name;

#[zink::storage(u32)]
pub struct Symbol;
struct Symbol;

#[zink::storage(u32)]
pub struct TotalSupply;
struct TotalSupply;

// /// Set value to the storage.
// #[zink::constructor]
// pub fn constructor(name: u32, symbol: u32) {
// Name::set(name);
// Symbol::set(symbol);
// }
// #[zink::storage(i32 => i32)]
// struct Balances;

/// Get value from the storage.
#[zink::external]
Expand All @@ -35,54 +31,68 @@ pub fn decimals() -> u32 {
2
}

#[zink::external]
pub fn name() -> u32 {
Name::get()
}

#[zink::external]
pub fn symbol() -> u32 {
Symbol::get()
}

#[zink::external]
pub fn name() -> u32 {
Name::get()
pub fn total_supply() -> u32 {
TotalSupply::get()
}

#[cfg(not(target_arch = "wasm32"))]
fn main() {}

#[ignore]
#[test]
fn deploy() -> anyhow::Result<()> {
use zint::{Bytes32, Contract, EVM};

let contract = Contract::search("erc20")?.compile()?;
let mut contract = Contract::search("erc20")?.compile()?;

let mut evm = EVM::default();
let mut calldata: Vec<u8> = Default::default();
calldata.extend_from_slice(&42.to_bytes32());
calldata.extend_from_slice(&42.to_bytes32());

// 1. deploy
let info = evm.deploy(&contract.bytecode()?)?;
let info = evm.deploy(
&contract
.construct(
[
(vec![0].try_into()?, vec![42].try_into()?),
(vec![1].try_into()?, vec![42].try_into()?),
(vec![2].try_into()?, vec![42].try_into()?),
]
.into_iter()
.collect(),
)?
.bytecode()?,
)?;
let address = info.address;

// 2. init state
evm.calldata(&contract.encode(&[
b"init(uint32,uint32)".to_vec(),
16u64.to_bytes32().to_vec(),
42u64.to_bytes32().to_vec(),
])?)
.call(address)?;

// 3. get name
// 2. get name
let info = evm
.calldata(&contract.encode(&[b"name()".to_vec()])?)
.call(address)?;
assert_eq!(info.ret, 16u64.to_bytes32());
assert_eq!(info.ret, 42u64.to_bytes32());

// 4. get symbol
// 3. get symbol
let info = evm
.calldata(&contract.encode(&[b"symbol()".to_vec()])?)
.call(address)?;
assert_eq!(info.ret, 42u64.to_bytes32());

// 3. get symbol
let info = evm
.calldata(&contract.encode(&[b"total_supply()".to_vec()])?)
.call(address)?;
assert_eq!(info.ret, 42u64.to_bytes32());

Ok(())
}
Loading