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

Solution #2

Open
wants to merge 1 commit into
base: main
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
66 changes: 66 additions & 0 deletions src/main.leo
Original file line number Diff line number Diff line change
@@ -1,3 +1,69 @@
program store.aleo {
mapping items: u8 => u64;
mapping prices: u8 => u64;

record Token {
owner: address,
amount: u64
}

record Item {
owner: address,
item: u8,
quantity: u64
}

// Address of store owner: aleo10ppqgwqzdr2h49z3w4faze74genk6zs0ye4tzemk3d8uxerfkqzspkzp7w
// Step 1: Mint tokens to certain address
transition mint(receiver: address, amount: u64) -> Token {
assert_eq(self.caller, aleo1fu0k2qfytzs5fhesgfgjuax6wsh9xx4ftpdapnhzrtruy0tx3urqx3p0ut);

return Token {
owner: receiver,
amount: amount
};
}

// Step 2: Add item
transition add_item(item: u8, quantity: u64, price: u64) {
assert_eq(self.caller, aleo1fu0k2qfytzs5fhesgfgjuax6wsh9xx4ftpdapnhzrtruy0tx3urqx3p0ut);

return then finalize(item, quantity, price);
}

// Increment items quantity, set item price
finalize add_item (item: u8, quantity: u64, price: u64) {
let item_count: u64 = Mapping::get_or_use(items, item, 0u64);
Mapping::set(items, item, item_count + quantity);

Mapping::set(prices, item, price);
}

transition buy(token: Token, item: u8, quantity: u64, bill_amount: u64) -> (Token, Item) {
let difference: u64 = token.amount - bill_amount;

let remaining: Token = Token {
owner: token.owner,
amount: difference
};

let item_record: Item = Item {
owner: self.caller,
item: item,
quantity: quantity
};

return (remaining, item_record) then finalize(item, quantity, bill_amount);
}

finalize buy (item: u8, quantity: u64, bill_amount: u64) {
let available_quantity: u64 = Mapping::get_or_use(items, item, 0u64);
assert(available_quantity >= quantity);

let item_price: u64 = Mapping::get_or_use(prices, item, 0u64);
assert_neq(item_price, 0u64);

let total_price: u64 = item_price * quantity;
assert_eq(total_price, bill_amount);
}
}