Skip to content

Commit

Permalink
refactor: using U128 in contract function args
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Jan 10, 2025
1 parent b883a7d commit 3f474f1
Show file tree
Hide file tree
Showing 19 changed files with 266 additions and 306 deletions.
181 changes: 66 additions & 115 deletions noir-projects/noir-contracts/contracts/amm_contract/src/main.nr

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ contract AppSubscription {
target_address: PublicImmutable<AztecAddress, Context>,
subscription_token_address: PublicImmutable<AztecAddress, Context>,
subscription_recipient_address: PublicImmutable<AztecAddress, Context>,
subscription_price: PublicImmutable<Field, Context>,
subscription_price: PublicImmutable<U128, Context>,
subscriptions: Map<AztecAddress, PrivateMutable<SubscriptionNote, Context>, Context>,
fee_juice_limit_per_tx: PublicImmutable<Field, Context>,
}
Expand Down Expand Up @@ -68,7 +68,7 @@ contract AppSubscription {
target_address: AztecAddress,
subscription_recipient_address: AztecAddress,
subscription_token_address: AztecAddress,
subscription_price: Field,
subscription_price: U128,
fee_juice_limit_per_tx: Field,
) {
storage.target_address.initialize(target_address);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ contract Claim {
context.push_nullifier(nullifier);

// 4) Finally we mint the reward token to the sender of the transaction
Token::at(storage.reward_token.read()).mint_to_public(recipient, proof_note.value).enqueue(
&mut context,
);
// TODO(benesjan): Instead of ValueNote use UintNote to avoid the conversion to U128 below.
Token::at(storage.reward_token.read())
.mint_to_public(recipient, U128::from_integer(proof_note.value))
.enqueue(&mut context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ contract Crowdfunding {
#[event]
struct WithdrawalProcessed {
who: AztecAddress,
amount: u64,
amount: U128,
}

// docs:start:storage
Expand Down Expand Up @@ -65,7 +65,7 @@ contract Crowdfunding {

// docs:start:donate
#[private]
fn donate(amount: u64) {
fn donate(amount: U128) {
// 1) Check that the deadline has not passed --> we do that via the router contract to conceal which contract
// is performing the check.
// docs:start:call-check-deadline
Expand All @@ -76,13 +76,14 @@ contract Crowdfunding {
// 2) Transfer the donation tokens from donor to this contract
let donor = context.msg_sender();
Token::at(storage.donation_token.read())
.transfer_in_private(donor, context.this_address(), amount as Field, 0)
.transfer_in_private(donor, context.this_address(), amount, 0)
.call(&mut context);
// docs:end:do-transfer
// 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.
// docs:start:valuenote_new
let mut note = ValueNote::new(amount as Field, donor);
// TODO(benesjan): Instead of ValueNote use UintNote to avoid the conversion to a Field below.
let mut note = ValueNote::new(amount.to_field(), donor);

// docs:end:valuenote_new
storage.donation_receipts.insert(&mut note).emit(encode_and_encrypt_note(
Expand All @@ -96,13 +97,13 @@ contract Crowdfunding {
// docs:start:operator-withdrawals
// Withdraws balance to the operator. Requires that msg_sender() is the operator.
#[private]
fn withdraw(amount: u64) {
fn withdraw(amount: U128) {
// 1) Check that msg_sender() is the operator
let operator_address = storage.operator.read();
assert(context.msg_sender() == operator_address, "Not an operator");

// 2) Transfer the donation tokens from this contract to the operator
Token::at(storage.donation_token.read()).transfer(operator_address, amount as Field).call(
Token::at(storage.donation_token.read()).transfer(operator_address, amount).call(
&mut context,
);
// 3) Emit an unencrypted event so that anyone can audit how much the operator has withdrawn
Expand All @@ -114,7 +115,7 @@ contract Crowdfunding {

#[public]
#[internal]
fn _publish_donation_receipts(amount: u64, to: AztecAddress) {
fn _publish_donation_receipts(amount: U128, to: AztecAddress) {
WithdrawalProcessed { amount, who: to }.emit(encode_event(&mut context));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ contract Escrow {

// Withdraws balance. Requires that msg.sender is the owner.
#[private]
fn withdraw(token: AztecAddress, amount: Field, recipient: AztecAddress) {
fn withdraw(token: AztecAddress, amount: U128, recipient: AztecAddress) {
let sender = context.msg_sender();

let note = storage.owner.get_note();
Expand Down
22 changes: 15 additions & 7 deletions noir-projects/noir-contracts/contracts/fpc_contract/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ contract FPC {
/// - which FPC has been used to make the payment;
/// - the asset which was used to make the payment.
#[private]
fn fee_entrypoint_private(max_fee: Field, nonce: Field) {
fn fee_entrypoint_private(max_fee: U128, nonce: Field) {
// TODO(PR #8022): Once PublicImmutable performs only 1 merkle proof here, we'll save ~4k gates
let config = storage.config.read();

Expand Down Expand Up @@ -110,7 +110,7 @@ contract FPC {
/// Protocol-enshrined fee-payment phase:
/// 4. The protocol deducts the actual fee denominated in fee juice from the FPC's balance.
#[private]
fn fee_entrypoint_public(max_fee: Field, nonce: Field) {
fn fee_entrypoint_public(max_fee: U128, nonce: Field) {
// TODO(PR #8022): Once PublicImmutable performs only 1 merkle proof here, we'll save ~4k gates
let config = storage.config.read();

Expand All @@ -124,10 +124,18 @@ contract FPC {
context.set_as_fee_payer();
// TODO(#6277) for improving interface:
// FPC::at(context.this_address()).pay_refund(...).set_public_teardown_function(&mut context);
let max_fee_serialized = max_fee.serialize();
context.set_public_teardown_function(
context.this_address(),
comptime { FunctionSelector::from_signature("pay_refund((Field),Field,(Field))") },
[context.msg_sender().to_field(), max_fee, config.accepted_asset.to_field()],
comptime {
FunctionSelector::from_signature("pay_refund((Field),(Field,Field),(Field))")
},
[
context.msg_sender().to_field(),
max_fee_serialized[0],
max_fee_serialized[1],
config.accepted_asset.to_field(),
],
);
}

Expand All @@ -136,9 +144,9 @@ contract FPC {
/// to avoid the need for another read from public storage.
#[public]
#[internal]
fn pay_refund(refund_recipient: AztecAddress, max_fee: Field, accepted_asset: AztecAddress) {
let actual_fee = context.transaction_fee();
assert(!max_fee.lt(actual_fee), "Max fee paid to the paymaster does not cover actual fee");
fn pay_refund(refund_recipient: AztecAddress, max_fee: U128, accepted_asset: AztecAddress) {
let actual_fee = U128::from_integer(context.transaction_fee());
assert(actual_fee <= max_fee, "Max fee paid to the paymaster does not cover actual fee");
// TODO(#10805): Introduce a real exchange rate
let refund = max_fee - actual_fee;

Expand Down
78 changes: 36 additions & 42 deletions noir-projects/noir-contracts/contracts/lending_contract/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ contract Lending {
collateral_asset: PublicMutable<AztecAddress, Context>,
stable_coin: PublicMutable<AztecAddress, Context>,
assets: Map<Field, PublicMutable<Asset, Context>, Context>,
collateral: Map<AztecAddress, PublicMutable<Field, Context>, Context>,
static_debt: Map<AztecAddress, PublicMutable<Field, Context>, Context>, // abusing keys very heavily
collateral: Map<AztecAddress, PublicMutable<U128, Context>, Context>,
static_debt: Map<AztecAddress, PublicMutable<U128, Context>, Context>, // abusing keys very heavily
}

// Constructs the contract.
Expand All @@ -46,18 +46,18 @@ contract Lending {
#[public]
fn init(
oracle: AztecAddress,
loan_to_value: Field,
loan_to_value: U128,
collateral_asset: AztecAddress,
stable_coin: AztecAddress,
) {
let asset_loc = storage.assets.at(0);
let asset: Asset = asset_loc.read();

let loan_to_value = U128::from_integer(loan_to_value);
let loan_to_value = loan_to_value;

assert(loan_to_value <= U128::from_integer(10000));
assert(asset.last_updated_ts == 0);
assert(asset.interest_accumulator == U128::from_integer(0));
assert(asset.interest_accumulator == U128::zero());

let last_updated_ts = context.timestamp();

Expand Down Expand Up @@ -103,7 +103,7 @@ contract Lending {
#[private]
fn deposit_private(
from: AztecAddress,
amount: Field,
amount: U128,
nonce: Field,
secret: Field,
on_behalf_of: Field,
Expand All @@ -123,7 +123,7 @@ contract Lending {

#[public]
fn deposit_public(
amount: Field,
amount: U128,
nonce: Field,
on_behalf_of: Field,
collateral_asset: AztecAddress,
Expand All @@ -140,7 +140,7 @@ contract Lending {

#[public]
#[internal]
fn _deposit(owner: AztecAddress, amount: Field, collateral_asset: AztecAddress) {
fn _deposit(owner: AztecAddress, amount: U128, collateral_asset: AztecAddress) {
let _asset = Lending::at(context.this_address()).update_accumulator().call(&mut context);

let coll_asset = storage.collateral_asset.read();
Expand All @@ -152,46 +152,41 @@ contract Lending {
}

#[private]
fn withdraw_private(secret: Field, to: AztecAddress, amount: Field) {
fn withdraw_private(secret: Field, to: AztecAddress, amount: U128) {
let on_behalf_of = compute_identifier(secret, 0, context.msg_sender().to_field());
Lending::at(context.this_address())
._withdraw(AztecAddress::from_field(on_behalf_of), to, amount)
.enqueue(&mut context);
}

#[public]
fn withdraw_public(to: AztecAddress, amount: Field) {
fn withdraw_public(to: AztecAddress, amount: U128) {
let _ = Lending::at(context.this_address())
._withdraw(context.msg_sender(), to, amount)
.call(&mut context);
}

#[public]
#[internal]
fn _withdraw(owner: AztecAddress, recipient: AztecAddress, amount: Field) {
fn _withdraw(owner: AztecAddress, recipient: AztecAddress, amount: U128) {
let asset = Lending::at(context.this_address()).update_accumulator().call(&mut context);
let price = PriceFeed::at(asset.oracle).get_price(0).view(&mut context).price;

let coll_loc = storage.collateral.at(owner);
let collateral: Field = coll_loc.read();
let collateral = coll_loc.read();

let debt_loc = storage.static_debt.at(owner);
let static_debt: Field = debt_loc.read();
let static_debt = debt_loc.read();

// debt_covered will revert if decrease would leave insufficient collateral to cover debt.
// or trying to remove more collateral than available
let debt_covered = covered_by_collateral(
price,
asset.loan_to_value,
U128::from_integer(collateral),
U128::from_integer(0),
U128::from_integer(amount),
);
let debt_covered =
covered_by_collateral(price, asset.loan_to_value, collateral, U128::zero(), amount);
let debt_returns = debt_updates(
asset.interest_accumulator,
U128::from_integer(static_debt),
U128::from_integer(0),
U128::from_integer(0),
static_debt,
U128::zero(),
U128::zero(),
);

assert(debt_returns.debt_value < debt_covered);
Expand All @@ -206,47 +201,47 @@ contract Lending {
}

#[private]
fn borrow_private(secret: Field, to: AztecAddress, amount: Field) {
fn borrow_private(secret: Field, to: AztecAddress, amount: U128) {
let on_behalf_of = compute_identifier(secret, 0, context.msg_sender().to_field());
let _ = Lending::at(context.this_address())
._borrow(AztecAddress::from_field(on_behalf_of), to, amount)
.enqueue(&mut context);
}

#[public]
fn borrow_public(to: AztecAddress, amount: Field) {
fn borrow_public(to: AztecAddress, amount: U128) {
let _ = Lending::at(context.this_address())._borrow(context.msg_sender(), to, amount).call(
&mut context,
);
}

#[public]
#[internal]
fn _borrow(owner: AztecAddress, to: AztecAddress, amount: Field) {
fn _borrow(owner: AztecAddress, to: AztecAddress, amount: U128) {
let asset = Lending::at(context.this_address()).update_accumulator().call(&mut context);
let price = PriceFeed::at(asset.oracle).get_price(0).view(&mut context).price;

// Fetch collateral and static_debt, compute health of current position
let collateral = U128::from_integer(storage.collateral.at(owner).read());
let static_debt = U128::from_integer(storage.static_debt.at(owner).read());
let collateral = storage.collateral.at(owner).read();
let static_debt = storage.static_debt.at(owner).read();

let debt_covered = covered_by_collateral(
price,
asset.loan_to_value,
collateral,
U128::from_integer(0),
U128::from_integer(0),
U128::zero(),
U128::zero(),
);
let debt_returns = debt_updates(
asset.interest_accumulator,
static_debt,
U128::from_integer(amount),
U128::from_integer(0),
amount,
U128::zero(),
);

assert(debt_returns.debt_value < debt_covered);

storage.static_debt.at(owner).write(debt_returns.static_debt.to_integer());
storage.static_debt.at(owner).write(debt_returns.static_debt);

// @todo @LHerskind Need to support both private and public minting.
let stable_coin = storage.stable_coin.read();
Expand All @@ -256,7 +251,7 @@ contract Lending {
#[private]
fn repay_private(
from: AztecAddress,
amount: Field,
amount: U128,
nonce: Field,
secret: Field,
on_behalf_of: Field,
Expand All @@ -273,7 +268,7 @@ contract Lending {
}

#[public]
fn repay_public(amount: Field, nonce: Field, owner: AztecAddress, stable_coin: AztecAddress) {
fn repay_public(amount: U128, nonce: Field, owner: AztecAddress, stable_coin: AztecAddress) {
let _ = Token::at(stable_coin).burn_public(context.msg_sender(), amount, nonce).call(
&mut context,
);
Expand All @@ -284,21 +279,21 @@ contract Lending {

#[public]
#[internal]
fn _repay(owner: AztecAddress, amount: Field, stable_coin: AztecAddress) {
fn _repay(owner: AztecAddress, amount: U128, stable_coin: AztecAddress) {
let asset = Lending::at(context.this_address()).update_accumulator().call(&mut context);

// To ensure that private is using the correct token.
assert(stable_coin.eq(storage.stable_coin.read()));

let static_debt = U128::from_integer(storage.static_debt.at(owner).read());
let static_debt = storage.static_debt.at(owner).read();
let debt_returns = debt_updates(
asset.interest_accumulator,
static_debt,
U128::from_integer(0),
U128::from_integer(amount),
U128::zero(),
amount,
);

storage.static_debt.at(owner).write(debt_returns.static_debt.to_integer());
storage.static_debt.at(owner).write(debt_returns.static_debt);
}

#[public]
Expand All @@ -313,8 +308,7 @@ contract Lending {
let collateral = storage.collateral.at(owner).read();
let static_debt = storage.static_debt.at(owner).read();
let asset: Asset = storage.assets.at(0).read();
let debt =
debt_value(U128::from_integer(static_debt), asset.interest_accumulator).to_integer();
let debt = debt_value(static_debt, asset.interest_accumulator);
Position { collateral, static_debt, debt }
}

Expand Down
Loading

0 comments on commit 3f474f1

Please sign in to comment.