From 35f7f5a3fd80747c52de483e8d986285d5dcf425 Mon Sep 17 00:00:00 2001 From: Bucur David Date: Mon, 29 Jul 2024 10:53:11 +0300 Subject: [PATCH] feat: stake rewards to vault --- src/lib.rs | 40 ++++++++++++++++++++++++++++++++++++ src/life_bonding_sc_proxy.rs | 21 ++++++++++++++++++- wasm/src/lib.rs | 5 +++-- 3 files changed, 63 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9793a72..401f056 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -434,4 +434,44 @@ pub trait LifeBondingContract: self.total_bond_amount() .update(|value| *value += &payment.amount); } + + #[endpoint(stakeRewards)] + fn stake_rewards( + &self, + original_caller: ManagedAddress, + token_identifier: TokenIdentifier, + amount: BigUint, + ) { + let caller = self.blockchain().get_caller(); + require!( + caller == self.liveliness_stake_address().get(), + ERR_ENDPOINT_CALLABLE_ONLY_BY_ACCEPTED_CALLERS + ); + + require!( + !self + .address_vault_nonce(&caller, &token_identifier) + .is_empty(), + ERR_VAULT_NONCE_NOT_SET + ); + + let vault_nonce = self.address_vault_nonce(&caller, &token_identifier).get(); + + let bond_id = self + .bonds_ids() + .get_id_non_zero((token_identifier.clone(), vault_nonce)); + + let mut bond_cache = BondCache::new(self, bond_id); + + require!(bond_cache.address == original_caller, ERR_BOND_NOT_FOUND); + + let current_timestamp = self.blockchain().get_block_timestamp(); + + bond_cache.unbond_timestamp = current_timestamp + bond_cache.lock_period; + bond_cache.bond_timestamp = current_timestamp; + bond_cache.bond_amount += &amount; + bond_cache.remaining_amount += &amount; + + self.total_bond_amount().update(|value| *value += amount); + } } diff --git a/src/life_bonding_sc_proxy.rs b/src/life_bonding_sc_proxy.rs index 64b5db8..93cab18 100644 --- a/src/life_bonding_sc_proxy.rs +++ b/src/life_bonding_sc_proxy.rs @@ -189,6 +189,25 @@ where .original_result() } + pub fn stake_rewards< + Arg0: ProxyArg>, + Arg1: ProxyArg>, + Arg2: ProxyArg>, + >( + self, + original_caller: Arg0, + token_identifier: Arg1, + amount: Arg2, + ) -> TxTypedCall { + self.wrapped_tx + .payment(NotPayable) + .raw_call("stakeRewards") + .argument(&original_caller) + .argument(&token_identifier) + .argument(&amount) + .original_result() + } + pub fn compensation_blacklist< Arg0: ProxyArg, >( @@ -837,7 +856,7 @@ where } #[type_abi] -#[derive(TopEncode, TopDecode, NestedDecode, NestedEncode)] +#[derive(TopEncode, TopDecode)] pub struct ContractConfiguration where Api: ManagedTypeApi, diff --git a/wasm/src/lib.rs b/wasm/src/lib.rs index 4c2caa1..f08929f 100644 --- a/wasm/src/lib.rs +++ b/wasm/src/lib.rs @@ -6,9 +6,9 @@ // Init: 1 // Upgrade: 1 -// Endpoints: 55 +// Endpoints: 56 // Async Callback (empty): 1 -// Total number of exported functions: 58 +// Total number of exported functions: 59 #![no_std] @@ -27,6 +27,7 @@ multiversx_sc_wasm_adapter::endpoints! { claimRefund => claim_refund setVaultNonce => set_vault_nonce topUpVault => top_up_vault + stakeRewards => stake_rewards getCompensationBlacklist => compensation_blacklist getTotalBondAmount => total_bond_amount getAddressVaultNone => address_vault_nonce