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"
}
}
}}