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

Update examples to ARC-003 specification. #13

Merged
merged 2 commits into from
Oct 6, 2022
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
4 changes: 2 additions & 2 deletions auction/program.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"version": "0.0.0",
"description": "",
"development": {
"private_key": "APrivateKey1zkp5wvamYgK3WCAdpBQxZqQX8XnuN2u11Y6QprZTriVwZVc",
"address": "aleo1fxs9s0w97lmkwlcmgn0z3nuxufdee5yck9wqrs0umevp7qs0sg9q5xxxzh"
"private_key": "APrivateKey1zkpG9Af9z5Ha4ejVyMCqVFXRKknSm8L1ELEwcc4htk9YhVK",
"address": "aleo1yzlta2q5h8t0fqe0v6dyh9mtv4aggd53fgzr068jvplqhvqsnvzq7pj2ke"
},
"license": "MIT"
}
131 changes: 66 additions & 65 deletions auction/src/main.leo
Original file line number Diff line number Diff line change
@@ -1,70 +1,71 @@
// A bid in an auction.
// - `owner` : The address of the account that owns the record associated with this bid.
// This is separate from the address of the account that placed the bid.
// - `gates` : The value associated with the record (always zero).
// - `bidder` : The address of the account that placed the bid.
// - `amount` : The amount of the bid.
// - `is_winner` : Whether the bid is the winning bid.
record Bid {
owner: address,
gates: u64,
bidder: address,
amount: u64,
is_winner: bool,
}
// The `program` scope defines the data types, functions, and state associated with the `auction` program.
program auction.aleo {

// Returns a new bid.
// - `bidder` : The address of the account that placed the bid.
// - `amount` : The amount of the bid.
// Requires that `bidder` matches the function caller.
// The owner of the record is set to the entity responsible for running the auction (auction runner).
// The address of the auction runner is aleo1fxs9s0w97lmkwlcmgn0z3nuxufdee5yck9wqrs0umevp7qs0sg9q5xxxzh.
@program
function place_bid(bidder: address, amount: u64) -> Bid {
// Ensure the caller is the auction bidder.
console.assert_eq(self.caller, bidder);
// Return a new 'Bid' record for the auction bidder.
return Bid {
owner: aleo1fxs9s0w97lmkwlcmgn0z3nuxufdee5yck9wqrs0umevp7qs0sg9q5xxxzh,
gates: 0u64,
bidder: bidder,
amount: amount,
is_winner: false,
};
}
// A bid in an auction.
// - `owner` : The address of the account that owns the record associated with this bid.
// This is separate from the address of the account that placed the bid.
// - `gates` : The value associated with the record (always zero).
// - `bidder` : The address of the account that placed the bid.
// - `amount` : The amount of the bid.
// - `is_winner` : Whether the bid is the winning bid.
record Bid {
owner: address,
gates: u64,
bidder: address,
amount: u64,
is_winner: bool,
}

// Returns the winning bid.
// - `first` : The first bid.
// - `second` : The second bid.
// Requires that the function caller is the auction runner.
// Assumes that the function is invoked only after the bidding period has ended.
// In the event of a tie, the first bid is selected.
@program
function resolve(first: Bid, second: Bid) -> Bid {
// Ensure the caller is the auctioneer.
console.assert_eq(self.caller, aleo1fxs9s0w97lmkwlcmgn0z3nuxufdee5yck9wqrs0umevp7qs0sg9q5xxxzh);
// Resolve the winner of the auction.
if (first.amount >= second.amount) {
return first;
} else {
return second;
// Returns a new bid.
// - `bidder` : The address of the account that placed the bid.
// - `amount` : The amount of the bid.
// Requires that `bidder` matches the function caller.
// The owner of the record is set to the entity responsible for running the auction (auction runner).
// The address of the auction runner is aleo1fxs9s0w97lmkwlcmgn0z3nuxufdee5yck9wqrs0umevp7qs0sg9q5xxxzh.
transition place_bid(bidder: address, amount: u64) -> Bid {
// Ensure the caller is the auction bidder.
console.assert_eq(self.caller, bidder);
// Return a new 'Bid' record for the auction bidder.
return Bid {
owner: aleo1fxs9s0w97lmkwlcmgn0z3nuxufdee5yck9wqrs0umevp7qs0sg9q5xxxzh,
gates: 0u64,
bidder: bidder,
amount: amount,
is_winner: false,
};
}
}

// Returns ownership of the bid to bidder.
// - `bid` : The winning bid.
// Requires that the function caller is the auction runner.
// Assumes that the function is invoked only after all bids have been resolved.
@program
function finish(bid: Bid) -> Bid {
// Ensure the caller is the auctioneer.
console.assert_eq(self.caller, aleo1fxs9s0w97lmkwlcmgn0z3nuxufdee5yck9wqrs0umevp7qs0sg9q5xxxzh);
// Return 'is_winner' as 'true' in the winning 'Bid'.
return Bid {
owner: bid.bidder,
gates: bid.gates,
bidder: bid.bidder,
amount: bid.amount,
is_winner: true,
};
// Returns the winning bid.
// - `first` : The first bid.
// - `second` : The second bid.
// Requires that the function caller is the auction runner.
// Assumes that the function is invoked only after the bidding period has ended.
// In the event of a tie, the first bid is selected.
transition resolve(first: Bid, second: Bid) -> Bid {
// Ensure the caller is the auctioneer.
console.assert_eq(self.caller, aleo1fxs9s0w97lmkwlcmgn0z3nuxufdee5yck9wqrs0umevp7qs0sg9q5xxxzh);
// Resolve the winner of the auction.
if (first.amount >= second.amount) {
return first;
} else {
return second;
}
}

// Returns ownership of the bid to bidder.
// - `bid` : The winning bid.
// Requires that the function caller is the auction runner.
// Assumes that the function is invoked only after all bids have been resolved.
transition finish(bid: Bid) -> Bid {
// Ensure the caller is the auctioneer.
console.assert_eq(self.caller, aleo1fxs9s0w97lmkwlcmgn0z3nuxufdee5yck9wqrs0umevp7qs0sg9q5xxxzh);
// Return 'is_winner' as 'true' in the winning 'Bid'.
return Bid {
owner: bid.bidder,
gates: bid.gates,
bidder: bid.bidder,
amount: bid.amount,
is_winner: true,
};
}
}
175 changes: 88 additions & 87 deletions basic_bank/src/main.leo
Original file line number Diff line number Diff line change
@@ -1,107 +1,108 @@
// A token, issued by a bank.
// - 'owner' : The address of the account that owns the record associated with this token.
// - 'gates' : The value associated with the record (always zero).
// - 'amount' : The amount of tokens owned by the account.
record Token {
owner: address,
gates: u64,
amount: u64,
}

// An on-chain mapping, storing the amount of tokens owned by each account
// The account is stored as a to preserve user privacy.
mapping balances: field => u64;

// Returns a new Token.
// - `owner` : The address of the account to issue the token to.
// - `amount`: The amount of tokens to issue.
// Requires that the function caller is the bank.
// The bank's address is aleo1t0uer3jgtsgmx5tq6x6f9ecu8tr57rzzfnc2dgmcqldceal0ls9qf6st7a.
@program
function issue(owner: address, amount: u64) -> Token {
console.assert_eq(self.caller, aleo1t0uer3jgtsgmx5tq6x6f9ecu8tr57rzzfnc2dgmcqldceal0ls9qf6st7a);
return Token {
owner: owner,
gates: 0u64,
amount: amount,
};
}
// The `program` scope defines the data types, functions, and state associated with the `basic_bank` program.
program basic_bank.aleo {
// A token, issued by a bank.
// - 'owner' : The address of the account that owns the record associated with this token.
// - 'gates' : The value associated with the record (always zero).
// - 'amount' : The amount of tokens owned by the account.
record Token {
owner: address,
gates: u64,
amount: u64,
}

// Deposits some amount of money into the bank.
// Returns a new Token with the remaining amount of money.
// - `token` : A record containing tokens to deposit.
// - `amount`: The amount of tokens to deposit.
@program
function deposit(token: Token, amount: u64) -> Token {
let difference: u64 = token.amount - amount;
// An on-chain mapping, storing the amount of tokens owned by each account
// The account is stored as a to preserve user privacy.
mapping balances: field => u64;

// Returns a new Token.
// - `owner` : The address of the account to issue the token to.
// - `amount`: The amount of tokens to issue.
// Requires that the function caller is the bank.
// The bank's address is aleo1t0uer3jgtsgmx5tq6x6f9ecu8tr57rzzfnc2dgmcqldceal0ls9qf6st7a.
transition issue(owner: address, amount: u64) -> Token {
console.assert_eq(self.caller, aleo1t0uer3jgtsgmx5tq6x6f9ecu8tr57rzzfnc2dgmcqldceal0ls9qf6st7a);
return Token {
owner: owner,
gates: 0u64,
amount: amount,
};
}

let remaining: Token = Token {
owner: token.owner,
gates: token.gates,
amount: difference,
};
// Deposits some amount of money into the bank.
// Returns a new Token with the remaining amount of money.
// - `token` : A record containing tokens to deposit.
// - `amount`: The amount of tokens to deposit.
transition deposit(token: Token, amount: u64) -> Token {
let difference: u64 = token.amount - amount;

// Compute the hash of the token owner.
let hash: field = BHP256::hash(token.owner);
let remaining: Token = Token {
owner: token.owner,
gates: token.gates,
amount: difference,
};

async finalize(hash, amount);
// Compute the hash of the token owner.
let hash: field = BHP256::hash(token.owner);

return remaining;
}
async finalize(hash, amount);

// Updates on-chain state by the amount of tokens deposited.
// - `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);
}
return remaining;
}

// Returns a new Token containing the amount of money withdrawn.
// - `recipient`: The address of the account to withdraw the tokens to.
// - `amount` : The amount of tokens to withdraw.
// - `rate` : The compound interest rate.
// - `periods` : The number of periods to compound the interest over.
// Requires that the function caller is the bank.
@program
function withdraw(recipient: address, amount: u64, rate: u64, periods: u64) -> Token {
console.assert_eq(self.caller, aleo1t0uer3jgtsgmx5tq6x6f9ecu8tr57rzzfnc2dgmcqldceal0ls9qf6st7a);
let hash: field = BHP256::hash(recipient);
// Updates on-chain state by the amount of tokens deposited.
// - `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 total: u64 = calculate_interest(amount, rate, periods);
// Returns a new Token containing the amount of money withdrawn.
// - `recipient`: The address of the account to withdraw the tokens to.
// - `amount` : The amount of tokens to withdraw.
// - `rate` : The compound interest rate.
// - `periods` : The number of periods to compound the interest over.
// Requires that the function caller is the bank.
transition withdraw(recipient: address, amount: u64, rate: u64, periods: u64) -> Token {
console.assert_eq(self.caller, aleo1t0uer3jgtsgmx5tq6x6f9ecu8tr57rzzfnc2dgmcqldceal0ls9qf6st7a);
let hash: field = BHP256::hash(recipient);

let token: Token = Token {
owner: recipient,
gates: 0u64,
amount: total,
};
let total: u64 = calculate_interest(amount, rate, periods);

async finalize(hash, amount);
let token: Token = Token {
owner: recipient,
gates: 0u64,
amount: total,
};

return token;
}
async finalize(hash, amount);

// Updates on-chain state by the amount of tokens withdrawn.
// - `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);
}
return token;
}

// Returns the total amount of tokens after compounding interest.
// - `principal`: The amount of tokens to compound interest over.
// - `rate` : The compound interest rate.
// - `periods` : The number of periods to compound the interest over.
function calculate_interest(principal: u64, rate: u64, periods: u64) -> u64 {
let amount: u64 = principal;
// Updates on-chain state by the amount of tokens withdrawn.
// - `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);
}

for i:u64 in 0u64..100u64 {
if i < periods {
amount += (amount * rate) / 10000u64;
// Returns the total amount of tokens after compounding interest.
// - `principal`: The amount of tokens to compound interest over.
// - `rate` : The compound interest rate.
// - `periods` : The number of periods to compound the interest over.
function calculate_interest(principal: u64, rate: u64, periods: u64) -> u64 {
let amount: u64 = principal;

for i:u64 in 0u64..100u64 {
if i < periods {
amount += (amount * rate) / 10000u64;
}
}

return amount;
}

return amount;
}



}
Loading