Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/rewarder clock #26

Merged
merged 3 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions contracts/oraiswap_rewarder/src/clock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{entry_point, Addr, StdResult};
use cosmwasm_std::{DepsMut, Env, Response};

use crate::contract::{distribute, read_staking_tokens};
use crate::state::{read_config, Config};

#[cw_serde]
pub enum SudoMsg {
// default message for Sudo, hard-coded in the Juno module: https://github.com/CosmosContracts/juno/blob/main/x/clock/types/msgs.go#L13
ClockEndBlock {},
}

// contract.rs
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn sudo(deps: DepsMut, env: Env, msg: SudoMsg) -> StdResult<Response> {
match msg {
SudoMsg::ClockEndBlock {} => {
if env.block.height % 100 != 0 {
return Ok(Response::new());
}
// Every 10 blocks this config value increases 1
let config: Config = read_config(deps.storage)?;
let staking_contract = deps.api.addr_humanize(&config.staking_contract)?;
let staking_tokens: Vec<Addr> = read_staking_tokens(&deps.querier, staking_contract)?
.into_iter()
.map(|token| deps.api.addr_validate(&token))
.collect::<StdResult<Vec<Addr>>>()?;
distribute(deps, env, staking_tokens)
}
}
}
27 changes: 23 additions & 4 deletions contracts/oraiswap_rewarder/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use crate::state::{
read_config, read_last_distributed, store_config, store_last_distributed, Config,
};

use oraiswap::staking::{ExecuteMsg as StakingExecuteMsg, RewardsPerSecResponse};
use oraiswap::staking::{
ExecuteMsg as StakingExecuteMsg, QueryPoolInfoResponse, RewardsPerSecResponse,
};
use oraiswap::staking::{QueryMsg as StakingQueryMsg, RewardMsg};

use oraiswap::rewarder::{
Expand Down Expand Up @@ -116,6 +118,10 @@ pub fn distribute(deps: DepsMut, env: Env, staking_tokens: Vec<Addr>) -> StdResu
staking_contract.clone(),
staking_token.clone(),
)?;
// no need to create a new distribute msg if the reward amount is 0
if reward_amount.is_zero() {
continue;
}

// get total reward amount for a pool
let distribution_amount = Uint128::from(reward_amount.u128() * (last_time_elapsed as u128));
Expand Down Expand Up @@ -190,10 +196,23 @@ fn _read_pool_reward_per_sec(
staking_contract: Addr,
staking_token: Addr,
) -> StdResult<Uint128> {
let res: RewardsPerSecResponse = querier.query_wasm_smart(
let res: StdResult<RewardsPerSecResponse> = querier.query_wasm_smart(
staking_contract,
&StakingQueryMsg::RewardsPerSec { staking_token },
)?;
);
if let Some(res) = res.ok() {
return Ok(res.assets.iter().map(|a| a.amount).sum());
} else {
Ok(Uint128::zero())
}
}

pub fn read_staking_tokens(
querier: &QuerierWrapper,
staking_contract: Addr,
) -> StdResult<Vec<String>> {
let res: Vec<QueryPoolInfoResponse> =
querier.query_wasm_smart(staking_contract, &StakingQueryMsg::GetPoolsInformation {})?;

Ok(res.assets.iter().map(|a| a.amount).sum())
Ok(res.into_iter().map(|res| res.asset_key).collect())
}
1 change: 1 addition & 0 deletions contracts/oraiswap_rewarder/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod clock;
pub mod contract;
pub mod state;

Expand Down