Venture-Miner / Linea-Dev-Cook-Off-Mini-Bootcamp

Material for the Linea Dev Cook-Off Mini-Bootcamp
GNU General Public License v3.0
9 stars 2 forks source link

Getting 'Failed to decode private key' while deploying smart contract via Infura #8

Closed scripnichenko closed 3 months ago

scripnichenko commented 3 months ago

Scenario

GIVEN DutchAuction.sol smart contract AND Infura API Key (copied from created API Key , example 61ce278a3d124rtfa6afbbe0946f4a78) AND Infura Private Key (copied from API Key settings, example hJ13h6CJXRi/5TFOrFEq42iYyxGcz09klmdOkDWZ6RKteGM3gdb6oQ) WHEN deploying a contract with command forge create --rpc-url linea-testnet src/DutchAuction.sol:DutchAuction --private-key $PRIVATE_KEY THEN getting Error: Failed to decode private key

Set-up

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

interface IERC721 {
    function transferFrom(address _from, address _to, uint256 _nftId)
        external;
}

contract DutchAuction {
    uint256 private constant DURATION = 7 days;

    IERC721 public immutable nft;
    uint256 public immutable nftId;

    address payable public immutable seller;
    uint256 public immutable startingPrice;
    uint256 public immutable startAt;
    uint256 public immutable expiresAt;
    uint256 public immutable discountRate;

    constructor(
        uint256 _startingPrice,
        uint256 _discountRate,
        address _nft,
        uint256 _nftId
    ) {
        seller = payable(msg.sender);
        startingPrice = _startingPrice;
        startAt = block.timestamp;
        expiresAt = block.timestamp + DURATION;
        discountRate = _discountRate;

        require(
            _startingPrice >= _discountRate * DURATION, "starting price < min"
        );

        nft = IERC721(_nft);
        nftId = _nftId;
    }

    function getPrice() public view returns (uint256) {
        uint256 timeElapsed = block.timestamp - startAt;
        uint256 discount = discountRate * timeElapsed;
        return startingPrice - discount;
    }

    function buy() external payable {
        require(block.timestamp < expiresAt, "auction expired");

        uint256 price = getPrice();
        require(msg.value >= price, "ETH < price");

        nft.transferFrom(seller, msg.sender, nftId);
        uint256 refund = msg.value - price;
        if (refund > 0) {
            payable(msg.sender).transfer(refund);
        }
        selfdestruct(seller);
    }
}

foundry.toml

[rpc_endpoints]
linea-testnet = "https://linea-sepolia.infura.io/v3/${INFURA_API_KEY}"
linea-mainnet = "https://linea-mainnet.infura.io/v3/${INFURA_API_KEY}"

ENV

PRIVATE_KEY=xxxx
INFURA_API_KEY=xxxx
MatheusDaros commented 3 months ago

Hello @scripnichenko This problem seems to be related to a wrong private key being passed to the forge create command.

Could you try printing $PRIVATE_KEY to the console to see if the variable is set up correctly? If not, you could repeat the command by manually replacing the $PRIVATE_KEY with your account's private key, for example: forge create --rpc-url linea-testnet src/DutchAuction.sol:DutchAuction --private-key afdfd9c3d2095ef696594f6cedcae59e72dcd697e2a7521b1578140

scripnichenko commented 3 months ago

@MatheusDaros getting the same error when keeping the secret key inline. Sharing the entire command with valid creds (will remove the API key in 3 days):

forge create --rpc-url https://linea-sepolia.infura.io/v3/61ce278a3d124bafa6afbbe0946f4a78 \                                
    --private-key hJ13h6CJXRi/5TFOrFEq42iYyxGcz8hskmdOkDWZ6RKteGM3gdb6oQ \
    src/DutchAuction.sol:DutchAuction
image image
MatheusDaros commented 3 months ago

I see the problem. The parameter should be a valid private key and not the API Key/Secret. You can export the private key from your wallet (for example, Metamask) to use there. Beware that if you share this key, anyone would be able to transact your assets, so make sure to keep it safe. To be extra careful, I'd advise creating a new wallet specifically for development, so you don't risk to lose assets.

scripnichenko commented 3 months ago

Thanks, @MatheusDaros, for your valuable suggestions; it was not quite clear from the docs! So, I was able to deploy a contract when I updated the private key based on my MetaMask wallet doc.

Command šŸ› šŸ› šŸ› 

forge create --rpc-url linea-testnet \
    --private-key ${PRIVATE_KEY} \
    src/DutchAuction.sol:DutchAuction \
    --constructor-args 10000000 8  0xED0Ff7E8B655dFFfCA471ea3B6B649ce7C5C3b83  33434

Result šŸŒŸšŸŒŸšŸŒŸ

[ā Š] Compiling...
No files changed, compilation skipped
Deployer: 0xD9E5053ae9de9ca409Ef1bA2241A92b56Ca9F1f3
Deployed to: 0x7A8f628F63461972602E020034E73Ca982E1d9e0
Transaction hash: 0x6836c751be665ecc49b1f81130e998fd362bda0b183b3e558cb2e13a67dd6eda

šŸ˜ŽšŸ˜ŽšŸ˜Ž The contract is available in sepolia.lineascan.build : https://sepolia.lineascan.build/address/0x7A8f628F63461972602E020034E73Ca982E1d9e0