Skip to content

Commit

Permalink
added legacy NFT claim test
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahSaso committed Oct 31, 2024
1 parent 8999c82 commit ed90960
Show file tree
Hide file tree
Showing 4 changed files with 392 additions and 3 deletions.
25 changes: 25 additions & 0 deletions contracts/voting/dao-voting-cw721-staked/src/testing/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,31 @@ pub fn claim_nfts(app: &mut App, module: &Addr, sender: &str) -> AnyResult<AppRe
)
}

pub fn claim_specific_nfts(
app: &mut App,
module: &Addr,
sender: &str,
token_ids: &[String],
) -> AnyResult<AppResponse> {
app.execute_contract(
addr!(sender),
module.clone(),
&ExecuteMsg::ClaimNfts {
token_ids: Some(token_ids.to_vec()),
},
&[],
)
}

pub fn claim_legacy_nfts(app: &mut App, module: &Addr, sender: &str) -> AnyResult<AppResponse> {
app.execute_contract(
addr!(sender),
module.clone(),
&ExecuteMsg::ClaimNfts { token_ids: None },
&[],
)
}

pub fn add_hook(app: &mut App, module: &Addr, sender: &str, hook: &str) -> AnyResult<AppResponse> {
app.execute_contract(
addr!(sender),
Expand Down
169 changes: 168 additions & 1 deletion contracts/voting/dao-voting-cw721-staked/src/testing/tests.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
use cosmwasm_std::storage_keys::to_length_prefixed_nested;
use cosmwasm_std::testing::{mock_dependencies, mock_env};
use cosmwasm_std::{to_json_binary, Addr, Coin, Decimal, Empty, Uint128, WasmMsg};
use cosmwasm_std::{
to_json_binary, to_json_vec, Addr, Coin, Decimal, Empty, Storage, Uint128, WasmMsg,
};
use cw721_base::msg::{ExecuteMsg as Cw721ExecuteMsg, InstantiateMsg as Cw721InstantiateMsg};
use cw721_controllers::{NftClaim, NftClaimsResponse};
use cw_multi_test::{next_block, App, BankSudo, Executor, SudoMsg};
use cw_storage_plus::Map;
use cw_utils::Duration;
use dao_interface::voting::IsActiveResponse;
use dao_testing::contracts::{
cw721_base_contract, dao_test_custom_factory_contract, dao_voting_cw721_staked_contract,
};
use dao_voting::threshold::{ActiveThreshold, ActiveThresholdResponse};

use crate::testing::execute::{claim_legacy_nfts, claim_specific_nfts};
use crate::{
contract::{migrate, CONTRACT_NAME, CONTRACT_VERSION},
msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, NftContract, QueryMsg},
Expand Down Expand Up @@ -300,6 +305,168 @@ fn test_claims() -> anyhow::Result<()> {
Ok(())
}

// I can query and claim my pending legacy claims and non-legacy claims.
#[test]
pub fn test_legacy_claims_work() -> anyhow::Result<()> {
let CommonTest {
mut app,
module,
nft,
} = setup_test(Some(Duration::Height(1)));

mint_and_stake_nft(&mut app, &nft, &module, CREATOR_ADDR, "1")?;
mint_and_stake_nft(&mut app, &nft, &module, CREATOR_ADDR, "2")?;
mint_and_stake_nft(&mut app, &nft, &module, CREATOR_ADDR, "3")?;
mint_and_stake_nft(&mut app, &nft, &module, CREATOR_ADDR, "4")?;
mint_and_stake_nft(&mut app, &nft, &module, CREATOR_ADDR, "5")?;

let claims = query_claims(&app, &module, CREATOR_ADDR)?;
assert_eq!(claims.nft_claims, vec![]);

let res = claim_legacy_nfts(&mut app, &module, CREATOR_ADDR);
is_error!(res => "Nothing to claim");
let res = claim_nfts(&mut app, &module, CREATOR_ADDR);
is_error!(res => "Nothing to claim");

// insert legacy claims manually

// taken from cw-multi-test's WasmKeeper::contract_storage in wasm.rs
let mut module_namespace = b"contract_data/".to_vec();
module_namespace.extend_from_slice(module.as_bytes());
let prefix = to_length_prefixed_nested(&[b"wasm", &module_namespace]);
let key = Map::<&Addr, Vec<NftClaim>>::new("nft_claims").key(&Addr::unchecked(CREATOR_ADDR));
let mut legacy_nft_claims_key = prefix;
legacy_nft_claims_key.extend_from_slice(&key);

let block = app.block_info();
app.storage_mut().set(
&legacy_nft_claims_key,
&to_json_vec(&vec![cw721_controllers_v250::NftClaim {
token_id: "4".to_string(),
release_at: Duration::Height(1).after(&block),
}])
.unwrap(),
);

let claims = query_claims(&app, &module, CREATOR_ADDR)?;
assert_eq!(
claims.nft_claims,
vec![NftClaim {
token_id: "4".to_string(),
release_at: cw_utils::Expiration::AtHeight(app.block_info().height + 1)
}]
);

// claim now exists, but is not yet expired. Nothing to claim.
let res = claim_legacy_nfts(&mut app, &module, CREATOR_ADDR);
is_error!(res => "Nothing to claim");
let res = claim_nfts(&mut app, &module, CREATOR_ADDR);
is_error!(res => "Nothing to claim");

app.update_block(next_block);

// no non-legacy claims
let res = claim_nfts(&mut app, &module, CREATOR_ADDR);
is_error!(res => "Nothing to claim");

// legacy claim works
claim_legacy_nfts(&mut app, &module, CREATOR_ADDR).unwrap();
let owner = query_nft_owner(&app, &nft, "4")?;
assert_eq!(owner.owner, CREATOR_ADDR.to_string());

// unstake non-legacy
unstake_nfts(&mut app, &module, CREATOR_ADDR, &["2"])?;

let claims = query_claims(&app, &module, CREATOR_ADDR)?;
assert_eq!(
claims.nft_claims,
vec![NftClaim {
token_id: "2".to_string(),
release_at: cw_utils::Expiration::AtHeight(app.block_info().height + 1)
}]
);

// Claim now exists, but is not yet expired. Nothing to claim.
let res = claim_legacy_nfts(&mut app, &module, CREATOR_ADDR);
is_error!(res => "Nothing to claim");
let res = claim_nfts(&mut app, &module, CREATOR_ADDR);
is_error!(res => "Nothing to claim");

app.update_block(next_block);

// no legacy claims
let res = claim_legacy_nfts(&mut app, &module, CREATOR_ADDR);
is_error!(res => "Nothing to claim");

claim_nfts(&mut app, &module, CREATOR_ADDR)?;

let owner = query_nft_owner(&app, &nft, "2")?;
assert_eq!(owner.owner, CREATOR_ADDR.to_string());

// unstake another non-legacy
unstake_nfts(&mut app, &module, CREATOR_ADDR, &["3"])?;

let claims = query_claims(&app, &module, CREATOR_ADDR)?;
assert_eq!(
claims.nft_claims,
vec![NftClaim {
token_id: "3".to_string(),
release_at: cw_utils::Expiration::AtHeight(app.block_info().height + 1)
}]
);

app.update_block(next_block);

claim_specific_nfts(&mut app, &module, CREATOR_ADDR, &["3".to_string()])?;
let owner = query_nft_owner(&app, &nft, "3")?;
assert_eq!(owner.owner, CREATOR_ADDR.to_string());

// unstake legacy
let block = app.block_info();
app.storage_mut().set(
&legacy_nft_claims_key,
&to_json_vec(&vec![cw721_controllers_v250::NftClaim {
token_id: "5".to_string(),
release_at: Duration::Height(1).after(&block),
}])
.unwrap(),
);
// unstake non-legacy
unstake_nfts(&mut app, &module, CREATOR_ADDR, &["1"])?;

let claims = query_claims(&app, &module, CREATOR_ADDR)?;
assert_eq!(
claims.nft_claims,
vec![
NftClaim {
token_id: "5".to_string(),
release_at: cw_utils::Expiration::AtHeight(app.block_info().height + 1)
},
NftClaim {
token_id: "1".to_string(),
release_at: cw_utils::Expiration::AtHeight(app.block_info().height + 1)
}
]
);

app.update_block(next_block);

// both claims should be ready to claim
claim_legacy_nfts(&mut app, &module, CREATOR_ADDR)?;
claim_nfts(&mut app, &module, CREATOR_ADDR)?;

let owner = query_nft_owner(&app, &nft, "1")?;
assert_eq!(owner.owner, CREATOR_ADDR.to_string());
let owner = query_nft_owner(&app, &nft, "5")?;
assert_eq!(owner.owner, CREATOR_ADDR.to_string());

// no claims left
let claims = query_claims(&app, &module, CREATOR_ADDR)?;
assert_eq!(claims.nft_claims, vec![]);

Ok(())
}

// I can list all of the currently staked NFTs for an address.
#[test]
fn test_list_staked_nfts() -> anyhow::Result<()> {
Expand Down
29 changes: 29 additions & 0 deletions contracts/voting/dao-voting-onft-staked/src/testing/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,35 @@ pub fn claim_nfts(app: &mut OmniflixApp, module: &Addr, sender: &str) -> AnyResu
)
}

pub fn claim_specific_nfts(
app: &mut OmniflixApp,
module: &Addr,
sender: &str,
token_ids: &[String],
) -> AnyResult<AppResponse> {
app.execute_contract(
addr!(sender),
module.clone(),
&ExecuteMsg::ClaimNfts {
token_ids: Some(token_ids.to_vec()),
},
&[],
)
}

pub fn claim_legacy_nfts(
app: &mut OmniflixApp,
module: &Addr,
sender: &str,
) -> AnyResult<AppResponse> {
app.execute_contract(
addr!(sender),
module.clone(),
&ExecuteMsg::ClaimNfts { token_ids: None },
&[],
)
}

pub fn add_hook(
app: &mut OmniflixApp,
module: &Addr,
Expand Down
Loading

0 comments on commit ed90960

Please sign in to comment.