Skip to content

Commit

Permalink
fixed: change status on close, added: consider different denom on upd…
Browse files Browse the repository at this point in the history
…ate, test all of this (#720)

Co-authored-by: Tai 'Mr. T' Truong <[email protected]>
  • Loading branch information
2 people authored and JakeHartnell committed Jul 9, 2023
1 parent 1e2ae79 commit 7cbde57
Show file tree
Hide file tree
Showing 4 changed files with 465 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions contracts/external/cw-bounties/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ thiserror = { workspace = true }
[dev-dependencies]
cosmwasm-schema = { workspace = true }
cw-multi-test = { workspace = true }
anyhow = { workspace = true }
44 changes: 38 additions & 6 deletions contracts/external/cw-bounties/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn instantiate(
cw_ownable::initialize_owner(deps.storage, deps.api, Some(owner.as_str()))?;

// Initialize the next ID
ID.save(deps.storage, &1)?;
ID.save(deps.storage, &0)?;

Ok(Response::default())
}
Expand All @@ -49,7 +49,7 @@ pub fn execute(
cw_ownable::assert_owner(deps.storage, &info.sender)?;

match msg {
ExecuteMsg::Close { id } => close(deps, info, id),
ExecuteMsg::Close { id } => close(deps, env, info, id),
ExecuteMsg::Create {
amount,
title,
Expand Down Expand Up @@ -114,15 +114,25 @@ pub fn create(
.add_attribute("id", id.to_string()))
}

pub fn close(deps: DepsMut, info: MessageInfo, id: u64) -> Result<Response, ContractError> {
pub fn close(
deps: DepsMut,
env: Env,
info: MessageInfo,
id: u64,
) -> Result<Response, ContractError> {
// Check bounty exists
let bounty = BOUNTIES.load(deps.storage, id)?;
let mut bounty = BOUNTIES.load(deps.storage, id)?;

// Check bounty is open
if bounty.status != BountyStatus::Open {
return Err(ContractError::NotOpen {});
};

bounty.status = BountyStatus::Closed {
closed_at: env.block.time.seconds(),
};
BOUNTIES.save(deps.storage, id, &bounty)?;

// Pay out remaining funds to owner
// Only owner can call this, so sender is owner
let msg = BankMsg::Send {
Expand Down Expand Up @@ -205,13 +215,35 @@ pub fn update(
},
)?;

// Check if amount is greater or less than original amount
let old_amount = bounty.amount;
let res = Response::new()
.add_attribute("action", "update_bounty")
.add_attribute("bounty_id", id.to_string())
.add_attribute("amount", new_amount.amount.to_string());

// check if new amount has different denom
if new_amount.denom != bounty.amount.denom {
// If denom is different, check funds sent match new amount
let sent_amount = must_pay(&info, &new_amount.denom)?;
if sent_amount != new_amount.amount {
return Err(ContractError::InvalidAmount {
expected: new_amount.amount,
actual: sent_amount,
});
}
// send back old amount
let msg = BankMsg::Send {
to_address: info.sender.to_string(),
amount: vec![Coin {
denom: bounty.amount.denom,
amount: bounty.amount.amount,
}],
};

return Ok(res.add_message(msg));
};

// Check if amount is greater or less than original amount
let old_amount = bounty.amount;
match new_amount.amount.cmp(&old_amount.amount) {
Ordering::Greater => {
// If new amount is greater, check funds sent plus
Expand Down
Loading

0 comments on commit 7cbde57

Please sign in to comment.