Hi, I'm using the ERC20 and ERC721 contracts to create a game, and I'm needing to do something that my coin is burned when I'm going to create an ERC721 token.
I won't put the ERC20 code because I technically used the contract wizard to generate it. Below I'm putting my erc721 code.
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Burnable.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
contract HEROES is Ownable, ERC721URIStorage {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
//event para novos hero invocados
event CreatedNewHero(uint heroId, string uri);
//TEST COIN
address coin;
constructor() ERC721("HEROES", "SAKH") {}
function setCoin(address _coin) public{
coin = _coin;
}
function invokeRandomHero(address _player, string memory _tokenURI, address _recepient) public returns (uint){
//CHECK IF EXISTS COINS
IERC20 tokenA = IERC20(coin);
require(tokenA.balanceOf(_player) > 300, "No funds");
// tokenA.transferFrom(_player, _recepient, 300);
_tokenIds.increment();
uint256 newHeroId = _tokenIds.current();
_mint(_player, newHeroId);
_setTokenURI(newHeroId, _tokenURI);
emit CreatedNewHero(newHeroId, _tokenURI);
return newHeroId;
}
function totalHeroes() public view returns(uint){
return _tokenIds.current();
}
}
If you notice within the "invokeRandomHero" method I'm trying to use the IERC20 with my contract address to get access (I don't know if it's the best way), however I'm having the following error and I can't know where the failure.
Error: Returned error: VM Exception while processing transaction: revert
My file Test.JS
contract("HEROES", (accounts) => {
let [alice, bob] = accounts;
let contractInstance;
beforeEach(async () => {
coin = await MyCoin.new();
console.log("ADDRESS "+coin.address);
let transfer = await coin.transfer(alice, 500);
let totalAlice = await coin.balanceOf(alice);
let totalBob = await coin.balanceOf(bob);
//output: 100000
console.log("TOTAL ALICE "+web3.utils.fromWei(totalAlice.toString()));
/// output: 0
console.log("TOTAL BOB "+web3.utils.fromWei(totalBob.toString()));
contractInstance = await HEROES.new();
contractInstance.setCoin(coin.address);
const hero = await contractInstance.invokeRandomHero(alice,
'https://link.test.com/heroes/8356281049284737', bob, {from: alice, gas:3000000});
});