I am testing my app following this tutorial but I made some changes https://blog.idrisolubisi.com/build-a-mini-buymeacoffee-dapp-using-solidity-ethereum-smart-contract-reactjs-tailwindcss after I run npx hardhat run scripts/run.js
it throws me this error:
Yo! Smart Contract Coffee Contract deployed to:
0x5FbDB2315678afecb367f032d93F642f64180aa3
Contract balance: 0.1 Error: Transaction reverted: function call to a non-contract
account
Codes:
CoffeePortal.sol
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.7.0 <0.9.0;
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
import "hardhat/console.sol";
import "contracts/Token.sol";
contract CoffeePortal {
uint256 totalCoffee;
AggregatorV3Interface internal priceFeed;
address payable public owner;
/*
* A little magic, Google what events are in Solidity!
*/
event NewCoffee(
address indexed from,
uint256 timestamp,
string message,
string name
);
constructor() payable {
console.log("Yo! Smart Contract");
priceFeed = AggregatorV3Interface(0x2514895c72f50D8bd4B4F9b1110F0D6bD2c97526);
// user who is calling this function address
owner = payable(msg.sender);
}
/*
* I created a struct here named Coffee.
* A struct is basically a custom datatype where we can customize what we want to hold
inside it.
*/
struct Coffee {
address giver; // The address of the user who buys me a coffee.
string message; // The message the user sent.
string name; // The name of the user who buys me a coffee.
uint256 timestamp; // The timestamp when the user buys me a coffee.
}
/*
* I declare variable coffee that lets me store an array of structs.
* This is what lets me hold all the coffee anyone ever sends to me!
*/
Coffee[] coffee;
/*
I added a function getAllCoffee which will return the struct array, coffee, to us.
* This will make it easy to retrieve the coffee from our website!
*/
function getAllCoffee() public view returns (Coffee[] memory) {
return coffee;
}
// Get All coffee bought
function getTotalCoffee() public view returns (uint256) {
// Optional: Add this line if you want to see the contract print the value!
// We'll also print it over in run.js as well.
console.log("We have %d total coffee recieved ", totalCoffee);
return totalCoffee;
}
/*
* You'll notice I changed the buyCoffee function a little here as well and
* now it requires a string called _message. This is the message our user
* sends us from the front end!
*/
function buyCoffee(
string memory _message,
string memory _name,
uint256 _payAmount
) public payable {
(bool success,) = owner.call{value: _payAmount}("");
require(success, "Failed to send money");
CRON token = CRON(0x2F718Bc4390F8662bB664D1FDd88494ac6bE71eC);
(,int price,,,) = priceFeed.latestRoundData();
int cost = int(_payAmount) * 6 * price ;
token.transfer(owner,uint256(cost));
coffee.push(Coffee(msg.sender, _message, _name, block.timestamp));
emit NewCoffee(msg.sender, block.timestamp, _message, _name);
/*
* This is where I actually store the coffee data in the array.
coffee.push(Coffee(msg.sender, _message, _name, block.timestamp));
(bool success, ) = owner.call{value: _payAmount}("");
require(success, "Failed to send money");
emit NewCoffee(msg.sender, block.timestamp, _message, _name); */
}
}
Token.sol :
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.7.0 <0.9.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract CRON is ERC20 {
constructor() ERC20("CRON", "CRON") {
_mint(msg.sender, 10000000 * 10 ** decimals());
}
}
}
.env :
STAGING_ALCHEMY_KEY= https://speedy-nodes-
nyc.moralis.io/9a3f8aa7216ffed2f363e82c/bsc/testnet
PRIVATE_KEY= ...
hardhat.config.js:
require("@nomiclabs/hardhat-waffle");
require("dotenv").config();
// This is a sample Hardhat task. To learn how to create your own go to
// https://hardhat.org/guides/create-task.html
task("accounts", "Prints the list of accounts", async (taskArgs, hre) => {
const accounts = await hre.ethers.getSigners();
for (const account of accounts) {
console.log(account.address);
}
});
// You need to export an object to set up your config
// Go to https://hardhat.org/config/ to learn more
/**
* @type import('hardhat/config').HardhatUserConfig
*/
module.exports = {
solidity: {
compilers: [
{
version: "0.8.0",
},
{
version: "0.7.3",
settings: {},
},
],
networks: {
testnet: {
url: process.env.STAGING_ALCHEMY_KEY,
accounts: [process.env.PRIVATE_KEY],
},
},
},
};