How to deploy upgradable contracts on the eth chain by use JavaScript

I can deploy the contract on private chain with the following code. How can I deploy on public chain now。

1 Like

Hi @peacexu :wave: Welcome to the community.

The Using the OpenZeppelin SDK programmatic library guide shows deploying to a local blockchain using a script run with truffle exec

npx truffle exec index.js --network development

I assume this is what you are doing based on your screenshot.

To deploy to a public chain, you can configure an additional network in truffle-config.js and then specify this network when you execute your script.

A good place to start is the guide on how to do this when using OpenZeppelin SDK interactive commands: Deploy your contracts to a public network

I use Infura, truffle-hdwallet-provider and .env to deploy to public networks.

The example deploy script uses two addresses and your script uses three addresses, so we need to expose additional addresses from the same mnemonic by setting num_addresses in our config for truffle-hdwallet-provider. See the truffle-hdwallet-provider repository for details.

truffle-config.js

My sample configuration with development and ropsten networks.
I use Infura Ethereum nodes, and setup the provider using truffle-hdwallet-provider with my mnemonic and Infura Project ID saved in .env.
I expose 3 addresses from the mnemonic when configuring truffle-hdwallet-provider (as your script uses 3 addresses).

require('dotenv').config();

const HDWalletProvider = require('truffle-hdwallet-provider');
const infuraProjectId = process.env.INFURA_PROJECT_ID;

module.exports = {
  networks: {
    development: {
     host: "127.0.0.1",     // Localhost (default: none)
     port: 8545,            // Standard Ethereum port (default: none)
     network_id: "*",       // Any network (default: none)
    },

    // NB: It's important to wrap the provider as a function.
    ropsten: {
      provider: () => new HDWalletProvider(process.env.DEV_MNEMONIC, 'https://ropsten.infura.io/v3/' + infuraProjectId, 0, 3),
      network_id: 3,       // Ropsten's id
      gas: 5500000,        // Ropsten has a lower block limit than mainnet
      confirmations: 2,    // # of confs to wait between deployments. (default: 0)
      timeoutBlocks: 200,  // # of blocks before a deployment times out  (minimum/default: 50)
      skipDryRun: true     // Skip dry run before migrations? (default: false for public nets )
    },
  },
}

The deploy script can then be run specifying the configured network.

npx truffle exec index.js --network ropsten

You can configure different public test networks:

  • Ropsten (Proof of Work, multi-client, ~ 15 second blocktimes);
  • Rinkeby (Proof of Authority, geth client only, 15 second blocktimes);
  • Kovan (Proof of Authority, parity client only, 4 second blocktimes);
  • Goerli (Proof of Authority, multi-client, 15 second blocktimes)

You can even configure to deploy on mainnet, though please ensure that you appropriately protect your mnemonic.

Let me know if you have any questions.


I moved this to the #support:sdk category.

I noticed that the script and the contracts in the Using the OpenZeppelin SDK programmatic library guide have different names, I will get this fixed.

Added a Pull Request: https://github.com/OpenZeppelin/openzeppelin-sdk/pull/1184 to update the documentation.

Hi @peacexu
Were you able to deploy to a public testnet?

If my response solved your question, can you please mark it as the solution, otherwise let me know if you still need assistance.