Skip to content

Commit

Permalink
recursive token
Browse files Browse the repository at this point in the history
  • Loading branch information
akonior committed Jun 7, 2024
1 parent d4bc431 commit 410b039
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
11 changes: 10 additions & 1 deletion vlayer/ethereum/circuits/lib/src/token.nr
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,20 @@ impl ERC20Token {
}

trait ERC20 {
fn get_balance(self, wallet_address: Address, block_number: u64) -> U128 ;
fn get_balance(self, wallet_address: Address, block_number: u64) -> U128;
fn get_balance_recursive(self, wallet_address: Address, block_number: u64) -> U128;
}

impl ERC20 for ERC20Token {
fn get_balance(self, wallet_address: Address, block_number: u64) -> U128 {
let storage_key = self.calculate_balance_storage_key(wallet_address);
let account = get_account_with_storage(self.chain_id, block_number, self.address, storage_key);
let balance = account.values[TOKEN_BALANCE_INDEX];

U128::from_integer(bytes32_to_field(balance))
}

fn get_balance_recursive(self, wallet_address: Address, block_number: u64) -> U128 {
let storage_key = self.calculate_balance_storage_key(wallet_address);
let account = get_account_with_storage_recursive(self.chain_id, block_number, self.address, storage_key);
let balance = account.values[TOKEN_BALANCE_INDEX];
Expand Down
8 changes: 7 additions & 1 deletion vlayer/ethereum/circuits/lib/src/token_int_test.nr
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@ mod test_ERC20Token {
use dep::ethereum::fixtures::mainnet::{
paris::usdc_circle::header::{block_header_partial as paris_block_header_partial, block_header_rlp as paris_block_header_rlp},
paris::usdc_circle::header::{hash, number, state_root, transactions_root, receipts_root},
paris::usdc_circle::account::account,
paris::usdc_circle::account::account, paris::usdc_circle::storage::values,
paris::usdc_circle::state_proof::proof_input_serialized as state_proof_input_serialized,
paris::usdc_circle::storage_proof::proofs_serialized
};
use crate::chain_id::MAINNET;
use dep::ethereum::account_with_storage_recursive::RecursiveProof;
use dep::ethereum::account_with_storage::StorageWithinBlock;

#[test]
fn success() {
let result = StorageWithinBlock { block_hash: hash, account, values };
let recursive_proof = RecursiveProof { key_hash: 1, verification_key: [0; 114], proof: [0; 93] };

let _ = OracleMock::mock("get_storage_recursive").returns((result.serialize(), recursive_proof));
let _ = OracleMock::mock("get_header").returns((paris_block_header_partial, paris_block_header_rlp));
let _ = OracleMock::mock("get_proof").returns((account, state_proof_input_serialized, proofs_serialized[0]));

Expand Down

0 comments on commit 410b039

Please sign in to comment.