区块链web3的deploy.js实例程序

发布于:2025-06-18 ⋅ 阅读:(17) ⋅ 点赞:(0)

1.deploy.js

const ethers = require("ethers");

const fs = require("fs-extra");

require("dotenv").config();


 

async function main()

{

    const provider = new ethers.providers.JsonRpcProvider(process.env.RPC_URL);

    const wallet = new ethers.Wallet(process.env.PRIVATE_KEY,provider);

    const abi = fs.readFileSync("./SimpleStorage_sol_SimpleStorage.abi","utf8");

    const binary = fs.readFileSync("./SimpleStorage_sol_SimpleStorage.bin","utf8");

    const contractFactory = new ethers.ContractFactory(abi,binary,wallet);

    console.log("Deploying, please waiting...");

    const contract = await ContractFactory.deploy({gasLimit:100000000000000000});//stop here,wait for contract for deploy!

    console.log(contract);

    const deploymentReceipt = await contract.deployTransaction.wait(1);

    console.log("Here is the deployment transaction (transaction response):");

    console.log(contract.deployTransaction);

    console.log("Here is the transaction receipt:");

    console.log(transactionReceipt);

    //console.log("lets a deploy with only transaction  data!");

    //const nonce = await wallet.getTransactionCount();

    //const tx ={

    //    nonce:  4,

    //    gasPrice: 200000000,

    //    gasLimit: 1000000,

    //    to: null,

    //    value: 0,


 

    //};

    const currentFavoriteNumber = await contract.retrieve();

    console.log(`Current Favorite Number: ${currentFavoriteNumber.toString()}`);

    //const sendTxResponse = await wallet.sendTransaction(tx);

    //console.log(sendTxResponse);

    const transactionReponse = await contract.store("7");

    const transactionReceipt = await transactionReponse.wait(1);

    const updatedFavoriteNumber = await contract.retrieve();

    console.log(`Updated favorite number is: ${updatedFavoriteNumber}`);

}

main()

    .then(()=>process.exit(0))

    .catch((error) =>{

        console.error(error);

        process.exit(1);

    });


网站公告

今日签到

点亮在社区的每一天
去签到