0

I was trying to deploy my contracts to Ropsten testnet. But in the process, my Dapp suddenly stopped working even on Ganache. When I now try to "truffle migrate --reset," I get the message "Error: Could not find artifacts for Migrations from any sources."

I have searched through answer after answer on stack exchange, and the only advice I've gotten is that the artifacts.require must be the same name as the contract name. But in this case, the artifacts.require is Migrations and in the contract is called Migrations. In addition, the Token and BlackstoneCoinSwap contracts are also the same names.

Does anyone know why it may be producing this error?

Here is the code:

1_initial_migration.js

const Migrations = artifacts.require("Migrations");

module.exports = function(deployer) {
  deployer.deploy(Migrations);
};

2_deploy_contracts.js

const Token = artifacts.require("Token");
const BlackstoneCoinSwap = artifacts.require("BlackstoneCoinSwap");

module.exports = async function(deployer) {
    //Deploy Token
    await deployer.deploy(Token);
    const token = await Token.deployed()

    //Deploy BlackstoneCoinSwap
  await deployer.deploy(BlackstoneCoinSwap, token.address);
  const blackstoneCoinSwap = await BlackstoneCoinSwap.deployed()
  //Transfer all tokens to BlackstoneCoinSwap (1 million)
  await token.transfer(blackstoneCoinSwap.address, '1000000000000000000000000') 
};

BlackstoneCoinSwap.sol

pragma solidity ^0.5.0;

import "./Token.sol";

contract BlackstoneCoinSwap {
    string public name = "BlackstoneCoin Instant Exchange";
    Token public token;
    uint public rate = 100;

    event TokensPurchased(
        address account,
        address token,
        uint amount,
        uint rate
    );

    event TokensSold(
        address account,
        address token,
        uint amount,
        uint rate
    );

    constructor(Token _token) public {
        token = _token;
    }
    
    function buyTokens() public payable {
        // Calculate the number of tokens to buy
        uint tokenAmount = msg.value * rate;

        // Require that BlackstoneCoinSwap has enough tokens
        require(token.balanceOf(address(this)) >= tokenAmount);

        // Transfer tokens to the user
        token.transfer(msg.sender, tokenAmount);

        // Emit TokensPurchased event
        emit TokensPurchased(msg.sender, address(token), tokenAmount, rate);
    }
    function sellTokens(uint _amount) public {
        // User can't sell more tokens than they have
        require(token.balanceOf(msg.sender) >= _amount);
        
        // Calculate amount of Ether to redeem
        uint etherAmount = _amount / rate;

        // Require that BlackstoneCoinSwap has enough Ether to pay out
        require(address(this).balance >= etherAmount);

        // Perform sale
        token.transferFrom(msg.sender, address(this), _amount);
        msg.sender.transfer(etherAmount);

        // Emit TokensSold event
        emit TokensSold(msg.sender, address(token), _amount, rate);
    }
    
    
    }

Migrations.sol

pragma solidity >=0.4.21 <0.6.0;

contract Migrations {
  address public owner;
  uint public last_completed_migration;

  constructor() public {
    owner = msg.sender;
  }

  modifier restricted() {
    if (msg.sender == owner) _;
  }

  function setCompleted(uint completed) public restricted {
    last_completed_migration = completed;
  }

  function upgrade(address new_address) public restricted {
    Migrations upgraded = Migrations(new_address);
    upgraded.setCompleted(last_completed_migration);
  }
}

Token.sol

pragma solidity ^0.5.0;

contract Token {
    string  public name = "BlackstoneCoin";
    string  public symbol = "BC";
    uint256 public totalSupply = 1000000000000000000000000; // 1 million tokens
    uint8   public decimals = 18;

    event Transfer(
        address indexed _from,
        address indexed _to,
        uint256 _value
    );

    event Approval(
        address indexed _owner,
        address indexed _spender,
        uint256 _value
    );

    mapping(address => uint256) public balanceOf;
    mapping(address => mapping(address => uint256)) public allowance;

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

    function transfer(address _to, uint256 _value) public returns (bool success) {
        require(balanceOf[msg.sender] >= _value);
        balanceOf[msg.sender] -= _value;
        balanceOf[_to] += _value;
        emit Transfer(msg.sender, _to, _value);
        return true;
    }

    function approve(address _spender, uint256 _value) public returns (bool success) {
        allowance[msg.sender][_spender] = _value;
        emit Approval(msg.sender, _spender, _value);
        return true;
    }

    function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
        require(_value <= balanceOf[_from]);
        require(_value <= allowance[_from][msg.sender]);
        balanceOf[_from] -= _value;
        balanceOf[_to] += _value;
        allowance[_from][msg.sender] -= _value;
        emit Transfer(_from, _to, _value);
        return true;
    }
}

When the app broke, I was not messing with these files at all. Instead, I was editing truffle-config.js

Here is the code for it:

require('babel-register');
require('babel-polyfill');
const Web3 = require("web3");
const { projectUrl, mnemonicPhrase } = require('./secrets.json');
const HDWalletProvider = require("@truffle/hdwallet-provider");
 
module.exports = {
  networks: {
    development: {
      host: "127.0.0.1",
      port: 7545,
      network_id: "*" // Match any network id
    },
    ropsten: {
      provider: () =>
      new HDWalletProvider({
        mnemonic: {
          phrase: mnemonicPhrase
        }, 
        providerOrUrl: projectUrl
      }),
    network_id: '3'
  },
  contracts_directory: './src/contracts/',
  contracts_build_directory: './src/abis/',
 
  compilers: {
    solc: {
      optimizer: {
        enabled: true,
        runs: 200
      },
      evmVersion: "petersburg"
    }
  }
}}
2
  • Is your truffle-config.js correct? It appears to have some problems with sections, "compilers", "contracts_build_directory" and "contracts_directory" are inside "networks" and they should be at the same level.
    – Ismael
    Jan 5, 2021 at 20:01
  • 1
    Thanks @Ismael. It turned out that you were right. I had to pay close attention to the vertical lines in Visual Studio Code that link brackets { } to each other. This allowed me to get each line in the correct section. Before doing this, it alternated between giving me syntax errors and "Could not find artifacts for Migrations" errors. Once I had everything in the correct place, it migrated with no problem. Jan 7, 2021 at 15:02

1 Answer 1

2

As @Ismael pointed out, some of my lines of code in the config file were in the wrong sections. Once I edited it to get the sections right, it worked fine.

Here is the corrected code:

require('babel-register');
require('babel-polyfill');
const Web3 = require("web3");
const { projectUrl, mnemonicPhrase } = require('./secrets.json');
const HDWalletProvider = require("@truffle/hdwallet-provider");
 
module.exports = {
  networks: {
    development: {
      host: "127.0.0.1",
      port: 7545,
      network_id: "*" // Match any network id
    },
    ropsten: {
      provider: () => new HDWalletProvider({
        mnemonic: {
          phrase: mnemonicPhrase
        }, 
        providerOrUrl: projectUrl
      }),
      network_id: '3'
    }
  },
  contracts_directory: './src/contracts/',
  contracts_build_directory: './src/abis/',

  compilers: {
    solc: {
      optimizer: {
        enabled: true,
        runs: 200
      },
      evmVersion: "petersburg"
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.