Skip to content

Commit

Permalink
fix reward amount not getting changed
Browse files Browse the repository at this point in the history
  • Loading branch information
alyn509 committed Dec 16, 2024
1 parent e6093b1 commit 15f9b29
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"step": "checkState",
"accounts": {
"address:my_address": {
"nonce": "3",
"nonce": "2",
"balance": "1,000,000",
"storage": {}
},
Expand All @@ -74,7 +74,13 @@
"sc:lottery#42": {
"nonce": "0",
"balance": "0",
"storage": {},
"storage": {
"str:addressToIdMapper|str:lastId": "2",
"str:addressToIdMapper|str:addr|address:acc1": "1",
"str:addressToIdMapper|str:addrId|u64:1": "address:acc1",
"str:addressToIdMapper|str:addr|address:acc2": "2",
"str:addressToIdMapper|str:addrId|u64:2": "address:acc2"
},
"code": "mxsc:../output/lottery-esdt.mxsc.json"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"to": "sc:lottery#42",
"function": "claim_rewards",
"arguments": [
"str:lottery_name"
"str:LOTTERY-123456"
],
"gasLimit": "100,000,000",
"gasPrice": "0"
Expand All @@ -56,7 +56,7 @@
"step": "checkState",
"accounts": {
"address:my_address": {
"nonce": "3",
"nonce": "2",
"balance": "1,000,000",
"storage": {}
},
Expand All @@ -69,7 +69,7 @@
"nonce": "2",
"balance": "1,000,000",
"esdt": {
"str:LOTTERY-123456": "0"
"str:LOTTERY-123456": "200"
},
"storage": {}
},
Expand All @@ -84,7 +84,13 @@
"sc:lottery#42": {
"nonce": "0",
"balance": "0",
"storage": {},
"storage": {
"str:addressToIdMapper|str:lastId": "2",
"str:addressToIdMapper|str:addr|address:acc1": "1",
"str:addressToIdMapper|str:addrId|u64:1": "address:acc1",
"str:addressToIdMapper|str:addr|address:acc2": "2",
"str:addressToIdMapper|str:addrId|u64:2": "address:acc2"
},
"code": "mxsc:../output/lottery-esdt.mxsc.json"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,25 @@
"refund": "*"
}
},
{
"step": "scCall",
"tx": {
"from": "address:acc1",
"to": "sc:lottery#42",
"function": "claim_rewards",
"arguments": [
"str:LOTTERY-123456"
],
"gasLimit": "100,000,000",
"gasPrice": "0"
},
"expect": {
"out": [],
"status": "0",
"gas": "*",
"refund": "*"
}
},
{
"step": "checkState",
"accounts": {
Expand All @@ -48,7 +67,7 @@
"storage": {}
},
"address:acc1": {
"nonce": "2",
"nonce": "3",
"balance": "1,000,000",
"esdt": {
"str:LOTTERY-123456": "200"
Expand All @@ -63,7 +82,11 @@
"sc:lottery#42": {
"nonce": "0",
"balance": "0",
"storage": {},
"storage": {
"str:addressToIdMapper|str:lastId": "1",
"str:addressToIdMapper|str:addr|address:acc1": "1",
"str:addressToIdMapper|str:addrId|u64:1": "address:acc1"
},
"code": "mxsc:../output/lottery-esdt.mxsc.json"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,31 @@
"refund": "*"
}
},
{
"step": "scCall",
"tx": {
"from": "address:acc1",
"to": "sc:lottery#42",
"function": "claim_rewards",
"arguments": [
"str:LOTTERY-123456"
],
"gasLimit": "100,000,000",
"gasPrice": "0"
},
"expect": {
"out": [],
"status": "0",
"gas": "*",
"refund": "*"
}
},
{
"step": "checkState",
"comment": "check that 50% was burned, and 50% returned to acc1",
"accounts": {
"address:acc1": {
"nonce": "2",
"nonce": "3",
"balance": "1,000,000",
"esdt": {
"str:LOTTERY-123456": "100"
Expand All @@ -274,6 +293,9 @@
}
},
"storage": {
"str:addressToIdMapper|str:lastId": "1",
"str:addressToIdMapper|str:addr|address:acc1": "1",
"str:addressToIdMapper|str:addrId|u64:1": "address:acc1",
"str:lotteryInfo|nested:str:lottery_name": "",
"str:burnPercentageForLottery|nested:str:lottery_name": "0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"step": "checkState",
"accounts": {
"address:my_address": {
"nonce": "4",
"nonce": "3",
"balance": "1,000,000",
"storage": {}
},
Expand All @@ -47,7 +47,7 @@
"storage": {}
},
"address:acc1": {
"nonce": "1",
"nonce": "2",
"balance": "1,000,000",
"esdt": {
"str:LOTTERY-123456": "200"
Expand All @@ -63,6 +63,11 @@
"nonce": "0",
"balance": "0",
"storage": {
"str:addressToIdMapper|str:lastId": "2",
"str:addressToIdMapper|str:addr|address:acc1": "1",
"str:addressToIdMapper|str:addrId|u64:1": "address:acc1",
"str:addressToIdMapper|str:addr|address:acc2": "2",
"str:addressToIdMapper|str:addrId|u64:2": "address:acc2",
"str:lotteryInfo|nested:str:lottery_name": {
"0-token_identifier": "nested:str:LOTTERY-123456",
"1-ticket_price": "biguint:1000",
Expand Down
30 changes: 12 additions & 18 deletions contracts/examples/lottery-esdt/src/lottery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,17 +176,15 @@ pub trait Lottery {
match self.status(&lottery_name) {
Status::Inactive => sc_panic!("Lottery is inactive!"),
Status::Running => sc_panic!("Lottery is still running!"),
Status::Ended => {
if self.total_winning_tickets(&lottery_name).is_empty() {
self.prepare_awarding(&lottery_name);
}
if self.distribute_prizes(&lottery_name) == AwardingStatus::Finished {
self.clear_storage(&lottery_name);
return AwardingStatus::Finished;
}
AwardingStatus::Ongoing
},
Status::Ended => self.handle_awarding(&lottery_name),
}
}

fn handle_awarding(&self, lottery_name: &ManagedBuffer) -> AwardingStatus {
if self.total_winning_tickets(&lottery_name).is_empty() {

Check warning on line 184 in contracts/examples/lottery-esdt/src/lottery.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] contracts/examples/lottery-esdt/src/lottery.rs#L184

warning: this expression creates a reference which is immediately dereferenced by the compiler --> contracts/examples/lottery-esdt/src/lottery.rs:184:39 | 184 | if self.total_winning_tickets(&lottery_name).is_empty() { | ^^^^^^^^^^^^^ help: change this to: `lottery_name` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `#[warn(clippy::needless_borrow)]` on by default
Raw output
contracts/examples/lottery-esdt/src/lottery.rs:184:39:w:warning: this expression creates a reference which is immediately dereferenced by the compiler
   --> contracts/examples/lottery-esdt/src/lottery.rs:184:39
    |
184 |         if self.total_winning_tickets(&lottery_name).is_empty() {
    |                                       ^^^^^^^^^^^^^ help: change this to: `lottery_name`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
    = note: `#[warn(clippy::needless_borrow)]` on by default


__END__

Check warning on line 184 in contracts/examples/lottery-esdt/src/lottery.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] contracts/examples/lottery-esdt/src/lottery.rs#L184

warning: this expression creates a reference which is immediately dereferenced by the compiler --> contracts/examples/lottery-esdt/src/lottery.rs:184:39 | 184 | if self.total_winning_tickets(&lottery_name).is_empty() { | ^^^^^^^^^^^^^ help: change this to: `lottery_name` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `#[warn(clippy::needless_borrow)]` on by default
Raw output
contracts/examples/lottery-esdt/src/lottery.rs:184:39:w:warning: this expression creates a reference which is immediately dereferenced by the compiler
   --> contracts/examples/lottery-esdt/src/lottery.rs:184:39
    |
184 |         if self.total_winning_tickets(&lottery_name).is_empty() {
    |                                       ^^^^^^^^^^^^^ help: change this to: `lottery_name`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
    = note: `#[warn(clippy::needless_borrow)]` on by default


__END__
self.prepare_awarding(&lottery_name);

Check warning on line 185 in contracts/examples/lottery-esdt/src/lottery.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] contracts/examples/lottery-esdt/src/lottery.rs#L185

warning: this expression creates a reference which is immediately dereferenced by the compiler --> contracts/examples/lottery-esdt/src/lottery.rs:185:35 | 185 | self.prepare_awarding(&lottery_name); | ^^^^^^^^^^^^^ help: change this to: `lottery_name` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
Raw output
contracts/examples/lottery-esdt/src/lottery.rs:185:35:w:warning: this expression creates a reference which is immediately dereferenced by the compiler
   --> contracts/examples/lottery-esdt/src/lottery.rs:185:35
    |
185 |             self.prepare_awarding(&lottery_name);
    |                                   ^^^^^^^^^^^^^ help: change this to: `lottery_name`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow


__END__

Check warning on line 185 in contracts/examples/lottery-esdt/src/lottery.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] contracts/examples/lottery-esdt/src/lottery.rs#L185

warning: this expression creates a reference which is immediately dereferenced by the compiler --> contracts/examples/lottery-esdt/src/lottery.rs:185:35 | 185 | self.prepare_awarding(&lottery_name); | ^^^^^^^^^^^^^ help: change this to: `lottery_name` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
Raw output
contracts/examples/lottery-esdt/src/lottery.rs:185:35:w:warning: this expression creates a reference which is immediately dereferenced by the compiler
   --> contracts/examples/lottery-esdt/src/lottery.rs:185:35
    |
185 |             self.prepare_awarding(&lottery_name);
    |                                   ^^^^^^^^^^^^^ help: change this to: `lottery_name`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow


__END__
}
self.distribute_prizes(&lottery_name)

Check warning on line 187 in contracts/examples/lottery-esdt/src/lottery.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] contracts/examples/lottery-esdt/src/lottery.rs#L187

warning: this expression creates a reference which is immediately dereferenced by the compiler --> contracts/examples/lottery-esdt/src/lottery.rs:187:32 | 187 | self.distribute_prizes(&lottery_name) | ^^^^^^^^^^^^^ help: change this to: `lottery_name` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
Raw output
contracts/examples/lottery-esdt/src/lottery.rs:187:32:w:warning: this expression creates a reference which is immediately dereferenced by the compiler
   --> contracts/examples/lottery-esdt/src/lottery.rs:187:32
    |
187 |         self.distribute_prizes(&lottery_name)
    |                                ^^^^^^^^^^^^^ help: change this to: `lottery_name`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow


__END__

Check warning on line 187 in contracts/examples/lottery-esdt/src/lottery.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] contracts/examples/lottery-esdt/src/lottery.rs#L187

warning: this expression creates a reference which is immediately dereferenced by the compiler --> contracts/examples/lottery-esdt/src/lottery.rs:187:32 | 187 | self.distribute_prizes(&lottery_name) | ^^^^^^^^^^^^^ help: change this to: `lottery_name` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
Raw output
contracts/examples/lottery-esdt/src/lottery.rs:187:32:w:warning: this expression creates a reference which is immediately dereferenced by the compiler
   --> contracts/examples/lottery-esdt/src/lottery.rs:187:32
    |
187 |         self.distribute_prizes(&lottery_name)
    |                                ^^^^^^^^^^^^^ help: change this to: `lottery_name`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow


__END__
}

#[view]
Expand Down Expand Up @@ -266,6 +264,8 @@ pub trait Lottery {
self.total_winning_tickets(lottery_name)
.set(total_winning_tickets);
self.index_last_winner(lottery_name).set(1);

self.lottery_info(lottery_name).set(info);
}

fn burn_prize_percentage(
Expand All @@ -275,7 +275,6 @@ pub trait Lottery {
) {
let burn_percentage = self.burn_percentage_for_lottery(lottery_name).get();
if burn_percentage == 0 {
sc_print!("no burns occured {}", 0);
return;
}

Expand All @@ -289,12 +288,8 @@ pub trait Lottery {
self.send().esdt_local_burn(&esdt_token_id, 0, &burn_amount);
}

sc_print!("amount to burn: {}", burn_amount);
sc_print!("prize left after burn: {}", info.prize_pool);
info.prize_pool -= &burn_amount;
info.unawarded_amount -= burn_amount;
sc_print!("prize left after burn: {}", info.prize_pool);
sc_print!("unawarded amount after burn: {}", info.unawarded_amount);
}

fn distribute_prizes(&self, lottery_name: &ManagedBuffer) -> AwardingStatus {
Expand Down Expand Up @@ -324,6 +319,7 @@ pub trait Lottery {
self.lottery_info(lottery_name).set(info);
self.index_last_winner(lottery_name).set(index_last_winner);
if index_last_winner > total_winning_tickets {
self.clear_storage(&lottery_name);

Check warning on line 322 in contracts/examples/lottery-esdt/src/lottery.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] contracts/examples/lottery-esdt/src/lottery.rs#L322

warning: this expression creates a reference which is immediately dereferenced by the compiler --> contracts/examples/lottery-esdt/src/lottery.rs:322:32 | 322 | self.clear_storage(&lottery_name); | ^^^^^^^^^^^^^ help: change this to: `lottery_name` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
Raw output
contracts/examples/lottery-esdt/src/lottery.rs:322:32:w:warning: this expression creates a reference which is immediately dereferenced by the compiler
   --> contracts/examples/lottery-esdt/src/lottery.rs:322:32
    |
322 |             self.clear_storage(&lottery_name);
    |                                ^^^^^^^^^^^^^ help: change this to: `lottery_name`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow


__END__

Check warning on line 322 in contracts/examples/lottery-esdt/src/lottery.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] contracts/examples/lottery-esdt/src/lottery.rs#L322

warning: this expression creates a reference which is immediately dereferenced by the compiler --> contracts/examples/lottery-esdt/src/lottery.rs:322:32 | 322 | self.clear_storage(&lottery_name); | ^^^^^^^^^^^^^ help: change this to: `lottery_name` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
Raw output
contracts/examples/lottery-esdt/src/lottery.rs:322:32:w:warning: this expression creates a reference which is immediately dereferenced by the compiler
   --> contracts/examples/lottery-esdt/src/lottery.rs:322:32
    |
322 |             self.clear_storage(&lottery_name);
    |                                ^^^^^^^^^^^^^ help: change this to: `lottery_name`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow


__END__
return AwardingStatus::Finished;
}
AwardingStatus::Ongoing
Expand Down Expand Up @@ -352,7 +348,7 @@ pub trait Lottery {
// distribute to the first place last. Laws of probability say that order doesn't matter.
// this is done to mitigate the effects of BigUint division leading to "spare" prize money being left out at times
// 1st place will get the spare money instead.
if *index_last_winner < total_winning_tickets {
if *index_last_winner <= total_winning_tickets {
let prize = self.calculate_percentage_of(
&info.prize_pool,
&BigUint::from(
Expand Down Expand Up @@ -385,7 +381,6 @@ pub trait Lottery {
) {
self.accumulated_rewards(&token_id, winner_id)
.update(|value| *value += amount);

self.user_accumulated_token_rewards(winner_id)
.insert(token_id);
}
Expand Down Expand Up @@ -478,7 +473,6 @@ pub trait Lottery {
accumulated_esdt_rewards: &mut ManagedVec<Self::Api, EsdtTokenPayment>,
) {
let value = self.accumulated_rewards(&token_id, caller_id).take();
sc_print!("caller {:x} has rewards {}", caller_id, value);
if token_id.is_egld() {
*accumulated_egld_rewards += value;
} else {
Expand Down

0 comments on commit 15f9b29

Please sign in to comment.