From 05f6eaa210243b254b4c6d2997b99d45da21cac3 Mon Sep 17 00:00:00 2001 From: Mauro Lacy Date: Thu, 28 Sep 2023 09:46:15 +0200 Subject: [PATCH] Add ensure_authorized helper --- contracts/consumer/converter/src/contract.rs | 23 +++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/contracts/consumer/converter/src/contract.rs b/contracts/consumer/converter/src/contract.rs index ce8f4aa8..d384961a 100644 --- a/contracts/consumer/converter/src/contract.rs +++ b/contracts/consumer/converter/src/contract.rs @@ -1,6 +1,6 @@ use cosmwasm_std::{ ensure_eq, to_binary, Addr, BankMsg, Coin, CosmosMsg, Decimal, Deps, DepsMut, Event, IbcMsg, - Reply, Response, SubMsg, SubMsgResponse, Validator, WasmMsg, + MessageInfo, Reply, Response, SubMsg, SubMsgResponse, Validator, WasmMsg, }; use cw2::set_contract_version; use cw_storage_plus::Item; @@ -266,6 +266,13 @@ impl ConverterContract<'_> { }; Ok(msg.into()) } + + fn ensure_authorized(&self, deps: &DepsMut, info: &MessageInfo) -> Result<(), ContractError> { + let virtual_stake = self.virtual_stake.load(deps.storage)?; + ensure_eq!(info.sender, virtual_stake, ContractError::Unauthorized {}); + + Ok(()) + } } #[contract] @@ -339,12 +346,7 @@ impl ConverterApi for ConverterContract<'_> { additions: Vec, tombstones: Vec, ) -> Result { - let virtual_stake = self.virtual_stake.load(ctx.deps.storage)?; - ensure_eq!( - ctx.info.sender, - virtual_stake, - ContractError::Unauthorized {} - ); + self.ensure_authorized(&ctx.deps, &ctx.info)?; // Send over IBC to the Consumer let channel = IBC_CHANNEL.load(ctx.deps.storage)?; @@ -387,12 +389,7 @@ impl ConverterApi for ConverterContract<'_> { time: u64, tombstone: bool, ) -> Result { - let virtual_stake = self.virtual_stake.load(ctx.deps.storage)?; - ensure_eq!( - ctx.info.sender, - virtual_stake, - ContractError::Unauthorized {} - ); + self.ensure_authorized(&ctx.deps, &ctx.info)?; // Send over IBC to the Consumer let channel = IBC_CHANNEL.load(ctx.deps.storage)?;