From 49f410a978c86aeb04ccfb31f63950db29a4e5db Mon Sep 17 00:00:00 2001 From: Virgil Date: Tue, 28 Jan 2025 19:05:50 +0200 Subject: [PATCH] Add endpoints for the erc20 name and symbol --- pykwasm/src/pykwasm/call.py | 2 + tests/ulm/erc20/erc20_test.sh | 62 ++++++++++++++++++++ tests/ulm/erc20/rust/src/erc20.rs | 8 +++ tests/ulm/erc20/rust/src/erc20_dispatcher.rs | 30 ++++++++++ tests/ulm/erc20/rust/src/erc20_tests.rs | 18 ++++++ 5 files changed, 120 insertions(+) diff --git a/pykwasm/src/pykwasm/call.py b/pykwasm/src/pykwasm/call.py index c9f5cc357..6638d181b 100644 --- a/pykwasm/src/pykwasm/call.py +++ b/pykwasm/src/pykwasm/call.py @@ -11,6 +11,8 @@ ABI_MAP = { 'erc20': [ {'type': 'function', 'name': 'decimals', 'inputs': [], 'outputs': ['uint8'], 'stateMutability': 'view'}, + {'type': 'function', 'name': 'name', 'inputs': [], 'outputs': ['string'], 'stateMutability': 'view'}, + {'type': 'function', 'name': 'symbol', 'inputs': [], 'outputs': ['string'], 'stateMutability': 'view'}, {'type': 'function', 'name': 'totalSupply', 'inputs': [], 'outputs': ['uint256'], 'stateMutability': 'view'}, { 'type': 'function', diff --git a/tests/ulm/erc20/erc20_test.sh b/tests/ulm/erc20/erc20_test.sh index 9781127e0..7ea97cf8e 100755 --- a/tests/ulm/erc20/erc20_test.sh +++ b/tests/ulm/erc20/erc20_test.sh @@ -37,6 +37,18 @@ function erc20_deploy { deploy build/erc20/erc20.bin http://localhost:8545 /dev/stdin <<< $key } +function erc20_name { + local key=$1 + local contract=$2 + call http://localhost:8545 erc20 $contract /dev/stdin 0 name <<< $key +} + +function erc20_symbol { + local key=$1 + local contract=$2 + call http://localhost:8545 erc20 $contract /dev/stdin 0 symbol <<< $key +} + function erc20_decimals { local key=$1 local contract=$2 @@ -97,6 +109,54 @@ function erc20_transfer_from { call http://localhost:8545 erc20 $contract /dev/stdin 0 transferFrom $from_account $to_account $amount <<< $key } +function test_name { + echo -n "Name test " + + # generate some accounts + account1=($(mkacct)) + a1=${account1[0]} + k1=${account1[1]} + echo -n "." + + # fund accounts + fund /dev/stdin <<< $k1 + echo -n "." + + # deploy contract + contract=$(erc20_deploy $k1) + echo -n "." + + name=$(erc20_name $k1 $contract) + echo -n "." + assert_eq "Doge Coin" "$name" "Name" + + echo -e " ${GREEN}passed${NC}" +} + +function test_symbol { + echo -n "Symbol test " + + # generate some accounts + account1=($(mkacct)) + a1=${account1[0]} + k1=${account1[1]} + echo -n "." + + # fund accounts + fund /dev/stdin <<< $k1 + echo -n "." + + # deploy contract + contract=$(erc20_deploy $k1) + echo -n "." + + symbol=$(erc20_symbol $k1 $contract) + echo -n "." + assert_eq "DOGE" "$symbol" "Symbol" + + echo -e " ${GREEN}passed${NC}" +} + function test_decimals { echo -n "Decimals test " @@ -551,6 +611,8 @@ function test_transfer_from { } +test_name +test_symbol test_decimals test_mint test_transfer diff --git a/tests/ulm/erc20/rust/src/erc20.rs b/tests/ulm/erc20/rust/src/erc20.rs index de410c795..84493466b 100644 --- a/tests/ulm/erc20/rust/src/erc20.rs +++ b/tests/ulm/erc20/rust/src/erc20.rs @@ -66,6 +66,14 @@ impl Erc20 { pub fn init(&self) {} + pub fn name(&self) -> &str { + "Doge Coin" + } + + pub fn symbol(&self) -> &str { + "DOGE" + } + pub fn decimals(&self) -> u8 { 18 } diff --git a/tests/ulm/erc20/rust/src/erc20_dispatcher.rs b/tests/ulm/erc20/rust/src/erc20_dispatcher.rs index e99e50762..26b215ea4 100644 --- a/tests/ulm/erc20/rust/src/erc20_dispatcher.rs +++ b/tests/ulm/erc20/rust/src/erc20_dispatcher.rs @@ -33,6 +33,10 @@ fn dispatch(api: Rc>, init: bool) { let signature = buffer; if same_signature(&*api.borrow(), &signature, "decimals()") { decimalsCaller(api, arguments); + } else if same_signature(&*api.borrow(), &signature, "name()") { + nameCaller(api, arguments); + } else if same_signature(&*api.borrow(), &signature, "symbol()") { + symbolCaller(api, arguments); } else if same_signature(&*api.borrow(), &signature, "totalSupply()") { totalSupplyCaller(api, arguments); } else if same_signature(&*api.borrow(), &signature, "balanceOf(address)") { @@ -65,6 +69,32 @@ fn initCaller(api: Rc>, arguments: Bytes) { ulm::set_output(&mut *api.borrow_mut(), &encoder.encode()); } +#[allow(non_snake_case)] +fn nameCaller(api: Rc>, arguments: Bytes) { + let decoder: Decoder<()> = Decoder::from_buffer(arguments); + decoder.check_done(); + + let contract = Erc20::new(api.clone()); + let value = contract.name(); + + let mut encoder = Encoder::new(); + encoder.add(&value.to_string()); + ulm::set_output(&mut *api.borrow_mut(), &encoder.encode()); +} + +#[allow(non_snake_case)] +fn symbolCaller(api: Rc>, arguments: Bytes) { + let decoder: Decoder<()> = Decoder::from_buffer(arguments); + decoder.check_done(); + + let contract = Erc20::new(api.clone()); + let value = contract.symbol(); + + let mut encoder = Encoder::new(); + encoder.add(&value.to_string()); + ulm::set_output(&mut *api.borrow_mut(), &encoder.encode()); +} + #[allow(non_snake_case)] fn decimalsCaller(api: Rc>, arguments: Bytes) { let decoder: Decoder<()> = Decoder::from_buffer(arguments); diff --git a/tests/ulm/erc20/rust/src/erc20_tests.rs b/tests/ulm/erc20/rust/src/erc20_tests.rs index 936debfad..f58f93f77 100644 --- a/tests/ulm/erc20/rust/src/erc20_tests.rs +++ b/tests/ulm/erc20/rust/src/erc20_tests.rs @@ -22,6 +22,24 @@ mod erc20_tests { assert_eq!(18, erc20.decimals()); } + #[test] + fn name_test() { + let api = ulm::mock::UlmMock::new(); + + let erc20 = Erc20::new(api); + + assert_eq!("Doge Coin", erc20.name()); + } + + #[test] + fn symbol_test() { + let api = ulm::mock::UlmMock::new(); + + let erc20 = Erc20::new(api); + + assert_eq!("DOGE", erc20.symbol()); + } + #[test] fn mint_test() { let api = ulm::mock::UlmMock::new();