Skip to content

Commit

Permalink
test: claim with tax and royalty in auction
Browse files Browse the repository at this point in the history
  • Loading branch information
joemonem committed May 9, 2024
1 parent c4912c6 commit 961a526
Show file tree
Hide file tree
Showing 2 changed files with 208 additions and 14 deletions.
11 changes: 5 additions & 6 deletions contracts/non-fungible-tokens/andromeda-auction/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ fn execute_claim(
}

// Calculate the funds to be received after tax
let after_tax_payment =
let (after_tax_payment, tax_messages) =
purchase_token(deps.as_ref(), &info, token_auction_state.clone(), action)?;

let resp: Response = Response::new()
Expand All @@ -778,11 +778,11 @@ fn execute_claim(
&deps.as_ref(),
Cw20Coin {
address: auction_currency.clone(),
amount: after_tax_payment.0.amount,
amount: after_tax_payment.amount,
},
)?;
// After tax payment is returned in Native, we need to change it to cw20
let (tax_recipient, tax_amount) = match after_tax_payment.1.first().map(|msg| {
let (tax_recipient, tax_amount) = match tax_messages.first().map(|msg| {
if let CosmosMsg::Bank(BankMsg::Send { to_address, amount }) = &msg.msg {
(
Some(to_address.clone()),
Expand Down Expand Up @@ -833,11 +833,10 @@ fn execute_claim(
.recipient
.unwrap_or(Recipient::from_string(token_auction_state.clone().owner));

let msg =
recipient.generate_direct_msg(&deps.as_ref(), vec![after_tax_payment.clone().0])?;
let msg = recipient.generate_direct_msg(&deps.as_ref(), vec![after_tax_payment.clone()])?;

Ok(resp
.add_submessages(after_tax_payment.1)
.add_submessages(tax_messages)
// Send native funds to the original owner or recipient (if provided).
.add_submessage(msg)
// Send NFT to auction winner.
Expand Down
211 changes: 203 additions & 8 deletions contracts/non-fungible-tokens/andromeda-auction/src/testing/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use andromeda_non_fungible_tokens::{
use andromeda_std::{
ado_base::{
modules::Module,
rates::{LocalRate, LocalRateType, LocalRateValue, Rate},
rates::{LocalRate, LocalRateType, LocalRateValue, PercentRate, Rate},
},
ado_contract::ADOContract,
amp::AndrAddr,
Expand All @@ -34,8 +34,8 @@ use andromeda_std::{amp::Recipient, testing::mock_querier::MOCK_CW20_CONTRACT};
use cosmwasm_std::{
attr, coin, coins, from_json,
testing::{mock_dependencies, mock_env, mock_info},
to_json_binary, Addr, BankMsg, CosmosMsg, Deps, DepsMut, Env, Response, SubMsg, Timestamp,
Uint128, WasmMsg,
to_json_binary, Addr, BankMsg, CosmosMsg, Decimal, Deps, DepsMut, Env, Response, SubMsg,
Timestamp, Uint128, WasmMsg,
};
use cw20::{Cw20ExecuteMsg, Cw20ReceiveMsg};
use cw721::Cw721ReceiveMsg;
Expand Down Expand Up @@ -1142,25 +1142,26 @@ fn execute_claim_no_bids_cw20() {
}

#[test]
fn execute_claim() {
fn execute_claim_with_tax() {
let mut deps = mock_dependencies_custom(&[]);
let mut env = mock_env();
let _res = init(deps.as_mut());
let tax_recipient = "tax_recipient";

let rate = Rate::Local(LocalRate {
let rate: Rate = Rate::Local(LocalRate {
rate_type: LocalRateType::Additive,
recipients: vec![Recipient {
address: AndrAddr::from_string("owner".to_string()),
address: AndrAddr::from_string(tax_recipient.to_string()),
msg: None,
ibc_recovery_address: None,
}],
value: LocalRateValue::Flat(coin(100_u128, "uusd")),
value: LocalRateValue::Flat(coin(20_u128, "uusd")),
description: None,
});

// Set rates
ADOContract::default()
.set_rates(deps.as_mut().storage, "auction", rate)
.set_rates(deps.as_mut().storage, "AuctionClaim", rate)
.unwrap();

start_auction(deps.as_mut(), None, None);
Expand Down Expand Up @@ -1191,6 +1192,10 @@ fn execute_claim() {
};
assert_eq!(
Response::new()
.add_message(CosmosMsg::Bank(BankMsg::Send {
to_address: tax_recipient.to_owned(),
amount: coins(20, "uusd"),
}))
.add_message(CosmosMsg::Bank(BankMsg::Send {
to_address: MOCK_TOKEN_OWNER.to_owned(),
amount: coins(100, "uusd"),
Expand Down Expand Up @@ -1223,6 +1228,93 @@ fn execute_claim() {
);
}

#[test]
fn execute_claim_with_royalty() {
let mut deps = mock_dependencies_custom(&[]);
let mut env = mock_env();
let _res = init(deps.as_mut());
let royalty_recipient = "royalty_recipient";

let rate: Rate = Rate::Local(LocalRate {
rate_type: LocalRateType::Deductive,
recipients: vec![Recipient {
address: AndrAddr::from_string(royalty_recipient.to_string()),
msg: None,
ibc_recovery_address: None,
}],
value: LocalRateValue::Flat(coin(20_u128, "uusd")),
description: None,
});

// Set rates
ADOContract::default()
.set_rates(deps.as_mut().storage, "AuctionClaim", rate)
.unwrap();

start_auction(deps.as_mut(), None, None);

let msg = ExecuteMsg::PlaceBid {
token_id: MOCK_UNCLAIMED_TOKEN.to_owned(),
token_address: MOCK_TOKEN_ADDR.to_string(),
};

let info = mock_info("sender", &coins(100, "uusd".to_string()));
env.block.time = env.block.time.plus_seconds(1);

let _res = execute(deps.as_mut(), env.clone(), info, msg).unwrap();

// Auction ended by that time
env.block.time = env.block.time.plus_days(1);

let msg = ExecuteMsg::Claim {
token_id: MOCK_UNCLAIMED_TOKEN.to_owned(),
token_address: MOCK_TOKEN_ADDR.to_string(),
};

let info = mock_info("any_user", &[]);
let res = execute(deps.as_mut(), env, info, msg).unwrap();
let transfer_nft_msg = Cw721ExecuteMsg::TransferNft {
recipient: AndrAddr::from_string("sender".to_string()),
token_id: MOCK_UNCLAIMED_TOKEN.to_owned(),
};
assert_eq!(
Response::new()
.add_message(CosmosMsg::Bank(BankMsg::Send {
to_address: royalty_recipient.to_owned(),
amount: coins(20, "uusd"),
}))
.add_message(CosmosMsg::Bank(BankMsg::Send {
to_address: MOCK_TOKEN_OWNER.to_owned(),
amount: coins(80, "uusd"),
}))
.add_message(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: MOCK_TOKEN_ADDR.to_string(),
msg: encode_binary(&transfer_nft_msg).unwrap(),
funds: vec![],
}))
.add_attribute("action", "claim")
.add_attribute("token_id", MOCK_UNCLAIMED_TOKEN)
.add_attribute("token_contract", MOCK_TOKEN_ADDR)
.add_attribute("recipient", "sender")
.add_attribute("winning_bid_amount", Uint128::from(100u128))
.add_attribute("auction_id", "1")
// Economics message
.add_submessage(SubMsg::reply_on_error(
CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: "economics_contract".to_string(),
msg: to_json_binary(&EconomicsExecuteMsg::PayFee {
payee: Addr::unchecked("any_user"),
action: "Claim".to_string()
})
.unwrap(),
funds: vec![],
}),
ReplyId::PayFee.repr(),
)),
res
);
}

#[test]
fn execute_claim_cw20() {
let mut deps = mock_dependencies_custom(&[]);
Expand Down Expand Up @@ -1299,6 +1391,109 @@ fn execute_claim_cw20() {
);
}

#[test]
fn execute_claim_cw20_with_tax() {
let mut deps = mock_dependencies_custom(&[]);
let mut env = mock_env();
let _res = init_cw20(deps.as_mut(), None);
let tax_recipient = "tax_recipient";
let rate: Rate = Rate::Local(LocalRate {
rate_type: LocalRateType::Additive,
recipients: vec![Recipient {
address: AndrAddr::from_string(tax_recipient.to_string()),
msg: None,
ibc_recovery_address: None,
}],
value: LocalRateValue::Percent(PercentRate {
percent: Decimal::percent(20),
}),
description: None,
});

// Set rates
ADOContract::default()
.set_rates(deps.as_mut().storage, "AuctionClaim", rate)
.unwrap();

start_auction_cw20(deps.as_mut(), None, None);

let hook_msg = Cw20HookMsg::PlaceBid {
token_id: MOCK_UNCLAIMED_TOKEN.to_owned(),
token_address: MOCK_TOKEN_ADDR.to_string(),
};
let msg = ExecuteMsg::Receive(Cw20ReceiveMsg {
sender: "sender".to_string(),
amount: Uint128::new(100),
msg: encode_binary(&hook_msg).unwrap(),
});

let info = mock_info(MOCK_CW20_CONTRACT, &[]);
env.block.time = env.block.time.plus_seconds(1);

let _res = execute(deps.as_mut(), env.clone(), info, msg).unwrap();

// Auction ended by that time
env.block.time = env.block.time.plus_days(1);

let msg = ExecuteMsg::Claim {
token_id: MOCK_UNCLAIMED_TOKEN.to_owned(),
token_address: MOCK_TOKEN_ADDR.to_string(),
};

let info = mock_info("any_user", &[]);
let res = execute(deps.as_mut(), env, info, msg).unwrap();
let transfer_nft_msg = Cw721ExecuteMsg::TransferNft {
recipient: AndrAddr::from_string("sender".to_string()),
token_id: MOCK_UNCLAIMED_TOKEN.to_owned(),
};
assert_eq!(
Response::new()
.add_message(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: MOCK_CW20_CONTRACT.to_string(),
msg: encode_binary(&Cw20ExecuteMsg::Transfer {
recipient: tax_recipient.to_owned(),
amount: Uint128::new(20)
})
.unwrap(),
funds: vec![]
}))
.add_message(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: MOCK_CW20_CONTRACT.to_string(),
msg: encode_binary(&Cw20ExecuteMsg::Transfer {
recipient: MOCK_TOKEN_OWNER.to_owned(),
amount: Uint128::new(100)
})
.unwrap(),
funds: vec![]
}))
.add_message(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: MOCK_TOKEN_ADDR.to_string(),
msg: encode_binary(&transfer_nft_msg).unwrap(),
funds: vec![],
}))
.add_attribute("action", "claim")
.add_attribute("token_id", MOCK_UNCLAIMED_TOKEN)
.add_attribute("token_contract", MOCK_TOKEN_ADDR)
.add_attribute("recipient", "sender")
.add_attribute("winning_bid_amount", Uint128::from(100u128))
.add_attribute("auction_id", "1")
// Economics message
.add_submessage(SubMsg::reply_on_error(
CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: "economics_contract".to_string(),
msg: to_json_binary(&EconomicsExecuteMsg::PayFee {
payee: Addr::unchecked("any_user"),
action: "Claim".to_string()
})
.unwrap(),
funds: vec![],
}),
ReplyId::PayFee.repr(),
)),
res
);
}

#[test]
fn execute_claim_auction_not_ended() {
let mut deps = mock_dependencies_custom(&[]);
Expand Down

0 comments on commit 961a526

Please sign in to comment.