-
-
Save nnez/c76b1a867dd8dc441dbe552e048b796e to your computer and use it in GitHub Desktop.
A boilerplate of proof-of-concept for codedestate code4rena audit contest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// A boilerplate of proof-of-concept for codedestate code4rena audit contest | |
// | |
use cosmwasm_std::{Addr, BalanceResponse, BankQuery, BlockInfo, Coin, Empty, QueryRequest, Uint128}; | |
use cw721::OwnerOfResponse; | |
use cw_multi_test::{App, ContractWrapper, Executor}; | |
use cosmwasm_std::Timestamp; | |
use crate::entry::{instantiate, execute, query}; | |
use crate::{ | |
ExecuteMsg, InstantiateMsg, QueryMsg | |
}; | |
const ADMIN: &str = "admin"; | |
const CONTRACT_NAME: &str = "contract-name"; | |
const SYMBOL: &str = "symbol"; | |
const MINTER: &str = "minter"; | |
const TOKEN_ID: &str = "token-id"; | |
const TRAVELER: &str = "traveler"; | |
const USDC: &str = "ibc/F082B65C88E4B6D5EF1DB243CDA1D331D002759E938A0F5CD3FFDC5D53B3E349"; | |
fn mock_app_init_contract() -> (App, Addr) { | |
let contract = ContractWrapper::new(execute, instantiate, query); | |
let mut app = App::default(); | |
let contract_id = app.store_code(Box::new(contract)); | |
// Initial block height | |
let initial_height = app.block_info().height; | |
println!("Initial block height: {}", initial_height); | |
// Instantiate the contract | |
let instantiate_msg = InstantiateMsg { | |
name: CONTRACT_NAME.to_string(), | |
symbol: SYMBOL.to_string(), | |
minter: String::from(ADMIN), | |
}; | |
let contract_addr = app.instantiate_contract( | |
contract_id, | |
Addr::unchecked(ADMIN), | |
&instantiate_msg, | |
&[], | |
"Test Contract", | |
None, | |
).unwrap(); | |
return (app, contract_addr); | |
} | |
fn advance_blocks(app: &mut App, num_blocks: u64) { | |
app.update_block(|block: &mut BlockInfo| { | |
block.height += num_blocks; // Move ahead by a certain number of blocks | |
block.time = block.time.plus_seconds(num_blocks); // Assuming each block is 1 second | |
}); | |
// new block height | |
let new_height = app.block_info().height; | |
println!("New block height: {}", new_height); | |
} | |
fn execute_mint(app: &mut App, contract_addr: &Addr, minter: &str, token_id: &str) { | |
// Execute: MINTER mints the token | |
let mint_msg: ExecuteMsg<Option<Empty>, Empty> = ExecuteMsg::Mint { | |
token_id: token_id.to_string(), | |
owner: minter.to_string(), | |
token_uri: None, | |
extension: None | |
}; | |
let res = app.execute_contract( | |
Addr::unchecked(minter), | |
contract_addr.clone(), | |
&mint_msg, | |
&[], | |
); | |
assert!(res.is_ok()); | |
} | |
fn init_usdc_balance(app: &mut App, addr: &str, balance: u128) { | |
app.init_modules(|router, _, storage| { | |
router | |
.bank | |
.init_balance(storage, &Addr::unchecked(addr), vec![Coin { | |
denom: USDC.to_string(), | |
amount: Uint128::new(balance), | |
}]) | |
.unwrap(); | |
}); | |
} | |
fn init_denom_balance(app: &mut App, addr: &str, denom: &str, balance: u128) { | |
app.init_modules(|router, _, storage| { | |
router | |
.bank | |
.init_balance(storage, &Addr::unchecked(addr), vec![Coin { | |
denom: denom.to_string(), | |
amount: Uint128::new(balance), | |
}]) | |
.unwrap(); | |
}); | |
} | |
fn query_usdc_balance(app: &App, addr: &str) -> u128 { | |
let balance: BalanceResponse = app.wrap().query(&QueryRequest::Bank(BankQuery::Balance { | |
address: addr.to_string(), | |
denom: USDC.to_string(), | |
})).unwrap(); | |
balance.amount.amount.u128() | |
} | |
fn query_denom_balance(app: &App, contract_addr: &str, denom: &str) -> u128 { | |
let balance: BalanceResponse = app.wrap().query(&QueryRequest::Bank(BankQuery::Balance { | |
address: contract_addr.to_string(), | |
denom: denom.to_string(), | |
})).unwrap(); | |
balance.amount.amount.u128() | |
} | |
fn query_token_count(app: &App, contract_addr: &str) -> u64 { | |
let num_tokens_query_msg: QueryMsg<Empty> = QueryMsg::NumTokens {}; | |
let num_tokens_reponse: cw721::NumTokensResponse = app.wrap().query_wasm_smart(contract_addr, &num_tokens_query_msg).unwrap(); | |
num_tokens_reponse.count | |
} | |
fn query_fee_balance(app: &App, contract_addr: &str) -> u128 { | |
let get_balance_msg: QueryMsg<Empty> = QueryMsg::GetBalance { denom:USDC.to_string() }; | |
let fee_balance: Uint128 = app.wrap().query_wasm_smart(contract_addr, &get_balance_msg).unwrap(); | |
fee_balance.u128() | |
} | |
fn query_token_owner(app: &App, contract_addr: &str, token_id: &str) -> String { | |
let owner_of_msg: QueryMsg<OwnerOfResponse> = QueryMsg::OwnerOf { token_id: token_id.to_string(), include_expired: Some(true) }; | |
let response: OwnerOfResponse = app.wrap().query_wasm_smart(contract_addr, &owner_of_msg).unwrap(); | |
response.owner | |
} | |
// Insert proof-of-concept test here! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment