Skip to content
This repository has been archived by the owner on Oct 30, 2023. It is now read-only.

Commit

Permalink
Feature/add cw20 send api (#370)
Browse files Browse the repository at this point in the history
* Added cw20 send api

* Format

* Changed documentation
  • Loading branch information
Kayanski committed Jun 13, 2023
1 parent e894b71 commit 2bcc9e5
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/abstract-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ The API can then be used by any contract that implements its required traits, in
# }
use abstract_sdk::TransferInterface;
fn forward_deposit(deps: Deps, my_contract: MyContract, message_info: MessageInfo) -> AbstractSdkResult<AccountAction> {
fn forward_deposit(deps: Deps, my_contract: MyContract, message_info: MessageInfo) -> AbstractSdkResult<Vec<CosmosMsg>> {
let forward_deposit_msg = my_contract.bank(deps).deposit(message_info.funds)?;
Ok(forward_deposit_msg)
Expand Down
75 changes: 75 additions & 0 deletions packages/abstract-sdk/src/apis/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ use crate::features::AccountIdentification;
use crate::AccountAction;
use crate::{ans_resolve::Resolve, features::AbstractNameService, AbstractSdkResult};
use core::objects::{AnsAsset, AssetEntry};
use cosmwasm_std::to_binary;
use cosmwasm_std::{Addr, Coin, CosmosMsg, Deps, Env};
use cw_asset::Asset;
use serde::Serialize;

/// Query and Transfer assets from and to the Abstract Account.
pub trait TransferInterface: AbstractNameService + AccountIdentification {
Expand Down Expand Up @@ -148,6 +150,24 @@ impl<'a, T: TransferInterface> Bank<'a, T> {
let recipient = &env.contract.address;
self.transfer(funds, recipient)
}

/// Move cw20 assets from the Account to a recipient with the possibility using the cw20 send/receive hook
///
/// Note: **Native coins are NOT and will NEVER be supported by this method**.
///
/// In order to send funds with your message, you need to construct the message yourself
pub fn send<R: Transferable, M: Serialize>(
&self,
funds: R,
recipient: &Addr,
message: &M,
) -> AbstractSdkResult<AccountAction> {
let transferable_funds = funds.transferable_asset(self.base, self.deps)?;

let msgs = transferable_funds.send_msg(recipient, to_binary(message)?)?;

Ok(AccountAction::from_vec(vec![msgs]))
}
}

/// Turn an object that represents an asset into the blockchain representation of an asset, i.e. [`Asset`].
Expand Down Expand Up @@ -279,4 +299,59 @@ mod test {
assert_that!(actual_res.unwrap().messages()[0]).is_equal_to::<CosmosMsg>(expected_msg);
}
}

mod send_coins {
use cw20::Cw20ExecuteMsg;
use cw_asset::AssetError;

use crate::AbstractSdkError;

use super::*;

#[test]
fn send_cw20() {
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 hook_msg = Empty {};
let asset = Addr::unchecked("asset");
let coin = Asset::cw20(asset.clone(), expected_amount);
let actual_res = bank.send(coin, &expected_recipient, &hook_msg);

let expected_msg: CosmosMsg = CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: asset.to_string(),
msg: to_binary(&Cw20ExecuteMsg::Send {
contract: expected_recipient.to_string(),
amount: expected_amount.into(),
msg: to_binary(&hook_msg).unwrap(),
})
.unwrap(),
funds: vec![],
});

assert_that!(actual_res.unwrap().messages()[0]).is_equal_to::<CosmosMsg>(expected_msg);
}

#[test]
fn send_coins() {
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 coin = coin(expected_amount, "asset");
let hook_msg = Empty {};
let actual_res = bank.send(coin, &expected_recipient, &hook_msg);

assert_that!(actual_res.unwrap_err()).is_equal_to::<AbstractSdkError>(
AbstractSdkError::Asset(AssetError::UnavailableMethodForNative {
method: "send".into(),
}),
);
}
}
}

0 comments on commit 2bcc9e5

Please sign in to comment.