Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimism and base networks verification #996

Merged
merged 3 commits into from
Dec 2, 2024

Conversation

rin-st
Copy link
Member

@rin-st rin-st commented Nov 25, 2024

Description

Adds possibility to use yarn verify with contracts on optimism, optimismSepolia, base and baseSepolia networks

Tested all four, verification works great for me

part of #965

These networks are chosen because they are most popular ones, and also we need optimism for SRE extensions. Please lmk if I need to add/change something

Additional Information

Copy link
Collaborator

@technophile-04 technophile-04 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be just a me problem or maybe macos problem? not sure what I am doing wrong tested it on my new mac too and get same thing :( :

Screenshot 2024-11-27 at 4 20 36 PM

Would love if others could tes itt.

Here the contract I tried deploying :

Example contract
//SPDX-License-Identifier: MIT
pragma solidity >=0.8.0 <0.9.0;

// Useful for debugging. Remove when deploying to a live network.
import "hardhat/console.sol";

/**
 * A smart contract that allows changing a state variable of the contract and tracking the changes
 * It also allows the owner to withdraw the Ether in the contract
 * @author BuidlGuidl
 */
contract YourContract {
    // State Variables
    address public immutable owner;
    string public greeting = "Building Unstoppable Apps!!!";
    uint256 public constant PREMIUM_PRICE = 0.01 ether;
    uint256 public constant MAX_GREETING_LENGTH = 140;
    
    uint256 public totalCounter = 0;
    mapping(address => bool) public isPremium;
    mapping(address => uint256) public userGreetingCounter;
    mapping(address => uint256) public lastGreetingTime;

    // Events
    event GreetingChange(
        address indexed greetingSetter, 
        string newGreeting, 
        bool isPremium, 
        uint256 timestamp
    );
    event PremiumPurchased(address indexed user, uint256 amount);

    // Constructor: Called once on contract deployment
    constructor(address _owner) {
        owner = _owner;
    }

    // Modifiers
    modifier isOwner() {
        require(msg.sender == owner, "Not the Owner");
        _;
    }

    modifier validGreeting(string memory _greeting) {
        require(bytes(_greeting).length > 0, "Greeting cannot be empty");
        require(bytes(_greeting).length <= MAX_GREETING_LENGTH, "Greeting too long");
        _;
    }

    /**
     * Function to become a premium user
     */
    function purchasePremium() public payable {
        require(!isPremium[msg.sender], "Already premium");
        require(msg.value == PREMIUM_PRICE, "Incorrect payment amount");
        
        isPremium[msg.sender] = true;
        emit PremiumPurchased(msg.sender, msg.value);
    }

    /**
     * Function that allows anyone to change the greeting
     * Premium users can set greetings more frequently
     */
    function setGreeting(string memory _newGreeting) public validGreeting(_newGreeting) {
        // Check time constraints for non-premium users
        if (!isPremium[msg.sender]) {
            require(
                block.timestamp >= lastGreetingTime[msg.sender] + 1 days,
                "Non-premium users can only set greeting once per day"
            );
        }

        console.log("Setting new greeting '%s' from %s", _newGreeting, msg.sender);

        greeting = _newGreeting;
        totalCounter += 1;
        userGreetingCounter[msg.sender] += 1;
        lastGreetingTime[msg.sender] = block.timestamp;

        emit GreetingChange(
            msg.sender,
            _newGreeting,
            isPremium[msg.sender],
            block.timestamp
        );
    }

    /**
     * View function to get user stats
     */
    function getUserStats(address _user) public view returns (
        uint256 greetingCount,
        bool userIsPremium,
        uint256 timeUntilNextGreeting
    ) {
        greetingCount = userGreetingCounter[_user];
        userIsPremium = isPremium[_user];
        
        if (!userIsPremium && lastGreetingTime[_user] > 0) {
            uint256 nextAvailableTime = lastGreetingTime[_user] + 1 days;
            timeUntilNextGreeting = block.timestamp >= nextAvailableTime ? 
                0 : nextAvailableTime - block.timestamp;
        }
    }

    /**
     * Function that allows the owner to withdraw all the Ether in the contract
     */
    function withdraw() public isOwner {
        uint256 balance = address(this).balance;
        require(balance > 0, "No funds to withdraw");
        
        (bool success, ) = owner.call{value: balance}("");
        require(success, "Failed to send Ether");
    }

    /**
     * Function that allows the contract to receive ETH
     */
    receive() external payable {}
}

@carletex
Copy link
Member

It works for me (deploy & verify):

image

This might be just a me problem or maybe macos problem? not sure what I am doing wrong tested it on my new mac too and get same thing :( :

It was just a conspiration for make the verify fail before the CTF :P

Maybe could you temporary tweak the hardhat-deploy lib to see if you can get more info. e.g:

  • see what that [object object] is
  • print the API key there and see if it's picking it.
packages/hardhat/node_modules/hardhat-deploy/src/etherscan.ts:,
packages/hardhat/node_modules/hardhat-deploy/dist/src/etherscan.js:

@rin-st
Copy link
Member Author

rin-st commented Nov 27, 2024

This might be just a me problem or maybe macos problem?

Works fine on my macos 🤷‍♂️ , tried with your contract too

@rin-st
Copy link
Member Author

rin-st commented Nov 27, 2024

maybe you switched from a branch with different deps versions and forgot to update?

@rin-st
Copy link
Member Author

rin-st commented Nov 27, 2024

Tried to reproduce errors. I'm getting first error when API key is wrong (removed one character from current). And getting second error, same as yours, when there's no API key const etherscanOptimisticApiKey = process.env.ETHERSCAN_OPTIMISTIC_API_KEY || "";

image

Sorry for spamming messages

@rin-st
Copy link
Member Author

rin-st commented Nov 28, 2024

@technophile-04 have you tried base/baseSepolia? Also, could you try to use VPN with different countries?

@technophile-04
Copy link
Collaborator

Ohh I think found the bug.

the problem is that hardhat-deploy has reserved the name ETHERSCAN_API_KEY . If it's present in .env it uses only that key directly for all the networks irrespective of how you have configured hardhat.config.ts. It will only use ETHERSCAN_API_KEY for all the network

The above problem was occurring to me because I had an .env file which had ETHERSCAN_API_KEY=DNXJA8RX2Q3VZ4URQIWP7Z68CJXQZSC6AW (mainnet API key) and even while doing

yarn verify --network baseSepolia since I had ETHERSCAN_API_KEY set in .env file it as using that instead of how we have configured in hardhat.config.ts.

A solution might be that we change this to ETHERSCAN_MAINNET_API_KEY :

const etherscanApiKey = process.env.ETHERSCAN_API_KEY || "DNXJA8RX2Q3VZ4URQIWP7Z68CJXQZSC6AW";

So while setting up .env file people don't mess up other networks.

@rin-st
Copy link
Member Author

rin-st commented Dec 2, 2024

Great find!

the problem is that hardhat-deploy has reserved the name ETHERSCAN_API_KEY . If it's present in .env it uses only that key directly for all the networks irrespective of how you have configured hardhat.config.ts. It will only use ETHERSCAN_API_KEY for all the network

Looks like a bug 🤨

A solution might be that we change this to ETHERSCAN_MAINNET_API_KEY

Changed it ETHERSCAN_MAINNET_API_KEY

Copy link
Collaborator

@technophile-04 technophile-04 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @rin-st!

@technophile-04 technophile-04 merged commit 95f05f9 into main Dec 2, 2024
1 check passed
@technophile-04 technophile-04 deleted the add-optimism-and-base-verification branch December 2, 2024 09:42
@carletex
Copy link
Member

carletex commented Dec 2, 2024

the problem is that hardhat-deploy has reserved the name ETHERSCAN_API_KEY . If it's present in .env it uses only that key directly for all the networks irrespective of how you have configured hardhat.config.ts. It will only use ETHERSCAN_API_KEY for all the network

Damn, good catch. It tried to kill our CTF haha

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants