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

fix: use Mapping rather than increment or decrement #26

Merged
merged 4 commits into from
Apr 28, 2023
Merged
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
6 changes: 4 additions & 2 deletions basic_bank/src/main.leo
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
16 changes: 11 additions & 5 deletions token/src/main.leo
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -47,11 +48,14 @@ 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` 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.
Expand Down Expand Up @@ -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.
Expand All @@ -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);
}
}
11 changes: 7 additions & 4 deletions vote/src/main.leo
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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);
}
}