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

Updated token example for testnet-beta #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
24 changes: 12 additions & 12 deletions token/src/main.leo
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ program token.aleo {
/* Mint */

// The function `mint_public` issues the specified token amount for the token receiver publicly on the network.
transition mint_public(public receiver: address, public amount: u64) {
async transition mint_public(public receiver: address, public amount: u64) -> Future {
// Mint the tokens publicly by invoking the computation on-chain.
return then finalize(receiver, amount);
return mint_public_state(receiver, amount);
}

finalize mint_public(public receiver: address, public amount: u64) {
async function mint_public_state(public receiver: address, public amount: u64) {
// Increments `account[receiver]` by `amount`.
// If `account[receiver]` does not exist, it will be created.
// If `account[receiver] + amount` overflows, `mint_public` is reverted.
Expand All @@ -36,12 +36,12 @@ program token.aleo {
}

/* Transfer */
transition transfer_public(public receiver: address, public amount: u64) {
async transition transfer_public(public receiver: address, public amount: u64) -> Future {
// Transfer the tokens publicly, by invoking the computation on-chain.
return then finalize(self.caller, receiver, amount);
return transfer_public_tokens(self.caller, receiver, amount);
}

finalize transfer_public(public sender: address, public receiver: address, public amount: u64) {
async function transfer_public_tokens(public sender: address, public receiver: address, public amount: u64) {
// Decrements `account[sender]` by `amount`.
// If `account[sender]` does not exist, it will be created.
// If `account[sender] - amount` underflows, `transfer_public` is reverted.
Expand Down Expand Up @@ -80,7 +80,7 @@ program token.aleo {

// The function `transfer_private_to_public` turns a specified token amount from a token record into public tokens for the specified receiver.
// This function preserves privacy for the sender's record, however it publicly reveals the token receiver and the token amount.
transition transfer_private_to_public(sender: token, public receiver: address, public amount: u64) -> token {
async transition transfer_private_to_public(sender: token, public receiver: address, public amount: u64) -> (token, Future) {
// Checks the given token record has a sufficient token amount.
// This `sub` operation is safe, and the proof will fail if an underflow occurs.
// `difference` holds the change amount for the caller.
Expand All @@ -94,10 +94,10 @@ program token.aleo {

// Output the sender's change record.
// Increment the token amount publicly for the token receiver.
return remaining then finalize(receiver, amount);
return (remaining, transfer_from_private_to_public(receiver, amount));
}

finalize transfer_private_to_public(public receiver: address, public amount: u64) {
async function transfer_from_private_to_public(public receiver: address, public amount: u64) {
// 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.
Expand All @@ -107,7 +107,7 @@ program token.aleo {

// The function `transfer_public_to_private` turns a specified token amount from `account` into a token record for the specified receiver.
// This function preserves privacy for the receiver's record, however it publicly reveals the caller and the specified token amount.
transition transfer_public_to_private(public receiver: address, public amount: u64) -> token {
async transition transfer_public_to_private(public receiver: address, public amount: u64) -> (token, Future) {
// Produces a token record for the token receiver.
let transferred: token = token {
owner: receiver,
Expand All @@ -116,10 +116,10 @@ program token.aleo {

// Output the receiver's record.
// Decrement the token amount of the caller publicly.
return transferred then finalize(self.caller, amount);
return (transferred, transfer_from_public_to_private(self.caller, amount));
}

finalize transfer_public_to_private(public sender: address, public amount: u64) {
async function transfer_from_public_to_private(public sender: address, public amount: u64) {
// 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.
Expand Down