I have a main contract to help me deploy ERC721 && ERC20 token
but it seems to have some problems when i tried deploy both ERC20 and ERC721 in a same factory contract.
i use @openzeppelin/contracts": "^2.5.1"
in package.json
My ERC721 contract
pragma solidity ^0.5.0;
import "@openzeppelin/contracts/token/ERC721/ERC721Full.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721Mintable.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721MetadataMintable.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721Holder.sol";
import "@openzeppelin/contracts/drafts/Counters.sol";
contract myERC721 is
ERC721Full,
ERC721Mintable,
ERC721MetadataMintable,
ERC721Holder
{
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
event awardNewItem(uint256 indexed newID);
constructor(
string memory name,
string memory symbol,
string memory baseURI
) public ERC721Full(name, symbol) {
_setBaseURI(baseURI);
}
function awardItem(
address player,
string memory _tokenURI
) public returns (uint256) {
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
mint(player, newItemId);
_setTokenURI(newItemId, _tokenURI);
return newItemId;
}
}
My ERC20 contract
pragma solidity ^0.5.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20Detailed.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20Burnable.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20Mintable.sol";
contract myERC20 is ERC20, ERC20Detailed, ERC20Burnable, ERC20Mintable {
constructor(
string memory name,
string memory symbol,
uint8 decimals,
uint256 initialSupply
) public ERC20Detailed(name, symbol, decimals) {
_mint(msg.sender, initialSupply * (10**uint256(decimals)));
}
}
My main factory contract
pragma solidity ^0.5.0;
import "./myERC721.sol";
import "./myERC20.sol";
contract tokenFactory {
address[] tokenAddress;
function deploy721Contract(
string calldata name,
string calldata symbol,
string calldata baseUrl
) external returns (myERC721 cardAddress) {
myERC721 newCards = new myERC721(name, symbol, baseUrl);
tokenAddress.push(address(newCards));
return newCards;
}
function deploy20Contract(
string calldata name,
string calldata symbol,
uint8 decimals,
uint256 initialSupply
) external returns (myERC20 creditsAddress) {
myERC20 newCredits = new myERC20(
name,
symbol,
decimals,
initialSupply
);
tokenAddress.push(address(newCredits));
return newCredits;
}
}
Problem
-
when i comment out the
deploy20Contract
function code. the factory contract can deploy and use 3598155 gas
-
when i comment out
deploy721Contract
function code. the factory contract can deploy and use 2208084 gas
BOTH 1 and 2 do not have cost much gas.
- when i don’t comment out any code, both have the
deploy20Contract
anddeploy721Contract
in the factory contract
it will get out of gas
. and i increase the gasLimit. it still out of gas
and finally tell me Exceeds block gas limit
It seems that when i depoy both ERC20 && ERC721 contract in the same contrat. it will stuck in an endless loop and use unlimited gas.
how should i do? Is there any conflict about importing the 721 & 20 contract at the same contract?