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

Donotion contract #27767

Open
wants to merge 3 commits into
base: testnet3
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions examples/donation/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.env
*.avm
*.prover
*.verifier
outputs/
53 changes: 53 additions & 0 deletions examples/donation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# donation.aleo

# Donation Program

The Donation program is a smart contract written in Aleo that facilitates token donations and manages user records. Users can donate tokens to the contract, and their donation amounts are recorded in a mapping.

## Features

- Token donation functionality
- User record management
- Hashing functionality for token finalization

## Smart Contract Structure

### Data Structures

- `Token`: Represents a token with an owner and an amount.
- `user_record`: Represents a user record with the owner's address, the amount donated, whether an ID has been assigned, and the user's address.

### Functions

- `donate`: Allows users to donate tokens to the contract.
- `finalize`: Finalizes the token donation by updating the token's amount in the mapping.
- `ajo`: Manages user records and assigns an ID to the user.

## Usage

1. **Deploying the Contract**: Deploy the smart contract to the Aleo blockchain.
2. **Donating Tokens**: Users can donate tokens to the contract by calling the `donate` transition and providing the token and amount.
3. **Managing User Records**: Users can manage their records by calling the `ajo` transition and providing their address and donation amount.

## Example Usage

```aleo
transition donate(token: Token, amount: u64) -> Token {
// Implementation details
}

transition ajo(user: address, amount: u64) -> user_record {
// Implementation details
}

## Build Guide

To compile this Aleo program, run:
```bash
snarkvm build
```

To execute this Aleo program, run:
```bash
snarkvm run hello
```
47 changes: 47 additions & 0 deletions examples/donation/build/main.aleo
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
program donation.aleo;

record user_record:
owner as address.private;
amount as u64.private;
id_assigned as boolean.private;
user as address.private;

record Token:
owner as address.private;
amount as u64.private;


mapping balances:
key as field.public;
value as u64.public;

closure only_owner:
input r0 as address;
assert.eq self.caller r0;
output r0 as address;


function donate:
input r0 as Token.record;
input r1 as u64.private;
sub r0.amount r1 into r2;
cast r0.owner r2 into r3 as Token.record;
hash.bhp256 r0.owner into r4 as field;
async donate r4 r1 into r5;
output r3 as Token.record;
output r5 as donation.aleo/donate.future;

finalize donate:
input r0 as field.public;
input r1 as u64.public;
get.or_use balances[r0] 0u64 into r2;
add r2 r1 into r3;
set r3 into balances[r0];


function ajo:
input r0 as address.private;
input r1 as u64.private;
assert.eq self.caller r0;
cast aleo1fxs9s0w97lmkwlcmgn0z3nuxufdee5yck9wqrs0umevp7qs0sg9q5xxxzh r1 true r0 into r2 as user_record.record;
output r2 as user_record.record;
6 changes: 6 additions & 0 deletions examples/donation/build/program.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"program": "donation.aleo",
"version": "0.0.0",
"description": "",
"license": "MIT"
}
1 change: 1 addition & 0 deletions examples/donation/leo.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package = []
6 changes: 6 additions & 0 deletions examples/donation/program.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"program": "donation.aleo",
"version": "0.0.0",
"description": "",
"license": "MIT"
}
55 changes: 55 additions & 0 deletions examples/donation/src/main.leo
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// The 'donation' program.
program donation.aleo {
mapping balances: field => u64;

record user_record {
owner: address;
amount: u64;
id_assigned: bool;
user: address;

}


record Token {
owner: address,
amount: u64,

}
function only_owner (owner: address) -> address {

assert_eq(self.caller, owner );
return owner;
}

transition donate(token: Token, amount: u64) -> Token {
let difference: u64 = token.amount - amount;

let remaining: Token = Token {
owner: token.owner,
amount: difference,
};
let hash: field = BHP256::hash_to_field(token.owner);
return remaining then finalize(hash, amount);


}


finalize donate(hash: field, amount: u64) {
let current_amount: u64 = Mapping::get_or_use(balances, hash, 0u64);
Mapping::set(balances, hash, current_amount + amount);
}

transition ajo ( user: address, amount: u64) -> user_record {
assert_eq(self.caller,user );

return user_record {
owner: aleo1fxs9s0w97lmkwlcmgn0z3nuxufdee5yck9wqrs0umevp7qs0sg9q5xxxzh,
amount: amount,
id_assigned: true,
user: user,
};
}

}