Truffle

truffle migrate –network kovan >> ProviderError: VM Exception while processing transaction: revert

  • September 15, 2019

我是初學者。我可以部署到專用網路,但不能部署到公共網路。我需要部署到 kovan 測試網路。請幫我。

>truffle init

>npm install truffle-hdwallet-provider

***** 令牌.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;
   }
}

***** 商店.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);
   });
};

***** 松露-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>

但是,您希望將令牌發送到accounts[1]地址,但HDWalletProvider預設情況下僅解鎖一個地址。您應該添加額外的參數來HDWalletProvider呼叫。HDWalletProvider(mnemonic, "your_infura_link", 0, 2). 第三個參數是發送交易的預設地址(從零開始)。第 4 個參數是解鎖地址的總數。更多資訊:這裡

引用自:https://ethereum.stackexchange.com/questions/74827