Skip to content

Commit

Permalink
implemented all remaining transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
jstuczyn committed Feb 6, 2025
1 parent f494acf commit 2ac260e
Show file tree
Hide file tree
Showing 11 changed files with 211 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use crate::ContractBuildInformation;
use cosmwasm_std::{StdError, StdResult, Storage};
use cosmwasm_std::{StdResult, Storage};
use cw_storage_plus::Item;

pub const CONTRACT_BUILD_INFO_STORAGE_KEY: &str = "contract_build_info";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,13 @@ pub enum NymPoolContractError {

#[error("attempted to spend more tokens than permitted by the current allowance")]
SpendingAboveAllowance,

#[error("attempted to send an empty allowance usage request")]
EmptyUsageRequest,

#[error("the associated grant has already expired")]
GrantExpired,

#[error("the associated grant hasn't expired yet")]
GrantNotExpired,
}
3 changes: 3 additions & 0 deletions common/cosmwasm-smart-contracts/nym-pool-contract/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ pub enum ExecuteMsg {

/// Attempt to withdraw the specified amount of locked tokens into the grantee's account
WithdrawLockedAllowance { amount: Coin },

/// Attempt to remove expired grant from the storage and unlock (if any) locked tokens
RemoveExpiredGrant { grantee: String },
}

#[cw_serde]
Expand Down
11 changes: 9 additions & 2 deletions common/cosmwasm-smart-contracts/nym-pool-contract/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ pub mod grants {
}
}

fn within_spendable_limits(&self, amount: &Coin) -> bool {
pub fn within_spendable_limits(&self, amount: &Coin) -> bool {
match self {
Allowance::Basic(allowance) => allowance.within_spendable_limits(amount),
Allowance::ClassicPeriodic(allowance) => allowance.within_spendable_limits(amount),
Expand All @@ -126,7 +126,7 @@ pub mod grants {

// check whether given the current allowance state, the provided amount could be spent
// note: it's responsibility of the caller to call `try_update_state` before the call.
fn can_spend(&self, env: &Env, amount: &Coin) -> bool {
pub fn can_spend(&self, env: &Env, amount: &Coin) -> bool {
match self {
Allowance::Basic(allowance) => allowance.can_spend(env, amount),
Allowance::ClassicPeriodic(allowance) => allowance.can_spend(env, amount),
Expand All @@ -145,6 +145,13 @@ pub mod grants {
Allowance::Delayed(allowance) => allowance.try_spend(env, amount),
}
}

pub fn is_used_up(&self) -> bool {
let Some(ref limit) = self.basic().spend_limit else {
return false;
};
limit.amount.is_zero()
}
}

/// BasicAllowance is an allowance with a one-time grant of coins
Expand Down
7 changes: 4 additions & 3 deletions contracts/nym-pool/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use crate::queries::{
};
use crate::storage::NYM_POOL_STORAGE;
use crate::transactions::{
try_grant_allowance, try_lock_allowance, try_revoke_grant, try_unlock_allowance,
try_update_contract_admin, try_use_allowance, try_use_locked_allowance, try_withdraw_allowance,
try_withdraw_locked_allowance,
try_grant_allowance, try_lock_allowance, try_remove_expired, try_revoke_grant,
try_unlock_allowance, try_update_contract_admin, try_use_allowance, try_use_locked_allowance,
try_withdraw_allowance, try_withdraw_locked_allowance,
};
use cosmwasm_std::{
entry_point, to_json_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response,
Expand Down Expand Up @@ -60,6 +60,7 @@ pub fn execute(
ExecuteMsg::WithdrawLockedAllowance { amount } => {
try_withdraw_locked_allowance(deps, env, info, amount)
}
ExecuteMsg::RemoveExpiredGrant { grantee } => try_remove_expired(deps, env, info, grantee),
}
}

Expand Down
23 changes: 23 additions & 0 deletions contracts/nym-pool/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2025 - Nym Technologies SA <[email protected]>
// SPDX-License-Identifier: Apache-2.0

use crate::storage::NYM_POOL_STORAGE;
use cosmwasm_std::{Coin, Storage};
use nym_pool_contract_common::NymPoolContractError;

pub fn validate_usage_coin(storage: &dyn Storage, coin: &Coin) -> Result<(), NymPoolContractError> {
let denom = NYM_POOL_STORAGE.pool_denomination.load(storage)?;

if coin.amount.is_zero() {
return Err(NymPoolContractError::EmptyUsageRequest);
}

if coin.denom != denom {
return Err(NymPoolContractError::InvalidDenom {
expected: denom,
got: coin.denom.to_string(),
});
}

Ok(())
}
1 change: 1 addition & 0 deletions contracts/nym-pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod contract;
pub mod queued_migrations;
pub mod storage;

mod helpers;
mod queries;
#[cfg(test)]
pub mod testing;
Expand Down
2 changes: 1 addition & 1 deletion contracts/nym-pool/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use crate::storage::{retrieval_limits, NYM_POOL_STORAGE};
use cosmwasm_std::{Coin, Deps, Env, Order, StdResult, Uint128};
use cosmwasm_std::{Coin, Deps, Env, Order, StdResult};
use cw_controllers::AdminResponse;
use cw_storage_plus::Bound;
use nym_pool_contract_common::{
Expand Down
54 changes: 35 additions & 19 deletions contracts/nym-pool/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl NymPoolStorage {
// add all initial grants
for (grantee, allowance) in initial_grants {
let grantee = deps.api.addr_validate(&grantee)?;
self.add_grant(deps.branch(), &env, &admin, grantee, allowance)?;
self.insert_new_grant(deps.branch(), &env, &admin, grantee, allowance)?;
}

// set the denom
Expand Down Expand Up @@ -174,7 +174,7 @@ impl NymPoolStorage {
})
}

pub fn add_grant(
pub fn insert_new_grant(
&self,
deps: DepsMut,
env: &Env,
Expand Down Expand Up @@ -231,12 +231,39 @@ impl NymPoolStorage {
pub fn update_grant(
&self,
deps: DepsMut,
env: Env,
grantee_address: GranteeAddress,
grant: Grant,
) -> Result<(), NymPoolContractError> {
let grant = self.load_grant(deps.as_ref(), &grantee_address)?;
let locked = self
.locked
.grantee_locked(deps.storage, grantee_address.clone())?;

// if we used up all allowance and have no locked tokens, we can just remove the grant from storage
if grant.allowance.is_used_up() && locked.is_zero() {
self.grants.remove(deps.storage, grantee_address)
} else {
self.grants.save(deps.storage, grantee_address, &grant)?;
}

Ok(())
}

pub fn remove_grant(
&self,
deps: DepsMut,
grantee_address: GranteeAddress,
) -> Result<(), NymPoolContractError> {
self.grants.remove(deps.storage, grantee_address.clone());

// if there are any tokens still locked associated with this grantee, unlock them
if let Some(grantee_locked) = self
.locked
.maybe_grantee_locked(deps.storage, grantee_address.clone())?
{
self.locked.unlock(deps, grantee_address, grantee_locked)?;
}

todo!()
Ok(())
}

pub fn revoke_grant(
Expand Down Expand Up @@ -264,18 +291,7 @@ impl NymPoolStorage {
return Err(NymPoolContractError::UnauthorizedGrantRevocation);
}

self.grants.remove(deps.storage, grantee_address.clone());

// if there are any tokens still locked associated with this grantee, unlock them
let grantee_locked = self
.locked
.grantee_locked(deps.storage, grantee_address.clone())?;
if !grantee_locked.is_zero() {
self.locked
.unlock(deps, grantee_address.clone(), grantee_locked)?;
}

Ok(())
self.remove_grant(deps, grantee_address)
}
}

Expand Down Expand Up @@ -318,7 +334,7 @@ impl LockedStorage {

/// unconditionally attempts to load specified amount of tokens for the particular grantee
/// it does not validate permissions nor allowances - that's up to the caller
fn lock(
pub(super) fn lock(
&self,
deps: DepsMut,
grantee: GranteeAddress,
Expand All @@ -336,7 +352,7 @@ impl LockedStorage {
Ok(())
}

fn unlock(
pub(super) fn unlock(
&self,
deps: DepsMut,
grantee: GranteeAddress,
Expand Down
2 changes: 1 addition & 1 deletion contracts/nym-pool/src/testing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ impl TestSetup {
let granter = self.admin_unchecked();
let env = self.env();
NYM_POOL_STORAGE
.add_grant(
.insert_new_grant(
self.deps_mut(),
&env,
&granter,
Expand Down
Loading

0 comments on commit 2ac260e

Please sign in to comment.