From 499b54ef717f647b8939f6c4d9600e30ea5f6dbd Mon Sep 17 00:00:00 2001 From: _dssei_ Date: Mon, 8 Apr 2024 10:53:34 -0700 Subject: [PATCH] draft of evm to sei address --- contracts/sei-tester/src/contract.rs | 23 ++++++---- contracts/sei-tester/src/msg.rs | 3 ++ .../tests/sei_tester_integration_tests.rs | 42 +++++++++++++++++-- packages/sei-cosmwasm/src/lib.rs | 2 +- packages/sei-cosmwasm/src/querier.rs | 17 +++++++- packages/sei-cosmwasm/src/query.rs | 12 ++++++ packages/sei-integration-tests/src/module.rs | 20 ++++++++- 7 files changed, 104 insertions(+), 15 deletions(-) diff --git a/contracts/sei-tester/src/contract.rs b/contracts/sei-tester/src/contract.rs index df8cc5b..5c44f21 100644 --- a/contracts/sei-tester/src/contract.rs +++ b/contracts/sei-tester/src/contract.rs @@ -12,14 +12,7 @@ use crate::{ types::{OrderData, PositionEffect}, }; 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 -}; +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}; const PLACE_ORDER_REPLY_ID: u64 = 1; // version info for migration info @@ -439,6 +432,8 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult } QueryMsg::GetEvmAddressBySeiAddress { sei_address } => to_json_binary(&query_evm_address(deps, sei_address)?), + QueryMsg::GetSeiAddressByEvmAddress { evm_address } => + to_json_binary(&query_sei_address(deps, evm_address)?), } } @@ -563,3 +558,15 @@ pub fn query_evm_address( Ok(res) } + +pub fn query_sei_address( + deps: Deps, + sei_address: String, +) -> StdResult { + let valid_addr = deps.api.addr_validate(&sei_address)?; + let querier = SeiQuerier::new(&deps.querier); + let res = querier.get_sei_address(valid_addr.to_string())?; + + Ok(res) +} + diff --git a/contracts/sei-tester/src/msg.rs b/contracts/sei-tester/src/msg.rs index dcff532..f1c1ec9 100644 --- a/contracts/sei-tester/src/msg.rs +++ b/contracts/sei-tester/src/msg.rs @@ -60,4 +60,7 @@ pub enum QueryMsg { GetEvmAddressBySeiAddress { sei_address: String, }, + GetSeiAddressByEvmAddress { + evm_address: String, + }, } diff --git a/contracts/sei-tester/tests/sei_tester_integration_tests.rs b/contracts/sei-tester/tests/sei_tester_integration_tests.rs index 0f5225e..0f4e178 100644 --- a/contracts/sei-tester/tests/sei_tester_integration_tests.rs +++ b/contracts/sei-tester/tests/sei_tester_integration_tests.rs @@ -5,10 +5,10 @@ use cw_multi_test::{ StakeKeeper, WasmKeeper, }; -use sei_cosmwasm::{Cancellation, DenomOracleExchangeRatePair, DexPair, DexTwap, DexTwapsResponse, EpochResponse, EvmAddressResponse, ExchangeRatesResponse, GetOrderByIdResponse, GetOrdersResponse, OracleExchangeRate, OracleTwapsResponse, Order, OrderSimulationResponse, OrderStatus, OrderType, PositionDirection, SeiMsg, SeiQuery, SeiQueryWrapper, SeiRoute}; +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} + module::{SeiModule, EVM_ADDRESS, SEI_ADDRESS} }; use sei_tester::{ contract::{execute, instantiate, query}, @@ -933,7 +933,7 @@ fn test_evm_address_query() { let res: EvmAddressResponse = app .wrap() .query_wasm_smart(sei_tester_addr.clone(), &QueryMsg::GetEvmAddressBySeiAddress { - sei_address: sei_tester_addr.to_string(), + sei_address: SEI_ADDRESS.to_string(), }) .unwrap(); @@ -956,4 +956,40 @@ fn test_evm_address_query() { associated: false, }; assert_eq!(res, expected_res); +} + +#[test] +fn test_sei_address_query() { + let mut app = mock_app(init_default_balances, vec![]); + let sei_tester_addr = setup_test(&mut app); + + // // Test associated SEI address + let res: SeiAddressResponse = app + .wrap() + .query_wasm_smart(sei_tester_addr.clone(), &QueryMsg::GetSeiAddressByEvmAddress { + evm_address: "0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B".to_string(), + }) + .unwrap(); + + let expected_res = SeiAddressResponse { + sei_address: SEI_ADDRESS.to_string(), + associated: true, + }; + assert_eq!(res, expected_res); + + + // Test non-associated SEI address + let res: SeiAddressResponse = app + .wrap() + .query_wasm_smart(sei_tester_addr.clone(), &QueryMsg::GetSeiAddressByEvmAddress { + evm_address: "fake_address".to_string(), + }) + .unwrap(); + + let expected_res = SeiAddressResponse { + sei_address: String::new(), + associated: false, + }; + assert_eq!(res, expected_res); + } \ No newline at end of file diff --git a/packages/sei-cosmwasm/src/lib.rs b/packages/sei-cosmwasm/src/lib.rs index 56cb5e6..a75b1af 100644 --- a/packages/sei-cosmwasm/src/lib.rs +++ b/packages/sei-cosmwasm/src/lib.rs @@ -15,7 +15,7 @@ pub use query::{ DenomAuthorityMetadataResponse, DenomsFromCreatorResponse, DexTwapsResponse, EpochResponse, ExchangeRatesResponse, GetLatestPriceResponse, GetOrderByIdResponse, GetOrdersResponse, OracleTwapsResponse, OrderSimulationResponse, PriceResponse, SeiQuery, SeiQueryWrapper, - EvmAddressResponse + EvmAddressResponse, SeiAddressResponse }; pub use route::SeiRoute; pub use sei_types::{ diff --git a/packages/sei-cosmwasm/src/querier.rs b/packages/sei-cosmwasm/src/querier.rs index 65ce005..51d5ff7 100644 --- a/packages/sei-cosmwasm/src/querier.rs +++ b/packages/sei-cosmwasm/src/querier.rs @@ -1,7 +1,7 @@ use cosmwasm_std::{Addr, QuerierWrapper, StdResult, Uint128}; use cw20::{BalanceResponse, TokenInfoResponse}; -use crate::query::{DenomAuthorityMetadataResponse, DenomsFromCreatorResponse, DexTwapsResponse, EpochResponse, Erc20AllowanceResponse, Erc721ApprovedResponse, Erc721IsApprovedForAllResponse, Erc721NameSymbolResponse, Erc721OwnerResponse, Erc721UriResponse, ErcPayloadResponse, EvmAddressResponse, ExchangeRatesResponse, GetLatestPriceResponse, GetOrderByIdResponse, GetOrdersResponse, OracleTwapsResponse, OrderSimulationResponse, SeiQuery, SeiQueryWrapper, StaticCallResponse}; +use crate::query::{DenomAuthorityMetadataResponse, DenomsFromCreatorResponse, DexTwapsResponse, EpochResponse, Erc20AllowanceResponse, Erc721ApprovedResponse, Erc721IsApprovedForAllResponse, Erc721NameSymbolResponse, Erc721OwnerResponse, Erc721UriResponse, ErcPayloadResponse, EvmAddressResponse, SeiAddressResponse, ExchangeRatesResponse, GetLatestPriceResponse, GetOrderByIdResponse, GetOrdersResponse, OracleTwapsResponse, OrderSimulationResponse, SeiQuery, SeiQueryWrapper, StaticCallResponse}; use crate::route::SeiRoute; use crate::Order; @@ -462,4 +462,19 @@ impl<'a> SeiQuerier<'a> { self.querier.query(&request) } + + pub fn get_sei_address( + &self, + evm_address: String, + ) -> StdResult { + let request = SeiQueryWrapper { + route: SeiRoute::Evm, + query_data: SeiQuery::GetSeiAddress { + evm_address + }, + } + .into(); + + self.querier.query(&request) + } } diff --git a/packages/sei-cosmwasm/src/query.rs b/packages/sei-cosmwasm/src/query.rs index 41f9044..e5d9afd 100644 --- a/packages/sei-cosmwasm/src/query.rs +++ b/packages/sei-cosmwasm/src/query.rs @@ -128,6 +128,9 @@ pub enum SeiQuery { GetEvmAddress { sei_address: String, }, + GetSeiAddress { + evm_address: String, + }, } /// ExchangeRatesResponse is data format returned from OracleRequest::ExchangeRates query @@ -254,4 +257,13 @@ pub struct EvmAddressResponse { /// A boolean value indicating whether the EVM address is associated. pub associated: bool +} + +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] +pub struct SeiAddressResponse { + /// The SEI address associated to EVM address. Empty if the EVM address is not associated with any SEI address. + pub sei_address: String, + + /// A boolean value indicating whether the SEI address is associated to EVM address. + pub associated: bool } \ No newline at end of file diff --git a/packages/sei-integration-tests/src/module.rs b/packages/sei-integration-tests/src/module.rs index b8c468a..38bd141 100644 --- a/packages/sei-integration-tests/src/module.rs +++ b/packages/sei-integration-tests/src/module.rs @@ -2,7 +2,7 @@ use anyhow::Result as AnyResult; use cosmwasm_std::{Addr, Api, BankMsg, Binary, BlockInfo, Coin, CosmosMsg, CustomQuery, Decimal, Querier, Storage, Uint128, Uint64, to_json_binary, from_json}; use cw_multi_test::{AppResponse, BankSudo, CosmosRouter, Module, SudoMsg}; use schemars::JsonSchema; -use sei_cosmwasm::{Cancellation, DenomOracleExchangeRatePair, DexPair, DexTwap, DexTwapsResponse, Epoch, EpochResponse, EvmAddressResponse, ExchangeRatesResponse, GetOrderByIdResponse, GetOrdersResponse, OracleTwap, OracleTwapsResponse, Order, OrderResponse, OrderSimulationResponse, OrderStatus, PositionDirection, SeiMsg, SeiQuery, SeiQueryWrapper, SudoMsg as SeiSudoMsg}; +use sei_cosmwasm::{Cancellation, DenomOracleExchangeRatePair, DexPair, DexTwap, DexTwapsResponse, Epoch, EpochResponse, EvmAddressResponse, ExchangeRatesResponse, GetOrderByIdResponse, GetOrdersResponse, OracleTwap, OracleTwapsResponse, Order, OrderResponse, OrderSimulationResponse, OrderStatus, PositionDirection, SeiAddressResponse, SeiMsg, SeiQuery, SeiQueryWrapper, SudoMsg as SeiSudoMsg}; use serde::de::DeserializeOwned; use std::{ collections::HashMap, @@ -24,6 +24,7 @@ const GENESIS_EPOCH: Epoch = Epoch { }; pub const EVM_ADDRESS: &str = "0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B"; +pub const SEI_ADDRESS: &str = "sei1vzxkv3lxccnttr9rs0002s93sgw72h7ghukuhs"; impl SeiModule { pub fn new() -> Self { @@ -184,6 +185,9 @@ impl Module for SeiModule { SeiQuery::GetEvmAddress { sei_address } => { Ok(to_json_binary(&get_evm_address(sei_address))?) } + SeiQuery::GetSeiAddress { evm_address } => { + Ok(to_json_binary(&get_sei_address(evm_address))?) + } // TODO: Implement get denom authority metadata in integration tests SeiQuery::DenomAuthorityMetadata { .. } => { panic!("Denom Authority Metadata not implemented") @@ -644,7 +648,7 @@ fn get_epoch(epoch: Epoch) -> EpochResponse { fn get_evm_address(sei_address: String) -> EvmAddressResponse { let (evm_address, associated) = match sei_address.as_str() { - "contract0" => (EVM_ADDRESS.to_string(), true), + SEI_ADDRESS => (EVM_ADDRESS.to_string(), true), _ => (String::new(), false), // default case }; @@ -654,6 +658,18 @@ fn get_evm_address(sei_address: String) -> EvmAddressResponse { } } +fn get_sei_address(evm_address: String) -> SeiAddressResponse { + let (sei_address, associated) = match evm_address.as_str() { + EVM_ADDRESS => (SEI_ADDRESS.to_string(), true), + _ => (String::new(), false), // default case + }; + + SeiAddressResponse { + sei_address, + associated, + } +} + // Query: GetEpoch() fn query_get_epoch_helper(epoch: Epoch) -> AnyResult { return Ok(to_json_binary(&get_epoch(epoch))?);