Skip to content

Commit

Permalink
Add ensure_authorized helper
Browse files Browse the repository at this point in the history
  • Loading branch information
maurolacy committed Sep 28, 2023
1 parent 15ac523 commit 05f6eaa
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions contracts/consumer/converter/src/contract.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -339,12 +346,7 @@ impl ConverterApi for ConverterContract<'_> {
additions: Vec<Validator>,
tombstones: Vec<Validator>,
) -> Result<Response, Self::Error> {
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)?;
Expand Down Expand Up @@ -387,12 +389,7 @@ impl ConverterApi for ConverterContract<'_> {
time: u64,
tombstone: bool,
) -> Result<Response, Self::Error> {
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)?;
Expand Down

0 comments on commit 05f6eaa

Please sign in to comment.