From 119456207b2d100dfe7726fe1831a5b68ada7ead Mon Sep 17 00:00:00 2001 From: _dssei_ Date: Mon, 8 Apr 2024 11:59:52 -0700 Subject: [PATCH] working draft --- contracts/sei-tester/Cargo.toml | 1 + contracts/sei-tester/src/contract.rs | 8 ++++-- .../tests/sei_tester_integration_tests.rs | 26 ++++++++++++++----- packages/sei-cosmwasm/src/query.rs | 2 +- 4 files changed, 27 insertions(+), 10 deletions(-) diff --git a/contracts/sei-tester/Cargo.toml b/contracts/sei-tester/Cargo.toml index 1b901f9..e5ddbd1 100644 --- a/contracts/sei-tester/Cargo.toml +++ b/contracts/sei-tester/Cargo.toml @@ -50,6 +50,7 @@ serde_json = { version = "1.0", default-features = false, features = ["alloc"] } base64 = { version = "0.13.0" } thiserror = { version = "1.0.31" } protobuf = { version = "3.2.0", features = ["with-bytes"] } +ethaddr = "0.2.2" [dev-dependencies] cosmwasm-schema = "1.0.0" diff --git a/contracts/sei-tester/src/contract.rs b/contracts/sei-tester/src/contract.rs index 5c44f21..61d9f8a 100644 --- a/contracts/sei-tester/src/contract.rs +++ b/contracts/sei-tester/src/contract.rs @@ -13,6 +13,7 @@ use crate::{ }; use protobuf::Message; use sei_cosmwasm::{BulkOrderPlacementsResponse, Cancellation, DenomAuthorityMetadataResponse, DenomUnit, DenomsFromCreatorResponse, DepositInfo, DexTwapsResponse, EpochResponse, ExchangeRatesResponse, GetLatestPriceResponse, GetOrderByIdResponse, GetOrdersResponse, Metadata, MsgPlaceOrdersResponse, OracleTwapsResponse, Order, OrderSimulationResponse, OrderType, PositionDirection, SeiMsg, SeiQuerier, SeiQueryWrapper, SettlementEntry, SudoMsg, EvmAddressResponse, SeiAddressResponse}; +use ethaddr::Address; const PLACE_ORDER_REPLY_ID: u64 = 1; // version info for migration info @@ -561,9 +562,12 @@ pub fn query_evm_address( pub fn query_sei_address( deps: Deps, - sei_address: String, + evm_address: String, ) -> StdResult { - let valid_addr = deps.api.addr_validate(&sei_address)?; + let valid_addr = match Address::from_str_checksum(&*evm_address) { + Ok(addr) => addr, + Err(_) => return Err(StdError::generic_err("Failed to parse Ethereum address")), + }; let querier = SeiQuerier::new(&deps.querier); let res = querier.get_sei_address(valid_addr.to_string())?; diff --git a/contracts/sei-tester/tests/sei_tester_integration_tests.rs b/contracts/sei-tester/tests/sei_tester_integration_tests.rs index 0f4e178..afe9c18 100644 --- a/contracts/sei-tester/tests/sei_tester_integration_tests.rs +++ b/contracts/sei-tester/tests/sei_tester_integration_tests.rs @@ -8,7 +8,7 @@ use cw_multi_test::{ use sei_cosmwasm::{Cancellation, DenomOracleExchangeRatePair, DexPair, DexTwap, DexTwapsResponse, EpochResponse, EvmAddressResponse, ExchangeRatesResponse, GetOrderByIdResponse, GetOrdersResponse, OracleExchangeRate, OracleTwapsResponse, Order, OrderSimulationResponse, OrderStatus, OrderType, PositionDirection, SeiAddressResponse, SeiMsg, SeiQuery, SeiQueryWrapper, SeiRoute}; use sei_integration_tests::{ helper::{get_balance, mock_app}, - module::{SeiModule, EVM_ADDRESS, SEI_ADDRESS} + module::{SeiModule, EVM_ADDRESS, SEI_ADDRESS}, }; use sei_tester::{ contract::{execute, instantiate, query}, @@ -145,7 +145,7 @@ fn test_tokenfactory_integration_foundation() { }, })], ) - .unwrap(); + .unwrap(); let res: BalanceResponse = get_balance(&app, ADMIN.to_string(), out.to_string()); assert_eq!(res.amount.amount, Uint128::new(1)); @@ -178,7 +178,7 @@ fn test_tokenfactory_integration_foundation() { }, })], ) - .unwrap(); + .unwrap(); let res: BalanceResponse = get_balance(&app, ADMIN.to_string(), out.to_string()); assert_eq!(res.amount.amount, Uint128::new(0)); @@ -564,7 +564,7 @@ fn test_dex_module_query_order_simulation() { contract_address: Addr::unchecked(&sei_tester_addr.to_string()), })], ) - .unwrap(); + .unwrap(); // Test all of sim order can be fulfilled let res: OrderSimulationResponse = app @@ -844,7 +844,7 @@ fn test_dex_module_query_dex_twap() { contract_address: Addr::unchecked(&sei_tester_addr.to_string()), })], ) - .unwrap(); + .unwrap(); app.set_block(BlockInfo { height: 2, @@ -888,7 +888,7 @@ fn test_dex_module_query_dex_twap() { contract_address: Addr::unchecked(&sei_tester_addr.to_string()), })], ) - .unwrap(); + .unwrap(); app.set_block(BlockInfo { height: 3, @@ -982,7 +982,7 @@ fn test_sei_address_query() { let res: SeiAddressResponse = app .wrap() .query_wasm_smart(sei_tester_addr.clone(), &QueryMsg::GetSeiAddressByEvmAddress { - evm_address: "fake_address".to_string(), + evm_address: "0x999999cf1046e68e36E1aA2E0E07105eDDD1f08E".to_string(), }) .unwrap(); @@ -992,4 +992,16 @@ fn test_sei_address_query() { }; assert_eq!(res, expected_res); + // Test error case when EVM address is invalid + let res: Result = app + .wrap() + .query_wasm_smart(sei_tester_addr.clone(), &QueryMsg::GetSeiAddressByEvmAddress { + evm_address: "fakeaddress".to_string(), + }); + + assert!(res.is_err()); + + let err = res.expect_err("Expected an error because the EVM address is invalid"); + assert_eq!(err.to_string(), + "Generic error: Querier contract error: Generic error: Failed to parse Ethereum address"); } \ No newline at end of file diff --git a/packages/sei-cosmwasm/src/query.rs b/packages/sei-cosmwasm/src/query.rs index e5d9afd..9ab013e 100644 --- a/packages/sei-cosmwasm/src/query.rs +++ b/packages/sei-cosmwasm/src/query.rs @@ -266,4 +266,4 @@ pub struct SeiAddressResponse { /// A boolean value indicating whether the SEI address is associated to EVM address. pub associated: bool -} \ No newline at end of file +}