Skip to content

Commit

Permalink
Add endpoints for the erc20 name and symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
virgil-serbanuta committed Jan 30, 2025
1 parent 089a630 commit 49f410a
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pykwasm/src/pykwasm/call.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
62 changes: 62 additions & 0 deletions tests/ulm/erc20/erc20_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 "

Expand Down Expand Up @@ -551,6 +611,8 @@ function test_transfer_from {
}


test_name
test_symbol
test_decimals
test_mint
test_transfer
Expand Down
8 changes: 8 additions & 0 deletions tests/ulm/erc20/rust/src/erc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
30 changes: 30 additions & 0 deletions tests/ulm/erc20/rust/src/erc20_dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ fn dispatch(api: Rc<RefCell<dyn ulm::Ulm>>, 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)") {
Expand Down Expand Up @@ -65,6 +69,32 @@ fn initCaller(api: Rc<RefCell<dyn ulm::Ulm>>, arguments: Bytes) {
ulm::set_output(&mut *api.borrow_mut(), &encoder.encode());
}

#[allow(non_snake_case)]
fn nameCaller(api: Rc<RefCell<dyn ulm::Ulm>>, 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<RefCell<dyn ulm::Ulm>>, 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<RefCell<dyn ulm::Ulm>>, arguments: Bytes) {
let decoder: Decoder<()> = Decoder::from_buffer(arguments);
Expand Down
18 changes: 18 additions & 0 deletions tests/ulm/erc20/rust/src/erc20_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 49f410a

Please sign in to comment.