#![expect(clippy::doc_markdown)]
#![expect(clippy::expect_used, clippy::panic)]
use std::path::PathBuf;
use alloy::hex;
use alloy::primitives::{keccak256, B256};
use serde_json::Value;
const INIT_CODE_PAIR_HASH: &str =
"0x57224589c67f3f30a6b0d7a1b54cf3153ab84563bc609ef41dfb34f8b2974d2d";
const EMBEDDED_METADATA_HASH: &str =
"0x361d24efe9bc43b2f8ddececbe5ea7e170556d840eab342be622f3e57ca7317a";
const DSOLC_MARKER: &str = "64736f6c63";
fn oracle_root() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("../../../..")
.join("tier3-oracle")
.canonicalize()
.expect("canonicalize tier3-oracle")
}
fn load_artifact() -> Value {
let path = oracle_root().join("artifacts/PancakeV2Pair/PancakeV2Pair.json");
let raw = std::fs::read_to_string(&path)
.unwrap_or_else(|e| panic!("missing pinned pair artifact {}: {e}", path.display()));
serde_json::from_str(&raw).expect("valid pinned pair JSON")
}
#[test]
fn pinned_pair_creation_code_hits_init_code_pair_hash() {
let v = load_artifact();
let obj = v["bytecode"]["object"]
.as_str()
.expect("creation bytecode.object");
let bytes = hex::decode(obj.trim_start_matches("0x")).expect("hex creation bytecode");
let got = keccak256(&bytes);
let expected: B256 = INIT_CODE_PAIR_HASH
.parse()
.expect("const init-code hash hex");
assert_eq!(
got, expected,
"keccak256(pinned pair creation code) must equal the PancakeSwap V2 INIT_CODE_PAIR_HASH 0x57224589…"
);
let prov = v["provenance"]["initCodeHash"]
.as_str()
.expect("provenance.initCodeHash");
assert_eq!(
prov.to_lowercase(),
INIT_CODE_PAIR_HASH,
"artifact provenance.initCodeHash must match the canonical value"
);
}
#[test]
fn pinned_pair_runtime_embeds_canonical_metadata_hash() {
let v = load_artifact();
let deployed = v["deployedBytecode"]["object"]
.as_str()
.expect("runtime bytecode.object");
let hex_s = deployed.trim_start_matches("0x").to_lowercase();
let idx = hex_s
.rfind(DSOLC_MARKER)
.expect("deployed runtime must carry the solc metadata marker (dsolc)");
assert!(idx >= 64, "bzzr1 hash must precede the dsolc marker");
let meta_hex = &hex_s[idx - 64..idx];
let expected: B256 = EMBEDDED_METADATA_HASH
.parse()
.expect("const metadata hash hex");
assert_eq!(
meta_hex,
hex::encode(expected),
"embedded bzzr1 metadata hash must equal the canonical on-chain value 0x361d24ef…"
);
}