From bae594303699cdf1227f15c92748140c107621f4 Mon Sep 17 00:00:00 2001 From: LouisWT Date: Wed, 26 Apr 2023 22:26:25 +0800 Subject: [PATCH 1/4] fix: use Mapping rather than increment or decrement --- token/src/main.leo | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/token/src/main.leo b/token/src/main.leo index 39d5440..36afc7c 100644 --- a/token/src/main.leo +++ b/token/src/main.leo @@ -25,7 +25,8 @@ program token.aleo { // Increments `account[receiver]` by `amount`. // If `account[receiver]` does not exist, it will be created. // If `account[receiver] + amount` overflows, `mint_public` is reverted. - increment(account, receiver, amount); + let receiver_amount: u64 = Mapping::get_or_init(account, receiver, 0u64); + Mapping::set(account, receiver, receiver_amount + amount); } // The function `mint_private` initializes a new record with the specified amount of tokens for the receiver. @@ -44,14 +45,17 @@ program token.aleo { } finalize transfer_public(public sender: address, public receiver: address, public amount: u64) { - // Decrements `account[sender]` by `amount`. + // Decrements `account[sender]` by `amount`. // If `account[sender]` does not exist, it will be created. // If `account[sender] - amount` underflows, `transfer_public` is reverted. - decrement(account, sender, amount); + let sender_amount: u64 = Mapping::get_or_init(account, sender, 0u64); + Mapping::set(account, sender, sender_amount - amount); + // Increments `account[receiver]` by `amount`. // If `account[receiver]` does not exist, it will be created. // If `account[receiver] + amount` overflows, `transfer_public` is reverted. - increment(account, receiver, amount); + let receiver_amount: u64 = Mapping::get_or_init(account, receiver, 0u64); + Mapping::set(account, receiver, receiver_amount + amount); } // The function `transfer_private` sends the specified token amount to the token receiver from the specified token record. @@ -103,7 +107,8 @@ program token.aleo { // Increments `account[receiver]` by `amount`. // If `account[receiver]` does not exist, it will be created. // If `account[receiver] + amount` overflows, `transfer_private_to_public` is reverted. - increment(account, receiver, amount); + let receiver_amount: u64 = Mapping::get_or_init(account, receiver, 0u64); + Mapping::set(account, receiver, receiver_amount + amount); } // The function `transfer_public_to_private` turns a specified token amount from `account` into a token record for the specified receiver. @@ -125,6 +130,7 @@ program token.aleo { // Decrements `account[sender]` by `amount`. // If `account[sender]` does not exist, it will be created. // If `account[sender] - amount` underflows, `transfer_public_to_private` is reverted. - decrement(account, sender, amount); + let sender_amount: u64 = Mapping::get_or_init(account, sender, 0u64); + Mapping::set(account, sender, sender_amount - amount); } } From 93f30c946e30f0092d23baaa88a8803976901209 Mon Sep 17 00:00:00 2001 From: LouisWT Date: Wed, 26 Apr 2023 22:28:03 +0800 Subject: [PATCH 2/4] style --- token/src/main.leo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/token/src/main.leo b/token/src/main.leo index 36afc7c..d520ed7 100644 --- a/token/src/main.leo +++ b/token/src/main.leo @@ -45,7 +45,7 @@ program token.aleo { } finalize transfer_public(public sender: address, public receiver: address, public amount: u64) { - // Decrements `account[sender]` by `amount`. + // Decrements `account[sender]` by `amount`. // If `account[sender]` does not exist, it will be created. // If `account[sender] - amount` underflows, `transfer_public` is reverted. let sender_amount: u64 = Mapping::get_or_init(account, sender, 0u64); From 48300a4e3b9c0f8c78adf22b78c626ac408748ce Mon Sep 17 00:00:00 2001 From: LouisWT Date: Wed, 26 Apr 2023 22:40:41 +0800 Subject: [PATCH 3/4] fix: baisc_bank don't work --- basic_bank/src/main.leo | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/basic_bank/src/main.leo b/basic_bank/src/main.leo index 52281ad..d1fbd7c 100644 --- a/basic_bank/src/main.leo +++ b/basic_bank/src/main.leo @@ -51,7 +51,8 @@ program basic_bank.aleo { // - `hash` : The hash of the token owner. // - `amount`: The amount of tokens that were deposited. finalize deposit(hash: field, amount: u64) { - increment(balances, hash, amount); + let balance: u64 = Mapping::get_or_init(balances, hash, 0u64); + Mapping::set(balances, hash, amount + balance); } // Returns a new Token containing the amount of money withdrawn. @@ -79,7 +80,8 @@ program basic_bank.aleo { // - `hash` : The hash of the token owner. // - `amount`: The amount of tokens that were withdrawn. finalize withdraw(hash: field, amount: u64) { - decrement(balances, hash, amount); + let balance: u64 = Mapping::get_or_init(balances, hash, 0u64); + Mapping::set(balances, hash, balance - amount); } // Returns the total amount of tokens after compounding interest. From 6c0eacf31503dc5a8c7dc60ee554927e85391527 Mon Sep 17 00:00:00 2001 From: LouisWT Date: Wed, 26 Apr 2023 22:58:16 +0800 Subject: [PATCH 4/4] fix: vote example --- vote/src/main.leo | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/vote/src/main.leo b/vote/src/main.leo index 126b923..cb4e382 100644 --- a/vote/src/main.leo +++ b/vote/src/main.leo @@ -51,7 +51,7 @@ program vote.aleo { } // Create a new proposal in the "tickets" mapping. finalize propose(public id: field) { - increment(tickets, id, 0u64); + Mapping::set(tickets, id, 0u64); } // Create a new ticket to vote with. @@ -68,7 +68,8 @@ program vote.aleo { } // Create a new ticket on a proposal in the "tickets" mapping. finalize new_ticket(public pid: field) { - increment(tickets, pid, 1u64); + let vote: u64 = Mapping::get_or_init(tickets, pid, 0u64); + Mapping::set(tickets, pid, vote + 1u64); } // Vote privately to agree with a proposal. @@ -78,7 +79,8 @@ program vote.aleo { } finalize agree(public pid: field) { // Publicly increment the number of agree votes. - increment(agree_votes, pid, 1u64); + let agree_vote: u64 = Mapping::get_or_init(agree_votes, pid, 0u64); + Mapping::set(agree_votes, pid, agree_vote + 1u64); } // Vote privately to disagree with a proposal. @@ -88,6 +90,7 @@ program vote.aleo { } finalize disagree(pid: field) { // Publicly increment the number of disagree votes. - increment(disagree_votes, pid, 1u64); + let disagree_vote: u64 = Mapping::get_or_init(disagree_votes, pid, 0u64); + Mapping::set(disagree_votes, pid, disagree_vote + 1u64); } }