GardenBlockchain

Deploying a contract with Truffle

A historical walkthrough of Ganache, environment-based wallet configuration, and Truffle migrations.

A Truffle deployment connects three things: compiled contracts, a network provider, and the account that signs the deployment. This older setup used Ganache locally and an HD wallet provider for remote networks.

Historical workflow. The source uses the old truffle-hdwallet-provider package and names Ropsten, Kovan, and Rinkeby. These are records of that setup, not a currently supported deployment configuration. No contracts were deployed for this revision.

Configure the network

For the local example, Ganache listened on port 7545 and loaded the project’s truffle-config.js. The remote configuration read provider URLs from the environment:

ROPSTEN_URL=https://ropsten.infura.io/v3/<your-api-key>
KOVAN_URL=https://kovan.infura.io/v3/<your-api-key>
RINKEBY_URL=https://rinkeby.infura.io/v3/<your-api-key>
MAINNET_URL=https://mainnet.infura.io/v3/<your-api-key>

Keep wallet configuration outside the source

The original installation and mnemonic-generation commands were:

npm install truffle-hdwallet-provider --save
npm install bip39 dotenv --save

# where
* bip39 – used to generate wallet mnemonic
* dotenv – simple way to read environment variable files
node -e "console.log(require('bip39').generateMnemonic())"

The example .env used symbolic values:

MNEMONIC=wallet mnemonic 12 words
ROPSTEN_URL=https://ropsten.infura.io/v3/<your-api-key>
KOVAN_URL=https://kovan.infura.io/v3/<your-api-key>
RINKEBY_URL=https://rinkeby.infura.io/v3/<your-api-key>
MAINNET_URL=https://mainnet.infura.io/v3/<your-api-key>

A mnemonic controls the signing account. Keep real values out of source files, logs, and shared examples. Generating one for this historical example is not part of building this website.

Wire up Truffle

The configuration below keeps the local and remote network definitions together. The frontend ABI directory is specific to the original application.

require('dotenv').config()
const HDWalletProvider = require('truffle-hdwallet-provider')
const MNEMONIC = process.env.MNEMONIC
const ROPSTEN_URL = process.env.ROPSTEN_URL
const KOVAN_URL = process.env.KOVAN_URL
const RINKEBY_URL = process.env.RINKEBY_URL
const MAINNET_URL = process.env.MAINNET_URL

module.exports = {
    // Uncommenting the defaults below
    // provides for an easier quick-start with Ganache.
    // You can also follow this format for other networks;
    // see <http://truffleframework.com/docs/advanced/configuration>
    // for more details on how to specify configuration options!
    contracts_build_directory: "../frontend/src/Contract/abi",
    networks: {
        development: {
            host: "127.0.0.1",
            port: 7545,
            network_id: "*"
        },
        test: {
            host: "127.0.0.1",
            port: 7545,
            network_id: "*"
        },
        ropsten: {
            provider: () => new HDWalletProvider(MNEMONIC, ROPSTEN_URL),
            network_id: 3
        },
        kovan: {
            provider: () => new HDWalletProvider(MNEMONIC, KOVAN_URL),
            network_id: 42
        },
        rinkeby: {
            provider: () => new HDWalletProvider(MNEMONIC, RINKEBY_URL),
            network_id: 4
        },
        // main ethereum network(mainnet)
        mainnet: {
            provider: () => new HDWalletProvider(MNEMONIC, MAINNET_URL),
            network_id: 1
        }
    }
};

The original console check inspected the provider’s public account list:

truffle console --network <network>
truffle(ropsten)> web3.eth.getAccounts((err, accounts) => console.log(accounts))

eg:
truffle console --network ropsten
truffle(ropsten)> web3.eth.getAccounts((err, accounts) => console.log(accounts))
[ '0x627306090abab3a6e1400e9345bc60c78a8bef57' ]

Run the recorded migration workflow

The source removed stale generated ABI files before migrating. These were its migration commands, including network-specific examples:

# For local network
truffle migrate

# Fow specific network
truffle migrate --network <network>

eg:
truffle migrate --network ropsten
truffle migrate --network mainnet

# Migrate contract only
truffle migrate -f 2 --network ropsten

Selecting a different network changes where transactions are sent. The old faucet list is omitted; reproducing this workflow requires a supported toolchain, an intentional target network, and a reviewed signing configuration.

← Back to the garden