mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-09-11 05:45:30 +03:00
More examples
This commit is contained in:
parent
028a01efde
commit
35f77f2a12
@ -1,4 +1,4 @@
|
||||
program token.aleo;
|
||||
program token.aleo
|
||||
|
||||
record token:
|
||||
owner as address.private;
|
||||
|
@ -1,131 +1,134 @@
|
||||
// On-chain storage of an `account` map, with `address` as the key,
|
||||
// and `u64` as the value.
|
||||
mapping account: address => u64;
|
||||
program token.aleo {
|
||||
// On-chain storage of an `account` map, with `address` as the key,
|
||||
// and `u64` as the value.
|
||||
mapping account: address => u64;
|
||||
|
||||
record token {
|
||||
// The token owner.
|
||||
owner: address,
|
||||
// The Aleo balance (in gates).
|
||||
gates: u64,
|
||||
// The token amount.
|
||||
amount: u64,
|
||||
}
|
||||
|
||||
/* 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) {
|
||||
// Mint the tokens publicly by invoking the computation on-chain.
|
||||
async finalize(receiver, amount);
|
||||
}
|
||||
|
||||
finalize mint_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, `mint_public` is reverted.
|
||||
increment(account, receiver, amount);
|
||||
}
|
||||
|
||||
// The function `mint_private` initializes a new record with the specified amount of tokens for the receiver.
|
||||
transition mint_private(receiver: address, amount: u64) -> token {
|
||||
return token {
|
||||
owner: receiver,
|
||||
gates: 0u64,
|
||||
amount: amount,
|
||||
};
|
||||
}
|
||||
|
||||
/* Transfer */
|
||||
transition transfer_public(public receiver: address, public amount: u64) {
|
||||
// Transfer the tokens publicly, by invoking the computation on-chain.
|
||||
async finalize(self.caller, receiver, amount);
|
||||
}
|
||||
|
||||
finalize transfer_public(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.
|
||||
decrement(account, sender, 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);
|
||||
}
|
||||
|
||||
// The function `transfer_private` sends the specified token amount to the token receiver from the specified token record.
|
||||
transition transfer_private(sender: token, receiver: address, amount: u64) -> (token, token) {
|
||||
// Checks the given token record has sufficient balance.
|
||||
// This `sub` operation is safe, and the proof will fail if an overflow occurs.
|
||||
// `difference` holds the change amount to be returned to sender.
|
||||
let difference: u64 = sender.amount - amount;
|
||||
|
||||
// Produce a token record with the change amount for the sender.
|
||||
let remaining: token = token {
|
||||
owner: sender.owner,
|
||||
gates: sender.gates,
|
||||
amount: difference,
|
||||
};
|
||||
|
||||
// Produce a token record for the specified receiver.
|
||||
let transferred: token = token {
|
||||
owner: receiver,
|
||||
gates: 0u64,
|
||||
amount: amount,
|
||||
};
|
||||
|
||||
// Output the sender's change record and the receiver's record.
|
||||
return (remaining, transferred);
|
||||
}
|
||||
|
||||
// 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 {
|
||||
// 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.
|
||||
let difference: u64 = sender.amount - amount;
|
||||
|
||||
// Produces a token record with the change amount for the caller.
|
||||
let remaining: token = token {
|
||||
owner: sender.owner,
|
||||
gates: sender.gates,
|
||||
amount: difference,
|
||||
};
|
||||
|
||||
// Increment the token amount publicly for the token receiver.
|
||||
async finalize(receiver, amount);
|
||||
|
||||
// Output the sender's change record.
|
||||
return remaining;
|
||||
}
|
||||
|
||||
finalize transfer_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.
|
||||
increment(account, receiver, amount);
|
||||
}
|
||||
|
||||
// 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 {
|
||||
// Produces a token record for the token receiver.
|
||||
let transferred: token = token {
|
||||
owner: receiver,
|
||||
gates: 0u64,
|
||||
amount: amount,
|
||||
};
|
||||
|
||||
// Decrement the token amount of the caller publicly.
|
||||
async finalize(self.caller, amount);
|
||||
|
||||
// Output the receiver's record.
|
||||
return transferred;
|
||||
}
|
||||
|
||||
finalize transfer_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.
|
||||
decrement(account, sender, amount);
|
||||
}
|
||||
|
||||
record token {
|
||||
// The token owner.
|
||||
owner: address,
|
||||
// The Aleo balance (in gates).
|
||||
gates: u64,
|
||||
// The token amount.
|
||||
amount: u64,
|
||||
}
|
||||
|
||||
/* 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) {
|
||||
// Mint the tokens publicly by invoking the computation on-chain.
|
||||
async finalize(receiver, amount);
|
||||
}
|
||||
|
||||
finalize mint_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, `mint_public` is reverted.
|
||||
increment(account, receiver, amount);
|
||||
}
|
||||
|
||||
// The function `mint_private` initializes a new record with the specified amount of tokens for the receiver.
|
||||
transition mint_private(receiver: address, amount: u64) -> token {
|
||||
return token {
|
||||
owner: receiver,
|
||||
gates: 0u64,
|
||||
amount: amount,
|
||||
};
|
||||
}
|
||||
|
||||
/* Transfer */
|
||||
transition transfer_public(public receiver: address, public amount: u64) {
|
||||
// Transfer the tokens publicly, by invoking the computation on-chain.
|
||||
async finalize(self.caller, receiver, amount);
|
||||
}
|
||||
|
||||
finalize transfer_public(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.
|
||||
decrement(account, sender, 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);
|
||||
}
|
||||
|
||||
// The function `transfer_private` sends the specified token amount to the token receiver from the specified token record.
|
||||
transition transfer_private(sender: token, receiver: address, amount: u64) -> (token, token) {
|
||||
// Checks the given token record has sufficient balance.
|
||||
// This `sub` operation is safe, and the proof will fail if an overflow occurs.
|
||||
// `difference` holds the change amount to be returned to sender.
|
||||
let difference: u64 = sender.amount - amount;
|
||||
|
||||
// Produce a token record with the change amount for the sender.
|
||||
let remaining: token = token {
|
||||
owner: sender.owner,
|
||||
gates: sender.gates,
|
||||
amount: difference,
|
||||
};
|
||||
|
||||
// Produce a token record for the specified receiver.
|
||||
let transferred: token = token {
|
||||
owner: receiver,
|
||||
gates: 0u64,
|
||||
amount: amount,
|
||||
};
|
||||
|
||||
// Output the sender's change record and the receiver's record.
|
||||
return (remaining, transferred);
|
||||
}
|
||||
|
||||
// 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 {
|
||||
// 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.
|
||||
let difference: u64 = sender.amount - amount;
|
||||
|
||||
// Produces a token record with the change amount for the caller.
|
||||
let remaining: token = token {
|
||||
owner: sender.owner,
|
||||
gates: sender.gates,
|
||||
amount: difference,
|
||||
};
|
||||
|
||||
// Increment the token amount publicly for the token receiver.
|
||||
async finalize(receiver, amount);
|
||||
|
||||
// Output the sender's change record.
|
||||
return remaining;
|
||||
}
|
||||
|
||||
finalize transfer_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.
|
||||
increment(account, receiver, amount);
|
||||
}
|
||||
|
||||
// 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 {
|
||||
// Produces a token record for the token receiver.
|
||||
let transferred: token = token {
|
||||
owner: receiver,
|
||||
gates: 0u64,
|
||||
amount: amount,
|
||||
};
|
||||
|
||||
// Decrement the token amount of the caller publicly.
|
||||
async finalize(self.caller, amount);
|
||||
|
||||
// Output the receiver's record.
|
||||
return transferred;
|
||||
}
|
||||
|
||||
finalize transfer_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.
|
||||
decrement(account, sender, amount);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user