Skip to content

Commit

Permalink
pulled
Browse files Browse the repository at this point in the history
  • Loading branch information
mymiracle0118 committed Apr 20, 2024
2 parents 472bea2 + f8a4af5 commit 26312d5
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 145 deletions.
28 changes: 17 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,32 @@ optimize:
test:
cargo unit-test

<<<<<<< HEAD
FROM=nibi10rdtquh3jl44hg00x0plzeawuclqqet0he4692
AIRDROP_CONTRACT=nibi10jwwkmmn0rzxvfec5swhgvtkh4l94fshz0r8ndj9ldym87lkgdjs4z4cyp
=======
WALLET=nibi1d3lmwkgjgdyfpsn4xgh299jnpk4r89kd5xs420
AIRDROP_CONTRACT=nibi1lz6h4tav6q8jrpxxs6kv63hztlmpu27jj50jcnrt9p7wd2nrmz9s9600vz
CODE_ID=421
WALLET_NAME=jarvis
>>>>>>> f8a4af504d3b45ae487ba064c7d39c846c54be0e

make-wallet:
@nibid keys add wallet
@nibid keys add wallet ${WALLET_NAME}

show-wallet:
@nibid keys show -a ${id}
@nibid keys show -a ${WALLET}

get-balance:
@nibid query bank balances ${WALLET} --denom unibi

upload-testnet:
@nibid tx wasm store artifacts/jarvis_airdrop.wasm --from ${FROM} --gas auto --gas-adjustment 1.5 --gas-prices 0.025unibi --yes
@nibid tx wasm store artifacts/jarvis_airdrop.wasm --from ${WALLET} --gas auto --gas-adjustment 1.5 --gas-prices 0.025unibi --yes

instantiate-testnet:
@nibid tx wasm instantiate ${id} '{"count": 1}' --admin ${FROM} --label airdrop --from ${FROM} --gas auto --gas-adjustment 1.5 --gas-prices 0.025unibi --yes

get-count:
$(eval GET_COUNT := $$(shell cat ./commands/get_count.json))
@nibid query wasm contract-state smart ${AIRDROP_CONTRACT} '$(GET_COUNT)'
@nibid tx wasm instantiate ${CODE_ID} --admin ${WALLET} --label airdrop --from ${WALLET} --gas auto --gas-adjustment 1.5 --gas-prices 0.025unibi --yes

get-nft-contract-addres:
get-nft-contract-address:
$(eval GET_NFT_CONTRACT_ADDRESS := $$(shell cat ./commands/get_nft_contract_addr.json))
@nibid query wasm contract-state smart ${AIRDROP_CONTRACT} '$(GET_NFT_CONTRACT_ADDRESS)'

Expand All @@ -38,8 +44,8 @@ get-all-nfts:

exe-set-nft-contract-addr:
$(eval SET_NFT_CONTRACT_ADDR := $$(shell cat ./commands/set_nft_contract_addr.json))
@nibid tx wasm execute ${AIRDROP_CONTRACT} '$(SET_NFT_CONTRACT_ADDR)' --from ${FROM} --gas auto --gas-adjustment 1.5 --gas-prices 0.025unibi --yes
@nibid tx wasm execute ${AIRDROP_CONTRACT} '$(SET_NFT_CONTRACT_ADDR)' --from ${WALLET} --gas auto --gas-adjustment 1.5 --gas-prices 0.025unibi --yes

exe-send-nfts:
$(eval SEND_NFTS := $$(shell cat ./commands/send_nfts.json))
@nibid tx wasm execute ${AIRDROP_CONTRACT} '$(SEND_NFTS)' --from ${FROM} --gas auto --gas-adjustment 1.5 --gas-prices 0.025unibi --yes
@nibid tx wasm execute ${AIRDROP_CONTRACT} '$(SEND_NFTS)' --from ${WALLET} --gas auto --gas-adjustment 1.5 --gas-prices 0.025unibi --yes
4 changes: 4 additions & 0 deletions artifacts/checksums.txt
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
<<<<<<< HEAD
6cad51fd1cd844c98c7e367b1b0910c4389e8f0c4a69edab1821e7aeb99094f9 jarvis_airdrop.wasm
=======
d63576084b69def9913aae0899dcba97a22482511bd860bac1345e455332fbfc jarvis_airdrop.wasm
>>>>>>> f8a4af504d3b45ae487ba064c7d39c846c54be0e
4 changes: 4 additions & 0 deletions artifacts/checksums_intermediate.txt
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
<<<<<<< HEAD
deddb63d42e3a8126c7edda8a28755f4ec6f28ee7f40becb1755b5e3c1be49f7 /target/wasm32-unknown-unknown/release/jarvis_airdrop.wasm
=======
5f9d4a5aeeef32dfe13b0866f83378a51934ead6a95938042e1cc2966ab31032 /target/wasm32-unknown-unknown/release/jarvis_airdrop.wasm
>>>>>>> f8a4af504d3b45ae487ba064c7d39c846c54be0e
124 changes: 26 additions & 98 deletions src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{to_json_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult, Addr, CosmosMsg, WasmMsg, SubMsg};
use cosmwasm_std::{
to_json_binary, Addr, Binary, CosmosMsg, Deps, DepsMut, Env, MessageInfo, Response, StdResult,
SubMsg, WasmMsg,
};
use cw2::set_contract_version;
use cw721::Cw721ExecuteMsg;

use crate::error::ContractError;
use crate::msg::{ExecuteMsg, GetCountResponse, InstantiateMsg, QueryMsg, AllNftsResponse, NftContractAddrResponse, SendNftParam};
use crate::msg::{ExecuteMsg, InstantiateMsg, QueryMsg, AllNftsResponse, NftContractAddrResponse, SendNftParam};
use crate::state::{State, STATE, NFTS, NFT_CONTRACT_ADDR};

// version info for migration info
Expand All @@ -20,16 +23,14 @@ pub fn instantiate(
msg: InstantiateMsg,
) -> Result<Response, ContractError> {
let state = State {
count: msg.count,
owner: info.sender.clone(),
};
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
STATE.save(deps.storage, &state)?;

Ok(Response::new()
.add_attribute("method", "instantiate")
.add_attribute("owner", info.sender)
.add_attribute("count", msg.count.to_string()))
.add_attribute("owner", info.sender))
}

#[cfg_attr(not(feature = "library"), entry_point)]
Expand All @@ -40,37 +41,21 @@ pub fn execute(
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
match msg {
ExecuteMsg::Increment {} => execute::increment(deps),
ExecuteMsg::Reset { count } => execute::reset(deps, info, count),
ExecuteMsg::SetNftContractAddr { addr } => execute::set_nft_contract_addr(deps, env, info, addr),
ExecuteMsg::ReceiveNft { sender, token_id, msg } => execute::receive_nft(deps, env, info, token_id),
ExecuteMsg::SendNfts { allocations } => execute::send_nfts(deps, env, info, allocations),
ExecuteMsg::SetNftContractAddr { addr } => {
execute::set_nft_contract_addr(deps, env, info, addr)
}
ExecuteMsg::ReceiveNft {
sender,
token_id,
msg,
} => execute::receive_nft(deps, env, info, token_id),
ExecuteMsg::SendNfts { allocations } => execute::send_nfts(deps, env, info, allocations),
}
}

pub mod execute {
use super::*;

pub fn increment(deps: DepsMut) -> Result<Response, ContractError> {
STATE.update(deps.storage, |mut state| -> Result<_, ContractError> {
state.count += 1;
Ok(state)
})?;

Ok(Response::new().add_attribute("action", "increment"))
}

pub fn reset(deps: DepsMut, info: MessageInfo, count: i32) -> Result<Response, ContractError> {
STATE.update(deps.storage, |mut state| -> Result<_, ContractError> {
if info.sender != state.owner {
return Err(ContractError::Unauthorized {});
}
state.count = count;
Ok(state)
})?;
Ok(Response::new().add_attribute("action", "reset"))
}

pub fn set_nft_contract_addr(
deps: DepsMut,
_env: Env,
Expand All @@ -87,7 +72,9 @@ pub mod execute {
// Optionally, add authorization checks here to ensure only specific addresses can update this
let nft_contract_addr = deps.api.addr_validate(&addr)?;
NFT_CONTRACT_ADDR.save(deps.storage, &nft_contract_addr)?;
Ok(Response::new().add_attribute("action", "set_nft_contract_addr").add_attribute("address", addr))
Ok(Response::new()
.add_attribute("action", "set_nft_contract_addr")
.add_attribute("address", addr))
}

pub fn receive_nft(
Expand All @@ -99,10 +86,9 @@ pub mod execute {
let mut nfts = NFTS.load(deps.storage).unwrap_or_default();
nfts.push(token_id);
NFTS.save(deps.storage, &nfts)?;

Ok(Response::new().add_attribute("action", "receive_nft"))
}


pub fn send_nfts(
deps: DepsMut,
Expand Down Expand Up @@ -132,20 +118,20 @@ pub mod execute {
recipient: ele.recipient.to_string(),
token_id: token_id,
};

let msg = CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: validated_addr.clone().to_string(),
msg: to_json_binary(&transfer_msg)?,
funds: vec![],
});

response.messages.push(SubMsg::new(msg));
} else {
return Err(ContractError::InsufficientNFTs {});
}
}
}

NFTS.save(deps.storage, &nfts)?;
Ok(response)
}
Expand All @@ -154,25 +140,19 @@ pub mod execute {
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::GetCount {} => to_json_binary(&query::count(deps)?),
QueryMsg::GetAllNfts { } => to_json_binary(&query::all_nfts(deps)?),
QueryMsg::GetNftContractAddr { } => to_json_binary(&query::nft_contract_addr(deps)?),
QueryMsg::GetAllNfts {} => to_json_binary(&query::all_nfts(deps)?),
QueryMsg::GetNftContractAddr {} => to_json_binary(&query::nft_contract_addr(deps)?),
}
}

pub mod query {
use super::*;

pub fn count(deps: Deps) -> StdResult<GetCountResponse> {
let state = STATE.load(deps.storage)?;
Ok(GetCountResponse { count: state.count })
}

pub fn all_nfts(deps: Deps) -> StdResult<AllNftsResponse> {
let nfts = NFTS.load(deps.storage)?;
Ok(AllNftsResponse { nfts })
}

pub fn nft_contract_addr(deps: Deps) -> StdResult<NftContractAddrResponse> {
let nft_contract_addr = NFT_CONTRACT_ADDR.load(deps.storage)?;
Ok(NftContractAddrResponse { nft_contract_addr })
Expand All @@ -189,63 +169,11 @@ mod tests {
fn proper_initialization() {
let mut deps = mock_dependencies();

let msg = InstantiateMsg { count: 17 };
let msg = InstantiateMsg {};
let info = mock_info("creator", &coins(1000, "earth"));

// we can just call .unwrap() to assert this was a success
let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap();
assert_eq!(0, res.messages.len());

// it worked, let's query the state
let res = query(deps.as_ref(), mock_env(), QueryMsg::GetCount {}).unwrap();
let value: GetCountResponse = from_json(&res).unwrap();
assert_eq!(17, value.count);
}

#[test]
fn increment() {
let mut deps = mock_dependencies();

let msg = InstantiateMsg { count: 17 };
let info = mock_info("creator", &coins(2, "token"));
let _res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap();

// beneficiary can release it
let info = mock_info("anyone", &coins(2, "token"));
let msg = ExecuteMsg::Increment {};
let _res = execute(deps.as_mut(), mock_env(), info, msg).unwrap();

// should increase counter by 1
let res = query(deps.as_ref(), mock_env(), QueryMsg::GetCount {}).unwrap();
let value: GetCountResponse = from_json(&res).unwrap();
assert_eq!(18, value.count);
}

#[test]
fn reset() {
let mut deps = mock_dependencies();

let msg = InstantiateMsg { count: 17 };
let info = mock_info("creator", &coins(2, "token"));
let _res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap();

// beneficiary can release it
let unauth_info = mock_info("anyone", &coins(2, "token"));
let msg = ExecuteMsg::Reset { count: 5 };
let res = execute(deps.as_mut(), mock_env(), unauth_info, msg);
match res {
Err(ContractError::Unauthorized {}) => {}
_ => panic!("Must return unauthorized error"),
}

// only the original creator can reset the counter
let auth_info = mock_info("creator", &coins(2, "token"));
let msg = ExecuteMsg::Reset { count: 5 };
let _res = execute(deps.as_mut(), mock_env(), auth_info, msg).unwrap();

// should now be 5
let res = query(deps.as_ref(), mock_env(), QueryMsg::GetCount {}).unwrap();
let value: GetCountResponse = from_json(&res).unwrap();
assert_eq!(5, value.count);
}
}
19 changes: 1 addition & 18 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use cosmwasm_std::{
WasmQuery,
};

use crate::msg::{ExecuteMsg, GetCountResponse, QueryMsg};
use crate::msg::{ExecuteMsg, QueryMsg};

/// CwTemplateContract is a wrapper around Addr that provides a lot of helpers
/// for working with this.
Expand All @@ -27,21 +27,4 @@ impl CwTemplateContract {
}
.into())
}

/// Get Count
pub fn count<Q, T, CQ>(&self, querier: &Q) -> StdResult<GetCountResponse>
where
Q: Querier,
T: Into<String>,
CQ: CustomQuery,
{
let msg = QueryMsg::GetCount {};
let query = WasmQuery::Smart {
contract_addr: self.addr().into(),
msg: to_json_binary(&msg)?,
}
.into();
let res: GetCountResponse = QuerierWrapper::<CQ>::new(querier).query(&query)?;
Ok(res)
}
}
3 changes: 1 addition & 2 deletions src/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ mod tests {
let mut app = mock_app();
let cw_template_id = app.store_code(contract_template());

let msg = InstantiateMsg { count: 1i32 };
let msg = InstantiateMsg {};
let cw_template_contract_addr = app
.instantiate_contract(
cw_template_id,
Expand All @@ -63,7 +63,6 @@ mod tests {
fn count() {
let (mut app, cw_template_contract) = proper_instantiate();

let msg = ExecuteMsg::Increment {};
let cosmos_msg = cw_template_contract.call(msg).unwrap();
app.execute(Addr::unchecked(USER), cosmos_msg).unwrap();
}
Expand Down
16 changes: 1 addition & 15 deletions src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@ use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::{Addr, Binary};

#[cw_serde]
pub struct InstantiateMsg {
pub count: i32,
}
pub struct InstantiateMsg {}

#[cw_serde]
pub enum ExecuteMsg {
Increment {},
Reset { count: i32 },
ReceiveNft {
sender: String,
token_id: String,
Expand All @@ -24,23 +20,13 @@ pub enum ExecuteMsg {
#[cw_serde]
#[derive(QueryResponses)]
pub enum QueryMsg {
// GetCount returns the current count as a json-encoded number
#[returns(GetCountResponse)]
GetCount {},

#[returns(AllNftsResponse)]
GetAllNfts {},

#[returns(NftContractAddrResponse)]
GetNftContractAddr {},
}

// We define a custom struct for each query response
#[cw_serde]
pub struct GetCountResponse {
pub count: i32,
}

#[cw_serde]
pub struct AllNftsResponse {
pub nfts: Vec<String>,
Expand Down
1 change: 0 additions & 1 deletion src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use cw_storage_plus::Item;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct State {
pub count: i32,
pub owner: Addr,
}

Expand Down

0 comments on commit 26312d5

Please sign in to comment.