1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
//! # Bank
//! The Bank object handles asset transfers to and from the Account.
use crate::features::AccountIdentification;
use crate::AccountAction;
use crate::{ans_resolve::Resolve, features::AbstractNameService, AbstractSdkResult};
use core::objects::{AnsAsset, AssetEntry};
use cosmwasm_std::{Addr, Coin, CosmosMsg, Deps, Env};
use cw_asset::Asset;
/// Query and Transfer assets from and to the Abstract Account.
pub trait TransferInterface: AbstractNameService + AccountIdentification {
/**
API for transferring funds to and from the account.
# Example
```
use abstract_sdk::prelude::*;
# use cosmwasm_std::testing::mock_dependencies;
# use abstract_sdk::mock_module::MockModule;
# let module = MockModule::new();
# let deps = mock_dependencies();
let bank: Bank<MockModule> = module.bank(deps.as_ref());
```
*/
fn bank<'a>(&'a self, deps: Deps<'a>) -> Bank<Self> {
Bank { base: self, deps }
}
}
impl<T> TransferInterface for T where T: AbstractNameService + AccountIdentification {}
/**
API for transferring funds to and from the account.
# Example
```
use abstract_sdk::prelude::*;
# use cosmwasm_std::testing::mock_dependencies;
# use abstract_sdk::mock_module::MockModule;
# let module = MockModule::new();
# let deps = mock_dependencies();
let bank: Bank<MockModule> = module.bank(deps.as_ref());
```
*/
#[derive(Clone)]
pub struct Bank<'a, T: TransferInterface> {
base: &'a T,
deps: Deps<'a>,
}
impl<'a, T: TransferInterface> Bank<'a, T> {
/// Get the balances of the provided assets.
pub fn balances(&self, assets: &[AssetEntry]) -> AbstractSdkResult<Vec<Asset>> {
assets
.iter()
.map(|asset| self.balance(asset))
.collect::<AbstractSdkResult<Vec<Asset>>>()
}
/// Get the balance of the provided asset.
pub fn balance(&self, asset: &AssetEntry) -> AbstractSdkResult<Asset> {
let resolved_info = asset.resolve(&self.deps.querier, &self.base.ans_host(self.deps)?)?;
let balance =
resolved_info.query_balance(&self.deps.querier, self.base.proxy_address(self.deps)?)?;
Ok(Asset::new(resolved_info, balance))
}
/// Transfer the provided funds from the Account to the recipient.
/// ```
/// # use cosmwasm_std::{Addr, Response, Deps, DepsMut, MessageInfo};
/// # use abstract_core::objects::AnsAsset;
/// # use abstract_core::objects::ans_host::AnsHost;
/// # use abstract_sdk::{
/// # features::{AccountIdentification, AbstractNameService, ModuleIdentification},
/// # TransferInterface, AbstractSdkResult, Execution,
/// # };
/// # struct MockModule;
/// # impl AccountIdentification for MockModule {
/// # fn proxy_address(&self, _deps: Deps) -> AbstractSdkResult<Addr> {
/// # unimplemented!("Not needed for this example")
/// # }
/// # }
/// #
/// # impl ModuleIdentification for MockModule {
/// # fn module_id(&self) -> &'static str {
/// # "mock_module"
/// # }
/// # }
/// #
/// # impl AbstractNameService for MockModule {
/// # fn ans_host(&self, _deps: Deps) -> AbstractSdkResult<AnsHost> {
/// # unimplemented!("Not needed for this example")
/// # }
/// # }
/// fn transfer_asset_to_sender(app: MockModule, deps: DepsMut, info: MessageInfo, requested_asset: AnsAsset) -> AbstractSdkResult<Response> {
/// let bank = app.bank(deps.as_ref());
/// let executor = app.executor(deps.as_ref());
/// let transfer_action = bank.transfer(vec![requested_asset.clone()], &info.sender)?;
///
/// let transfer_msg = executor.execute(vec![transfer_action])?;
///
/// Ok(Response::new()
/// .add_message(transfer_msg)
/// .add_attribute("recipient", info.sender)
/// .add_attribute("asset_sent", requested_asset.to_string()))
/// }
/// ```
pub fn transfer<R: Transferable>(
&self,
funds: Vec<R>,
recipient: &Addr,
) -> AbstractSdkResult<AccountAction> {
let transferable_funds = funds
.into_iter()
.map(|asset| asset.transferable_asset(self.base, self.deps))
.collect::<AbstractSdkResult<Vec<Asset>>>()?;
transferable_funds
.iter()
.map(|asset| asset.transfer_msg(recipient.clone()))
.collect::<Result<Vec<_>, _>>()
.map_err(Into::into)
.map(Into::into)
}
/// Move funds from the contract into the Account.
pub fn deposit<R: Transferable>(&self, funds: Vec<R>) -> AbstractSdkResult<AccountAction> {
let recipient = self.base.proxy_address(self.deps)?;
let transferable_funds = funds
.into_iter()
.map(|asset| asset.transferable_asset(self.base, self.deps))
.collect::<AbstractSdkResult<Vec<Asset>>>()?;
transferable_funds
.iter()
.map(|asset| asset.transfer_msg(recipient.clone()))
.collect::<Result<Vec<_>, _>>()
.map_err(Into::into)
.map(Into::into)
}
/// Withdraw funds from the Account to this contract.
pub fn withdraw<R: Transferable>(
&self,
env: &Env,
funds: Vec<R>,
) -> AbstractSdkResult<AccountAction> {
let recipient = &env.contract.address;
let transferable_funds = funds
.into_iter()
.map(|asset| asset.transferable_asset(self.base, self.deps))
.collect::<AbstractSdkResult<Vec<Asset>>>()?;
transferable_funds
.iter()
.map(|asset| asset.transfer_msg(recipient.clone()))
.collect::<Result<Vec<CosmosMsg>, _>>()
.map_err(Into::into)
.map(Into::into)
}
}
/// Turn an object that represents an asset into the blockchain representation of an asset, i.e. [`Asset`].
pub trait Transferable {
/// Turn an object that represents an asset into the blockchain representation of an asset, i.e. [`Asset`].
fn transferable_asset<T: AbstractNameService>(
self,
base: &T,
deps: Deps,
) -> AbstractSdkResult<Asset>;
}
impl Transferable for &AnsAsset {
fn transferable_asset<T: AbstractNameService>(
self,
base: &T,
deps: Deps,
) -> AbstractSdkResult<Asset> {
self.resolve(&deps.querier, &base.ans_host(deps)?)
}
}
impl Transferable for AnsAsset {
fn transferable_asset<T: AbstractNameService>(
self,
base: &T,
deps: Deps,
) -> AbstractSdkResult<Asset> {
self.resolve(&deps.querier, &base.ans_host(deps)?)
}
}
impl Transferable for Asset {
fn transferable_asset<T: AbstractNameService>(
self,
_base: &T,
_deps: Deps,
) -> AbstractSdkResult<Asset> {
Ok(self)
}
}
impl Transferable for Coin {
fn transferable_asset<T: AbstractNameService>(
self,
_base: &T,
_deps: Deps,
) -> AbstractSdkResult<Asset> {
Ok(Asset::from(self))
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::mock_module::*;
use abstract_testing::prelude::*;
use cosmwasm_std::{testing::*, *};
use speculoos::prelude::*;
mod transfer_coins {
use super::*;
#[test]
fn transfer_asset_to_sender() {
let app = MockModule::new();
let deps = mock_dependencies();
let expected_amount = 100u128;
let expected_recipient = Addr::unchecked("recipient");
let bank = app.bank(deps.as_ref());
let coins = coins(expected_amount, "asset");
let actual_res = bank.transfer(coins.clone(), &expected_recipient);
assert_that!(actual_res).is_ok();
let expected_msg = CosmosMsg::Bank(BankMsg::Send {
to_address: expected_recipient.to_string(),
amount: coins,
});
assert_that!(actual_res.unwrap().messages()[0]).is_equal_to(&expected_msg);
}
}
// transfer must be tested via integration test
mod deposit {
use super::*;
#[test]
fn deposit() {
let app = MockModule::new();
let deps = mock_dependencies();
let expected_amount = 100u128;
let bank = app.bank(deps.as_ref());
let coins = coins(expected_amount, "asset");
let actual_res = bank.deposit(coins.clone()).unwrap().messages()[0].clone();
let expected_msg: CosmosMsg = CosmosMsg::Bank(BankMsg::Send {
to_address: TEST_PROXY.to_string(),
amount: coins,
});
assert_that!(actual_res).is_equal_to::<CosmosMsg>(expected_msg);
}
}
mod withdraw_coins {
use super::*;
#[test]
fn withdraw_coins() {
let app = MockModule::new();
let deps = mock_dependencies();
let expected_amount = 100u128;
let env = mock_env();
let bank = app.bank(deps.as_ref());
let coins = coins(expected_amount, "asset");
let actual_res = bank.withdraw(&env, coins.clone());
let expected_msg: CosmosMsg = CosmosMsg::Bank(BankMsg::Send {
to_address: env.contract.address.to_string(),
amount: coins,
});
assert_that!(actual_res.unwrap().messages()[0]).is_equal_to::<CosmosMsg>(expected_msg);
}
}
}