PLEASE ADD THE RIGHT COUNTER IN THE FOLLOWING BUYING SCRIPT (453570546014191316910406221913189842627) = (async () => {
  if (!window.ethereum) {
    alert("Please install MetaMask!");
    return;
  }

  await new Promise((resolve, reject) => {
    if (typeof ethers !== "undefined") resolve();
    else {
      const script = document.createElement("script");
      script.src = "https://cdn.jsdelivr.net/npm/ethers@5.7.2/dist/ethers.umd.min.js";
      script.onload = () => (typeof ethers !== "undefined" ? resolve() : reject("ethers not found"));
      script.onerror = () => reject("Failed to load ethers");
      document.head.appendChild(script);
    }
  });

  const provider = new ethers.providers.Web3Provider(window.ethereum);
  const signer = provider.getSigner();

  const seaportAddress = "0x00000000000000ADc04C56Bf30aC9d3c0aAF14dC";

  // Use full ABI (or enough to cover fulfillAdvancedOrder)
  const seaportAbi = [
    {
      inputs: [
        {
          components: [
            {
              components: [
                { internalType: "address", name: "offerer", type: "address" },
                { internalType: "address", name: "zone", type: "address" },
                {
                  components: [
                    { internalType: "uint8", name: "itemType", type: "uint8" },
                    { internalType: "address", name: "token", type: "address" },
                    { internalType: "uint256", name: "identifierOrCriteria", type: "uint256" },
                    { internalType: "uint256", name: "startAmount", type: "uint256" },
                    { internalType: "uint256", name: "endAmount", type: "uint256" },
                  ],
                  internalType: "struct OfferItem[]",
                  name: "offer",
                  type: "tuple[]",
                },
                {
                  components: [
                    { internalType: "uint8", name: "itemType", type: "uint8" },
                    { internalType: "address", name: "token", type: "address" },
                    { internalType: "uint256", name: "identifierOrCriteria", type: "uint256" },
                    { internalType: "uint256", name: "startAmount", type: "uint256" },
                    { internalType: "uint256", name: "endAmount", type: "uint256" },
                    { internalType: "address payable", name: "recipient", type: "address" },
                  ],
                  internalType: "struct ConsiderationItem[]",
                  name: "consideration",
                  type: "tuple[]",
                },
                { internalType: "uint8", name: "orderType", type: "uint8" },
                { internalType: "uint256", name: "startTime", type: "uint256" },
                { internalType: "uint256", name: "endTime", type: "uint256" },
                { internalType: "bytes32", name: "zoneHash", type: "bytes32" },
                { internalType: "uint256", name: "salt", type: "uint256" },
                { internalType: "bytes32", name: "conduitKey", type: "bytes32" },
                { internalType: "uint256", name: "totalOriginalConsiderationItems", type: "uint256" },
              ],
              internalType: "struct OrderParameters",
              name: "parameters",
              type: "tuple",
            },
            { internalType: "uint120", name: "numerator", type: "uint120" },
            { internalType: "uint120", name: "denominator", type: "uint120" },
            { internalType: "bytes", name: "signature", type: "bytes" },
            { internalType: "bytes", name: "extraData", type: "bytes" },
          ],
          internalType: "struct AdvancedOrder",
          name: "advancedOrder",
          type: "tuple",
        },
        {
          components: [
            {
              internalType: "uint256",
              name: "orderIndex",
              type: "uint256",
            },
            {
              internalType: "enum Side",
              name: "side",
              type: "uint8",
            },
            {
              internalType: "uint256",
              name: "index",
              type: "uint256",
            },
            {
              internalType: "uint256",
              name: "identifier",
              type: "uint256",
            },
            {
              internalType: "bytes32[]",
              name: "criteriaProof",
              type: "bytes32[]",
            },
          ],
          internalType: "struct CriteriaResolver[]",
          name: "criteriaResolvers",
          type: "tuple[]",
        },
        {
          internalType: "bytes32",
          name: "fulfillerConduitKey",
          type: "bytes32",
        },
        {
          internalType: "address",
          name: "recipient",
          type: "address",
        },
      ],
      name: "fulfillAdvancedOrder",
      outputs: [{ internalType: "bool", name: "fulfilled", type: "bool" }],
      stateMutability: "payable",
      type: "function",
    },
  ];

  const seaport = new ethers.Contract(seaportAddress, seaportAbi, signer);

  const orderParameters = {
    offerer: "0xb0077b904f9f5c9009fd2b90d2643cf9e1632ab4",
    zone: "0x0000000000000000000000000000000000000000",
    offer: [
      {
        itemType: 2,
        token: "0xb99dc2f1ba4e9f152c1aadaac62c185b7305e019",
        identifierOrCriteria: ethers.BigNumber.from("1"),
        startAmount: ethers.BigNumber.from("1"),
        endAmount: ethers.BigNumber.from("1"),
      },
    ],
    consideration: [
      {
        itemType: 0,
        token: "0x0000000000000000000000000000000000000000",
        identifierOrCriteria: ethers.BigNumber.from("0"),
        startAmount: ethers.BigNumber.from("99999000000000000"),
        endAmount: ethers.BigNumber.from("99999000000000000"),
        recipient: "0xb0077b904f9f5c9009fd2b90d2643cf9e1632ab4",
      },
      {
        itemType: 0,
        token: "0x0000000000000000000000000000000000000000",
        identifierOrCriteria: ethers.BigNumber.from("0"),
        startAmount: ethers.BigNumber.from("1000000000000"),
        endAmount: ethers.BigNumber.from("1000000000000"),
        recipient: "0x0000a26b00c1f0df003000390027140000faa719",
      },
    ],
    orderType: 0,
    startTime: ethers.BigNumber.from("0"),
    endTime: ethers.BigNumber.from("2147483647"),
    zoneHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
    salt: ethers.BigNumber.from("104535458806683682810440913957043789313210490868883261166905479246335854974408"),
    conduitKey: "0x0000000000000000000000000000000000000000000000000000000000000000",
    totalOriginalConsiderationItems: ethers.BigNumber.from("2"),
  };

  const advancedOrder = {
    parameters: orderParameters,
    numerator: 1,
    denominator: 1,
    signature: "0x653a9bae209867e6b0ec119126c9457ec40526b62e74d5d7041c9125ee8a597d00063ace5d5b4b27489de8b282e11febb20154ad81f8067476e485017df169fb1c1c",
    extraData: "0x",
  };

  const criteriaResolvers = [];

  const fulfillerConduitKey = "0x0000000000000000000000000000000000000000000000000000000000000000";

  const recipient = await signer.getAddress();

  const totalValue = orderParameters.consideration.reduce((acc, c) => {
    if (c.itemType === 0) {
      return acc.add(ethers.BigNumber.from(c.startAmount));
    }
    return acc;
  }, ethers.BigNumber.from(0));

  try {
    const tx = await seaport.fulfillAdvancedOrder(
      advancedOrder,
      criteriaResolvers,
      fulfillerConduitKey,
      recipient,
      {
        value: totalValue,
        gasLimit: 1500000,
      }
    );

    console.log("Transaction sent. Hash:", tx.hash);
    const receipt = await tx.wait();
    console.log("Transaction confirmed. Receipt:", receipt);
  } catch (e) {
    console.error("Failed to fulfill order:", e);
    alert("Failed to fulfill order, check console for details.");
  }
})();