Skip to content

Commit

Permalink
send enabled check
Browse files Browse the repository at this point in the history
  • Loading branch information
NYBACHOK committed Oct 11, 2024
1 parent b91659c commit 9ab409d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 35 deletions.
2 changes: 2 additions & 0 deletions gears/src/x/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ pub enum BankKeeperError {
AccountPermission,
#[error("{0} is not allowed to receive funds. Probably tried send to module as account")]
Blocked(AccAddress),
#[error("Send disabled for denom: {0}")]
SendDisabled(Denom),
#[error("{0}")]
GasError(#[from] GasStoreErrors),
}
Expand Down
32 changes: 32 additions & 0 deletions x/bank/src/keeper/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ impl<
amount,
}: MsgSend,
) -> Result<(), BankKeeperError> {
if let Some(denom) = self.is_send_enabled_for(ctx, amount.inner())? {
Err(BankKeeperError::SendDisabled(denom.clone()))?
}

let mut events = vec![];

for send_coin in amount.inner() {
Expand Down Expand Up @@ -669,6 +673,34 @@ impl<
Err(_) => Ok((None, total, pagination)),
}
}

fn is_send_enabled_for<'a, DB: Database, CTX: QueryableContext<DB, SK>>(
&self,
ctx: &CTX,
coins: impl IntoIterator<Item = &'a UnsignedCoin>,
) -> Result<Option<&'a Denom>, GasStoreErrors> {
let BankParams {
send_enabled,
default_send_enabled,
} = self.bank_params_keeper.try_get(ctx)?;

let send_enabled = send_enabled
.into_iter()
.map(|this| (this.denom, this.enabled))
.collect::<HashMap<_, _>>();
for UnsignedCoin { denom, amount: _ } in coins {
let enabled = send_enabled
.get(denom)
.map(bool::clone)
.unwrap_or(default_send_enabled);

if !enabled {
return Ok(Some(denom));
}
}

Ok(None)
}
}

//TODO: copy tests across
35 changes: 0 additions & 35 deletions x/bank/src/params/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
use gears::application::keepers::params::ParamsKeeper;
use gears::context::{InfallibleContext, QueryableContext};
use gears::derive::Protobuf;
use gears::extensions::corruption::UnwrapCorrupt;
use gears::params::{ParamKind, ParamsDeserialize, ParamsSerialize, ParamsSubspaceKey};
use gears::store::database::Database;
use gears::store::StoreKey;
use gears::types::denom::Denom;
use gears::types::store::gas::errors::GasStoreErrors;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};

Expand Down Expand Up @@ -94,37 +90,6 @@ pub struct BankParamsKeeper<PSK: ParamsSubspaceKey> {
pub params_subspace_key: PSK,
}

impl<PSK: ParamsSubspaceKey> BankParamsKeeper<PSK> {
pub fn send_enabled<DB: Database, SK: StoreKey, CTX: InfallibleContext<DB, SK>>(
&self,
ctx: &CTX,
denom: &Denom,
) -> Option<bool> {
self.get(ctx)
.send_enabled
.into_iter()
.find_map(|this| match denom == &this.denom {
true => Some(this.enabled),
false => None,
})
}

pub fn try_send_enabled<DB: Database, SK: StoreKey, CTX: QueryableContext<DB, SK>>(
&self,
ctx: &CTX,
denom: &Denom,
) -> Result<Option<bool>, GasStoreErrors> {
Ok(self
.try_get(ctx)?
.send_enabled
.into_iter()
.find_map(|this| match denom == &this.denom {
true => Some(this.enabled),
false => None,
}))
}
}

impl<PSK: ParamsSubspaceKey> ParamsKeeper<PSK> for BankParamsKeeper<PSK> {
type Param = BankParams;

Expand Down

0 comments on commit 9ab409d

Please sign in to comment.