-
Notifications
You must be signed in to change notification settings - Fork 599
fix: contracts with public keys txe #11910
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
sklppy88
merged 1 commit into
master
from
ek/fix/actually-fix-contracts-with-public-keys-txe
Feb 11, 2025
Merged
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions
2
noir-projects/noir-contracts/contracts/escrow_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
120 changes: 120 additions & 0 deletions
120
noir-projects/noir-contracts/contracts/escrow_contract/src/test.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,120 @@ | ||
| // TODO(ek): Clean up this test | ||
| use crate::Escrow; | ||
| use dep::token::Token; | ||
|
|
||
| use aztec::{ | ||
| oracle::{execution::{get_block_number, get_contract_address}, storage::storage_read}, | ||
| prelude::AztecAddress, | ||
| protocol_types::storage::map::derive_storage_slot_in_map, | ||
| test::helpers::{cheatcodes, test_environment::TestEnvironment}, | ||
| }; | ||
|
|
||
| pub unconstrained fn get_public_balance( | ||
| token_contract_address: AztecAddress, | ||
| address: AztecAddress, | ||
| ) -> U128 { | ||
| let current_contract_address = get_contract_address(); | ||
| cheatcodes::set_contract_address(token_contract_address); | ||
| let block_number = get_block_number(); | ||
|
|
||
| let balances_slot = Token::storage_layout().public_balances.slot; | ||
| let address_slot = derive_storage_slot_in_map(balances_slot, address); | ||
| let amount: U128 = storage_read(token_contract_address, address_slot, block_number); | ||
| cheatcodes::set_contract_address(current_contract_address); | ||
| amount | ||
| } | ||
|
|
||
| pub unconstrained fn get_private_balance( | ||
| token_contract_address: AztecAddress, | ||
| address: AztecAddress, | ||
| ) -> U128 { | ||
| let current_contract_address = get_contract_address(); | ||
| cheatcodes::set_contract_address(token_contract_address); | ||
| // Direct call to unconstrained | ||
| let amt = Token::balance_of_private(address); | ||
| cheatcodes::set_contract_address(current_contract_address); | ||
| amt | ||
| } | ||
|
|
||
| global MINT_AMOUNT: U128 = U128::from_integer(200000); | ||
|
|
||
| unconstrained fn deploy_contracts( | ||
| env: &mut TestEnvironment, | ||
| admin_and_owner: AztecAddress, | ||
| ) -> (AztecAddress, AztecAddress) { | ||
| env.impersonate(admin_and_owner); | ||
|
|
||
| // Deploy token contract | ||
| let donation_token_initializer_call_interface = Token::interface().constructor( | ||
| admin_and_owner, | ||
| "Token00000000000000000000000000", | ||
| "TKN0000000000000000000000000000", | ||
| 18, | ||
| ); | ||
| let donation_token_contract = env | ||
| .deploy("./@token_contract", "Token") | ||
| .with_public_void_initializer(donation_token_initializer_call_interface); | ||
| let token_contract_address = donation_token_contract.to_address(); | ||
| env.advance_block_by(1); | ||
|
|
||
| // Deploy Escrow contract with public keys | ||
| let escrow_contract_initializer_call_interface = | ||
| Escrow::interface().constructor(admin_and_owner); | ||
| let escrow_contract = env | ||
| .deploy_with_public_keys("./@escrow_contract", "Escrow", 6969) | ||
| .with_private_initializer(escrow_contract_initializer_call_interface); | ||
| let escrow_contract_address = escrow_contract.to_address(); | ||
|
|
||
| env.advance_block_by(1); | ||
|
|
||
| Token::at(token_contract_address) | ||
| .mint_to_private(admin_and_owner, admin_and_owner, MINT_AMOUNT) | ||
| .call(&mut env.private()); | ||
|
|
||
| env.advance_block_by(1); | ||
|
|
||
| let private_balance_after_mint = get_private_balance(token_contract_address, admin_and_owner); | ||
| assert(private_balance_after_mint == MINT_AMOUNT); | ||
|
|
||
| (token_contract_address, escrow_contract_address) | ||
| } | ||
|
|
||
| #[test] | ||
| unconstrained fn main() { | ||
| let mut env = TestEnvironment::new(); | ||
|
|
||
| let (account_1, account_2) = (env.create_account_contract(1), env.create_account_contract(2)); | ||
|
|
||
| let (token_contract_address, escrow_contract_address) = deploy_contracts(&mut env, account_1); | ||
|
|
||
| // We transfer tokens to the escrow contract | ||
| let TRANSFER_AMOUNT = U128::from_integer(20000); | ||
| Token::at(token_contract_address).transfer(escrow_contract_address, TRANSFER_AMOUNT).call( | ||
| &mut env.private(), | ||
| ); | ||
| env.advance_block_by(1); | ||
|
|
||
| let balance_of_escrow_after_transfer = | ||
| get_private_balance(token_contract_address, escrow_contract_address); | ||
|
|
||
| assert_eq(balance_of_escrow_after_transfer, TRANSFER_AMOUNT); | ||
|
|
||
| // We then withdraw some escrowed funds to account_2 | ||
| let balance_of_account_2_before_withdrawal = | ||
| get_private_balance(token_contract_address, account_2); | ||
| assert(balance_of_account_2_before_withdrawal == U128::zero()); | ||
|
|
||
| let WITHDRAWAL_AMOUNT = U128::from_integer(69); | ||
| Escrow::at(escrow_contract_address) | ||
| .withdraw(token_contract_address, WITHDRAWAL_AMOUNT, account_2) | ||
| .call(&mut env.private()); | ||
| env.advance_block_by(1); | ||
|
|
||
| let balance_of_account_2_after_withdrawal = | ||
| get_private_balance(token_contract_address, account_2); | ||
| assert(balance_of_account_2_after_withdrawal == WITHDRAWAL_AMOUNT); | ||
|
|
||
| let balance_of_escrow_after_withdrawal = | ||
| get_private_balance(token_contract_address, escrow_contract_address); | ||
| assert(balance_of_escrow_after_withdrawal == TRANSFER_AMOUNT - WITHDRAWAL_AMOUNT); | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✨💖✨