From 32680e2841218a7cecb2d2e123c52fad82460977 Mon Sep 17 00:00:00 2001 From: dzmitry-lahoda Date: Sat, 30 Mar 2024 09:39:48 +0000 Subject: [PATCH] updated to new hook --- Cargo.lock | 70 +++++------ Cargo.toml | 6 +- contracts/cosmwasm/executor/src/contract.rs | 4 +- contracts/cosmwasm/order/src/lib.rs | 1 - contracts/cosmwasm/order/src/validation.rs | 10 -- contracts/cosmwasm/outpost/src/auth.rs | 3 +- .../cosmwasm/outpost/src/contract/sudo.rs | 3 +- contracts/cosmwasm/outpost/src/error.rs | 6 +- crates/cvm-runtime/src/outpost/mod.rs | 5 +- crates/cvm-runtime/src/shared.rs | 3 +- crates/cvm-runtime/src/transport/ibc/mod.rs | 13 +- crates/cvm/src/address.rs | 114 ++++++++++++++++++ crates/cvm/src/exchange.rs | 2 - crates/cvm/src/lib.rs | 2 + crates/cvm/src/shared.rs | 109 ----------------- mantis/node/src/bin/mantis.rs | 2 +- mantis/node/src/mantis/blackbox/mod.rs | 7 +- 17 files changed, 175 insertions(+), 185 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 04935966..6f35d90f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -352,9 +352,9 @@ dependencies = [ [[package]] name = "bnum" -version = "0.8.1" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab9008b6bb9fc80b5277f2fe481c09e828743d9151203e804583eb4c9e15b31d" +checksum = "56953345e39537a3e18bdaeba4cb0c58a78c1f61f361dc0fa7c5c7340ae87c5f" [[package]] name = "borsh" @@ -584,9 +584,9 @@ dependencies = [ [[package]] name = "cosmwasm-crypto" -version = "1.5.0" +version = "1.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8bb3c77c3b7ce472056968c745eb501c440fbc07be5004eba02782c35bfbbe3" +checksum = "9934c79e58d9676edfd592557dee765d2a6ef54c09d5aa2edb06156b00148966" dependencies = [ "digest 0.10.7", "ecdsa", @@ -598,18 +598,18 @@ dependencies = [ [[package]] name = "cosmwasm-derive" -version = "1.5.0" +version = "1.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fea73e9162e6efde00018d55ed0061e93a108b5d6ec4548b4f8ce3c706249687" +checksum = "bc5e72e330bd3bdab11c52b5ecbdeb6a8697a004c57964caeb5d876f0b088b3c" dependencies = [ "syn 1.0.109", ] [[package]] name = "cosmwasm-schema" -version = "1.5.0" +version = "1.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0df41ea55f2946b6b43579659eec048cc2f66e8c8e2e3652fc5e5e476f673856" +checksum = "ac3e3a2136e2a60e8b6582f5dffca5d1a683ed77bf38537d330bc1dfccd69010" dependencies = [ "cosmwasm-schema-derive", "schemars", @@ -620,9 +620,9 @@ dependencies = [ [[package]] name = "cosmwasm-schema-derive" -version = "1.5.0" +version = "1.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43609e92ce1b9368aa951b334dd354a2d0dd4d484931a5f83ae10e12a26c8ba9" +checksum = "f5d803bea6bd9ed61bd1ee0b4a2eb09ee20dbb539cc6e0b8795614d20952ebb1" dependencies = [ "proc-macro2", "quote", @@ -631,9 +631,9 @@ dependencies = [ [[package]] name = "cosmwasm-std" -version = "1.5.0" +version = "1.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04d6864742e3a7662d024b51a94ea81c9af21db6faea2f9a6d2232bb97c6e53e" +checksum = "ef8666e572a3a2519010dde88c04d16e9339ae751b56b2bb35081fe3f7d6be74" dependencies = [ "base64 0.21.5", "bech32 0.9.1", @@ -645,7 +645,7 @@ dependencies = [ "hex", "schemars", "serde", - "serde-json-wasm 0.5.1", + "serde-json-wasm 0.5.2", "sha2 0.10.8", "static_assertions", "thiserror", @@ -1046,6 +1046,12 @@ dependencies = [ "syn 2.0.52", ] +[[package]] +name = "data-encoding" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e962a19be5cfc3f3bf6dd8f61eb50107f356ad6270fbb3ed41476571db78be5" + [[package]] name = "der" version = "0.7.8" @@ -1645,16 +1651,6 @@ dependencies = [ "tokio-io-timeout", ] -[[package]] -name = "ibc-app-transfer" -version = "0.48.1" -source = "git+https://github.com/dzmitry-lahoda-forks/ibc-rs.git?branch=dz/14#1f7bdc66c02299266bc140d5b17eb30722afa6c2" -dependencies = [ - "ibc-app-transfer-types", - "ibc-core", - "serde-json-wasm 1.0.1", -] - [[package]] name = "ibc-app-transfer-types" version = "0.48.1" @@ -1672,24 +1668,18 @@ dependencies = [ "uint 0.9.5 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "ibc-apps" -version = "0.48.1" -source = "git+https://github.com/dzmitry-lahoda-forks/ibc-rs.git?branch=dz/14#1f7bdc66c02299266bc140d5b17eb30722afa6c2" -dependencies = [ - "ibc-app-transfer", -] - [[package]] name = "ibc-apps-more" -version = "0.1.0" -source = "git+https://github.com/ComposableFi/ibc-apps-more-rs.git?branch=main#2e665779e1abe316e263633b5470104807605b17" +version = "0.3.0" +source = "git+https://github.com/ComposableFi/ibc-apps-more-rs.git?rev=bf01a0ba3ff4af816974d27456efc9751f188dac#bf01a0ba3ff4af816974d27456efc9751f188dac" dependencies = [ - "bech32 0.9.1", + "bech32 0.11.0", "cosmwasm-std", + "data-encoding", + "displaydoc", "hex", - "ibc-apps", - "ibc-core", + "ibc-app-transfer-types", + "ibc-core-host-types", "ibc-primitives", "prost", "schemars", @@ -2178,9 +2168,9 @@ dependencies = [ [[package]] name = "k256" -version = "0.13.2" +version = "0.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f01b677d82ef7a676aa37e099defd83a28e15687112cafdd112d60236b6115b" +checksum = "cadb76004ed8e97623117f3df85b17aaa6626ab0b0831e6573f104df16cd1bcc" dependencies = [ "cfg-if", "ecdsa", @@ -3343,9 +3333,9 @@ dependencies = [ [[package]] name = "serde-json-wasm" -version = "0.5.1" +version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "16a62a1fad1e1828b24acac8f2b468971dade7b8c3c2e672bcadefefb1f8c137" +checksum = "9e9213a07d53faa0b8dd81e767a54a8188a242fdb9be99ab75ec576a774bfdd7" dependencies = [ "serde", ] diff --git a/Cargo.toml b/Cargo.toml index 4bcf02ac..9b5eb587 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -70,7 +70,7 @@ num = { version = "^0.4.1", default-features = false } enumn = { version = "0.1.13" } prost-build = { version = "^0.12.2" } bech32 = { version = "^0.11.0", default-features = false } -cosmwasm-std = { version = "^1.5.0", features = [ +cosmwasm-std = { version = "^1.5.3", features = [ "iterator", ], default-features = false } @@ -79,7 +79,7 @@ cw20 = { version = "^1.1.2", default-features = false } cw-controllers = { version = "^1.1.1", default-features = false } sylvia = { version = "^0.9.1", default-features = false } schemars = { version = "^0.8.16", default-features = false } -cosmwasm-schema = { version = "^1.5.0", default-features = false } +cosmwasm-schema = { version = "^1.5.3", default-features = false } serde = { version = "^1.0.197", default-features = false } cw-storage-plus = { version = "^1.2.0", features = [ "iterator", @@ -128,7 +128,7 @@ thiserror = { version = "^1.0.50", default-features = false, package = "thiserro # xcm = { version = "^5.0.0", default-features = false, package = "staging-xcm" } cw-utils = { version = "^1.0.3", default-features = false } cw2 = { version = "^1.1.2", default-features = false } -ibc-apps-more = { git = "https://github.com/ComposableFi/ibc-apps-more-rs.git", branch = "main", default-features = false } +ibc-apps-more = { git = "https://github.com/ComposableFi/ibc-apps-more-rs.git", rev = "bf01a0ba3ff4af816974d27456efc9751f188dac", default-features = false } ibc-app-transfer-types = { git = "https://github.com/dzmitry-lahoda-forks/ibc-rs.git", branch = "dz/14", default-features = false, features = [ "serde", ] } diff --git a/contracts/cosmwasm/executor/src/contract.rs b/contracts/cosmwasm/executor/src/contract.rs index 18368280..f698eb07 100644 --- a/contracts/cosmwasm/executor/src/contract.rs +++ b/contracts/cosmwasm/executor/src/contract.rs @@ -16,7 +16,7 @@ use cosmwasm_std::{ SubMsgResult, WasmMsg, WasmQuery, }; use cvm_route::{asset::AssetReference, exchange::ExchangeItem}; -use cvm_runtime::executor::*; +use cvm_runtime::{executor::*, shared::CvmAddress}; use cvm_runtime::{ apply_bindings, exchange::*, @@ -457,7 +457,7 @@ pub fn interpret_transfer( deps: &mut DepsMut, env: &Env, tip: &Addr, - to: Destination, + to: Destination, assets: Funds, ) -> Result { let Config { diff --git a/contracts/cosmwasm/order/src/lib.rs b/contracts/cosmwasm/order/src/lib.rs index 827e80c5..a9dbcd87 100644 --- a/contracts/cosmwasm/order/src/lib.rs +++ b/contracts/cosmwasm/order/src/lib.rs @@ -245,7 +245,6 @@ impl OrderContract<'_> { let order: OrderItem = self.orders.load(ctx.deps.storage, order_id.u128())?; validation::validate_solver(ctx.deps.as_ref(), &ctx.info.sender, &order)?; self.orders.remove(ctx.deps.storage, order_id.u128()); - validation::validate_program(ctx.deps.as_ref(), &cvm_program, &order)?; let cvm = wasm_execute( self.cvm_address.load(ctx.deps.storage)?, &cvm_program, diff --git a/contracts/cosmwasm/order/src/validation.rs b/contracts/cosmwasm/order/src/validation.rs index 7590a2d6..ca5f7530 100644 --- a/contracts/cosmwasm/order/src/validation.rs +++ b/contracts/cosmwasm/order/src/validation.rs @@ -14,16 +14,6 @@ pub fn validate_solver( Ok(()) } -/// Validate program is sane -pub(crate) fn validate_program( - as_ref: cosmwasm_std::Deps<'_>, - cvm_program: &cvm_runtime::Program< - Vec, cvm_runtime::shared::XcAddr, cvm_runtime::Funds>>, - >, - order: &OrderItem, -) -> StdResult<()> { - Ok(()) -} /// Validate solver can solver amount he claimed pub(crate) fn validate_solvers( diff --git a/contracts/cosmwasm/outpost/src/auth.rs b/contracts/cosmwasm/outpost/src/auth.rs index 93f0fdd2..8742634d 100644 --- a/contracts/cosmwasm/outpost/src/auth.rs +++ b/contracts/cosmwasm/outpost/src/auth.rs @@ -6,6 +6,7 @@ use crate::{ use cosmwasm_std::{Deps, Env, MessageInfo}; use cvm::NetworkId; use cvm_route::transport::*; +use ibc_apps_more::types::hook::derive_intermediate_sender; /// Authorisation token indicating call is authorised according to policy /// `T`. @@ -99,7 +100,7 @@ impl Auth { .ok_or(ContractError::ICS20NotFound)? .source; let hash_of_channel_and_sender = - ibc_apps_more::hook::derive_intermediate_sender(&channel, &sender, &prefix)?; + derive_intermediate_sender(&channel, &sender, &prefix)?; deps.api.debug(&format!( "cvm::outpost:auth:: {0} {1}", &hash_of_channel_and_sender, &info.sender diff --git a/contracts/cosmwasm/outpost/src/contract/sudo.rs b/contracts/cosmwasm/outpost/src/contract/sudo.rs index f22dd10d..37a35472 100644 --- a/contracts/cosmwasm/outpost/src/contract/sudo.rs +++ b/contracts/cosmwasm/outpost/src/contract/sudo.rs @@ -1,6 +1,7 @@ use crate::{error::ContractError, state}; use cosmwasm_std::{entry_point, wasm_execute, Coin, DepsMut, Env, Event, Response}; -use ibc_apps_more::hook::{IBCLifecycleComplete, SudoMsg}; + +use ibc_apps_more::types::hook::{IBCLifecycleComplete, SudoMsg}; use ibc_core_host_types::identifiers::ChannelId; #[cfg_attr(not(feature = "library"), entry_point)] diff --git a/contracts/cosmwasm/outpost/src/error.rs b/contracts/cosmwasm/outpost/src/error.rs index 31492d1e..bb850713 100644 --- a/contracts/cosmwasm/outpost/src/error.rs +++ b/contracts/cosmwasm/outpost/src/error.rs @@ -52,7 +52,7 @@ pub enum ContractError { #[error("Route not found.")] RouteNotFound, #[error("{0}")] - Bech32(bech32::Error), + Bech32(bech32::EncodeError), #[error("{0}")] Serde(#[from] serde_json_wasm::ser::Error), #[error("Assets non transferrable")] @@ -94,8 +94,8 @@ impl From for ContractError { } } -impl From for ContractError { - fn from(value: bech32::Error) -> Self { +impl From for ContractError { + fn from(value: bech32::EncodeError) -> Self { Self::Bech32(value) } } diff --git a/crates/cvm-runtime/src/outpost/mod.rs b/crates/cvm-runtime/src/outpost/mod.rs index 19537846..13fd2a2b 100644 --- a/crates/cvm-runtime/src/outpost/mod.rs +++ b/crates/cvm-runtime/src/outpost/mod.rs @@ -8,6 +8,7 @@ use cvm_route::{ asset::{AssetItem, AssetReference}, exchange::ExchangeItem, }; +use ibc_apps_more::types::hook::{Callback, HookMemo, LazyHookMemo}; pub use query::*; use crate::{ @@ -92,9 +93,9 @@ impl ExecutePacketICS20Msg { pub fn into_wasm_hook(self, contract: ibc_primitives::Signer) -> Result { let ics20 = self.into_packet()?; - let memo: ibc_apps_more::memo::Memo = serde_json_wasm::from_str(&ics20.memo.to_string()) + let memo: LazyHookMemo = serde_json_wasm::from_str(&ics20.memo.to_string()) .map_err(|x| StdError::generic_err(format!("{:?}", x)))?; - let wasm: ibc_apps_more::hook::Callback = memo + let wasm: Callback = memo.base .wasm .ok_or(StdError::generic_err(format!("no wasm in memo")))?; ensure!( diff --git a/crates/cvm-runtime/src/shared.rs b/crates/cvm-runtime/src/shared.rs index bc97e067..778334f6 100644 --- a/crates/cvm-runtime/src/shared.rs +++ b/crates/cvm-runtime/src/shared.rs @@ -2,7 +2,7 @@ use core::panic; use crate::{prelude::*, AssetId}; use cosmwasm_std::{from_json, to_json_binary, Api, Binary, CanonicalAddr, StdError, StdResult}; -use cvm::NetworkId; +use cvm::{NetworkId, XcAddr}; use serde::{de::DeserializeOwned, Serialize}; pub use cvm::shared::*; @@ -12,6 +12,7 @@ pub type CvmFunds = Vec<(AssetId, Displayed)>; /// like `CvmFunds`, but allow relative(percentages) amounts. Similar to assets filters in XCM pub type CvmBalanceFilter = crate::asset::Amount; pub type CvmFundsFilter = crate::Funds; +pub type CvmAddress = XcAddr; pub type CvmInstruction = crate::Instruction, XcAddr, CvmFundsFilter>; pub type CvmPacket = crate::Packet; pub type CvmProgram = crate::Program>; diff --git a/crates/cvm-runtime/src/transport/ibc/mod.rs b/crates/cvm-runtime/src/transport/ibc/mod.rs index 1d9b169a..766390e4 100644 --- a/crates/cvm-runtime/src/transport/ibc/mod.rs +++ b/crates/cvm-runtime/src/transport/ibc/mod.rs @@ -9,10 +9,9 @@ use cosmwasm_std::{Api, BlockInfo, CosmosMsg, Deps, IbcEndpoint, StdResult}; use cvm_route::transport::RelativeTimeout; use ibc_core_host_types::identifiers::{ChannelId, ConnectionId, PortId}; -use ibc_apps_more::{ - hook::{Callback, SendMemo}, - memo::Memo, -}; +use ibc_apps_more::types::hook::Callback; +//, HookMemo}; +// use ibc_apps_more::types::memo::Memo; /// This message should be send as part of wasm termination memo. /// So that can match it to sender hash and know what channel and origin was used to send message. @@ -72,12 +71,14 @@ pub fn to_cosmwasm_message( block: BlockInfo, to_outpost: Addr, ) -> StdResult> { + use ibc_apps_more::types::{hook::LazyHookMemo, memo::Memo}; + let msg = outpost::ExecuteMsg::MessageHook(XcMessageData { from_network_id: route.from_network, packet, }); - let memo = SendMemo { - inner: Memo { + let memo = LazyHookMemo { + base: Memo { wasm: Some(Callback::new_cosmwasm( to_outpost.clone(), serde_cw_value::to_value(msg).expect("can always serde"), diff --git a/crates/cvm/src/address.rs b/crates/cvm/src/address.rs index e69de29b..2fd44b66 100644 --- a/crates/cvm/src/address.rs +++ b/crates/cvm/src/address.rs @@ -0,0 +1,114 @@ +use crate::{prelude::*, AssetId}; +use cosmwasm_std::{from_json, to_json_binary, Api, Binary, CanonicalAddr, StdError, StdResult}; +use serde::{de::DeserializeOwned, Serialize}; + + +/// A wrapper around any address on any chain. +/// Similar to `ibc_rs::Signer`(multi encoding), but not depend on ibc code bloat. +/// Unlike parity MultiLocation/Account32/Account20 which hard codes enum into code. +/// Better send canonical address to each chain for performance, +/// But it will also decode/reencode best effort. +/// Inner must be either base64 or hex encoded or contain only characters from these. +/// Added with helper per chain to get final address to use. +#[cfg_attr( + feature = "json-schema", // all(feature = "json-schema", not(target_arch = "wasm32")), + derive(schemars::JsonSchema) +)] +#[cfg_attr( + feature = "scale", + derive( + parity_scale_codec::Encode, + parity_scale_codec::Decode, + scale_info::TypeInfo + ) +)] +#[derive( + Clone, + PartialEq, + Eq, + Hash, + derive_more::Deref, + derive_more::From, + derive_more::Into, + serde::Deserialize, + serde::Serialize, +)] +#[into(owned, ref, ref_mut)] +#[repr(transparent)] +pub struct XcAddr(pub String); + +impl From for Vec { + fn from(value: XcAddr) -> Self { + value.0.into_bytes() + } +} + +impl TryFrom> for XcAddr { + type Error = StdError; + + fn try_from(value: Vec) -> Result { + Ok(Self(String::from_utf8(value)?)) + } +} + +impl XcAddr { + /// idea that whatever user plugs into, it works, really for adoption + /// sure for Ethereum he must plug exact binary address, but for others it's just a string + #[cfg(feature = "cosmwasm")] + pub fn encode_cosmwasm(&self, api: &dyn Api) -> Result { + let addr = self.parse()?; + + Ok(api.addr_humanize(&CanonicalAddr(addr))?.to_string()) + } + + #[cfg(feature = "cosmwasm")] + pub fn parse(&self) -> Result { + use bech32::{primitives::decode::CheckedHrpstring, Bech32}; + let addr = if let Ok(addr) = CheckedHrpstring::new::(&self.0) { + Binary(addr.byte_iter().into_iter().collect()) + } else if let Ok(addr) = Binary::from_base64(&self.0) { + addr + } + else { + return Err(StdError::generic_err("Failed to ensure XcAddr encoding")).into(); + }; + Ok(addr) + } +} + +#[cfg(test)] +mod tests { + #[cfg(feature = "cosmwasm")] + #[test] + fn xcaddr() { + let addr_a = "osmovalcons1qg7u70m2af8qpx9thg40y0eavkkryjz3rsxafg"; + let addr_b = "Aj3PP2rqTgCYq7oq8j89ZawySFE="; + let addr_c = "cosmosvalcons1qg7u70m2af8qpx9thg40y0eavkkryjz35gfmyw"; + + // this is valid base64 and this is very bad + let addr_d = "023DCF3F6AEA4E0098ABBA2AF23F3D65AC324851"; + + let xcaddr_a = super::XcAddr(addr_a.to_string()); + let xcaddr_b = super::XcAddr(addr_b.to_string()); + let xcaddr_c = super::XcAddr(addr_c.to_string()); + let xcaddr_d = super::XcAddr(addr_d.to_string()); + assert_eq!(addr_b, xcaddr_a.parse().unwrap().to_base64()); + assert_eq!(addr_b, xcaddr_b.parse().unwrap().to_base64()); + assert_eq!(addr_b, xcaddr_c.parse().unwrap().to_base64()); + + // next fails + // assert_eq!(addr_b, xcaddr_d.parse().unwrap().to_base64()); + } +} + +impl core::fmt::Display for XcAddr { + fn fmt(&self, fmtr: &mut core::fmt::Formatter) -> core::fmt::Result { + core::fmt::Display::fmt(&self.0, fmtr) + } +} + +impl core::fmt::Debug for XcAddr { + fn fmt(&self, fmtr: &mut core::fmt::Formatter) -> core::fmt::Result { + core::fmt::Debug::fmt(&self.0, fmtr) + } +} diff --git a/crates/cvm/src/exchange.rs b/crates/cvm/src/exchange.rs index 7a26bfea..571a1474 100644 --- a/crates/cvm/src/exchange.rs +++ b/crates/cvm/src/exchange.rs @@ -1,3 +1 @@ -use crate::{prelude::*, NetworkId}; - pub type ExchangeId = crate::shared::Displayed; diff --git a/crates/cvm/src/lib.rs b/crates/cvm/src/lib.rs index 32135919..4e79b6d2 100644 --- a/crates/cvm/src/lib.rs +++ b/crates/cvm/src/lib.rs @@ -8,5 +8,7 @@ pub mod network; mod prelude; pub mod proto; pub mod shared; +pub mod address; pub use asset::*; pub use network::*; +pub use address::*; diff --git a/crates/cvm/src/shared.rs b/crates/cvm/src/shared.rs index 6cf2b23c..e8310b6e 100644 --- a/crates/cvm/src/shared.rs +++ b/crates/cvm/src/shared.rs @@ -2,115 +2,6 @@ use crate::{prelude::*, AssetId}; use cosmwasm_std::{from_json, to_json_binary, Api, Binary, CanonicalAddr, StdError, StdResult}; use serde::{de::DeserializeOwned, Serialize}; -/// A wrapper around any address on any chain. -/// Similar to `ibc_rs::Signer`(multi encoding), but not depend on ibc code bloat. -/// Unlike parity MultiLocation/Account32/Account20 which hard codes enum into code. -/// Better send canonical address to each chain for performance, -/// But it will also decode/reencode best effort. -/// Inner must be either base64 or hex encoded or contain only characters from these. -/// Added with helper per chain to get final address to use. -#[cfg_attr( - feature = "json-schema", // all(feature = "json-schema", not(target_arch = "wasm32")), - derive(schemars::JsonSchema) -)] -#[cfg_attr( - feature = "scale", - derive( - parity_scale_codec::Encode, - parity_scale_codec::Decode, - scale_info::TypeInfo - ) -)] -#[derive( - Clone, - PartialEq, - Eq, - Hash, - derive_more::Deref, - derive_more::From, - derive_more::Into, - serde::Deserialize, - serde::Serialize, -)] -#[into(owned, ref, ref_mut)] -#[repr(transparent)] -pub struct XcAddr(pub String); - -impl From for Vec { - fn from(value: XcAddr) -> Self { - value.0.into_bytes() - } -} - -impl TryFrom> for XcAddr { - type Error = StdError; - - fn try_from(value: Vec) -> Result { - Ok(Self(String::from_utf8(value)?)) - } -} - -impl XcAddr { - /// idea that whatever user plugs into, it works, really for adoption - /// sure for Ethereum he must plug exact binary address, but for others it's just a string - #[cfg(feature = "cosmwasm")] - pub fn encode_cosmwasm(&self, api: &dyn Api) -> Result { - let addr = self.parse()?; - - Ok(api.addr_humanize(&CanonicalAddr(addr))?.to_string()) - } - - #[cfg(feature = "cosmwasm")] - pub fn parse(&self) -> Result { - use bech32::{primitives::decode::CheckedHrpstring, Bech32}; - let addr = if let Ok(addr) = CheckedHrpstring::new::(&self.0) { - Binary(addr.byte_iter().into_iter().collect()) - } else if let Ok(addr) = Binary::from_base64(&self.0) { - addr - } - else { - return Err(StdError::generic_err("Failed to ensure XcAddr encoding")).into(); - }; - Ok(addr) - } -} - -#[cfg(test)] -mod tests { - #[cfg(feature = "cosmwasm")] - #[test] - fn xcaddr() { - let addr_a = "osmovalcons1qg7u70m2af8qpx9thg40y0eavkkryjz3rsxafg"; - let addr_b = "Aj3PP2rqTgCYq7oq8j89ZawySFE="; - let addr_c = "cosmosvalcons1qg7u70m2af8qpx9thg40y0eavkkryjz35gfmyw"; - - // this is valid base64 and this is very bad - let addr_d = "023DCF3F6AEA4E0098ABBA2AF23F3D65AC324851"; - - let xcaddr_a = super::XcAddr(addr_a.to_string()); - let xcaddr_b = super::XcAddr(addr_b.to_string()); - let xcaddr_c = super::XcAddr(addr_c.to_string()); - let xcaddr_d = super::XcAddr(addr_d.to_string()); - assert_eq!(addr_b, xcaddr_a.parse().unwrap().to_base64()); - assert_eq!(addr_b, xcaddr_b.parse().unwrap().to_base64()); - assert_eq!(addr_b, xcaddr_c.parse().unwrap().to_base64()); - - // next fails - // assert_eq!(addr_b, xcaddr_d.parse().unwrap().to_base64()); - } -} - -impl core::fmt::Display for XcAddr { - fn fmt(&self, fmtr: &mut core::fmt::Formatter) -> core::fmt::Result { - core::fmt::Display::fmt(&self.0, fmtr) - } -} - -impl core::fmt::Debug for XcAddr { - fn fmt(&self, fmtr: &mut core::fmt::Formatter) -> core::fmt::Result { - core::fmt::Debug::fmt(&self.0, fmtr) - } -} /// A wrapper around a type which is serde-serialised as a string. /// diff --git a/mantis/node/src/bin/mantis.rs b/mantis/node/src/bin/mantis.rs index e35d4ca6..084d8cac 100644 --- a/mantis/node/src/bin/mantis.rs +++ b/mantis/node/src/bin/mantis.rs @@ -103,7 +103,7 @@ async fn solve_orders(solver_args: &SolverArgs) { get_latest_block_and_account_by_key(&args.rpc_centauri, &args.grpc_centauri, &signer) .await; let all_orders = get_all_orders(&args.order_contract, &mut wasm_read_client, &tip).await; - if all_orders.any() { + if !all_orders.is_empty() { solve( &mut write_client, &mut wasm_read_client, diff --git a/mantis/node/src/mantis/blackbox/mod.rs b/mantis/node/src/mantis/blackbox/mod.rs index 73636248..1a1f2236 100644 --- a/mantis/node/src/mantis/blackbox/mod.rs +++ b/mantis/node/src/mantis/blackbox/mod.rs @@ -4,7 +4,8 @@ use cvm_runtime::{ network, outpost::GetConfigResponse, proto::cvm, - shared::{CvmFundsFilter, CvmInstruction, CvmProgram, Displayed, XcAddr}, + + shared::{CvmAddress, CvmFundsFilter, CvmInstruction, CvmProgram, Displayed}, Amount, AssetId, ExchangeId, }; @@ -13,7 +14,7 @@ struct BankInput { pub in_asset_id: AssetId, pub in_asset_amount: Displayed, pub out_asset_id: AssetId, - pub order_accounts: Vec<(XcAddr, Amount)>, + pub order_accounts: Vec<(CvmAddress, Amount)>, } impl BankInput { @@ -21,7 +22,7 @@ impl BankInput { in_asset_id: AssetId, in_asset_amount: Displayed, out_asset_id: AssetId, - order_accounts: Vec<(XcAddr, Amount)>, + order_accounts: Vec<(CvmAddress, Amount)>, ) -> Self { Self { in_asset_id,