0

I used truffle to create a smartcontract, and then deployed it on Rinkeby (using a local geth node) with this command. It works well. truffle migrate --network rinkeby

The smartcontract is an updated version of this sample: https://medium.com/cryptronics/ethereum-development-guide-part-1-ad0c77c3683f

Now I'd like to deploy the smartcontract on the mainnet:

$ geth --syncmode light --rpc --datadir=$HOME/mainnet --nousb --allow-insecure-unlock
$ geth --nousb account new --datadir="$HOME/mainnet/" --password ./account-password.txt
$ truffle migrate --network mainnet

"Migrations" exceeded the block limit (with a gas value you set).
   * Block limit:  5000 (0x1388) * Gas sent:     4000000 (0x3d0900)  * Try: + Sending less gas.

I changed truffle-config.js to use a bas of 10000. By the way, how much ETH does it costs currently to deploy a simple contract, more or less? what gas should I define?

$ truffle migrate --network mainnet
"Migrations" -- Returned error: getDeleteStateObject (1422d7ccc0d1b857e00bef1c265b8907071434d9) error: no suitable peers available.

It seems that's not trivial to set-up a geth node. So I tried using a free development node from quiknode.io. But they do not support deploying a contract using truffle from the command line (only from its front end client Drizzle).

How can I deploy the smartcontract to mainnet? I prefer command line tools, rather than graphical interfaces. However, both solutions are appreciated.

0

There you go (tested with web3.js v1.2.1):

const fs   = require("fs");
const Web3 = require("web3");

const NODE_ADDRESS  = "https://mainnet.infura.io/v3/YourProjectID";
const PRIVATE_KEY   = "0xYourPrivateKey";
const CONTRACT_NAME = "YourContractName";
const CONTRACT_ARGS = ["arg1", "arg2", "arg3"];
const ARTIFACTS_DIR = "RelativePathToYourAbiAndBinFolder";

async function scan(message) {
    process.stdout.write(message);
    return await new Promise(function(resolve, reject) {
        process.stdin.resume();
        process.stdin.once("data", function(data) {
            process.stdin.pause();
            resolve(data.toString().trim());
        });
    });
}

async function getGasPrice(web3) {
    while (true) {
        const nodeGasPrice = await web3.eth.getGasPrice();
        const userGasPrice = await scan(`Enter gas-price or leave empty to use ${nodeGasPrice}: `);
        if (/^\d+$/.test(userGasPrice))
            return userGasPrice;
        if (userGasPrice == "")
            return nodeGasPrice;
        console.log("Illegal gas-price");
    }
}

async function getTransactionReceipt(web3) {
    while (true) {
        const hash = await scan("Enter transaction-hash or leave empty to retry: ");
        if (/^0x([0-9A-Fa-f]{64})$/.test(hash)) {
            const receipt = await web3.eth.getTransactionReceipt(hash);
            if (receipt)
                return receipt;
            console.log("Invalid transaction-hash");
        }
        else if (hash) {
            console.log("Illegal transaction-hash");
        }
        else {
            return null;
        }
    }
}

async function send(web3, account, transaction) {
    while (true) {
        try {
            const options = {
                data    : transaction.encodeABI(),
                gas     : await transaction.estimateGas({from: account.address}),
                gasPrice: await getGasPrice(web3),
            };
            const signed  = await web3.eth.accounts.signTransaction(options, account.privateKey);
            const receipt = await web3.eth.sendSignedTransaction(signed.rawTransaction);
            return receipt;
        }
        catch (error) {
            console.log(error.message);
            const receipt = await getTransactionReceipt(web3);
            if (receipt)
                return receipt;
        }
    }
}

async function run() {
    const web3        = new Web3(NODE_ADDRESS);
    const account     = web3.eth.accounts.privateKeyToAccount(PRIVATE_KEY);
    const path        = __dirname + "/" + ARTIFACTS_DIR + "/" + CONTRACT_NAME;
    const abi         = fs.readFileSync(path + ".abi", {encoding: "utf8"});
    const bin         = fs.readFileSync(path + ".bin", {encoding: "utf8"});
    const contract    = new web3.eth.Contract(JSON.parse(abi));
    const options     = {data: "0x" + bin, arguments: CONTRACT_ARGS};
    const transaction = contract.deploy(options);
    const receipt     = await send(web3, account, transaction);
    console.log("contract deployed at", receipt.contractAddress);
    if (web3.currentProvider.constructor.name == "WebsocketProvider")
        web3.currentProvider.connection.close();
}

run();
| improve this answer | |
  • thanks. where does this code come from? you wrote it yourself? is it available in github or somewhere else? – David Portabella Jun 22 at 12:25
  • @DavidPortabella: I wrote it. – goodvibration Jun 22 at 12:45
  • i would prefer to use existing tools/scripts ;) – David Portabella Jun 23 at 21:31
  • @DavidPortabella: That IS an existing tool. Tested and used for a very long time! – goodvibration Jun 23 at 23:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.