Skip to content

Commit

Permalink
chore(erc20): adapt to new constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
clearloop committed Sep 26, 2024
1 parent cc1e147 commit 9ac36b9
Showing 1 changed file with 35 additions and 25 deletions.
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(())
}

0 comments on commit 9ac36b9

Please sign in to comment.