Skip to content

Commit

Permalink
refactor: clean up auction's claim function
Browse files Browse the repository at this point in the history
  • Loading branch information
joemonem committed May 9, 2024
1 parent 961a526 commit 65ad51b
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 123 deletions.
174 changes: 71 additions & 103 deletions contracts/non-fungible-tokens/andromeda-auction/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,97 +758,41 @@ fn execute_claim(
let (after_tax_payment, tax_messages) =
purchase_token(deps.as_ref(), &info, token_auction_state.clone(), action)?;

let resp: Response = Response::new()
let mut resp: Response = Response::new()
// Send NFT to auction winner.
.add_message(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: token_auction_state.token_address.clone(),
msg: encode_binary(&Cw721ExecuteMsg::TransferNft {
recipient: token_auction_state.high_bidder_addr.to_string(),
token_id: token_id.clone(),
})?,
funds: vec![],
}))
// Send tax/royalty messages
.add_submessages(tax_messages)
.add_attribute("action", "claim")
.add_attribute("token_id", token_id.clone())
.add_attribute("token_contract", token_auction_state.clone().token_address)
.add_attribute("recipient", &token_auction_state.high_bidder_addr)
.add_attribute("winning_bid_amount", token_auction_state.high_bidder_amount)
.add_attribute("auction_id", token_auction_state.auction_id);

let is_cw20_auction = token_auction_state.uses_cw20;
if is_cw20_auction {
let auction_currency = token_auction_state.coin_denom;
let recipient = token_auction_state
.recipient
.unwrap_or(Recipient::from_string(token_auction_state.owner));

let recipient = token_auction_state
.recipient
.unwrap_or(Recipient::from_string(token_auction_state.owner));

let cw20_msg = recipient.generate_msg_cw20(
&deps.as_ref(),
Cw20Coin {
address: auction_currency.clone(),
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 tax_messages.first().map(|msg| {
if let CosmosMsg::Bank(BankMsg::Send { to_address, amount }) = &msg.msg {
(
Some(to_address.clone()),
amount.first().map(|coin| coin.amount),
)
} else {
(None, None)
}
}) {
Some((tax_recipient, tax_amount)) => (tax_recipient, tax_amount),
None => (None, None),
};
match (tax_recipient, tax_amount) {
(Some(recipient), Some(amount)) => {
let tax_transfer_msg = Cw20ExecuteMsg::Transfer { recipient, amount };
let tax_wasm_msg = wasm_execute(auction_currency, &tax_transfer_msg, vec![])?;
// Add tax message in case there's a tax recipient and amount
Ok(resp
.add_message(tax_wasm_msg)
// Send cw20 funds to the original owner or recipient (if provided).
.add_submessage(cw20_msg)
// Send NFT to auction winner.
.add_message(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: token_auction_state.token_address.clone(),
msg: encode_binary(&Cw721ExecuteMsg::TransferNft {
recipient: token_auction_state.high_bidder_addr.to_string(),
token_id,
})?,
funds: vec![],
})))
}
_ => Ok(resp
// Send cw20 funds to the original owner or recipient (if provided).
.add_submessage(cw20_msg)
// Send NFT to auction winner.
.add_message(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: token_auction_state.token_address.clone(),
msg: encode_binary(&Cw721ExecuteMsg::TransferNft {
recipient: token_auction_state.high_bidder_addr.to_string(),
token_id,
})?,
funds: vec![],
}))),
match after_tax_payment {
Funds::Native(native_funds) => {
// Send payment to recipient
resp = resp
.add_submessage(recipient.generate_direct_msg(&deps.as_ref(), vec![native_funds])?)
}
Funds::Cw20(cw20_funds) => {
let cw20_msg = recipient.generate_msg_cw20(&deps.as_ref(), cw20_funds)?;
resp = resp.add_submessage(cw20_msg)
}
} else {
let recipient = token_auction_state
.clone()
.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()])?;

Ok(resp
.add_submessages(tax_messages)
// Send native funds to the original owner or recipient (if provided).
.add_submessage(msg)
// Send NFT to auction winner.
.add_message(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: token_auction_state.token_address.clone(),
msg: encode_binary(&Cw721ExecuteMsg::TransferNft {
recipient: token_auction_state.high_bidder_addr.to_string(),
token_id,
})?,
funds: vec![],
})))
}
Ok(resp)
}

fn execute_authorize_token_contract(
Expand Down Expand Up @@ -907,29 +851,53 @@ fn purchase_token(
_info: &MessageInfo,
state: TokenAuctionState,
action: String,
) -> Result<(Coin, Vec<SubMsg>), ContractError> {
let total_cost = Coin::new(state.high_bidder_amount.u128(), state.coin_denom.clone());

let transfer_response = ADOContract::default().query_deducted_funds(
deps,
action,
Funds::Native(total_cost.clone()),
)?;
match transfer_response {
Some(transfer_response) => {
let remaining_amount = transfer_response.leftover_funds.try_get_coin()?;
let after_tax_payment = Coin {
denom: state.coin_denom,
amount: remaining_amount.amount,
};
Ok((after_tax_payment, transfer_response.msgs))
) -> Result<(Funds, Vec<SubMsg>), ContractError> {
if !state.uses_cw20 {
let total_cost = Coin::new(state.high_bidder_amount.u128(), state.coin_denom.clone());
let transfer_response = ADOContract::default().query_deducted_funds(
deps,
action,
Funds::Native(total_cost.clone()),
)?;
match transfer_response {
Some(transfer_response) => {
let remaining_amount = transfer_response.leftover_funds.try_get_coin()?;
let after_tax_payment = Coin {
denom: state.coin_denom,
amount: remaining_amount.amount,
};
Ok((Funds::Native(after_tax_payment), transfer_response.msgs))
}
None => {
let after_tax_payment = Coin {
denom: state.coin_denom,
amount: total_cost.amount,
};
Ok((Funds::Native(after_tax_payment), vec![]))
}
}
None => {
let after_tax_payment = Coin {
denom: state.coin_denom,
amount: total_cost.amount,
};
Ok((after_tax_payment, vec![]))
} else {
let total_cost = Cw20Coin {
address: state.coin_denom.clone(),
amount: state.high_bidder_amount,
};
let transfer_response = ADOContract::default().query_deducted_funds(
deps,
action,
Funds::Cw20(total_cost.clone()),
)?;
match transfer_response {
Some(transfer_response) => {
let remaining_amount = transfer_response.leftover_funds.try_get_cw20_coin()?;
Ok((Funds::Cw20(remaining_amount), transfer_response.msgs))
}
None => {
let after_tax_payment = Cw20Coin {
address: state.coin_denom,
amount: total_cost.amount,
};
Ok((Funds::Cw20(after_tax_payment), vec![]))
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1192,6 +1192,11 @@ fn execute_claim_with_tax() {
};
assert_eq!(
Response::new()
.add_message(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: MOCK_TOKEN_ADDR.to_string(),
msg: encode_binary(&transfer_nft_msg).unwrap(),
funds: vec![],
}))
.add_message(CosmosMsg::Bank(BankMsg::Send {
to_address: tax_recipient.to_owned(),
amount: coins(20, "uusd"),
Expand All @@ -1200,11 +1205,6 @@ fn execute_claim_with_tax() {
to_address: MOCK_TOKEN_OWNER.to_owned(),
amount: coins(100, "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)
Expand Down Expand Up @@ -1279,6 +1279,11 @@ fn execute_claim_with_royalty() {
};
assert_eq!(
Response::new()
.add_message(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: MOCK_TOKEN_ADDR.to_string(),
msg: encode_binary(&transfer_nft_msg).unwrap(),
funds: vec![],
}))
.add_message(CosmosMsg::Bank(BankMsg::Send {
to_address: royalty_recipient.to_owned(),
amount: coins(20, "uusd"),
Expand All @@ -1287,11 +1292,6 @@ fn execute_claim_with_royalty() {
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)
Expand Down Expand Up @@ -1354,6 +1354,11 @@ fn execute_claim_cw20() {
};
assert_eq!(
Response::new()
.add_message(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: MOCK_TOKEN_ADDR.to_string(),
msg: encode_binary(&transfer_nft_msg).unwrap(),
funds: vec![],
}))
.add_message(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: MOCK_CW20_CONTRACT.to_string(),
msg: encode_binary(&Cw20ExecuteMsg::Transfer {
Expand All @@ -1363,11 +1368,6 @@ fn execute_claim_cw20() {
.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)
Expand Down Expand Up @@ -1448,6 +1448,11 @@ fn execute_claim_cw20_with_tax() {
};
assert_eq!(
Response::new()
.add_message(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: MOCK_TOKEN_ADDR.to_string(),
msg: encode_binary(&transfer_nft_msg).unwrap(),
funds: vec![],
}))
.add_message(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: MOCK_CW20_CONTRACT.to_string(),
msg: encode_binary(&Cw20ExecuteMsg::Transfer {
Expand All @@ -1466,11 +1471,6 @@ fn execute_claim_cw20_with_tax() {
.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)
Expand Down

0 comments on commit 65ad51b

Please sign in to comment.