-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR adds a gas payment contract. --------- Co-authored-by: Rahul Kothari <rahul.kothari.201@gmail.com> Co-authored-by: Mitchell Tracy <mitchell@aztecprotocol.com>
- Loading branch information
1 parent
5920012
commit 5d4702b
Showing
20 changed files
with
215 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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 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
10 changes: 10 additions & 0 deletions
10
noir-projects/noir-contracts/contracts/gas_token_contract/Nargo.toml
This file contains 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,10 @@ | ||
[package] | ||
name = "gas_token_contract" | ||
authors = [""] | ||
compiler_version = ">=0.18.0" | ||
type = "contract" | ||
|
||
[dependencies] | ||
aztec = { path = "../../../aztec-nr/aztec" } | ||
safe_math = { path = "../../../aztec-nr/safe-math" } | ||
authwit = { path = "../../../aztec-nr/authwit" } |
6 changes: 6 additions & 0 deletions
6
noir-projects/noir-contracts/contracts/gas_token_contract/src/fee.nr
This file contains 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,6 @@ | ||
use dep::safe_math::SafeU120; | ||
use dep::aztec::context::PublicContext; | ||
|
||
pub fn calculate_fee(_context: PublicContext) -> SafeU120 { | ||
SafeU120::new(1) | ||
} |
63 changes: 63 additions & 0 deletions
63
noir-projects/noir-contracts/contracts/gas_token_contract/src/main.nr
This file contains 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,63 @@ | ||
mod fee; | ||
|
||
contract GasToken { | ||
use dep::aztec::protocol_types::{abis::function_selector::FunctionSelector, address::AztecAddress}; | ||
use dep::aztec::{hash::{compute_secret_hash}, state_vars::{public_state::PublicState, map::Map}}; | ||
|
||
use dep::safe_math::SafeU120; | ||
|
||
use crate::fee::calculate_fee; | ||
|
||
struct Storage { | ||
balances: Map<AztecAddress, PublicState<SafeU120>>, | ||
} | ||
|
||
#[aztec(private)] | ||
fn constructor() {} | ||
|
||
#[aztec(public)] | ||
fn redeem_bridged_balance(amount: Field) { | ||
// mock | ||
let amount_u120 = SafeU120::new(amount); | ||
let new_balance = storage.balances.at(context.msg_sender()).read().add(amount_u120); | ||
storage.balances.at(context.msg_sender()).write(new_balance); | ||
} | ||
|
||
#[aztec(public)] | ||
fn check_balance(fee_limit: Field) { | ||
let fee_limit_u120 = SafeU120::new(fee_limit); | ||
assert(storage.balances.at(context.msg_sender()).read().ge(fee_limit_u120), "Balance too low"); | ||
} | ||
|
||
#[aztec(public)] | ||
fn pay_fee(fee_limit: Field) -> Field { | ||
let fee_limit_u120 = SafeU120::new(fee_limit); | ||
let fee = calculate_fee(context); | ||
assert(fee.le(fee_limit_u120), "Fee too high"); | ||
|
||
let sender_new_balance = storage.balances.at(context.msg_sender()).read().sub(fee); | ||
storage.balances.at(context.msg_sender()).write(sender_new_balance); | ||
|
||
let recipient_new_balance = storage.balances.at(context.fee_recipient()).read().add(fee); | ||
storage.balances.at(context.fee_recipient()).write(recipient_new_balance); | ||
|
||
let rebate = fee_limit_u120.sub(fee); | ||
rebate.value as Field | ||
} | ||
|
||
// utility function for testing | ||
unconstrained fn balance_of(owner: AztecAddress) -> pub Field { | ||
storage.balances.at(owner).read().value as Field | ||
} | ||
|
||
// TODO: remove this placeholder once https://github.com/AztecProtocol/aztec-packages/issues/2918 is implemented | ||
unconstrained fn compute_note_hash_and_nullifier( | ||
contract_address: AztecAddress, | ||
nonce: Field, | ||
storage_slot: Field, | ||
note_type_id: Field, | ||
serialized_note: [Field; 0] | ||
) -> pub [Field; 4] { | ||
[0, 0, 0, 0] | ||
} | ||
} |
This file contains 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 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 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 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 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 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 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,66 @@ | ||
import { AztecAddress, ContractDeployer, NativeFeePaymentMethod } from '@aztec/aztec.js'; | ||
import { GasTokenContract, TokenContract } from '@aztec/noir-contracts.js'; | ||
import { getCanonicalGasToken } from '@aztec/protocol-contracts/gas-token'; | ||
|
||
import { setup } from './fixtures/utils.js'; | ||
|
||
describe('e2e_fees', () => { | ||
let aliceAddress: AztecAddress; | ||
let _bobAddress: AztecAddress; | ||
let sequencerAddress: AztecAddress; | ||
let gasTokenContract: GasTokenContract; | ||
let testContract: TokenContract; | ||
|
||
beforeAll(async () => { | ||
process.env.PXE_URL = ''; | ||
const { accounts, aztecNode, wallet } = await setup(3); | ||
|
||
await aztecNode.setConfig({ | ||
feeRecipient: accounts.at(-1)!.address, | ||
}); | ||
const canonicalGasToken = getCanonicalGasToken(); | ||
const deployer = new ContractDeployer(canonicalGasToken.artifact, wallet); | ||
const { contract } = await deployer | ||
.deploy() | ||
.send({ | ||
contractAddressSalt: canonicalGasToken.instance.salt, | ||
}) | ||
.wait(); | ||
|
||
gasTokenContract = contract as GasTokenContract; | ||
aliceAddress = accounts.at(0)!.address; | ||
_bobAddress = accounts.at(1)!.address; | ||
sequencerAddress = accounts.at(-1)!.address; | ||
|
||
testContract = await TokenContract.deploy(wallet, aliceAddress, 'Test', 'TEST', 1).send().deployed(); | ||
|
||
// Alice gets a balance of 1000 gas token | ||
await gasTokenContract.methods.redeem_bridged_balance(1000).send().wait(); | ||
}, 100_000); | ||
|
||
it('deploys gas token contract at canonical address', () => { | ||
expect(gasTokenContract.address).toEqual(getCanonicalGasToken().address); | ||
}); | ||
|
||
describe('NativeFeePaymentMethod', () => { | ||
it('pays out the expected fee to the sequencer', async () => { | ||
await testContract.methods | ||
.mint_public(aliceAddress, 1000) | ||
.send({ | ||
fee: { | ||
maxFee: 1, | ||
paymentMethod: new NativeFeePaymentMethod(), | ||
}, | ||
}) | ||
.wait(); | ||
|
||
const [sequencerBalance, aliceBalance] = await Promise.all([ | ||
gasTokenContract.methods.balance_of(sequencerAddress).view(), | ||
gasTokenContract.methods.balance_of(aliceAddress).view(), | ||
]); | ||
|
||
expect(sequencerBalance).toEqual(1n); | ||
expect(aliceBalance).toEqual(999n); | ||
}); | ||
}); | ||
}); |
This file contains 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 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 |
---|---|---|
|
@@ -48,6 +48,9 @@ | |
{ | ||
"path": "../p2p" | ||
}, | ||
{ | ||
"path": "../protocol-contracts" | ||
}, | ||
{ | ||
"path": "../pxe" | ||
}, | ||
|
This file contains 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 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,6 @@ | ||
import { loadContractArtifact } from '@aztec/types/abi'; | ||
import { NoirCompiledContract } from '@aztec/types/noir'; | ||
|
||
import GasTokenJson from '../artifacts/GasToken.json' assert { type: 'json' }; | ||
|
||
export const GasTokenArtifact = loadContractArtifact(GasTokenJson as NoirCompiledContract); |
This file contains 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,8 @@ | ||
import { GasTokenAddress, getCanonicalGasToken } from './index.js'; | ||
|
||
describe('GasToken', () => { | ||
it('returns canonical protocol contract', () => { | ||
const contract = getCanonicalGasToken(); | ||
expect(contract.address.toString()).toEqual(GasTokenAddress.toString()); | ||
}); | ||
}); |
This file contains 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 @@ | ||
import { ProtocolContract, getCanonicalProtocolContract } from '../protocol_contract.js'; | ||
import { GasTokenArtifact } from './artifact.js'; | ||
|
||
/** Returns the canonical deployment of the gas token. */ | ||
export function getCanonicalGasToken(): ProtocolContract { | ||
return getCanonicalProtocolContract(GasTokenArtifact, 1); | ||
} | ||
|
||
export const GasTokenAddress = getCanonicalGasToken().address; |
This file contains 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 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