Skip to content

Commit

Permalink
Aliasing anyhow::Result to AnyResult in multitest
Browse files Browse the repository at this point in the history
  • Loading branch information
hashedone committed Aug 19, 2021
1 parent 163b345 commit 267ad38
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 110 deletions.
16 changes: 8 additions & 8 deletions packages/multi-test/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::executor::{AppResponse, Executor};
use crate::transactions::transactional;
use crate::wasm::{ContractData, Wasm, WasmKeeper};

use anyhow::Result;
use anyhow::Result as AnyResult;

pub fn next_block(block: &mut BlockInfo) {
block.time = block.time.plus_seconds(5);
Expand Down Expand Up @@ -53,7 +53,7 @@ impl<C> Executor<C> for App<C>
where
C: Clone + fmt::Debug + PartialEq + JsonSchema + 'static,
{
fn execute(&mut self, sender: Addr, msg: CosmosMsg<C>) -> Result<AppResponse> {
fn execute(&mut self, sender: Addr, msg: CosmosMsg<C>) -> AnyResult<AppResponse> {
let mut all = self.execute_multi(sender, vec![msg])?;
let res = all.pop().unwrap();
Ok(res)
Expand Down Expand Up @@ -105,7 +105,7 @@ where
&mut self,
sender: Addr,
msgs: Vec<CosmosMsg<C>>,
) -> Result<Vec<AppResponse>> {
) -> AnyResult<Vec<AppResponse>> {
// we need to do some caching of storage here, once in the entry point:
// meaning, wrap current state, all writes go to a cache, only when execute
// returns a success do we flush it (otherwise drop it)
Expand All @@ -125,7 +125,7 @@ where
}

/// This is an "admin" function to let us adjust bank accounts
pub fn init_bank_balance(&mut self, account: &Addr, amount: Vec<Coin>) -> Result<()> {
pub fn init_bank_balance(&mut self, account: &Addr, amount: Vec<Coin>) -> AnyResult<()> {
self.router
.bank
.init_balance(&mut *self.storage, account, amount)
Expand All @@ -138,7 +138,7 @@ where
}

/// This allows to get `ContractData` for specific contract
pub fn contract_data(&self, address: &Addr) -> Result<ContractData> {
pub fn contract_data(&self, address: &Addr) -> AnyResult<ContractData> {
self.router.wasm.contract_data(&*self.storage, address)
}

Expand All @@ -149,7 +149,7 @@ where
&mut self,
contract_addr: U,
msg: &T,
) -> Result<AppResponse> {
) -> AnyResult<AppResponse> {
let msg = to_vec(msg)?;
self.router.wasm.sudo(
&*self.api,
Expand Down Expand Up @@ -201,7 +201,7 @@ where
storage: &dyn Storage,
block: &BlockInfo,
request: QueryRequest<Empty>,
) -> Result<Binary> {
) -> AnyResult<Binary> {
match request {
QueryRequest::Wasm(req) => {
self.wasm
Expand All @@ -219,7 +219,7 @@ where
block: &BlockInfo,
sender: Addr,
msg: CosmosMsg<C>,
) -> Result<AppResponse> {
) -> AnyResult<AppResponse> {
match msg {
CosmosMsg::Wasm(msg) => self.wasm.execute(api, storage, &self, block, sender, msg),
CosmosMsg::Bank(msg) => self.bank.execute(storage, sender, msg),
Expand Down
32 changes: 18 additions & 14 deletions packages/multi-test/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use cw0::NativeBalance;
use cw_storage_plus::Map;
use itertools::Itertools;

use anyhow::{bail, Error, Result};
use anyhow::{bail, Result as AnyResult};

const BALANCES: Map<&Addr, NativeBalance> = Map::new("balances");

Expand All @@ -18,18 +18,22 @@ pub const NAMESPACE_BANK: &[u8] = b"bank";
/// Bank is a minimal contract-like interface that implements a bank module
/// It is initialized outside of the trait
pub trait Bank {
fn execute(&self, storage: &mut dyn Storage, sender: Addr, msg: BankMsg)
-> Result<AppResponse>;
fn execute(
&self,
storage: &mut dyn Storage,
sender: Addr,
msg: BankMsg,
) -> AnyResult<AppResponse>;

fn query(&self, api: &dyn Api, storage: &dyn Storage, request: BankQuery) -> Result<Binary>;
fn query(&self, api: &dyn Api, storage: &dyn Storage, request: BankQuery) -> AnyResult<Binary>;

// Admin interface
fn init_balance(
&self,
storage: &mut dyn Storage,
account: &Addr,
amount: Vec<Coin>,
) -> Result<()>;
) -> AnyResult<()>;
}

#[derive(Default)]
Expand All @@ -45,16 +49,16 @@ impl BankKeeper {
bank_storage: &mut dyn Storage,
account: &Addr,
amount: Vec<Coin>,
) -> Result<()> {
) -> AnyResult<()> {
let mut balance = NativeBalance(amount);
balance.normalize();
BALANCES
.save(bank_storage, account, &balance)
.map_err(Error::from)
.map_err(Into::into)
}

// this is an "admin" function to let us adjust bank accounts
fn get_balance(&self, bank_storage: &dyn Storage, account: &Addr) -> Result<Vec<Coin>> {
fn get_balance(&self, bank_storage: &dyn Storage, account: &Addr) -> AnyResult<Vec<Coin>> {
let val = BALANCES.may_load(bank_storage, &account)?;
Ok(val.unwrap_or_default().into_vec())
}
Expand All @@ -65,7 +69,7 @@ impl BankKeeper {
from_address: Addr,
to_address: Addr,
amount: Vec<Coin>,
) -> Result<()> {
) -> AnyResult<()> {
self.burn(bank_storage, from_address, amount.clone())?;
self.mint(bank_storage, to_address, amount)
}
Expand All @@ -75,7 +79,7 @@ impl BankKeeper {
bank_storage: &mut dyn Storage,
to_address: Addr,
amount: Vec<Coin>,
) -> Result<()> {
) -> AnyResult<()> {
let b = self.get_balance(bank_storage, &to_address)?;
let b = NativeBalance(b) + NativeBalance(amount);
self.set_balance(bank_storage, &to_address, b.into_vec())
Expand All @@ -86,7 +90,7 @@ impl BankKeeper {
bank_storage: &mut dyn Storage,
from_address: Addr,
amount: Vec<Coin>,
) -> Result<()> {
) -> AnyResult<()> {
let a = self.get_balance(bank_storage, &from_address)?;
let a = (NativeBalance(a) - amount)?;
self.set_balance(bank_storage, &from_address, a.into_vec())
Expand All @@ -106,7 +110,7 @@ impl Bank for BankKeeper {
storage: &mut dyn Storage,
sender: Addr,
msg: BankMsg,
) -> Result<AppResponse> {
) -> AnyResult<AppResponse> {
let mut bank_storage = prefixed(storage, NAMESPACE_BANK);
match msg {
BankMsg::Send { to_address, amount } => {
Expand All @@ -132,7 +136,7 @@ impl Bank for BankKeeper {
}
}

fn query(&self, api: &dyn Api, storage: &dyn Storage, request: BankQuery) -> Result<Binary> {
fn query(&self, api: &dyn Api, storage: &dyn Storage, request: BankQuery) -> AnyResult<Binary> {
let bank_storage = prefixed_read(storage, NAMESPACE_BANK);
match request {
BankQuery::AllBalances { address } => {
Expand Down Expand Up @@ -161,7 +165,7 @@ impl Bank for BankKeeper {
storage: &mut dyn Storage,
account: &Addr,
amount: Vec<Coin>,
) -> Result<()> {
) -> AnyResult<()> {
let mut bank_storage = prefixed(storage, NAMESPACE_BANK);
self.set_balance(&mut bank_storage, account, amount)
}
Expand Down
Loading

0 comments on commit 267ad38

Please sign in to comment.