Skip to content

Commit

Permalink
draft of evm to sei address
Browse files Browse the repository at this point in the history
  • Loading branch information
dssei committed Apr 8, 2024
1 parent 0656824 commit 499b54e
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 15 deletions.
23 changes: 15 additions & 8 deletions contracts/sei-tester/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -439,6 +432,8 @@ pub fn query(deps: Deps<SeiQueryWrapper>, _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)?),
}
}

Expand Down Expand Up @@ -563,3 +558,15 @@ pub fn query_evm_address(

Ok(res)
}

pub fn query_sei_address(
deps: Deps<SeiQueryWrapper>,
sei_address: String,
) -> StdResult<SeiAddressResponse> {
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)
}

3 changes: 3 additions & 0 deletions contracts/sei-tester/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,7 @@ pub enum QueryMsg {
GetEvmAddressBySeiAddress {
sei_address: String,
},
GetSeiAddressByEvmAddress {
evm_address: String,
},
}
42 changes: 39 additions & 3 deletions contracts/sei-tester/tests/sei_tester_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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();

Expand All @@ -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);

}
2 changes: 1 addition & 1 deletion packages/sei-cosmwasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down
17 changes: 16 additions & 1 deletion packages/sei-cosmwasm/src/querier.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -462,4 +462,19 @@ impl<'a> SeiQuerier<'a> {

self.querier.query(&request)
}

pub fn get_sei_address(
&self,
evm_address: String,
) -> StdResult<SeiAddressResponse> {
let request = SeiQueryWrapper {
route: SeiRoute::Evm,
query_data: SeiQuery::GetSeiAddress {
evm_address
},
}
.into();

self.querier.query(&request)
}
}
12 changes: 12 additions & 0 deletions packages/sei-cosmwasm/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ pub enum SeiQuery {
GetEvmAddress {
sei_address: String,
},
GetSeiAddress {
evm_address: String,
},
}

/// ExchangeRatesResponse is data format returned from OracleRequest::ExchangeRates query
Expand Down Expand Up @@ -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
}
20 changes: 18 additions & 2 deletions packages/sei-integration-tests/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
};

Expand All @@ -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<Binary> {
return Ok(to_json_binary(&get_epoch(epoch))?);
Expand Down

0 comments on commit 499b54e

Please sign in to comment.