Skip to content

Commit

Permalink
Upgraded packages + new best streak field + increase max gap to 5
Browse files Browse the repository at this point in the history
  • Loading branch information
vladbucur1 committed Jan 8, 2024
1 parent eeeec30 commit bff6b6a
Show file tree
Hide file tree
Showing 8 changed files with 335 additions and 167 deletions.
4 changes: 2 additions & 2 deletions contracts/on-chain-claim/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ publish = false
path = "src/contract.rs"

[dependencies.multiversx-sc]
version = "0.45.1"
version = "0.45.2"

[dev-dependencies.multiversx-sc-scenario]
version = "0.45.1"
version = "0.45.2"
4 changes: 2 additions & 2 deletions contracts/on-chain-claim/meta/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ name = "on-chain-claim-meta"
version = "0.0.0"
edition = "2018"
publish = false
authors = [ "you",]
authors = [ "Vlad Bucur <[email protected]>"]

[dev-dependencies]

[dependencies.on-chain-claim]
path = ".."

[dependencies.multiversx-sc-meta]
version = "0.45.1"
version = "0.45.2"
default-features = false
3 changes: 3 additions & 0 deletions contracts/on-chain-claim/src/address_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub struct AddressInfo {
pub current_streak: u64,
pub last_epoch_claimed: u64,
pub total_epochs_claimed: u64,
pub best_streak: u64,
}

impl AddressInfo {
Expand All @@ -14,11 +15,13 @@ impl AddressInfo {
current_streak: u64,
last_epoch_claimed: u64,
total_epochs_claimed: u64,
best_streak: u64,
) -> Self {
AddressInfo {
current_streak,
last_epoch_claimed,
total_epochs_claimed,
best_streak,
}
}
}
14 changes: 12 additions & 2 deletions contracts/on-chain-claim/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@ multiversx_sc::derive_imports!();
use crate::address_info::*;

pub const SFT_AMOUNT: u64 = 1;
pub const MAX_REPAIR_GAP: u64 = 2;
pub const MAX_REPAIR_GAP: u64 = 5;

#[multiversx_sc::module]
pub trait ConfigModule {
fn get_missed_epochs(&self, current_epoch: u64, last_epoch_claimed: u64) -> u64 {
if current_epoch - last_epoch_claimed <= 1 {
return 0;
}

current_epoch - last_epoch_claimed - 1
}

#[view(canBeRepaired)]
fn can_be_repaired(&self, address: &ManagedAddress) -> bool {
let address_info_mapper = self.address_info(address);
Expand All @@ -16,8 +24,10 @@ pub trait ConfigModule {
}

let address_info = address_info_mapper.get();
let current_epoch = self.blockchain().get_block_epoch();
let missed_epochs = self.get_missed_epochs(current_epoch, address_info.last_epoch_claimed);

address_info.last_epoch_claimed + MAX_REPAIR_GAP == self.blockchain().get_block_epoch()
missed_epochs > 0 && missed_epochs <= MAX_REPAIR_GAP
}

#[view(getAddressInfo)]
Expand Down
27 changes: 21 additions & 6 deletions contracts/on-chain-claim/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub trait OnChainClaimContract: config::ConfigModule {

let address_info_mapper = self.address_info(&caller);
if address_info_mapper.is_empty() {
let address_info = AddressInfo::new(1, current_epoch, 1);
let address_info = AddressInfo::new(1, current_epoch, 1, 1);
self.address_info(&caller).set(address_info);
return;
}
Expand All @@ -50,6 +50,10 @@ pub trait OnChainClaimContract: config::ConfigModule {

address_info.total_epochs_claimed += 1;
address_info.last_epoch_claimed = current_epoch;

if address_info.best_streak < address_info.current_streak {
address_info.best_streak = address_info.current_streak;
}
});
}

Expand Down Expand Up @@ -79,14 +83,20 @@ pub trait OnChainClaimContract: config::ConfigModule {
);

address_info_mapper.update(|address_info| {
let missed_epochs =
self.get_missed_epochs(current_epoch, address_info.last_epoch_claimed);

require!(
address_info.last_epoch_claimed + MAX_REPAIR_GAP == current_epoch,
missed_epochs > 0 && missed_epochs <= MAX_REPAIR_GAP,
"can't repair streak for current epoch"
);

address_info.current_streak += MAX_REPAIR_GAP;
address_info.total_epochs_claimed += MAX_REPAIR_GAP;
address_info.current_streak += missed_epochs + 1;
address_info.total_epochs_claimed += missed_epochs + 1;
address_info.last_epoch_claimed = current_epoch;
if address_info.best_streak < address_info.current_streak {
address_info.best_streak = address_info.current_streak;
}
});

self.send().esdt_local_burn(
Expand All @@ -104,9 +114,14 @@ pub trait OnChainClaimContract: config::ConfigModule {
current_streak: u64,
last_epoch_claimed: u64,
total_epochs_claimed: u64,
best_streak: u64,
) {
let address_info =
AddressInfo::new(current_streak, last_epoch_claimed, total_epochs_claimed);
let address_info = AddressInfo::new(
current_streak,
last_epoch_claimed,
total_epochs_claimed,
best_streak,
);
self.address_info(address).set(address_info);
}
}
Loading

0 comments on commit bff6b6a

Please sign in to comment.