0

I am a beginner. I can deploy to a private network but I can not deploy to a public network. I need a Deploy to the kovan test network. please help me.

>truffle init

>npm install truffle-hdwallet-provider

***** Token.sol *****

pragma solidity >=0.4.25 <0.6.0;

contract Token {

    mapping (address => uint256) public balanceOf;

    constructor(uint256 initialSupply) public {
        balanceOf[msg.sender] = initialSupply;
    }

    function transfer(address _from, address _to, uint256 _value) public returns (bool success) {
        require(balanceOf[_from] >= _value);                // Check if the sender has enough
        require(balanceOf[_to] + _value >= balanceOf[_to]); // Check for overflows
        balanceOf[_from] -= _value;
        balanceOf[_to] += _value;
        return true;
    }
}

***** Shop.sol *****

pragma solidity >=0.4.25 <0.6.0;

import "./Token.sol";

contract Shop {

    struct Product {

        string name;

        string imgPath;

        uint256 price;

        uint256 quantity;

        address seller;
    }
    event AddedProduct(uint256 pid, address seller, uint256 timestamp);
    event BuyProduct(uint256 pid, address buyer, uint256 timestamp);
    mapping (uint256 => Product) products;
    mapping (uint256 => address[]) buying;
    Token token;

    constructor (address _tokenAddress) public {
        token = Token(_tokenAddress);
    }

    function addProduct(
        uint256 _pid,
        string memory _name,
        uint256 _price,
        uint256 _quantity,
        string memory _imgPath,
        uint256 timestamp
    ) public {
        products[_pid] = Product({
            name: _name,
            imgPath: _imgPath,
            price: _price,
            quantity: _quantity,
            seller: msg.sender
        });
        emit AddedProduct(_pid, msg.sender, timestamp);
    }

    function getProduct(uint256 _pid) public view returns (string memory, uint256, uint256, string memory, address) {
        Product memory product = products[_pid];
        return (product.name, product.price, product.quantity, product.imgPath, product.seller);
    }

    function buyProduct(uint256 _pid, uint256 _timestamp) public {
        require(products[_pid].quantity > 0, "Product is sold out");

        Product storage product = products[_pid];
        address _buyer = msg.sender;
        token.transfer(_buyer, product.seller, product.price);

        product.quantity -= 1;

        buying[_pid].push(_buyer);
        emit BuyProduct(_pid, _buyer, _timestamp);
    }
}

***** 2_deploy_contracts.js *****

const Token = artifacts.require('./Token.sol');
const Shop = artifacts.require('./Shop.sol');

module.exports = function(deployer, network , accounts) {

  deployer

    .deploy(Token, 1000000, {from: accounts[0]})

    .then(async () => {

      const tokenContract = await Token.deployed();
      return deployer.deploy(Shop, tokenContract.address);
    })
    .then(async () => {
      const token = await Token.deployed();
      const coinbase = accounts[0];
      const value = 50000;
      await token.transfer(coinbase, accounts[1], value);
    });
};

***** truffle-config.js *****

const HDWalletProvider = require('truffle-hdwallet-provider');

const mnemonic = ".....";

module.exports = {

  networks: {

    development: {
      host: "127.0.0.1",
      port: 7545,
      network_id: "*"
    },

    kovan: {
      provider: function() {
        return new HDWalletProvider(mnemonic, "https://kovan.infura.io/v3/.....")
      },
      network_id: 42,
      skipDryRun: true
    },
},

}
>truffle migrate --network kovan

Error: Error: ProviderError: VM Exception while processing transaction: revert 

at Object.run (C:\Users\WINDOWS\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\truffle-migrate\index.js:92:1) 

at processTicksAndRejections (internal/process/next_tick.js:81:5) 

Truffle v5.0.33 (core: 5.0.33) Node v11.12.0 

PS D:\Blockchain Dev\t2>
0

You want to send tokens to accounts[1] address, however, HDWalletProvider by default unlocks only one address. You should add additional arguments to HDWalletProvider call. HDWalletProvider(mnemonic, "your_infura_link", 0, 2). The third argument is the default address to send transactions from (starting from zero). The 4th argument is the total amount of unlocked addresses. More info: here.

  • I tried as you said but still got the same error. – YU Kunakorn Sep 10 at 11:59
  • You should add {from: accounts[0]} to deployer.deploy() function call (as third argument) to explicitly use account[0] to deploy the contract from. (Don't understand actually why truffle uses another address). – Igor Sobolev Sep 10 at 12:55
  • return deployer.deploy(Shop, tokenContract.address, accounts[0]); – YU Kunakorn Sep 11 at 13:38
  • Error: Error: Error: *** Deployment Failed *** "Shop" -- Invalid number of parameters for "undefined". Got 2 expected 1! – YU Kunakorn Sep 11 at 13:39
  • Try adding skipDryRun: true, to your network config in truffle-config.js. The problem appears when Truffle simulating deployment - accounts[0] is not the account that unlocked in the HDWalletProvider, but a simulated one. And in script you try to send tokens from accounts[0], but it does not have any. In real network everything should be ok. And if you do not want to skip simulation, try deployer.deploy(Token, 1000000, { from: accounts[0] }) – Igor Sobolev Sep 11 at 16:09

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.