Skip to content

Commit

Permalink
refactor: move receipt related fixtures into test_vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
Jannis committed Nov 24, 2023
1 parent 75ade30 commit 3accf00
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
14 changes: 8 additions & 6 deletions common/src/tap_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,14 @@ mod test {

use crate::prelude::{AllocationStatus, SubgraphDeployment};
use alloy_primitives::Address;
use alloy_sol_types::{eip712_domain, Eip712Domain};
use ethers::signers::{coins_bip39::English, LocalWallet, MnemonicBuilder, Signer};
use keccak_hash::H256;
use sqlx::postgres::PgListener;

use tap_core::tap_manager::SignedReceipt;
use tap_core::{eip_712_signed_message::EIP712SignedMessage, tap_receipt::Receipt};
use toolshed::thegraph::DeploymentId;

use crate::test_vectors;
use crate::test_vectors::{self, create_signed_receipt};

use super::*;

Expand Down Expand Up @@ -180,7 +179,6 @@ mod test {

let allocation_id =
Address::from_str("0xdeadbeefcafebabedeadbeefcafebabedeadbeef").unwrap();
let domain = domain();
let signed_receipt =
create_signed_receipt(allocation_id, u64::MAX, u64::MAX, u128::MAX).await;

Expand Down Expand Up @@ -211,8 +209,12 @@ mod test {
let escrow_accounts =
Eventual::from_value(HashMap::from_iter(vec![(keys().1, U256::from(123))]));

let tap_manager =
TapManager::new(pgpool.clone(), indexer_allocations, escrow_accounts, domain);
let tap_manager = TapManager::new(
pgpool.clone(),
indexer_allocations,
escrow_accounts,
test_vectors::TAP_EIP712_DOMAIN.to_owned(),
);

tap_manager
.verify_and_store_receipt(signed_receipt.clone())
Expand Down
46 changes: 46 additions & 0 deletions common/src/test_vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
use std::{collections::HashMap, str::FromStr};

use alloy_primitives::Address;
use alloy_sol_types::{eip712_domain, Eip712Domain};
use ethers::signers::{coins_bip39::English, LocalWallet, MnemonicBuilder, Signer};
use ethers_core::types::U256;
use lazy_static::lazy_static;
use tap_core::{
eip_712_signed_message::EIP712SignedMessage, tap_manager::SignedReceipt, tap_receipt::Receipt,
};
use toolshed::thegraph::DeploymentId;

use crate::prelude::{Allocation, AllocationStatus, SubgraphDeployment};
Expand Down Expand Up @@ -239,4 +244,45 @@ lazy_static! {
(Address::from_str("0x90f8bf6a479f320ead074411a4b0e7944ea8c9c1").unwrap(), U256::from(24)),
(Address::from_str("0x22d491bde2303f2f43325b2108d26f1eaba1e32b").unwrap(), U256::from(42)),
]);

/// Fixture to generate a wallet and address
pub static ref TAP_SENDER: (LocalWallet, Address) = {
let wallet: LocalWallet = MnemonicBuilder::<English>::default()
.phrase("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about")
.build()
.unwrap();
let address = wallet.address();

(wallet, Address::from_slice(address.as_bytes()))
};

pub static ref TAP_EIP712_DOMAIN: Eip712Domain = eip712_domain! {
name: "TAP",
version: "1",
chain_id: 1,
verifying_contract: Address::from([0x11u8; 20]),
};
}

/// Function to generate a signed receipt using the TAP_SENDER wallet.
pub async fn create_signed_receipt(
allocation_id: Address,
nonce: u64,
timestamp_ns: u64,
value: u128,
) -> SignedReceipt {
let (wallet, _) = &*self::TAP_SENDER;

EIP712SignedMessage::new(
&self::TAP_EIP712_DOMAIN,
Receipt {
allocation_id,
nonce,
timestamp_ns,
value,
},
wallet,
)
.await
.unwrap()
}

0 comments on commit 3accf00

Please sign in to comment.