-
Notifications
You must be signed in to change notification settings - Fork 615
feat: crowdfunding contract #4917
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
Merged
Merged
Changes from all commits
Commits
Show all changes
61 commits
Select commit
Hold shift + click to select a range
b207d7a
feat: first version of crowdfunding
sirasistant 70a938b
add to toml
sirasistant b9b2f77
Initial tests
73d68a7
start breaking stuffs
sirasistant 2d6d07c
try to fix authwit
sirasistant d5c2ab7
fix: test
sirasistant 79d7326
Fixing tests
251aa4e
add
94c6fa7
chore: refactor a bit test
sirasistant 5b9349f
Working claim
5e6655f
merge
261228f
Merge remote-tracking branch 'origin/aztec_juicebox' into aztec_juicebox
c7283dc
asdf
5879412
Adding note validity
b76cc1e
Merge branch 'master' into aztec_juicebox
ec90172
allow pxe http client retries
spypsy 64f519a
Merge branch 'master' into aztec_juicebox
d5463f9
Merge remote-tracking branch 'origin/aztec_juicebox' into aztec_juicebox
6df28df
feat: added event
sirasistant e317951
Merge branch 'master' into aztec_juicebox
benesjan e6fcbe4
fmt
benesjan 74a278d
fixes
benesjan dd3ad12
no juicebox
benesjan 5ad9915
enabling test in CI
benesjan bf3910f
fmt
benesjan 1975611
WIP
benesjan 18b6d8d
adding a TODO
benesjan 36cac5e
Merge branch 'master' into aztec_juicebox
benesjan 1b7e2f0
fix
benesjan 36669db
diff cleanup
benesjan f58313d
adding TODO
benesjan dd0078f
Merge branch 'master' into aztec_juicebox
benesjan c29dbbb
Address comments
ee68d37
Addressing comments pt 2
4ba8385
Apply suggestions from code review
benesjan 7e86486
import cleanup
benesjan 16c7be5
Merge branch 'master' into aztec_juicebox
benesjan b60f5f6
using SharedImmutable in Claim contract + checking note contract
benesjan 55a1ad6
Using SharedImmutable in Crowdfunding contract
benesjan e85a7a0
Merge branch 'master' into aztec_juicebox
benesjan 4304d88
cleanup
benesjan 864a9b3
fixes
benesjan 1d8845d
naming cleanup
benesjan f1fc8c1
cleanup
benesjan d5ea55a
better test name
benesjan 5010252
Merge branch 'master' into aztec_juicebox
benesjan bb3146d
final final cleanup
benesjan f9d2693
more negative tests
benesjan bd92b5b
deadline
benesjan 891d309
Merge branch 'master' into aztec_juicebox
benesjan c03209c
fix
benesjan 8a21aa2
test speedup
benesjan 53a0a85
adding TODO
benesjan 14dccd5
deadline test
benesjan 0fa7c45
fix
benesjan b08bb0d
Merge branch 'master' into aztec_juicebox
benesjan 6d56e9f
Update .circleci/config.yml
benesjan 51d6fdd
Merge branch 'master' into aztec_juicebox
benesjan fa743bd
interface cleanup
benesjan 790cff0
cleanup
benesjan 3e47da7
Merge branch 'master' into aztec_juicebox
benesjan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
noir-projects/noir-contracts/contracts/claim_contract/Nargo.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| [package] | ||
| name = "claim_contract" | ||
| authors = [""] | ||
| compiler_version = ">=0.18.0" | ||
| type = "contract" | ||
|
|
||
| [dependencies] | ||
| aztec = { path = "../../../aztec-nr/aztec" } | ||
| value_note = { path = "../../../aztec-nr/value-note" } |
37 changes: 37 additions & 0 deletions
37
noir-projects/noir-contracts/contracts/claim_contract/src/interfaces.nr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| use dep::aztec::{ | ||
| protocol_types::{abis::function_selector::FunctionSelector, address::AztecAddress}, | ||
| context::PrivateContext, | ||
| }; | ||
|
|
||
| struct Token { | ||
| address: AztecAddress, | ||
| } | ||
|
|
||
| impl Token { | ||
| pub fn at(address: AztecAddress) -> Self { | ||
| Self { address } | ||
| } | ||
|
|
||
| fn mint_public(self: Self, context: &mut PrivateContext, to: AztecAddress, amount: Field) { | ||
| let _ret = context.call_public_function( | ||
| self.address, | ||
| FunctionSelector::from_signature("mint_public((Field),Field)"), | ||
| [to.to_field(), amount] | ||
| ); | ||
| } | ||
|
|
||
| pub fn transfer( | ||
| self: Self, | ||
| context: &mut PrivateContext, | ||
| from: AztecAddress, | ||
| to: AztecAddress, | ||
| amount: Field, | ||
| nonce: Field | ||
| ) { | ||
| let _ret = context.call_private_function( | ||
| self.address, | ||
| FunctionSelector::from_signature("transfer((Field),(Field),Field,Field)"), | ||
| [from.to_field(), to.to_field(), amount, nonce] | ||
| ); | ||
| } | ||
| } |
59 changes: 59 additions & 0 deletions
59
noir-projects/noir-contracts/contracts/claim_contract/src/main.nr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| contract Claim { | ||
| mod interfaces; | ||
|
|
||
| use dep::aztec::{ | ||
| history::note_inclusion::prove_note_inclusion, | ||
| protocol_types::{ | ||
| abis::function_selector::FunctionSelector, | ||
| address::AztecAddress, | ||
| }, | ||
| state_vars::SharedImmutable, | ||
| }; | ||
| use dep::value_note::value_note::ValueNote; | ||
| use interfaces::Token; | ||
|
|
||
| struct Storage { | ||
| // Address of a contract based on whose notes we distribute the rewards | ||
| target_contract: SharedImmutable<AztecAddress>, | ||
| // Token to be distributed as a reward when claiming | ||
| reward_token: SharedImmutable<AztecAddress>, | ||
| } | ||
|
|
||
| #[aztec(private)] | ||
| fn constructor(target_contract: AztecAddress, reward_token: AztecAddress) { | ||
| let selector = FunctionSelector::from_signature("_initialize((Field),(Field))"); | ||
| context.call_public_function( | ||
| context.this_address(), | ||
| selector, | ||
| [target_contract.to_field(), reward_token.to_field()] | ||
| ); | ||
| } | ||
|
|
||
| #[aztec(public)] | ||
| #[aztec(internal)] | ||
| #[aztec(noinitcheck)] | ||
| fn _initialize(target_contract: AztecAddress, reward_token: AztecAddress) { | ||
| storage.target_contract.initialize(target_contract); | ||
| storage.reward_token.initialize(reward_token); | ||
| } | ||
|
|
||
| #[aztec(private)] | ||
| fn claim(proof_note: ValueNote) { | ||
| // 1) Check that the note corresponds to the target contract | ||
| let target_address = storage.target_contract.read_private(); | ||
| assert(target_address == proof_note.header.contract_address, "Note does not correspond to the target contract"); | ||
|
|
||
| // 2) Prove that the note hash exists in the note hash tree | ||
| prove_note_inclusion(proof_note, context); | ||
|
|
||
| // 3) Compute and emit a nullifier which is unique to the note and this contract to ensure the reward can be | ||
| // claimed only once with the given note. | ||
| // Note: The nullifier is unique to the note and THIS contract because the protocol siloes all nullifiers with | ||
| // the address of a contract it was emitted from. | ||
| context.push_new_nullifier(proof_note.compute_nullifier(&mut context), 0); | ||
|
|
||
| // 4) Finally we mint the reward token to the sender of the transaction | ||
| let reward_token = Token::at(storage.reward_token.read_private()); | ||
| reward_token.mint_public(&mut context, context.msg_sender(), proof_note.value); | ||
| } | ||
| } | ||
9 changes: 9 additions & 0 deletions
9
noir-projects/noir-contracts/contracts/crowdfunding_contract/Nargo.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| [package] | ||
| name = "crowdfunding_contract" | ||
| authors = [""] | ||
| compiler_version = ">=0.18.0" | ||
| type = "contract" | ||
|
|
||
| [dependencies] | ||
| aztec = { path = "../../../aztec-nr/aztec" } | ||
| value_note = { path = "../../../aztec-nr/value-note" } |
27 changes: 27 additions & 0 deletions
27
noir-projects/noir-contracts/contracts/crowdfunding_contract/src/interfaces.nr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| use dep::aztec::protocol_types::{abis::function_selector::FunctionSelector, address::{AztecAddress, EthAddress}}; | ||
| use dep::aztec::{context::{PrivateContext, PublicContext}}; | ||
|
|
||
| struct Token { | ||
|
nventuro marked this conversation as resolved.
|
||
| address: AztecAddress, | ||
| } | ||
|
|
||
| impl Token { | ||
| pub fn at(address: AztecAddress) -> Self { | ||
| Self { address } | ||
| } | ||
|
|
||
| pub fn transfer( | ||
| self: Self, | ||
| context: &mut PrivateContext, | ||
| from: AztecAddress, | ||
| to: AztecAddress, | ||
| amount: Field, | ||
| nonce: Field | ||
| ) { | ||
| let _ret = context.call_private_function( | ||
| self.address, | ||
| FunctionSelector::from_signature("transfer((Field),(Field),Field,Field)"), | ||
| [from.to_field(), to.to_field(), amount, nonce] | ||
| ); | ||
| } | ||
| } | ||
108 changes: 108 additions & 0 deletions
108
noir-projects/noir-contracts/contracts/crowdfunding_contract/src/main.nr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| contract Crowdfunding { | ||
| mod interfaces; | ||
|
|
||
| use dep::aztec::{ | ||
| log::emit_unencrypted_log_from_private, | ||
| protocol_types::{ | ||
| abis::function_selector::FunctionSelector, | ||
| address::AztecAddress, | ||
| traits::Serialize | ||
| }, | ||
| state_vars::{PrivateSet, PublicImmutable, SharedImmutable}, | ||
| }; | ||
| use dep::value_note::value_note::ValueNote; | ||
| use interfaces::Token; | ||
|
|
||
| #[event] | ||
| struct WithdrawalProcessed { | ||
| who: AztecAddress, | ||
| amount: u64, | ||
| } | ||
|
|
||
| impl Serialize<2> for WithdrawalProcessed { | ||
| fn serialize(self: Self) -> [Field; 2] { | ||
| [self.who.to_field(), self.amount as Field] | ||
| } | ||
| } | ||
|
|
||
| struct Storage { | ||
| // Token used for donations (e.g. DAI) | ||
| donation_token: SharedImmutable<AztecAddress>, | ||
| // Crowdfunding campaign operator | ||
| operator: SharedImmutable<AztecAddress>, | ||
| // End of the crowdfunding campaign after which no more donations are accepted | ||
| // TODO(#4990): Make deadline a u64 once the neccessary traits are implemented | ||
| deadline: PublicImmutable<Field>, | ||
| // Notes emitted to donors when they donate (later on used to claim rewards in the Claim contract) | ||
| claim_notes: PrivateSet<ValueNote>, | ||
| } | ||
|
|
||
| #[aztec(private)] | ||
| fn constructor(donation_token: AztecAddress, operator: AztecAddress, deadline: u64) { | ||
| let selector = FunctionSelector::from_signature("_initialize((Field),(Field),Field)"); | ||
| context.call_public_function( | ||
| context.this_address(), | ||
| selector, | ||
| [donation_token.to_field(), operator.to_field(), deadline as Field] | ||
| ); | ||
| } | ||
|
|
||
| #[aztec(public)] | ||
| #[aztec(internal)] | ||
| #[aztec(noinitcheck)] | ||
| // TODO(#4990): Make deadline a u64 once the neccessary traits are implemented | ||
| fn _initialize(donation_token: AztecAddress, operator: AztecAddress, deadline: Field) { | ||
| storage.donation_token.initialize(donation_token); | ||
| storage.operator.initialize(operator); | ||
| storage.deadline.initialize(deadline); | ||
| } | ||
|
|
||
| #[aztec(public)] | ||
| #[aztec(internal)] | ||
| fn _check_deadline() { | ||
| // TODO(#4990): Remove the cast here once u64 is used directly | ||
| let deadline = storage.deadline.read() as u64; | ||
| assert(context.timestamp() as u64 < deadline, "Deadline has passed"); | ||
| } | ||
|
|
||
| #[aztec(private)] | ||
| fn donate(amount: u64) { | ||
| // 1) Check that the deadline has not passed | ||
| context.call_public_function( | ||
| context.this_address(), | ||
| FunctionSelector::from_signature("_check_deadline()"), | ||
| [] | ||
| ); | ||
|
|
||
| // 2) Transfer the donation tokens from donor to this contract | ||
| let donation_token = Token::at(storage.donation_token.read_private()); | ||
| donation_token.transfer( | ||
| &mut context, | ||
| context.msg_sender(), | ||
| context.this_address(), | ||
| amount as Field, | ||
| 0 | ||
| ); | ||
|
|
||
| // 3) Create a value note for the donor so that he can later on claim a rewards token in the Claim | ||
| // contract by proving that the hash of this note exists in the note hash tree. | ||
| let mut note = ValueNote::new(amount as Field, context.msg_sender()); | ||
| storage.claim_notes.insert(&mut note, true); | ||
| } | ||
|
|
||
| // Withdraws balance to the operator. Requires that msg_sender() is the operator. | ||
| #[aztec(private)] | ||
| fn withdraw(amount: u64) { | ||
| // 1) Check that msg_sender() is the operator | ||
| let operator_address = storage.operator.read_private(); | ||
| assert(context.msg_sender() == operator_address, "Not an operator"); | ||
|
|
||
| // 2) Transfer the donation tokens from this contract to the operator | ||
| let donation_token = Token::at(storage.donation_token.read_private()); | ||
| donation_token.transfer(&mut context, context.this_address(), operator_address, amount as Field, 0); | ||
|
|
||
| // 3) Emit an unencrypted event so that anyone can audit how much the operator has withdrawn | ||
| let event = WithdrawalProcessed { amount, who: operator_address }; | ||
| emit_unencrypted_log_from_private(&mut context, event.serialize()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.