多次部署同一个合同是一种不好的做法吗?

Is it a bad practice to deploy same contract multiple time?

我开发了这个应用程序,这样我就可以通过单击一个按钮来部署具有相同目的的合约。由于我每次都使用相同的abi和字节码,我想知道这是不是一种不好的做法?

这是我的 dapp。

异步函数 deployContract() {

    const abi =[
      {
        "inputs": [
          {
            "internalType": "string",
            "name": "_name",
            "type": "string"
          },
          {
            "internalType": "string",
            "name": "_symbol",
            "type": "string"
          },
          {
            "internalType": "string",
            "name": "_initBaseURI",
            "type": "string"
          }
        ],
        "stateMutability": "nonpayable",
        "type": "constructor"
      },
      {
        "inputs": [],
        "name": "hasRedPill",
        "outputs": [
          {
            "internalType": "uint256",
            "name": "",
            "type": "uint256"
          }
        ],
        "stateMutability": "view",
        "type": "function"
      },
      {
        "inputs": [],
        "name": "similarity",
        "outputs": [
          {
            "internalType": "string",
            "name": "",
            "type": "string"
          }
        ],
        "stateMutability": "view",
        "type": "function"
      },
      {
        "inputs": [],
        "name": "test",
        "outputs": [
          {
            "internalType": "uint256",
            "name": "",
            "type": "uint256"
          }
        ],
        "stateMutability": "view",
        "type": "function"
      }
    ]

    

    const numberContractByteCode =  "60806040523480156200001157600080fd5b506040516200097138038062000971833981810160405281019062000037919062000187565b82600090805190602001906200004f92919062000059565b50505050620003c4565b8280546200006790620002d5565b90600052602060002090601f0160209004810192826200008b5760008555620000d7565b82601f10620000a657805160ff1916838001178555620000d7565b82800160010185558215620000d7579182015b82811115620000d6578251825591602001919060010190620000b9565b5b509050620000e69190620000ea565b5090565b5b8082111562000105576000816000905550600101620000eb565b5090565b6000620001206200011a8462000269565b62000240565b9050828152602081018484840111156200013f576200013e620003a4565b5b6200014c8482856200029f565b509392505050565b600082601f8301126200016c576200016b6200039f565b5b81516200017e84826020860162000109565b91505092915050565b600080600060608486031215620001a357620001a2620003ae565b5b600084015167ffffffffffffffff811115620001c457620001c3620003a9565b5b620001d28682870162000154565b935050602084015167ffffffffffffffff811115620001f657620001f5620003a9565b5b620002048682870162000154565b925050604084015167ffffffffffffffff811115620002285762000227620003a9565b5b620002368682870162000154565b9150509250925092565b60006200024c6200025f565b90506200025a82826200030b565b919050565b6000604051905090565b600067ffffffffffffffff82111562000287576200028662000370565b5b6200029282620003b3565b9050602081019050919050565b60005b83811015620002bf578082015181840152602081019050620002a2565b83811115620002cf576000848401525b50505050565b60006002820490506001821680620002ee57607f821691505b6020821081141562000305576200030462000341565b5b50919050565b6200031682620003b3565b810181811067ffffffffffffffff8211171562000338576200033762000370565b5b80604052505050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b600080fd5b600080fd5b600080fd5b600080fd5b6000601f19601f8301169050919050565b61059d80620003d46000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c806327bc5baa14610046578063b1c1472e14610064578063f8a8fd6d14610082575b600080fd5b61004e6100a0565b60405161005b9190610351565b60405180910390f35b61006c61012e565b6040516100799190610373565b60405180910390f35b61008a610134565b6040516100979190610373565b60405180910390f35b600080546100ad9061046a565b80601f01602080910402602001604051908101604052809291908181526020018280546100d99061046a565b80156101265780601f106100fb57610100808354040283529160200191610126565b820191906000526020600020905b81548152906001019060200180831161010957829003601f168201915b505050505081565b60015481565b60008073116486fd64ba04f7b789278b239e2e5a1e2f7b3990508073ffffffffffffffffffffffffffffffffffffffff1663438b6300336040518263ffffffff1660e01b81526004016101879190610336565b60006040518083038186803b15801561019f57600080fd5b505afa1580156101b3573d6000803e3d6000fd5b505050506040513d6000823e3d601f19601f820116820180604052508101906101dc9190610296565b5191505090565b60006101f66101f1846103b3565b61038e565b9050808382526020820190508285602086028201111561021957610218610530565b5b60005b85811015610249578161022f8882610281565b84526020840193506020830192505060018101905061021c565b5050509392505050565b600082601f8301126102685761026761052b565b5b81516102788482602086016101e3565b91505092915050565b60008151905061029081610550565b92915050565b6000602082840312156102ac576102ab61053a565b5b600082015167ffffffffffffffff8111156102ca576102c9610535565b5b6102d684828501610253565b91505092915050565b6102e8816103fb565b82525050565b60006102f9826103df565b61030381856103ea565b9350610313818560208601610437565b61031c8161053f565b840191505092915050565b6103308161042d565b82525050565b600060208201905061034b60008301846102df565b92915050565b6000602082019050818103600083015261036b81846102ee565b905092915050565b60006020820190506103886000830184610327565b92915050565b60006103986103a9565b90506103a4828261049c565b919050565b6000604051905090565b600067ffffffffffffffff8211156103ce576103cd6104fc565b5b602082029050602081019050919050565b600081519050919050565b600082825260208201905092915050565b60006104068261040d565b9050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000819050919050565b60005b8381101561045557808201518184015260208101905061043a565b83811115610464576000848401525b50505050565b6000600282049050600182168061048257607f821691505b60208210811415610496576104956104cd565b5b50919050565b6104a58261053f565b810181811067ffffffffffffffff821117156104c4576104c36104fc565b5b80604052505050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b600080fd5b600080fd5b600080fd5b600080fd5b6000601f19601f8301169050919050565b6105598161042d565b811461056457600080fd5b5056fea2646970667358221220d544a43c077d632f326a71cfd99ce5e2932c26f522114532d847bfd00702135164736f6c63430008070033"

    const factory = new ethers.ContractFactory(abi, numberContractByteCode, signer)

    const numberContract = await factory.deploy("1","2","3");

    const transactionReceipt = await numberContract.deployTransaction.wait();

    console.log(transactionReceipt)    
}

还有一个问题 - 我可以使用 ether.js 或任何其他库以编程方式在 etherscan 上验证我的合约吗?

我对区块链还很陌生。请帮忙。

同样的abi表示contract没有变化,所以不需要重新部署。如果你只是想测试一些第三方库的功能,只需专注于构建你的网站 services.Such 因为使用 console.log() of javascript 来输出按钮结果。希望对你有帮助

Since I am using the same abi and byte code every time, I wonder if it is a bad practice?

这取决于您的用例。例如,每个 Uniswap 矿池合约都有相同的字节码和相同的 ABI,但它们是多个合约——每个都有不同的变量值(例如,底层代币的地址)。

can I verify my contract on etherscan programmatically using ether.js or any other library

可以通过 their API and there are wrappers of this API, for example a Truffle plugin or a Hardhat plugin.

进行 Etherscan 验证