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

improve onchain claim reset action #98

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
48 changes: 36 additions & 12 deletions contracts/on-chain-claim/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,7 @@ pub trait OnChainClaimContract:

#[endpoint(claim)]
fn claim(&self) {
let caller = self.blockchain().get_caller();
require!(
!self.blockchain().is_smart_contract(&caller),
"Only user accounts can perform claim"
);
self.require_same_shard(&caller);

let caller = self.get_caller();
let current_epoch = self.blockchain().get_block_epoch();

let address_info_mapper = self.address_info(&caller);
Expand All @@ -51,12 +45,12 @@ pub trait OnChainClaimContract:
"epoch already claimed"
);

if address_info.last_epoch_claimed + 1 == current_epoch {
address_info.current_streak += 1;
} else {
address_info.current_streak = 1;
}
require!(
address_info.last_epoch_claimed + 1 == current_epoch,
"missed epoch"
);

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

Expand All @@ -68,6 +62,26 @@ pub trait OnChainClaimContract:
});
}

#[endpoint(reset)]
fn reset(&self) {
let caller = self.get_caller();
let current_epoch = self.blockchain().get_block_epoch();

let address_info_mapper = self.address_info(&caller);
require!(
!address_info_mapper.is_empty(),
"can't reset streak for new address"
);

address_info_mapper.update(|address_info| {
address_info.current_streak = 1;
address_info.total_epochs_claimed += 1;
address_info.last_epoch_claimed = current_epoch;

self.new_claim_event(&caller, address_info);
});
}

#[payable("*")]
#[endpoint(claimAndRepair)]
fn claim_and_repair(&self) {
Expand Down Expand Up @@ -173,4 +187,14 @@ pub trait OnChainClaimContract:
);
self.repair_streak_payment().set(payment);
}

fn get_caller(&self) -> ManagedAddress {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommendation for renaming: get_non_sc_caller

let caller = self.blockchain().get_caller();
require!(
!self.blockchain().is_smart_contract(&caller),
"Only user accounts can perform claim"
);
self.require_same_shard(&caller);
caller
}
}
16 changes: 13 additions & 3 deletions contracts/on-chain-claim/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,20 @@ fn check_after_claim() {
assert_eq!(address_info.last_epoch_claimed, 21);
})
.set_state_step(SetStateStep::new().block_epoch(25))
.whitebox_call_check(
&on_chain_claim_whitebox,
ScCallStep::new().from(USER1_ADDR).to(SC_ADDR).no_expect(),
|sc| {
sc.claim();
},
|r| {
r.assert_user_error("missed epoch");
},
)
.whitebox_call(
&on_chain_claim_whitebox,
ScCallStep::new().from(USER1_ADDR),
|sc| sc.claim(),
|sc| sc.reset(),
)
.whitebox_query(&on_chain_claim_whitebox, |sc| {
let address = AddressValue::from(USER1_ADDR).to_address();
Expand Down Expand Up @@ -552,7 +562,7 @@ fn test_best_streak() {
.whitebox_call(
&on_chain_claim_whitebox,
ScCallStep::new().from(USER1_ADDR),
|sc| sc.claim(),
|sc| sc.reset(),
)
.whitebox_query(&on_chain_claim_whitebox, |sc| {
let address = AddressValue::from(USER1_ADDR).to_address();
Expand Down Expand Up @@ -725,7 +735,7 @@ fn on_chain_claim_whitebox() {
.whitebox_call(
&on_chain_claim_whitebox,
ScCallStep::new().from(USER1_ADDR),
|sc| sc.claim(),
|sc| sc.reset(),
)
.whitebox_query(&on_chain_claim_whitebox, |sc| {
let address = AddressValue::from(USER1_ADDR).to_address();
Expand Down
5 changes: 3 additions & 2 deletions contracts/on-chain-claim/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

// Init: 1
// Upgrade: 1
// Endpoints: 11
// Endpoints: 12
// Async Callback (empty): 1
// Total number of exported functions: 14
// Total number of exported functions: 15

#![no_std]

Expand All @@ -21,6 +21,7 @@ multiversx_sc_wasm_adapter::endpoints! {
init => init
upgrade => upgrade
claim => claim
reset => reset
claimAndRepair => claim_and_repair
updateState => update_state
setRepairStreakPayment => set_repair_streak_payment
Expand Down
Loading