From d94100ef757e72874009d875baf78631a9227759 Mon Sep 17 00:00:00 2001 From: Joe Monem Date: Mon, 25 Nov 2024 13:20:14 +0200 Subject: [PATCH 1/9] feat: handle cross chain recipient in rates --- .../andromeda-auction/src/contract.rs | 23 +-- .../andromeda-cw721/src/contract.rs | 23 +-- contracts/os/andromeda-kernel/src/execute.rs | 1 - packages/std/src/ado_base/rates.rs | 62 ++++-- packages/std/src/amp/recipient.rs | 8 + tests-integration/tests/kernel_orch.rs | 178 +++++++++++++++++- 6 files changed, 253 insertions(+), 42 deletions(-) diff --git a/contracts/non-fungible-tokens/andromeda-auction/src/contract.rs b/contracts/non-fungible-tokens/andromeda-auction/src/contract.rs index 64be2c107..0d53e8dfa 100644 --- a/contracts/non-fungible-tokens/andromeda-auction/src/contract.rs +++ b/contracts/non-fungible-tokens/andromeda-auction/src/contract.rs @@ -95,13 +95,13 @@ pub fn execute( pub fn handle_execute(mut ctx: ExecuteContext, msg: ExecuteMsg) -> Result { let action = msg.as_ref().to_string(); - let action_response = call_action( - &mut ctx.deps, - &ctx.info, - &ctx.env, - &ctx.amp_ctx, - msg.as_ref(), - )?; + // let action_response = call_action( + // &mut ctx.deps, + // &ctx.info, + // &ctx.env, + // &ctx.amp_ctx, + // msg.as_ref(), + // )?; let res = match msg { ExecuteMsg::ReceiveNft(msg) => handle_receive_cw721(ctx, msg), @@ -154,10 +154,11 @@ pub fn handle_execute(mut ctx: ExecuteContext, msg: ExecuteMsg) -> Result ADOContract::default().execute(ctx, msg), }?; - Ok(res - .add_submessages(action_response.messages) - .add_attributes(action_response.attributes) - .add_events(action_response.events)) + Ok( + res, // .add_submessages(action_response.messages) + // .add_attributes(action_response.attributes) + // .add_events(action_response.events) + ) } fn handle_receive_cw721( diff --git a/contracts/non-fungible-tokens/andromeda-cw721/src/contract.rs b/contracts/non-fungible-tokens/andromeda-cw721/src/contract.rs index 4db47bfa1..116e06044 100644 --- a/contracts/non-fungible-tokens/andromeda-cw721/src/contract.rs +++ b/contracts/non-fungible-tokens/andromeda-cw721/src/contract.rs @@ -91,13 +91,13 @@ fn handle_execute(mut ctx: ExecuteContext, msg: ExecuteMsg) -> Result Result bool { + let protocol = self.address.get_protocol(); + match protocol { + Some("ibc") => true, + _ => false, + } + } + /// Generates a direct sub message for the given recipient. pub fn generate_direct_msg( &self, diff --git a/tests-integration/tests/kernel_orch.rs b/tests-integration/tests/kernel_orch.rs index 6a35c0be6..d03d51d9d 100644 --- a/tests-integration/tests/kernel_orch.rs +++ b/tests-integration/tests/kernel_orch.rs @@ -1,4 +1,5 @@ use andromeda_adodb::ADODBContract; +use andromeda_auction::{mock::mock_start_auction, AuctionContract}; use andromeda_counter::CounterContract; use andromeda_data_storage::counter::{ CounterRestriction, ExecuteMsg as CounterExecuteMsg, GetCurrentAmountResponse, @@ -9,14 +10,17 @@ use andromeda_finance::splitter::{ AddressPercent, ExecuteMsg as SplitterExecuteMsg, InstantiateMsg as SplitterInstantiateMsg, }; +use andromeda_cw721::CW721Contract; use andromeda_kernel::KernelContract; +use andromeda_non_fungible_tokens::cw721::TokenExtension; use andromeda_splitter::SplitterContract; use andromeda_std::{ + ado_base::rates::{LocalRate, LocalRateType, LocalRateValue, PercentRate, Rate, RatesMessage}, amp::{ messages::{AMPMsg, AMPMsgConfig}, AndrAddr, Recipient, }, - common::Milliseconds, + common::{denom::Asset, expiration::Expiry, Milliseconds}, os::{ self, kernel::{AcknowledgementMsg, ExecuteMsg, InstantiateMsg, SendMessageWithFundsResponse}, @@ -24,7 +28,7 @@ use andromeda_std::{ }; use andromeda_vfs::VFSContract; use cosmwasm_std::{ - to_json_binary, Addr, Binary, Decimal, IbcAcknowledgement, IbcEndpoint, IbcPacket, + coin, to_json_binary, Addr, Binary, Decimal, IbcAcknowledgement, IbcEndpoint, IbcPacket, IbcPacketAckMsg, IbcTimeout, Timestamp, Uint128, }; use cw_orch::prelude::*; @@ -586,6 +590,7 @@ fn test_kernel_ibc_execute_only_multi_hop() { fn test_kernel_ibc_funds_only() { // Here `juno-1` is the chain-id and `juno` is the address prefix for this chain let sender = Addr::unchecked("sender_for_all_chains").into_string(); + let buyer = Addr::unchecked("buyer").into_string(); let interchain = MockInterchainEnv::new(vec![ ("juno", &sender), @@ -596,12 +601,17 @@ fn test_kernel_ibc_funds_only() { let juno = interchain.get_chain("juno").unwrap(); let osmosis = interchain.get_chain("osmosis").unwrap(); - juno.set_balance(sender.clone(), vec![Coin::new(100000000000000, "juno")]) .unwrap(); + juno.set_balance(buyer.clone(), vec![Coin::new(100000000000000, "juno")]) + .unwrap(); let kernel_juno = KernelContract::new(juno.clone()); let vfs_juno = VFSContract::new(juno.clone()); + let adodb_juno = ADODBContract::new(juno.clone()); + let economics_juno = EconomicsContract::new(juno.clone()); + let mut auction_juno = AuctionContract::new(juno.clone()); + let cw721_juno = CW721Contract::new(juno.clone()); let kernel_osmosis = KernelContract::new(osmosis.clone()); let counter_osmosis = CounterContract::new(osmosis.clone()); let vfs_osmosis = VFSContract::new(osmosis.clone()); @@ -610,6 +620,11 @@ fn test_kernel_ibc_funds_only() { kernel_juno.upload().unwrap(); vfs_juno.upload().unwrap(); + adodb_juno.upload().unwrap(); + economics_juno.upload().unwrap(); + auction_juno.upload().unwrap(); + cw721_juno.upload().unwrap(); + kernel_osmosis.upload().unwrap(); counter_osmosis.upload().unwrap(); vfs_osmosis.upload().unwrap(); @@ -703,6 +718,38 @@ fn test_kernel_ibc_funds_only() { ) .unwrap(); + adodb_juno + .instantiate( + &os::adodb::InstantiateMsg { + kernel_address: kernel_juno.address().unwrap().into_string(), + owner: None, + }, + None, + None, + ) + .unwrap(); + + economics_juno + .instantiate( + &os::economics::InstantiateMsg { + kernel_address: kernel_juno.address().unwrap().into_string(), + owner: None, + }, + None, + None, + ) + .unwrap(); + + kernel_juno + .execute( + &ExecuteMsg::UpsertKeyAddress { + key: "economics".to_string(), + value: economics_juno.address().unwrap().into_string(), + }, + None, + ) + .unwrap(); + adodb_osmosis .instantiate( &os::adodb::InstantiateMsg { @@ -836,7 +883,7 @@ fn test_kernel_ibc_funds_only() { .execute( &ExecuteMsg::UpsertKeyAddress { key: "trigger_key".to_string(), - value: sender, + value: sender.clone(), }, None, ) @@ -892,6 +939,129 @@ fn test_kernel_ibc_funds_only() { // There was a decode error or the packet timed out // Else the packet timed-out, you may have a relayer error or something is wrong in your application }; + + // Set up cross chain rates recipient + auction_juno + .instantiate( + &andromeda_non_fungible_tokens::auction::InstantiateMsg { + authorized_token_addresses: None, + authorized_cw20_addresses: None, + kernel_address: kernel_juno.address().unwrap().into_string(), + owner: None, + }, + None, + None, + ) + .unwrap(); + + cw721_juno + .instantiate( + &andromeda_non_fungible_tokens::cw721::InstantiateMsg { + name: "test tokens".to_string(), + symbol: "TT".to_string(), + minter: AndrAddr::from_string(sender.clone()), + kernel_address: kernel_juno.address().unwrap().into_string(), + owner: None, + }, + None, + None, + ) + .unwrap(); + + auction_juno + .execute( + &andromeda_non_fungible_tokens::auction::ExecuteMsg::Rates(RatesMessage::SetRate { + action: "Claim".to_string(), + rate: Rate::Local(LocalRate { + rate_type: LocalRateType::Deductive, + recipient: Recipient::new( + AndrAddr::from_string(format!("ibc://osmosis/{}", recipient)), + None, + ), + value: LocalRateValue::Percent(PercentRate { + percent: Decimal::percent(50), + }), + description: None, + }), + }), + None, + ) + .unwrap(); + + cw721_juno + .execute( + &andromeda_non_fungible_tokens::cw721::ExecuteMsg::Mint { + token_id: "1".to_string(), + owner: sender.clone(), + token_uri: None, + extension: TokenExtension::default(), + }, + None, + ) + .unwrap(); + + let start_time = Milliseconds::from_nanos(juno.block_info().unwrap().time.nanos()); + let receive_msg = mock_start_auction( + None, + Expiry::AtTime(start_time.plus_milliseconds(Milliseconds(10000))), + None, + Asset::NativeToken("juno".to_string()), + None, + None, + None, + None, + ); + cw721_juno + .execute( + &andromeda_non_fungible_tokens::cw721::ExecuteMsg::SendNft { + contract: AndrAddr::from_string(auction_juno.address().unwrap()), + token_id: "1".to_string(), + msg: to_json_binary(&receive_msg).unwrap(), + }, + None, + ) + .unwrap(); + juno.wait_seconds(1).unwrap(); + + auction_juno.set_sender(&Addr::unchecked(buyer.clone())); + auction_juno + .execute( + &andromeda_non_fungible_tokens::auction::ExecuteMsg::PlaceBid { + token_id: "1".to_string(), + token_address: cw721_juno.address().unwrap().into_string(), + }, + Some(&[coin(50, "juno")]), + ) + .unwrap(); + juno.next_block().unwrap(); + juno.next_block().unwrap(); + + // Claim + let claim_request = auction_juno + .execute( + &andromeda_non_fungible_tokens::auction::ExecuteMsg::Claim { + token_id: "1".to_string(), + token_address: cw721_juno.address().unwrap().into_string(), + }, + None, + ) + .unwrap(); + let packet_lifetime = interchain.await_packets("juno", claim_request).unwrap(); + + // For testing a successful outcome of the first packet sent out in the tx, you can use: + if let IbcPacketOutcome::Success { .. } = &packet_lifetime.packets[0].outcome { + // Packet has been successfully acknowledged and decoded, the transaction has gone through correctly + + // Check recipient balance after trigger execute msg + let balances = osmosis.query_all_balances(recipient).unwrap(); + assert_eq!(balances.len(), 1); + assert_eq!(balances[0].denom, ibc_denom); + assert_eq!(balances[0].amount.u128(), 100 + 25); + } else { + panic!("packet timed out"); + // There was a decode error or the packet timed out + // Else the packet timed-out, you may have a relayer error or something is wrong in your application + }; } #[test] From 55a94a9047c312dfb8e09a917f4da81675b00c11 Mon Sep 17 00:00:00 2001 From: Joe Monem Date: Mon, 25 Nov 2024 15:43:09 +0200 Subject: [PATCH 2/9] test: fix test_kernel_ibc_funds_only --- tests-integration/tests/kernel_orch.rs | 57 ++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/tests-integration/tests/kernel_orch.rs b/tests-integration/tests/kernel_orch.rs index d03d51d9d..39eb5e663 100644 --- a/tests-integration/tests/kernel_orch.rs +++ b/tests-integration/tests/kernel_orch.rs @@ -829,6 +829,10 @@ fn test_kernel_ibc_funds_only() { .unwrap(); let recipient = "osmo1qzskhrca90qy2yjjxqzq4yajy842x7c50xq33d"; + println!( + "osmosis kernel address: {}", + kernel_osmosis.address().unwrap() + ); let kernel_juno_send_request = kernel_juno .execute( @@ -1048,6 +1052,59 @@ fn test_kernel_ibc_funds_only() { .unwrap(); let packet_lifetime = interchain.await_packets("juno", claim_request).unwrap(); + // For testing a successful outcome of the first packet sent out in the tx, you can use: + if let IbcPacketOutcome::Success { .. } = &packet_lifetime.packets[0].outcome { + // Packet has been successfully acknowledged and decoded, the transaction has gone through correctly + + // Check recipient balance after trigger execute msg + let balances = osmosis + .query_all_balances(kernel_osmosis.address().unwrap()) + .unwrap(); + assert_eq!(balances.len(), 1); + assert_eq!(balances[0].denom, ibc_denom); + assert_eq!(balances[0].amount.u128(), 25); + } else { + panic!("packet timed out"); + // There was a decode error or the packet timed out + // Else the packet timed-out, you may have a relayer error or something is wrong in your application + }; + + // Construct an Execute msg from the kernel on juno inteded for the splitter on osmosis + let kernel_juno_trigger_request = kernel_juno + .execute( + &ExecuteMsg::TriggerRelay { + packet_sequence: "2".to_string(), + packet_ack_msg: IbcPacketAckMsg::new( + IbcAcknowledgement::new( + to_json_binary(&AcknowledgementMsg::::Ok( + SendMessageWithFundsResponse {}, + )) + .unwrap(), + ), + IbcPacket::new( + Binary::default(), + IbcEndpoint { + port_id: "port_id".to_string(), + channel_id: "channel_id".to_string(), + }, + IbcEndpoint { + port_id: "port_id".to_string(), + channel_id: "channel_id".to_string(), + }, + 1, + IbcTimeout::with_timestamp(Timestamp::from_seconds(1)), + ), + Addr::unchecked("relayer"), + ), + }, + None, + ) + .unwrap(); + + let packet_lifetime = interchain + .await_packets("juno", kernel_juno_trigger_request) + .unwrap(); + // For testing a successful outcome of the first packet sent out in the tx, you can use: if let IbcPacketOutcome::Success { .. } = &packet_lifetime.packets[0].outcome { // Packet has been successfully acknowledged and decoded, the transaction has gone through correctly From da74efe17b166f4c69248af3cb17e219f859096e Mon Sep 17 00:00:00 2001 From: Joe Monem Date: Mon, 25 Nov 2024 16:48:09 +0200 Subject: [PATCH 3/9] ref: re-enable economics in cw721 and auction --- Cargo.lock | 2 +- .../andromeda-auction/src/contract.rs | 23 +++++++++--------- .../andromeda-cw721/src/contract.rs | 23 +++++++++--------- packages/std/Cargo.toml | 2 +- tests-integration/tests/kernel_orch.rs | 24 ++++++++++++++++++- 5 files changed, 47 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d6c6ff402..1d028b6bd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -812,7 +812,7 @@ dependencies = [ [[package]] name = "andromeda-std" -version = "1.4.0" +version = "1.5.0" dependencies = [ "andromeda-macros", "cosmwasm-schema 1.5.8", diff --git a/contracts/non-fungible-tokens/andromeda-auction/src/contract.rs b/contracts/non-fungible-tokens/andromeda-auction/src/contract.rs index 0d53e8dfa..64be2c107 100644 --- a/contracts/non-fungible-tokens/andromeda-auction/src/contract.rs +++ b/contracts/non-fungible-tokens/andromeda-auction/src/contract.rs @@ -95,13 +95,13 @@ pub fn execute( pub fn handle_execute(mut ctx: ExecuteContext, msg: ExecuteMsg) -> Result { let action = msg.as_ref().to_string(); - // let action_response = call_action( - // &mut ctx.deps, - // &ctx.info, - // &ctx.env, - // &ctx.amp_ctx, - // msg.as_ref(), - // )?; + let action_response = call_action( + &mut ctx.deps, + &ctx.info, + &ctx.env, + &ctx.amp_ctx, + msg.as_ref(), + )?; let res = match msg { ExecuteMsg::ReceiveNft(msg) => handle_receive_cw721(ctx, msg), @@ -154,11 +154,10 @@ pub fn handle_execute(mut ctx: ExecuteContext, msg: ExecuteMsg) -> Result ADOContract::default().execute(ctx, msg), }?; - Ok( - res, // .add_submessages(action_response.messages) - // .add_attributes(action_response.attributes) - // .add_events(action_response.events) - ) + Ok(res + .add_submessages(action_response.messages) + .add_attributes(action_response.attributes) + .add_events(action_response.events)) } fn handle_receive_cw721( diff --git a/contracts/non-fungible-tokens/andromeda-cw721/src/contract.rs b/contracts/non-fungible-tokens/andromeda-cw721/src/contract.rs index 116e06044..4db47bfa1 100644 --- a/contracts/non-fungible-tokens/andromeda-cw721/src/contract.rs +++ b/contracts/non-fungible-tokens/andromeda-cw721/src/contract.rs @@ -91,13 +91,13 @@ fn handle_execute(mut ctx: ExecuteContext, msg: ExecuteMsg) -> Result Result Date: Mon, 25 Nov 2024 16:54:41 +0200 Subject: [PATCH 4/9] test: add economics to adodb --- tests-integration/tests/kernel_orch.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests-integration/tests/kernel_orch.rs b/tests-integration/tests/kernel_orch.rs index 503cd8660..c259ba350 100644 --- a/tests-integration/tests/kernel_orch.rs +++ b/tests-integration/tests/kernel_orch.rs @@ -730,6 +730,19 @@ fn test_kernel_ibc_funds_only() { ) .unwrap(); + adodb_juno + .execute( + &os::adodb::ExecuteMsg::Publish { + code_id: 4, + ado_type: "economics".to_string(), + action_fees: None, + version: "1.1.1".to_string(), + publisher: None, + }, + None, + ) + .unwrap(); + economics_juno .instantiate( &os::economics::InstantiateMsg { From bf33106dbfdb795efbbe280e0542fc32efade5a2 Mon Sep 17 00:00:00 2001 From: Joe Monem Date: Mon, 25 Nov 2024 17:07:44 +0200 Subject: [PATCH 5/9] test: remove unused economics --- tests-integration/tests/kernel_orch.rs | 47 +++++++++++++------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/tests-integration/tests/kernel_orch.rs b/tests-integration/tests/kernel_orch.rs index c259ba350..8217e9f04 100644 --- a/tests-integration/tests/kernel_orch.rs +++ b/tests-integration/tests/kernel_orch.rs @@ -617,7 +617,6 @@ fn test_kernel_ibc_funds_only() { let vfs_osmosis = VFSContract::new(osmosis.clone()); let adodb_osmosis = ADODBContract::new(osmosis.clone()); let splitter_osmosis = SplitterContract::new(osmosis.clone()); - let economics_osmosis = EconomicsContract::new(osmosis.clone()); kernel_juno.upload().unwrap(); vfs_juno.upload().unwrap(); @@ -631,7 +630,7 @@ fn test_kernel_ibc_funds_only() { vfs_osmosis.upload().unwrap(); adodb_osmosis.upload().unwrap(); splitter_osmosis.upload().unwrap(); - economics_osmosis.upload().unwrap(); + let init_msg_juno = &InstantiateMsg { owner: None, chain_name: "juno".to_string(), @@ -754,17 +753,6 @@ fn test_kernel_ibc_funds_only() { ) .unwrap(); - economics_osmosis - .instantiate( - &os::economics::InstantiateMsg { - kernel_address: kernel_osmosis.address().unwrap().into_string(), - owner: None, - }, - None, - None, - ) - .unwrap(); - kernel_juno .execute( &ExecuteMsg::UpsertKeyAddress { @@ -775,16 +763,6 @@ fn test_kernel_ibc_funds_only() { ) .unwrap(); - kernel_osmosis - .execute( - &ExecuteMsg::UpsertKeyAddress { - key: "economics".to_string(), - value: economics_osmosis.address().unwrap().into_string(), - }, - None, - ) - .unwrap(); - adodb_osmosis .instantiate( &os::adodb::InstantiateMsg { @@ -809,6 +787,19 @@ fn test_kernel_ibc_funds_only() { ) .unwrap(); + adodb_osmosis + .execute( + &os::adodb::ExecuteMsg::Publish { + code_id: 6, + ado_type: "economics".to_string(), + action_fees: None, + version: "1.1.1".to_string(), + publisher: None, + }, + None, + ) + .unwrap(); + kernel_juno .execute( &ExecuteMsg::UpsertKeyAddress { @@ -819,6 +810,16 @@ fn test_kernel_ibc_funds_only() { ) .unwrap(); + kernel_juno + .execute( + &ExecuteMsg::UpsertKeyAddress { + key: "adodb".to_string(), + value: adodb_juno.address().unwrap().into_string(), + }, + None, + ) + .unwrap(); + kernel_osmosis .execute( &ExecuteMsg::UpsertKeyAddress { From c95f460df0879433f9dc04427188b6fd65ef7011 Mon Sep 17 00:00:00 2001 From: Joe Monem Date: Wed, 27 Nov 2024 11:02:13 +0200 Subject: [PATCH 6/9] ref: disallow cw20 rates with cross-chain recipients --- packages/std/src/ado_base/rates.rs | 20 ++++++++++++++++++++ packages/std/src/error.rs | 3 +++ 2 files changed, 23 insertions(+) diff --git a/packages/std/src/ado_base/rates.rs b/packages/std/src/ado_base/rates.rs index 230ff49bf..b4fe22e15 100644 --- a/packages/std/src/ado_base/rates.rs +++ b/packages/std/src/ado_base/rates.rs @@ -83,6 +83,20 @@ pub enum LocalRateValue { Flat(Coin), } impl LocalRateValue { + /// Used to see if the denom is potentially a cw20 address, if it is, it cannot be paired with a cross-chain recipient + pub fn is_valid_address(&self, deps: Deps) -> Result { + match self { + LocalRateValue::Flat(coin) => { + let denom = coin.denom.clone(); + let is_valid_address = deps.api.addr_validate(denom.as_str()); + match is_valid_address { + Ok(_) => Ok(true), + Err(_) => Ok(false), + } + } + LocalRateValue::Percent(_) => Ok(false), + } + } pub fn validate(&self, deps: Deps) -> Result<(), ContractError> { match self { // If it's a coin, make sure it's non-zero @@ -247,6 +261,12 @@ impl Rate { } } Rate::Local(local_rate) => { + if local_rate.recipient.is_cross_chain() { + ensure!( + !local_rate.value.is_valid_address(deps)?, + ContractError::InvalidCw20CrossChainRate {} + ); + } // Validate the local rate value local_rate.value.validate(deps)?; Ok(()) diff --git a/packages/std/src/error.rs b/packages/std/src/error.rs index 2d652287f..583c5f8a9 100644 --- a/packages/std/src/error.rs +++ b/packages/std/src/error.rs @@ -61,6 +61,9 @@ pub enum ContractError { #[error("NoDenomInfoProvided")] NoDenomInfoProvided {}, + #[error("Cannot assign cw20 rate to cross-chain recipient")] + InvalidCw20CrossChainRate {}, + #[error("InvalidAmount: {msg}")] InvalidAmount { msg: String }, From 471f6f402813828ecb7fbe7f4cceb812535065d2 Mon Sep 17 00:00:00 2001 From: Joe Monem Date: Wed, 27 Nov 2024 11:04:43 +0200 Subject: [PATCH 7/9] ref: apply previous change to the rates contract --- contracts/modules/andromeda-rates/src/contract.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/contracts/modules/andromeda-rates/src/contract.rs b/contracts/modules/andromeda-rates/src/contract.rs index e6a5883dd..804d7d935 100644 --- a/contracts/modules/andromeda-rates/src/contract.rs +++ b/contracts/modules/andromeda-rates/src/contract.rs @@ -88,6 +88,12 @@ fn execute_set_rate( ADOContract::default().is_contract_owner(deps.storage, info.sender.as_str())?, ContractError::Unauthorized {} ); + if rate.recipient.is_cross_chain() { + ensure!( + !rate.value.is_valid_address(deps.as_ref())?, + ContractError::InvalidCw20CrossChainRate {} + ); + } // Validate the local rate's value rate.value.validate(deps.as_ref())?; From 040cdfe7d8aa00ba204f5e6e8746669a91beacf4 Mon Sep 17 00:00:00 2001 From: Joe Monem Date: Wed, 27 Nov 2024 11:08:33 +0200 Subject: [PATCH 8/9] ref: simplify rates validation --- .../modules/andromeda-rates/src/contract.rs | 9 +------- packages/std/src/ado_base/rates.rs | 21 ++++++++++++------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/contracts/modules/andromeda-rates/src/contract.rs b/contracts/modules/andromeda-rates/src/contract.rs index 804d7d935..c59b4aaac 100644 --- a/contracts/modules/andromeda-rates/src/contract.rs +++ b/contracts/modules/andromeda-rates/src/contract.rs @@ -88,14 +88,7 @@ fn execute_set_rate( ADOContract::default().is_contract_owner(deps.storage, info.sender.as_str())?, ContractError::Unauthorized {} ); - if rate.recipient.is_cross_chain() { - ensure!( - !rate.value.is_valid_address(deps.as_ref())?, - ContractError::InvalidCw20CrossChainRate {} - ); - } - // Validate the local rate's value - rate.value.validate(deps.as_ref())?; + rate.validate(deps.as_ref())?; RATES.save(deps.storage, &action, &rate)?; diff --git a/packages/std/src/ado_base/rates.rs b/packages/std/src/ado_base/rates.rs index b4fe22e15..52e94c145 100644 --- a/packages/std/src/ado_base/rates.rs +++ b/packages/std/src/ado_base/rates.rs @@ -148,6 +148,18 @@ pub struct LocalRate { pub value: LocalRateValue, pub description: Option, } +impl LocalRate { + pub fn validate(&self, deps: Deps) -> Result<(), ContractError> { + if self.recipient.is_cross_chain() { + ensure!( + !self.value.is_valid_address(deps)?, + ContractError::InvalidCw20CrossChainRate {} + ); + } + self.value.validate(deps)?; + Ok(()) + } +} // Created this because of the very complex return value warning. type LocalRateResponse = (Vec, Vec, Vec); @@ -261,14 +273,7 @@ impl Rate { } } Rate::Local(local_rate) => { - if local_rate.recipient.is_cross_chain() { - ensure!( - !local_rate.value.is_valid_address(deps)?, - ContractError::InvalidCw20CrossChainRate {} - ); - } - // Validate the local rate value - local_rate.value.validate(deps)?; + local_rate.validate(deps)?; Ok(()) } } From aabea26ff6bf61b45cd37c16825c11f563d9b50d Mon Sep 17 00:00:00 2001 From: Joe Monem Date: Wed, 27 Nov 2024 11:16:52 +0200 Subject: [PATCH 9/9] ref: ensure native fees for cross chain rates recipient --- packages/std/src/ado_base/rates.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/std/src/ado_base/rates.rs b/packages/std/src/ado_base/rates.rs index 52e94c145..0c69bcc46 100644 --- a/packages/std/src/ado_base/rates.rs +++ b/packages/std/src/ado_base/rates.rs @@ -200,6 +200,7 @@ impl LocalRate { .to_string(), ); let msg = if self.recipient.is_cross_chain() { + ensure!(is_native, ContractError::InvalidCw20CrossChainRate {}); // Create a cross chain message to be sent to the kernel let kernel_address = ADOContract::default().get_kernel_address(deps.storage)?; let kernel_msg = crate::os::kernel::ExecuteMsg::Send {