Skip to content

Commit

Permalink
Add nonpayable checks
Browse files Browse the repository at this point in the history
Update / standardize comments
  • Loading branch information
maurolacy committed Jul 4, 2023
1 parent 9b4b596 commit c224b82
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 18 deletions.
4 changes: 2 additions & 2 deletions contracts/consumer/converter/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl ConverterContract<'_> {
Ok(Response::new())
}

/// This is only used for tests
/// This is only used for tests.
/// Ideally we want conditional compilation of these whole methods and the enum variants
#[msg(exec)]
fn test_stake(
Expand All @@ -133,7 +133,7 @@ impl ConverterContract<'_> {
}
}

/// This is only used for tests
/// This is only used for tests.
/// Ideally we want conditional compilation of these whole methods and the enum variants
#[msg(exec)]
fn test_unstake(
Expand Down
2 changes: 2 additions & 0 deletions contracts/consumer/simple-price-feed/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ impl SimplePriceFeedContract<'_> {
ctx: ExecCtx,
native_per_foreign: Decimal,
) -> Result<Response, ContractError> {
nonpayable(&ctx.info)?;

let mut config = self.config.load(ctx.deps.storage)?;
config.native_per_foreign = native_per_foreign;
self.config.save(ctx.deps.storage, &config)?;
Expand Down
2 changes: 1 addition & 1 deletion contracts/consumer/virtual-staking/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ impl VirtualStakingApi for VirtualStakingContract<'_> {

/// Requests to unbond tokens from a validator. This will be actually handled at the next epoch.
/// If the virtual staking module is over the max cap, it will trigger a rebalance in addition to unbond.
/// If the virtual staking contract doesn't have at least amont tokens staked to the given validator, this will return an error.
/// If the virtual staking contract doesn't have at least amount tokens staked to the given validator, this will return an error.
#[msg(exec)]
fn unbond(
&self,
Expand Down
47 changes: 32 additions & 15 deletions contracts/provider/external-staking/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use cosmwasm_std::{
};
use cw2::set_contract_version;
use cw_storage_plus::{Bounder, Item, Map};
use cw_utils::PaymentError;
use cw_utils::{nonpayable, PaymentError};

use sylvia::contract;
use sylvia::types::{ExecCtx, InstantiateCtx, QueryCtx};
Expand Down Expand Up @@ -123,8 +123,8 @@ impl ExternalStakingContract<'_> {
Ok(Response::new())
}

/// In test code, this is called from test_commit_stake.
/// In non-test code, this is called from ibc_packet_ack
/// In test code, this is called from `test_commit_stake`.
/// In non-test code, this is called from `ibc_packet_ack`
pub(crate) fn commit_stake(&self, deps: DepsMut, tx_id: u64) -> Result<WasmMsg, ContractError> {
// Load tx
let tx = self.pending_txs.load(deps.storage, tx_id)?;
Expand Down Expand Up @@ -182,8 +182,8 @@ impl ExternalStakingContract<'_> {
Ok(msg)
}

/// In test code, this is called from test_rollback_stake.
/// In non-test code, this is called from ibc_packet_ack or ibc_packet_timeout
/// In test code, this is called from `test_rollback_stake`.
/// In non-test code, this is called from `ibc_packet_ack` or `ibc_packet_timeout`
pub(crate) fn rollback_stake(
&self,
deps: DepsMut,
Expand Down Expand Up @@ -259,7 +259,7 @@ impl ExternalStakingContract<'_> {
}
}

/// Rollbacks a pending stake.
/// Updates the active validator set.
/// Method used for tests only.
#[msg(exec)]
fn test_set_active_validator(
Expand Down Expand Up @@ -291,15 +291,17 @@ impl ExternalStakingContract<'_> {
}
}

/// Schedules tokens for release, adding them to the pending unbonds. After `unbonding_period`
/// passes, funds are ready to be released with `withdraw_unbonded` call by the user
/// Schedules tokens for release, adding them to the pending unbonds. After the unbonding period
/// passes, funds are ready to be released through a `withdraw_unbonded` call by the user.
#[msg(exec)]
pub fn unstake(
&self,
ctx: ExecCtx,
validator: String,
amount: Coin,
) -> Result<Response, ContractError> {
nonpayable(&ctx.info)?;

let config = self.config.load(ctx.deps.storage)?;

ensure_eq!(
Expand Down Expand Up @@ -369,8 +371,8 @@ impl ExternalStakingContract<'_> {
Ok(resp)
}

/// In test code, this is called from test_commit_unstake.
/// Method used for tests only.
/// In test code, this is called from `test_commit_unstake`.
/// In non-test code, this is called from `ibc_packet_ack`
pub(crate) fn commit_unstake(
&self,
deps: DepsMut,
Expand Down Expand Up @@ -442,8 +444,8 @@ impl ExternalStakingContract<'_> {
Ok(())
}

/// In test code, this is called from test_rollback_unstake.
/// In non-test code, this is called from ibc_packet_ack or ibc_packet_timeout
/// In test code, this is called from `test_rollback_unstake`.
/// In non-test code, this is called from `ibc_packet_ack` or `ibc_packet_timeout`
pub(crate) fn rollback_unstake(&self, deps: DepsMut, tx_id: u64) -> Result<(), ContractError> {
// Load tx
let tx = self.pending_txs.load(deps.storage, tx_id)?;
Expand Down Expand Up @@ -512,10 +514,12 @@ impl ExternalStakingContract<'_> {

/// Withdraws all released tokens to the sender.
///
/// Tokens to be claimed has to be unbond before by calling the `unbond` message and
/// waiting the `unbond_period`
/// Tokens to be claimed have to be unbond before by calling the `unbond` message, and
/// their unbonding period must have passed.
#[msg(exec)]
pub fn withdraw_unbonded(&self, ctx: ExecCtx) -> Result<Response, ContractError> {
nonpayable(&ctx.info)?;

let config = self.config.load(ctx.deps.storage)?;

let stake_locks: Vec<_> = self
Expand Down Expand Up @@ -563,6 +567,8 @@ impl ExternalStakingContract<'_> {
Ok(resp)
}

/// Distribute rewards.
/// Method used for tests only.
#[msg(exec)]
pub fn test_distribute_rewards(
&self,
Expand All @@ -584,7 +590,8 @@ impl ExternalStakingContract<'_> {

/// Distributes reward among users staking via particular validator. Distribution is performed
/// proportionally to amount of tokens staked by user.
/// This is called by IBC packets in real code, but also exposed in a test only message "test_distribute_rewards"
/// In test code, this is called from `test_distribute_rewards`.
/// In non-test code, this is called from `ibc_packet_receive`
pub(crate) fn distribute_rewards(
&self,
deps: DepsMut,
Expand Down Expand Up @@ -632,6 +639,8 @@ impl ExternalStakingContract<'_> {
/// Address on the consumer side to receive the rewards
remote_recipient: String,
) -> Result<Response, ContractError> {
nonpayable(&ctx.info)?;

let mut stake_lock = self
.stakes
.may_load(ctx.deps.storage, (&ctx.info.sender, &validator))?
Expand Down Expand Up @@ -705,6 +714,8 @@ impl ExternalStakingContract<'_> {
Ok(resp)
}

/// Commits a withdraw rewards transaction.
/// Method used for tests only.
#[msg(exec)]
fn test_commit_withdraw_rewards(
&self,
Expand All @@ -723,6 +734,8 @@ impl ExternalStakingContract<'_> {
}
}

/// Rollbacks a withdraw rewards transaction.
/// Method used for tests only.
#[msg(exec)]
fn test_rollback_withdraw_rewards(
&self,
Expand All @@ -741,6 +754,8 @@ impl ExternalStakingContract<'_> {
}
}

/// In test code, this is called from `test_rollback_withdraw_rewards`.
/// In non-test code, this is called from `ibc_packet_ack` or `ibc_packet_timeout`
pub(crate) fn rollback_withdraw_rewards(
&self,
deps: DepsMut,
Expand Down Expand Up @@ -769,6 +784,8 @@ impl ExternalStakingContract<'_> {
Ok(())
}

/// In test code, this is called from `test_commit_withdraw_rewards`.
/// In non-test code, this is called from `ibc_packet_ack`
pub(crate) fn commit_withdraw_rewards(
&self,
deps: DepsMut,
Expand Down
5 changes: 5 additions & 0 deletions contracts/provider/external-staking/src/crdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ impl<'a> CrdtState<'a> {
}
}

/// Add / Update a validator.
/// In test code, this is called from `test_set_active_validator`.
/// In non-test code, this is called from `ibc_packet_receive`
pub fn add_validator(
&self,
storage: &mut dyn Storage,
Expand All @@ -83,6 +86,8 @@ impl<'a> CrdtState<'a> {
self.validators.save(storage, valoper, &state)
}

/// Remove a validator.
/// In non-test code, this is called from `ibc_packet_receive`
pub fn remove_validator(
&self,
storage: &mut dyn Storage,
Expand Down

0 comments on commit c224b82

Please sign in to comment.