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 13, 2023
1 parent 87ce32f commit d729d6a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 52 deletions.
59 changes: 7 additions & 52 deletions common/src/tap_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,63 +109,15 @@ 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::*;

/// Fixture to generate a wallet and address
pub fn keys() -> (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 fn domain() -> Eip712Domain {
eip712_domain! {
name: "TAP",
version: "1",
chain_id: 1,
verifying_contract: Address::from([0x11u8; 20]),
}
}

/// Fixture to generate a signed receipt using the wallet from `keys()`
/// and the given `query_id` and `value`
pub async fn create_signed_receipt(
allocation_id: Address,
nonce: u64,
timestamp_ns: u64,
value: u128,
) -> SignedReceipt {
let (wallet, _) = keys();

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

#[ignore]
#[sqlx::test]
async fn test_verify_and_store_receipt(pgpool: PgPool) {
Expand All @@ -178,7 +130,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 +162,12 @@ mod test {
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 d729d6a

Please sign in to comment.