Developers Forum for XinFin XDC Network

Fuzz
Fuzz

Posted on

Debugging & Solving Hardhat Flattening Issues

Hello!

I've discovered a few tricks to validate contracts, built and flattened by Hardhat, on the XDC blockchain explorer.

TL;DR

  1. Don't turn on the XDC Explorer's optimization flag.
  2. Remove the config: {...} preamble in the flattened file.
  3. In the flattened file, replace all SPDX-License-Identifiers with SPDX-License-Identifier-FLATTEN-SUPPRESS-WARNING

How it started ...

This is a brief description of a problem I ran into this week while using Hardhat version 2.12.4 with solc 0.8.17 and the current version of the XDC Explorer (for both Apothem and Mainnet).

I've been developing an XRC20 compliant Token using Open Zeppelin's contracts with several of the supporting interfaces such as ERC20Burnable and Pausable. The contract was compiled with the optimizer enabled and set to 200 runs.

Deploying the contract to Apothem went smoothly. I then flattened the contract using a technique that's been documented in the XDC docs:

hardhat flatten ./contracts/FuzzTest.sol > ./contracts-flattened/FuzzTest.sol
Enter fullscreen mode Exit fullscreen mode

... and then attempted to verify the contract on the Apothem Explorer. Every approach I tried failed either with an immediate non-descriptive error or a timeout (HTTP 524)!

The Quest ...

After a very useful conversation on the XDC Discord (thanks @menezesphill !) I went back to basics and setup the contract in Remix using this guide.

I diff'd the output of the Flattener Remix plugin and through a process of experimentation I discovered three key steps for solving this issue.

The Answer

1. Hardhat's config preamble needs to be removed

Hardhat emits this in the flattened file and it's not recognised by the XDC Explorer - it needs to be deleted before being uploaded:

config:
{"solidity":{"version":"0.8.17","settings":{"optimizer":{"enabled":true,"runs":200}}},"networks":{"xinfin":{"url":"https://erpc.apothem.network","accounts":["xxx"]}}}
Enter fullscreen mode Exit fullscreen mode

2. SPDX-License-Identifiers have to be replaced

Replace SPDX-License-Identifiers with SPDX-License-Identifier-FLATTEN-SUPPRESS-WARNING

3. XDC Explorer's Optimizer Flag

When uploading your flattened file, do not enable the Optimization flag.

That's it! Hope you find this post useful. Happy holidays and merry coding!

Discussion (3)

Collapse
menezesphill profile image
Phill Menezes

Amazing @dazraf. Thanks for your contribution. It will help other developers in the future facing the same issue.

Collapse
jg profile image
John Grimsey

I was just verifying a contract and the server response complained about multiple licenses - removing all license lines but leaving the very first one fixed the issue and the contract verified.

Collapse
ruslan_wing profile image
ruslan wing

Thank you soo much for the contribution !!!