Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion evm/abi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Solidity ABI implementation
//!
//! https://docs.soliditylang.org/en/latest/abi-spec.html#json
//! <https://docs.soliditylang.org/en/latest/abi-spec.html#json>
#![deny(missing_docs)]
#![cfg_attr(not(feature = "std"), no_std)]

Expand Down
60 changes: 52 additions & 8 deletions examples/erc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,23 @@ extern crate zink;

use zink::{
primitives::{Address, String32, U256},
DoubleKeyMapping, Mapping, Storage,
DoubleKeyMapping, Event, Mapping, Storage,
};
use crate::zink::Asm;

#[derive(Event)]
pub enum DebugEvent {
Caller(Address),
FromAddr(Address),
Balance(U256),
Insufficient(U256, U256),
TestLog(U256),
}

#[zink::external]
pub fn test_log(value: U256) {
DebugEvent::TestLog(value).emit();
}

#[zink::storage(String32)]
pub struct Name;
Expand Down Expand Up @@ -40,6 +55,8 @@ pub fn decimals() -> u32 {
#[zink::external]
pub fn transfer(to: Address, value: U256) -> bool {
let owner = Address::caller();
DebugEvent::Caller(owner).emit();
DebugEvent::TestLog(value).emit();
_transfer(owner, to, value);
true
}
Expand Down Expand Up @@ -78,18 +95,22 @@ fn _update(from: Address, to: Address, value: U256) {
TotalSupply::set(TotalSupply::get().add(value));
} else {
let from_balance = Balances::get(from);

DebugEvent::FromAddr(from).emit();
DebugEvent::Balance(from_balance).emit();
if from_balance.lt(value) {
DebugEvent::Insufficient(from_balance, value).emit();
zink::revert!("Insufficient balance");
}

Balances::set(from, from_balance.sub(value));
DebugEvent::TestLog(from_balance).emit();
}

if to.eq(Address::empty()) {
TotalSupply::set(TotalSupply::get().sub(value));
} else {
TotalSupply::set(TotalSupply::get().add(value));
let to_balance = Balances::get(to);
Balances::set(to, to_balance.add(value));
}
}

Expand Down Expand Up @@ -153,7 +174,7 @@ fn deploy() -> anyhow::Result<()> {
let name = "The Zink Language";
let symbol = "zink";
let value = 42;
//let half_value = 21;
let half_value = 21;

// 1. deploy
let info = evm.deploy(
Expand Down Expand Up @@ -241,6 +262,13 @@ fn deploy() -> anyhow::Result<()> {
assert_eq!(info.ret, allowance);
}

// Test log emission
let info = contract.execute(&[
b"test_log(uint256)".to_vec(),
U256::from(42).0.bytes32().to_vec(),
])?;
println!("Test log result: {info:?}");

// 4. check transfer
{
// 4.1. verify balance of the caller
Expand All @@ -252,18 +280,34 @@ fn deploy() -> anyhow::Result<()> {
.call(address)?;
assert_eq!(info.ret, value.to_bytes32(), "{info:?}");

// Debug: Check balance immediately before transfer
let balance_before = evm.storage(address, Balances::storage_key(Address::from(caller)))?;
println!("Balance in storage before transfer: {:?}", balance_before);
let info = evm
.calldata(&contract.encode(&[
b"balances(address)".to_vec(),
evm.caller.to_bytes32().to_vec(),
])?)
.call(address)?;
println!("Balance via balances() before transfer: {:?}", info.ret);

// TODO: see br_balance.rs (#287)
// 4.2. check transfer
/* evm = evm.commit(false);
evm = evm.commit(false);
println!(
"EVM storage after commit(false): {:?}",
evm.storage(address, Balances::storage_key(Address::from(caller)))?
);
let info = evm
.calldata(&contract.encode(&[
b"transfer(address,uint256)".to_vec(),
spender.to_bytes32().to_vec(),
half_value.to_bytes32().to_vec(),
])?)
.call(address)?;
println!("{info:?}");
assert_eq!(info.ret, true.to_bytes32(), "{info:?}"); */
println!("Transfer result: {info:?}");
println!("Logs: {:?}", info.logs);
assert_eq!(info.ret, true.to_bytes32(), "{info:?}");
}

Ok(())
Expand Down
4 changes: 2 additions & 2 deletions zink/src/primitives/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::{

/// Account address
#[repr(C)]
#[derive(Clone, Copy)]
pub struct Address(Bytes20);
#[derive(Clone, Copy, Debug)]
pub struct Address(pub Bytes20);

impl Address {
/// Returns empty address
Expand Down
2 changes: 1 addition & 1 deletion zink/src/primitives/u256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use core::ops::Sub;
/// Account address
#[repr(C)]
#[derive(Clone, Copy, PartialEq, PartialOrd, Debug)]
pub struct U256(Bytes32);
pub struct U256(pub Bytes32);

impl U256 {
/// Returns empty value
Expand Down
Loading