Skip to content

Commit

Permalink
Add EVM query/msg types
Browse files Browse the repository at this point in the history
  • Loading branch information
codchen committed Feb 28, 2024
1 parent 6d20612 commit b0996e9
Show file tree
Hide file tree
Showing 5 changed files with 303 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/sei-cosmwasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ schemars = "0.8.3"
serde = { version = "1.0.127", default-features = false, features = ["derive"] }
serde_repr = "0.1.8"
protobuf = { version = "=3.2.0", features = ["with-bytes"] }
cw20 = "1.1.2"

[dev-dependencies]
cosmwasm-schema = "1.0.0"
11 changes: 10 additions & 1 deletion packages/sei-cosmwasm/src/msg.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::sei_types::{Cancellation, DepositInfo, Metadata, Order, SettlementEntry};
use cosmwasm_std::{Addr, Coin, CosmosMsg, CustomMsg};
use cosmwasm_std::{Addr, Coin, CosmosMsg, CustomMsg, Uint128};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -41,6 +41,15 @@ pub enum SeiMsg {
SetMetadata {
metadata: Metadata,
},
DelegateCallEvm {
to: String,
data: String, // base64 encoded
},
CallEvm {
value: Uint128,
to: String,
data: String, // base64 encoded
},
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
Expand Down
181 changes: 179 additions & 2 deletions packages/sei-cosmwasm/src/querier.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use cosmwasm_std::{Addr, QuerierWrapper, StdResult};
use cosmwasm_std::{Addr, QuerierWrapper, StdResult, Uint128};
use cw20::{TokenInfoResponse, BalanceResponse};

use crate::query::{
DenomAuthorityMetadataResponse, DenomsFromCreatorResponse, DexTwapsResponse, EpochResponse,
ExchangeRatesResponse, GetLatestPriceResponse, GetOrderByIdResponse, GetOrdersResponse,
OracleTwapsResponse, OrderSimulationResponse, SeiQuery, SeiQueryWrapper,
OracleTwapsResponse, OrderSimulationResponse, StaticCallResponse, ErcPayloadResponse,
Erc20AllowanceResponse, Erc721OwnerResponse, Erc721ApprovedResponse, Erc721IsApprovedForAllResponse,
Erc721NameSymbolResponse, Erc721UriResponse, SeiQuery, SeiQueryWrapper,
};
use crate::route::SeiRoute;
use crate::Order;
Expand Down Expand Up @@ -167,4 +170,178 @@ impl<'a> SeiQuerier<'a> {
.into();
self.querier.query(&request)
}

pub fn static_call(&self, from: String, to: String, data: String) -> StdResult<StaticCallResponse> {
let request = SeiQueryWrapper {
route: SeiRoute::Evm,
query_data: SeiQuery::StaticCall {
from, to, data,
},
}
.into();

self.querier.query(&request)
}

// returns base64-encoded bytes
pub fn erc20_transfer_payload(&self, recipient: String, amount: Uint128) -> StdResult<ErcPayloadResponse> {
let request = SeiQueryWrapper {
route: SeiRoute::Evm,
query_data: SeiQuery::Erc20TransferPayload {
recipient, amount,
},
}
.into();

self.querier.query(&request)
}

// returns base64-encoded bytes
pub fn erc20_transfer_from_payload(&self, owner: String, recipient: String, amount: Uint128) -> StdResult<ErcPayloadResponse> {
let request = SeiQueryWrapper {
route: SeiRoute::Evm,
query_data: SeiQuery::Erc20TransferFromPayload {
owner, recipient, amount,
},
}
.into();

self.querier.query(&request)
}

// returns base64-encoded bytes
pub fn erc20_approve_payload(&self, spender: String, amount: Uint128) -> StdResult<ErcPayloadResponse> {
let request = SeiQueryWrapper {
route: SeiRoute::Evm,
query_data: SeiQuery::Erc20ApprovePayload {
spender, amount,
},
}
.into();

self.querier.query(&request)
}

pub fn erc20_allowance(&self, contract_address: String, owner: String, spender: String) -> StdResult<Erc20AllowanceResponse> {
let request = SeiQueryWrapper {
route: SeiRoute::Evm,
query_data: SeiQuery::Erc20Allowance {
contract_address, owner, spender,
},
}
.into();

self.querier.query(&request)
}

pub fn erc20_token_info(&self, contract_address: String, caller: String) -> StdResult<TokenInfoResponse> {
let request = SeiQueryWrapper {
route: SeiRoute::Evm,
query_data: SeiQuery::Erc20TokenInfo {
contract_address, caller,
},
}
.into();

self.querier.query(&request)
}

pub fn erc20_balance(&self, contract_address: String, account: String) -> StdResult<BalanceResponse> {
let request = SeiQueryWrapper {
route: SeiRoute::Evm,
query_data: SeiQuery::Erc20Balance {
contract_address, account,
},
}
.into();

self.querier.query(&request)
}

pub fn erc721_owner(&self, caller: String, contract_address: String, token_id: String) -> StdResult<Erc721OwnerResponse> {
let request = SeiQueryWrapper {
route: SeiRoute::Evm,
query_data: SeiQuery::Erc721Owner { caller, contract_address, token_id },
}
.into();

self.querier.query(&request)
}

pub fn erc721_approved(&self, caller: String, contract_address: String, token_id: String) -> StdResult<Erc721ApprovedResponse> {
let request = SeiQueryWrapper {
route: SeiRoute::Evm,
query_data: SeiQuery::Erc721Approved { caller, contract_address, token_id },
}
.into();

self.querier.query(&request)
}

pub fn erc721_is_approved_for_all(&self, caller: String, contract_address: String, owner: String, operator: String) -> StdResult<Erc721IsApprovedForAllResponse> {
let request = SeiQueryWrapper {
route: SeiRoute::Evm,
query_data: SeiQuery::Erc721IsApprovedForAll { caller, contract_address, owner, operator },
}
.into();

self.querier.query(&request)
}

pub fn erc721_name_symbol(&self, caller: String, contract_address: String) -> StdResult<Erc721NameSymbolResponse> {
let request = SeiQueryWrapper {
route: SeiRoute::Evm,
query_data: SeiQuery::Erc721NameSymbol { caller, contract_address },
}
.into();

self.querier.query(&request)
}

pub fn erc721_uri(&self, caller: String, contract_address: String, token_id: String,) -> StdResult<Erc721UriResponse> {
let request = SeiQueryWrapper {
route: SeiRoute::Evm,
query_data: SeiQuery::Erc721Uri { caller, contract_address, token_id },
}
.into();

self.querier.query(&request)
}

// returns base64-encoded bytes
pub fn erc721_transfer_payload(&self, from: String, recipient: String, token_id: String) -> StdResult<ErcPayloadResponse> {
let request = SeiQueryWrapper {
route: SeiRoute::Evm,
query_data: SeiQuery::Erc721TransferPayload {
from, recipient, token_id,
},
}
.into();

self.querier.query(&request)
}

// returns base64-encoded bytes
pub fn erc721_approve_payload(&self, spender: String, token_id: String) -> StdResult<ErcPayloadResponse> {
let request = SeiQueryWrapper {
route: SeiRoute::Evm,
query_data: SeiQuery::Erc721ApprovePayload {
spender, token_id,
},
}
.into();

self.querier.query(&request)
}

// returns base64-encoded bytes
pub fn erc721_set_approval_all_payload(&self, to: String, approved: bool) -> StdResult<ErcPayloadResponse> {
let request = SeiQueryWrapper {
route: SeiRoute::Evm,
query_data: SeiQuery::Erc721SetApprovalAllPayload { to, approved, },
}
.into();

self.querier.query(&request)
}
}
113 changes: 112 additions & 1 deletion packages/sei-cosmwasm/src/query.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cosmwasm_std::{Addr, CustomQuery, Decimal};
use cosmwasm_std::{Addr, CustomQuery, Decimal, Uint128};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -56,6 +56,75 @@ pub enum SeiQuery {
DenomsFromCreator {
creator: Addr,
},
StaticCall {
from: String,
to: String,
data: String, // base64
},
Erc20TransferPayload {
recipient: String,
amount: Uint128,
},
Erc20TransferFromPayload {
owner: String,
recipient: String,
amount: Uint128,
},
Erc20ApprovePayload {
spender: String,
amount: Uint128,
},
Erc20Allowance {
contract_address: String,
owner: String,
spender: String,
},
Erc20TokenInfo {
contract_address: String,
caller: String,
},
Erc20Balance {
contract_address: String,
account: String,
},
Erc721TransferPayload {
from: String,
recipient: String,
token_id: String,
},
Erc721ApprovePayload {
spender: String,
token_id: String,
},
Erc721Owner {
caller: String,
contract_address: String,
token_id: String,
},
Erc721Approved {
caller: String,
contract_address: String,
token_id: String,
},
Erc721IsApprovedForAll {
caller: String,
contract_address: String,
owner: String,
operator: String,
},
Erc721SetApprovalAllPayload {
to: String,
approved: bool,
},
Erc721NameSymbol {
caller: String,
contract_address: String,
},
Erc721Uri {
caller: String,
contract_address: String,
token_id: String,
},
}

/// ExchangeRatesResponse is data format returned from OracleRequest::ExchangeRates query
Expand Down Expand Up @@ -126,3 +195,45 @@ pub struct DenomAuthorityMetadataResponse {
pub struct DenomsFromCreatorResponse {
pub denoms: Vec<String>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct StaticCallResponse {
pub data: String, // base64
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct ErcPayloadResponse {
pub encoded_payload: String,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct Erc20AllowanceResponse {
pub allowance: Uint128,
}


#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct Erc721OwnerResponse {
pub owner: String,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct Erc721ApprovedResponse {
pub approved: String,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct Erc721IsApprovedForAllResponse {
pub is_approved: bool,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct Erc721NameSymbolResponse {
pub name: String,
pub symbol: String,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct Erc721UriResponse {
pub uri: String,
}
1 change: 1 addition & 0 deletions packages/sei-cosmwasm/src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ pub enum SeiRoute {
Dex,
Epoch,
Tokenfactory,
Evm,
}

0 comments on commit b0996e9

Please sign in to comment.