Skip to content

Commit

Permalink
build: artifacts + just tidy + docs + fix nibiru-std proto import (#117)
Browse files Browse the repository at this point in the history
* fix(bash-rs): type corrections for e2e tests

* build: artifacts + just tidy + docs + merge main

commit 2296e6f1e91db857318632089aa2cf888a4ce8ae
Merge: c14558d dd90c9a
Author: Unique-Divine <[email protected]>
Date:   Wed Jan 10 21:25:41 2024 -0600

    Merge branch 'main' into realu/std

commit c14558de1b80b739701ff3b146355ea815327192
Author: Unique-Divine <[email protected]>
Date:   Wed Jan 10 15:43:00 2024 -0600

    quick save wip!

commit dd90c9a
Merge: bd792e4 902b788
Author: Jonathan Gimeno <[email protected]>
Date:   Fri Jan 5 19:00:52 2024 +0100

    Merge pull request #111 from NibiruChain/feat/some-refactors-airdrop

    add some refactors to airdrop contract

commit 902b788
Author: Jonathan Gimeno <[email protected]>
Date:   Fri Jan 5 17:41:16 2024 +0100

    remove unused imports

commit 0f337d1
Author: Jonathan Gimeno <[email protected]>
Date:   Fri Jan 5 17:39:21 2024 +0100

    it fails when there are not enough funds

commit 1d42220
Author: Jonathan Gimeno <[email protected]>
Date:   Fri Jan 5 17:31:57 2024 +0100

    add some refactors, part 1

commit bd792e4
Author: Kevin Yang <[email protected]>
Date:   Wed Jan 3 14:22:23 2024 -0800

    feat: initial airdrop contract (#110)

    * feat: initial airdrop contract

    * create cargo config

    * test: instantiate message

    * remove owner from instantiate msg

    * test: query functions

    * test: execute functions

    * feat: add manager list

    * feat: implement manager list

    * Create README.md

    * Update contracts/airdrop/src/contract.rs

---
  • Loading branch information
Unique-Divine authored Jan 11, 2024
1 parent dd90c9a commit 349daec
Show file tree
Hide file tree
Showing 37 changed files with 519 additions and 241 deletions.
67 changes: 67 additions & 0 deletions Cargo.lock

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

13 changes: 12 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,15 @@ lazy_static = "1.4.0"
clap = { version = "4.4.7", features = ["derive", "cargo", "env", "string"] }
schemars = "0.8.15"
home = "0.5"
toml = "0.8"
toml = "0.8"

[profile.release]
opt-level = 3
debug = false
rpath = false
lto = true
debug-assertions = false
codegen-units = 1
panic = 'abort'
incremental = false
overflow-checks = true
Binary file added artifacts/airdrop.wasm
Binary file not shown.
19 changes: 10 additions & 9 deletions artifacts/checksums.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
6e43956a6ae41e032b611529bbbd0112699f96a8126fc32b879cfe14521ad85f airdrop.wasm
382c05baf544f2886de849933ecf59e8bc3bcdcdd552d5a63537bd6d63f2ecf1 controller.wasm
83d726be3842b4df7179b9cfd84c3c9f8856d4252738cf4a6d27fc699f5b43bf cw3_flex_multisig.wasm
f44fd4d9a266f9281bdbedec277d82484c88688ac436d908d41ded1643b0b3f3 incentives.wasm
4c3932c7bcf99112053da52ab15d7718e827d9e71a64d57181c3816dff141e71 lockup.wasm
e6e0180d424b4f546b8d29c8f49d7ced09011a33eccfe124af7daccbac005a37 nibi_stargate.wasm
e2741d21b4bc8a8628e340d1c119a6f5411b4a7d2b69b9e3c7f344bdcb15ec57 nibi_stargate_perp.wasm
ec015f78a854ae7450915b973193e65039050b156af21791899a72d532d2c623 nusd_valuator.wasm
d4b6a34c5c51c1e080b953fcc43b011f6616e7a49b50fb18bf93ec7dddbe9a95 pricefeed.wasm
ce1ff9cd1e5127ae94797f5d013074ef2d9fb4d6c41f7488ead3361626687d5d shifter.wasm
dd8fb20aefaf69951c09f42bfe5d9678678d51b9f3e2bba098f3175fb86785a9 token_vesting.wasm
0ee6293c7ab257139d6b10abb31cafe7a6c00f3fbf2c8be126363f3c1e4e6d80 cw3_flex_multisig.wasm
515a13e891e6bf6a95ab985f653a45668c24991931fc664b64d5a0e803e4ab33 incentives.wasm
159602bf4ca5b4b2430662f0f4cf198fc764608dc2ceb25e35970896e2e4563c lockup.wasm
ced175298f8e6b4af8f414609f3c05c0ec533f1312bac3e650a75974a0e1312f nibi_stargate.wasm
540c83759a1ec8b4a78565345275c6da6c7fdfb420748c2e60d392148eb21bda nibi_stargate_perp.wasm
96c155ab1314f818baf9c0c91373446c822a0f49dccfdbacd3e18cb3579028f7 nusd_valuator.wasm
fb19953e1fdba3ac783dcd3fce80fd28e93f09ac383e0b98326a04a9e6075c08 pricefeed.wasm
decbc751198e3a4fe0ee134f85b1983c0e97d6cfa4c2bad5c084ebb5080566ec shifter.wasm
d9b341cd6b3f5879159f60cd1f4a1eedf82f9358e0989068a2a6317cf3055beb token_vesting.wasm
Binary file modified artifacts/cw3_flex_multisig.wasm
Binary file not shown.
Binary file modified artifacts/incentives.wasm
Binary file not shown.
Binary file modified artifacts/lockup.wasm
Binary file not shown.
Binary file modified artifacts/nibi_stargate.wasm
Binary file not shown.
Binary file modified artifacts/nibi_stargate_perp.wasm
Binary file not shown.
Binary file modified artifacts/nusd_valuator.wasm
Binary file not shown.
Binary file modified artifacts/pricefeed.wasm
Binary file not shown.
Binary file modified artifacts/shifter.wasm
Binary file not shown.
Binary file modified artifacts/token_vesting.wasm
Binary file not shown.
19 changes: 0 additions & 19 deletions contracts/airdrop/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,6 @@ exclude = [
[lib]
crate-type = ["cdylib", "rlib"]

[profile.release]
opt-level = 3
debug = false
rpath = false
lto = true
debug-assertions = false
codegen-units = 1
panic = 'abort'
incremental = false
overflow-checks = true

[package.metadata.scripts]
optimize = """docker run --rm -v "$(pwd)":/code \
-e CARGO_TERM_COLOR=always \
--mount type=volume,source="$(basename "$(pwd)")_cache",target=/target \
--mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
cosmwasm/optimizer:0.15.0
"""

[features]
# for more explicit tests, cargo test --features=backtraces
backtraces = ["cosmwasm-std/backtraces"]
Expand Down
53 changes: 30 additions & 23 deletions contracts/airdrop/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ pub fn instantiate(
};
CAMPAIGN.save(deps.storage, &campaign)?;

return Ok(Response::new()
Ok(Response::new()
.add_attribute("method", "instantiate")
.add_attribute("owner", info.sender));
.add_attribute("owner", info.sender))
}

#[cfg_attr(not(feature = "library"), entry_point)]
Expand Down Expand Up @@ -95,6 +95,12 @@ pub fn execute(
}
}

/// Reward a set of users with native tokens from the campaign pool
///
/// - Requires sender to be the contract owner or a campaign manager.
/// - Ensures there are enough unallocated funds in the campaign
/// - Saves/updates user reward pool balances.
/// - Reduces the available campaign balance
pub fn reward_users(
deps: DepsMut,
_env: Env,
Expand All @@ -103,18 +109,16 @@ pub fn reward_users(
) -> Result<Response, StdError> {
let mut res = vec![];

let mut campaign = CAMPAIGN.load(deps.storage).map_err(|_| {
StdError::generic_err("Failed to load campaign data")
})?;
let mut campaign = CAMPAIGN
.load(deps.storage)
.map_err(|_| StdError::generic_err("Failed to load campaign data"))?;

if campaign.owner != info.sender
&& !campaign.managers.contains(&info.sender)
if campaign.owner != info.sender && !campaign.managers.contains(&info.sender)
{
return Err(StdError::generic_err("Unauthorized"));
}

for req in requests {

if campaign.unallocated_amount < req.amount {
return Err(StdError::generic_err(
"Not enough funds in the campaign",
Expand Down Expand Up @@ -148,11 +152,15 @@ pub fn reward_users(
});
}

return Ok(Response::new()
Ok(Response::new()
.add_attribute("method", "reward_users")
.set_data(to_json_binary(&res).unwrap()));
.set_data(to_json_binary(&res).unwrap()))
}

/// Allow a user to claim any rewards allocated to them
///
/// Transfers the user's full reward balance to their account. Resets their
/// reward balance to 0.
pub fn claim(
deps: DepsMut,
_env: Env,
Expand All @@ -162,8 +170,7 @@ pub fn claim(

match USER_REWARDS.may_load(deps.storage, info.sender.clone())? {
Some(user_reward) => {
USER_REWARDS
.remove(deps.storage, info.sender.clone());
USER_REWARDS.remove(deps.storage, info.sender.clone());

Ok(Response::new()
.add_attribute("method", "claim")
Expand All @@ -179,6 +186,10 @@ pub fn claim(
}
}

/// Allow the contract owner to withdraw native tokens
///
/// Ensures the requested amount is available in the contract balance. Transfers
/// tokens to the contract owner's account.
pub fn withdraw(
deps: DepsMut,
env: Env,
Expand All @@ -195,7 +206,7 @@ pub fn withdraw(

let own_balance: Uint128 = deps
.querier
.query_balance(&env.contract.address, bond_denom.clone())
.query_balance(env.contract.address, bond_denom.clone())
.map_err(|_| StdError::generic_err("Failed to query contract balance"))?
.amount;

Expand All @@ -213,7 +224,7 @@ pub fn withdraw(
}],
}));

return Ok(res);
Ok(res)
}

#[cfg_attr(not(feature = "library"), entry_point)]
Expand All @@ -228,10 +239,8 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {

pub fn query_campaign(deps: Deps, _env: Env) -> StdResult<Binary> {
match CAMPAIGN.load(deps.storage) {
Ok(campaign) => return to_json_binary(&campaign),
Err(_) => {
return Err(StdError::generic_err("Failed to load campaign data"))
}
Ok(campaign) => to_json_binary(&campaign),
Err(_) => Err(StdError::generic_err("Failed to load campaign data")),
}
}

Expand All @@ -241,9 +250,7 @@ pub fn query_user_reward(
user_address: Addr,
) -> StdResult<Binary> {
match USER_REWARDS.load(deps.storage, user_address) {
Ok(user_reward) => return to_json_binary(&user_reward),
Err(_) => {
return Err(StdError::generic_err("User reward does not exist"))
}
};
Ok(user_reward) => to_json_binary(&user_reward),
Err(_) => Err(StdError::generic_err("User reward does not exist")),
}
}
13 changes: 4 additions & 9 deletions contracts/airdrop/src/msg.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cosmwasm_std::{Uint128, Addr};
use cosmwasm_std::{Addr, Uint128};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
Expand All @@ -25,19 +25,14 @@ pub struct RewardUserResponse {
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum ExecuteMsg {
RewardUsers {
requests: Vec<RewardUserRequest>
},
RewardUsers { requests: Vec<RewardUserRequest> },
Claim {},
Withdraw {
amount: Uint128,
},
Withdraw { amount: Uint128 },
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum QueryMsg {
Campaign { },
Campaign {},
GetUserReward { user_address: Addr },
}

2 changes: 1 addition & 1 deletion contracts/airdrop/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cosmwasm_std::{Addr, Uint128};
use cw_storage_plus::{Map, Item};
use cw_storage_plus::{Item, Map};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

Expand Down
17 changes: 7 additions & 10 deletions contracts/airdrop/src/tests/execute/claim.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::contract::{claim, instantiate, reward_users};
use crate::msg::{InstantiateMsg, RewardUserRequest};
use crate::state::{USER_REWARDS};
use crate::state::USER_REWARDS;
use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info};
use cosmwasm_std::{coins, Addr, BankMsg, CosmosMsg, StdError, SubMsg, Uint128};
use std::vec;
Expand All @@ -18,7 +18,10 @@ fn test_claim() {
campaign_id: "campaign_id".to_string(),
campaign_name: "campaign_name".to_string(),
campaign_description: "campaign_description".to_string(),
managers: vec![Addr::unchecked("manager1"), Addr::unchecked("manager2")],
managers: vec![
Addr::unchecked("manager1"),
Addr::unchecked("manager2"),
],
},
)
.unwrap();
Expand Down Expand Up @@ -51,10 +54,7 @@ fn test_claim() {
amount: coins(750, ""),
}))]
);
assert_eq!(
USER_REWARDS.has(deps.as_ref().storage, Addr::unchecked("user1")),
false
);
assert!(!USER_REWARDS.has(deps.as_ref().storage, Addr::unchecked("user1")));

// try to claim from user2
let resp =
Expand All @@ -67,10 +67,7 @@ fn test_claim() {
amount: coins(250, ""),
}))]
);
assert_eq!(
USER_REWARDS.has(deps.as_ref().storage, Addr::unchecked("user2")),
false
);
assert!(!USER_REWARDS.has(deps.as_ref().storage, Addr::unchecked("user2")));

// try to claim from user3 who doesn't exist
let resp = claim(deps.as_mut(), env.clone(), mock_info("user3", &[]));
Expand Down
2 changes: 1 addition & 1 deletion contracts/airdrop/src/tests/execute/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
mod claim;
mod reward_users;
mod withdraw;
mod withdraw;
Loading

0 comments on commit 349daec

Please sign in to comment.