Skip to content

Commit

Permalink
feat: add flat-fee bindings (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
aelesbao committed Aug 10, 2023
2 parents f6daf0b + ad3047e commit 1c25fd8
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 6 deletions.
2 changes: 1 addition & 1 deletion contracts/increment/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ optimize = """docker run --rm -v "$(pwd)":/code \
[dependencies]
archway-bindings = { version = "0.1.0", path = "../../packages/bindings" }
cosmwasm-schema = { workspace = true }
cosmwasm-std = { workspace = true }
cosmwasm-std = { version = "1.1.9", features = ["staking", "stargate"] }
cosmwasm-storage = { workspace = true }
cw-storage-plus = { workspace = true }
cw-utils = { workspace = true }
Expand Down
33 changes: 30 additions & 3 deletions contracts/increment/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use archway_bindings::types::gov::VoteResponse;
use archway_bindings::types::rewards::{
ContractMetadataResponse, RewardsRecordsResponse, WithdrawRewardsResponse,
ContractMetadataResponse, FlatFeeResponse, RewardsRecordsResponse, WithdrawRewardsResponse,
};
use archway_bindings::{ArchwayMsg, ArchwayQuery, ArchwayResult, PageRequest};
use cosmwasm_std::{
entry_point, to_binary, Addr, Binary, Deps, DepsMut, Env, MessageInfo, Reply, Response,
StdError, StdResult, SubMsg,
coin, entry_point, to_binary, Addr, Binary, Deps, DepsMut, Env, MessageInfo, Reply, Response,
StdError, StdResult, SubMsg, Uint128,
};
use cw2::set_contract_version;
use cw_utils::NativeBalance;
Expand Down Expand Up @@ -54,6 +54,9 @@ pub fn execute(
ExecuteMsg::UpdateRewardsAddress { rewards_address } => {
update_rewards_address(env, rewards_address)
}
ExecuteMsg::SetFlatFee { amount } => {
set_flat_fee(deps.as_ref(), env.contract.address, amount)
}
ExecuteMsg::WithdrawRewards {} => withdraw_rewards(),
}
}
Expand Down Expand Up @@ -97,6 +100,21 @@ pub fn update_rewards_address(
Ok(res)
}

fn set_flat_fee(
deps: Deps<ArchwayQuery>,
contract_address: Addr,
amount: Uint128,
) -> ArchwayResult<ContractError> {
let denom = deps.querier.query_bonded_denom()?;
let msg = ArchwayMsg::set_flat_fee(contract_address, coin(amount.u128(), denom));

let res = Response::new()
.add_message(msg)
.add_attribute("method", "set_fees");

Ok(res)
}

fn withdraw_rewards() -> ArchwayResult<ContractError> {
let msg = ArchwayMsg::withdraw_rewards_by_limit(0);

Expand Down Expand Up @@ -146,6 +164,7 @@ pub fn query(deps: Deps<ArchwayQuery>, env: Env, msg: QueryMsg) -> StdResult<Bin
deps,
contract_address.unwrap_or(env.contract.address),
)?),
QueryMsg::FlatFee {} => to_binary(&flat_fee(deps, env.contract.address)?),
QueryMsg::OutstandingRewards {} => to_binary(&outstanding_rewards(deps, env)?),
QueryMsg::GovVote { proposal_id, voter } => to_binary(&gov_vote(deps, proposal_id, voter)?),
}
Expand All @@ -164,6 +183,14 @@ fn contract_metadata(
deps.querier.query(&req)
}

fn flat_fee(
deps: Deps<ArchwayQuery>,
contract_address: impl Into<String>,
) -> StdResult<FlatFeeResponse> {
let req = ArchwayQuery::flat_fee(contract_address).into();
deps.querier.query(&req)
}

fn outstanding_rewards(
deps: Deps<ArchwayQuery>,
env: Env,
Expand Down
5 changes: 4 additions & 1 deletion contracts/increment/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use archway_bindings::{
Coins,
};
use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::Addr;
use cosmwasm_std::{Addr, Uint128};

#[cw_serde]
pub struct InstantiateMsg {
Expand All @@ -15,6 +15,7 @@ pub enum ExecuteMsg {
Increment {},
Reset { count: i32 },
UpdateRewardsAddress { rewards_address: Option<Addr> },
SetFlatFee { amount: Uint128 },
WithdrawRewards {},
}

Expand All @@ -25,6 +26,8 @@ pub enum QueryMsg {
GetCount {},
#[returns(rewards::ContractMetadataResponse)]
Metadata { contract_address: Option<Addr> },
#[returns(rewards::FlatFeeResponse)]
FlatFee {},
#[returns(OutstandingRewardsResponse)]
OutstandingRewards {},
#[returns(gov::VoteResponse)]
Expand Down
13 changes: 12 additions & 1 deletion packages/bindings/src/msg.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{CosmosMsg, CustomMsg};
use cosmwasm_std::{Coin, CosmosMsg, CustomMsg};

#[cw_serde]
pub enum ArchwayMsg {
Expand All @@ -8,6 +8,10 @@ pub enum ArchwayMsg {
owner_address: Option<String>,
rewards_address: Option<String>,
},
SetFlatFee {
contract_address: Option<String>,
flat_fee_amount: Coin,
},
WithdrawRewards {
records_limit: Option<u64>,
record_ids: Vec<u64>,
Expand Down Expand Up @@ -61,6 +65,13 @@ impl ArchwayMsg {
}
}

pub fn set_flat_fee(contract_address: impl Into<String>, amount: Coin) -> Self {
ArchwayMsg::SetFlatFee {
contract_address: Some(contract_address.into()),
flat_fee_amount: amount,
}
}

pub fn withdraw_rewards_by_limit(limit: u64) -> Self {
ArchwayMsg::WithdrawRewards {
records_limit: Some(limit),
Expand Down
8 changes: 8 additions & 0 deletions packages/bindings/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use crate::types::{gov, rewards};
pub enum ArchwayQuery {
#[returns(rewards::ContractMetadataResponse)]
ContractMetadata { contract_address: String },
#[returns(rewards::FlatFeeResponse)]
FlatFee { contract_address: String },
#[returns(rewards::RewardsRecordsResponse)]
RewardsRecords {
rewards_address: String,
Expand All @@ -27,6 +29,12 @@ impl ArchwayQuery {
}
}

pub fn flat_fee(contract_address: impl Into<String>) -> Self {
ArchwayQuery::FlatFee {
contract_address: contract_address.into(),
}
}

pub fn rewards_records(rewards_address: impl Into<String>) -> Self {
ArchwayQuery::RewardsRecords {
rewards_address: rewards_address.into(),
Expand Down
6 changes: 6 additions & 0 deletions packages/bindings/src/types/rewards.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use cosmwasm_schema::cw_serde;
use cosmwasm_std::Coin;

use crate::{Coins, PageResponse};

Expand All @@ -14,6 +15,11 @@ pub struct ContractMetadataResponse {
pub rewards_address: String,
}

#[cw_serde]
pub struct FlatFeeResponse {
pub flat_fee_amount: Coin,
}

#[cw_serde]
pub struct RewardsRecordsResponse {
pub records: Vec<RewardsRecord>,
Expand Down

0 comments on commit 1c25fd8

Please sign in to comment.