I am working on dapp using hardhat and goerli testnet and when I run it using npx hardhat run scripts/deploy.js --network goerli, I receive this error, I do have goerli faucet eth on my wallet but still it will display that my account balance is 0:
Error: insufficient funds for intrinsic transaction cost [ See: https://links.ethers.org/v5-errors-INSUFFICIENT_FUNDS ] (error={"name":"ProviderError","_stack":"ProviderError: HttpProviderError\n at HttpProvider.request (C:\\Users\\SAIFUL\\RTW3-Week7-NFT-Marketplace\\node_modules\\hardhat\\src\\internal\\core\\providers\\http.ts:78:19)\n at LocalAccountsProvider.request (C:\\Users\\SAIFUL\\RTW3-Week7-NFT-Marketplace\\node_modules\\hardhat\\src\\internal\\core\\providers\\accounts.ts:181:36)\n at processTicksAndRejections (internal/process/task_queues.js:95:5)\n at EthersProviderWrapper.send (C:\\Users\\SAIFUL\\RTW3-Week7-NFT-Marketplace\\node_modules\\#nomiclabs\\hardhat-ethers\\src\\internal\\ethers-provider-wrapper.ts:13:20)","code":-32000,"_isProviderError":true}, method="sendTransaction", transaction=undefined, code=INSUFFICIENT_FUNDS, version=providers/5.7.2)
at Logger.makeError (C:\Users\SAIFUL\RTW3-Week7-NFT-Marketplace\node_modules\#ethersproject\logger\src.ts\index.ts:269:28)
at Logger.throwError (C:\Users\SAIFUL\RTW3-Week7-NFT-Marketplace\node_modules\#ethersproject\logger\src.ts\index.ts:281:20)
at checkError (C:\Users\SAIFUL\RTW3-Week7-NFT-Marketplace\node_modules\#ethersproject\providers\src.ts\json-rpc-provider.ts:98:16)
at C:\Users\SAIFUL\RTW3-Week7-NFT-Marketplace\node_modules\#ethersproject\providers\src.ts\json-rpc-provider.ts:265:24
at processTicksAndRejections (internal/process/task_queues.js:95:5) {
reason: 'insufficient funds for intrinsic transaction cost',
code: 'INSUFFICIENT_FUNDS',
error: ProviderError: HttpProviderError
at HttpProvider.request (C:\Users\SAIFUL\RTW3-Week7-NFT-Marketplace\node_modules\hardhat\src\internal\core\providers\http.ts:78:19)
at LocalAccountsProvider.request (C:\Users\SAIFUL\RTW3-Week7-NFT-Marketplace\node_modules\hardhat\src\internal\core\providers\accounts.ts:181:36)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at EthersProviderWrapper.send (C:\Users\SAIFUL\RTW3-Week7-NFT-Marketplace\node_modules\#nomiclabs\hardhat-ethers\src\internal\ethers-provider-wrapper.ts:13:20),
method: 'sendTransaction',
transaction: undefined
}
here is my deploy.js file everything is supposed to fine and working but error won't disappear
require("#nomiclabs/hardhat-waffle");
require("#nomiclabs/hardhat-ethers");
const fs = require('fs');
// const infuraId = fs.readFileSync(".infuraid").toString().trim() || "";
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);
}
});
module.exports = {
defaultNetwork: "hardhat",
networks: {
hardhat: {
chainId: 1337
},
goerli: {
url: "https://eth-mainnet.g.alchemy.com/v2/2Hmp7mz6XEpw6Z47cCZx2vEIAYXFGYOL",
accounts: ["private key here"]
}
},
solidity: {
version: "0.8.4",
settings: {
optimizer: {
enabled: true,
runs: 200
}
}
}
};
here is my solidity contract:
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;
import "hardhat/console.sol";
import "#openzeppelin/contracts/utils/Counters.sol";
import "#openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "#openzeppelin/contracts/token/ERC721/ERC721.sol";
contract NFTMarketplace is ERC721URIStorage {
address payable owner;
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
Counters.Counter private _itemsSold;
uint256 listPrice = 0.01 ether;
constructor() ERC721("NFTMarketplace", "NFTM") {
owner = payable(msg.sender);
}
struct ListedToken {
uint256 tokenId;
address payable owner;
address payable seller;
uint256 price;
bool currentlyListed;
}
mapping(uint256 => ListedToken) private idToListedToken;
function updateListPrice(uint256 _listprice) public payable {
require(owner == msg.sender, "only owner can update listing price ");
listPrice = _listprice;
}
function getListPrice() public view returns (uint256) {
return listPrice;
}
function getLatestIdToListedtoken()
public
view
returns (ListedToken memory)
{
uint256 currentTokenId = _tokenIds.current();
return idToListedToken[currentTokenId];
}
function getListedForToken(uint256 tokenId) public view returns (ListedToken memory) {
return idToListedToken[tokenId];
}
function getCurrentToken() public view returns (uint256) {
return _tokenIds.current();
}
function createToken(string memory tokenURI, uint256 price) public payable returns (uint) {
require(msg.value == listPrice, "Send enough ether to list");
require(price > 0, "Make sure the price isn't negative");
_tokenIds.increment();
uint256 currentTokenId = _tokenIds.current();
_safeMint(msg.sender, currentTokenId);
_setTokenURI(currentTokenId, tokenURI);
createListedToken(currentTokenId, price);
return currentTokenId;
}
function createListedToken(uint256 tokenId, uint256 price) private {
idToListedToken[tokenId] = ListedToken(
tokenId,
payable(address(this)),
payable(msg.sender),
price,
true
);
_transfer(msg.sender, address(this), tokenId);
}
function getAllNFTs() public view returns(ListedToken[] memory) {
uint nftCount = _tokenIds.current();
ListedToken[] memory tokens = new ListedToken[](nftCount);
uint currentIndex = 0;
for(uint i=0; i<nftCount; i++) {
uint currentId = i+1;
ListedToken storage currentItem = idToListedToken[currentId];
tokens[currentIndex] = currentItem;
currentIndex += 1;
}
return tokens;
}
function getMyNFTs() public view returns (ListedToken[] memory) {
uint totalItemCount = _tokenIds.current();
uint itemCount = 0;
uint currentIndex = 0;
uint currentId;
for(uint i=0; i < totalItemCount; i++)
{
if(idToListedToken[i+1].owner == msg.sender || idToListedToken[i+1].seller == msg.sender){
itemCount += 1;
}
}
ListedToken[] memory items = new ListedToken[](itemCount);
for(uint i=0; i < totalItemCount; i++) {
if(idToListedToken[i+1].owner == msg.sender || idToListedToken[i+1].seller == msg.sender) {
currentId = i+1;
ListedToken storage currentItem = idToListedToken[currentId];
items[currentIndex] = currentItem;
currentIndex += 1;
}
}
return items;
}
function executeSale(uint256 tokenId) public payable {
uint price = idToListedToken[tokenId].price;
require(msg.value == price, "please submit the relevant NFT price in order to purchase");
address seller = idToListedToken[tokenId].seller;
idToListedToken[tokenId].currentlyListed = true;
idToListedToken[tokenId].seller = payable(msg.sender);
_itemsSold.increment();
_transfer(address(this), msg.sender, tokenId);
approve(address(this), tokenId);
payable(owner).transfer(listPrice);
payable(seller).transfer(msg.value);
}
}
I tried different networks, but I have no choice except goerli because I will use alchemy Api.
You still need to get more Goerli ETH from faucets or from your other peers.
Try that and see if it will work
Finally I switched from INFURA to ALCHEMY and following the instructions here:
https://hardhat.org/tutorial/deploying-to-a-live-network
it worked!
Related
when i want to deploy this contract in REMIX IDE i get this error:
Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending?
Internal JSON-RPC error.
{
"code": -32000,
"message": "gas required exceeds allowance (20058647)"
}
this is my contract:
//SPDX-License-Identifier: MIT
pragma solidity >=0.8.0 <0.9.0;
import "#openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "#openzeppelin/contracts/access/Ownable.sol";
contract KINGs300 is ERC721Enumerable, Ownable {
using Strings for uint256;
string public baseURI;
string public baseExtension = ".json";
uint256 public cost = 0.5 ether;
uint256 public maxSupply = 300;
constructor(
string memory _name,
string memory _symbol,
string memory _initBaseURI
)
ERC721(_name, _symbol) {
setBaseURI(_initBaseURI);
mint(msg.sender, 300);
}
function _baseURI() internal view override virtual returns (string memory) {
return baseURI;
}
function mint(address _to, uint256 _mintAmount) public payable {
uint256 supply = totalSupply();
require(_mintAmount > 0);
require(supply + _mintAmount <= maxSupply);
for (uint256 i = 1; i <= _mintAmount; i++) {
_safeMint(_to, supply + i);
}
}
function walletOfOwner(address _owner) public view returns (uint256[] memory)
{
uint256 ownerTokenCount = balanceOf(_owner);
uint256[] memory tokenIds = new uint256[](ownerTokenCount);
for (uint256 i; i < ownerTokenCount; i++) {
tokenIds[i] = tokenOfOwnerByIndex(_owner, i);
}
return tokenIds;
}
function tokenURI(uint256 tokenId) public view override virtual returns (string memory)
{
require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");
string memory currentBaseURI = _baseURI();
return bytes(currentBaseURI).length > 0
? string(abi.encodePacked(currentBaseURI,tokenId.toString(),baseExtension))
: "";
}
function setCost(uint256 _newCost) public onlyOwner {
cost = _newCost;
}
function setBaseURI(string memory _newBaseURI) public onlyOwner {
baseURI = _newBaseURI;
}
function setBaseExtension(string memory _newBaseExtension) public onlyOwner {
baseExtension = _newBaseExtension;
}
}
i want to mint a collection that has 300 NFTs in it
You can't mint 300 NFTs at once because of gas Limit of blocks, check this link for the limit of ethereum
https://ethereum.org/en/developers/docs/gas/
Block limit of 30 million gas for ethereum.
Try minting less NFTs like 100 NFTs per transaction and you must send 3 tranasction for minting 300 NFTs.
My goal is to sell nfts that can be bought with my own cryptocurrency on my market. For this, I access my own token using the IERC20 interface within my market contract. But I'm not that good at solidity. If I come to the plain. How do I get permission to shop with the BVC token (my token) in the user's wallet when the buy button is clicked on my website and I make this payment.
THIS MARKET.sol CODE
// SPDX-License-Identifier: MIT LICENSE
pragma solidity ^0.8.9;
import "#openzeppelin/contracts/utils/Counters.sol";
import "#openzeppelin/contracts/security/ReentrancyGuard.sol";
import "#openzeppelin/contracts/token/ERC721/ERC721.sol";
import "#openzeppelin/contracts/access/Ownable.sol";
import "#openzeppelin/contracts/token/ERC20/IERC20.sol";
contract Market is ReentrancyGuard, Ownable {
struct TokenInfo{
IERC20 paytoken;
uint256 listingFee;
uint256 mintingFee;
uint256 price;
}
using Counters for Counters.Counter;
Counters.Counter private _itemIds;
Counters.Counter private _itemsSold;
IERC20 public paytoken;
TokenInfo[] public AllowedCrypto;
address payable holder;
//uint256 listingFee = 0.0025 ether;
//uint256 mintingFee = 0.0075 ether;
constructor() {
holder = payable(msg.sender);
}
struct VaultItem {
uint itemId;
address nftContract;
uint256 tokenId;
address payable seller;
address payable holder;
uint256 price;
bool sold;
}
mapping(uint256 => VaultItem) private idToVaultItem;
event VaultItemCreated (
uint indexed itemId,
address indexed nftContract,
uint256 indexed tokenId,
address seller,
address holder,
uint256 price,
bool sold
);
function AddCurrency (IERC20 _paytoken,uint256 _listingFee,uint256 _mintingFee,uint256 _price) public onlyOwner {
AllowedCrypto.push(TokenInfo({
paytoken:_paytoken,
listingFee:_listingFee,
mintingFee: _mintingFee,
price:_price
}));
}
function approveCRI (uint256 _pid) public payable nonReentrant{
TokenInfo storage tokens = AllowedCrypto[_pid];
paytoken = tokens.paytoken;
uint256 amount = 200000000000000000000000000;
paytoken.approve(msg.sender, amount);
}
function allowenceCRI (uint256 _pid) public payable nonReentrant{
TokenInfo storage tokens = AllowedCrypto[_pid];
paytoken = tokens.paytoken;
paytoken.allowance(address(this), msg.sender);
}
function getListingFee(uint256 _pid) public view returns (uint256) {
TokenInfo storage tokens = AllowedCrypto[_pid];
uint256 listingFee;
listingFee = tokens.listingFee;
return listingFee;
}
function createVaultItem(address nftContract,uint256 tokenId,uint256 _pid) public payable nonReentrant {
TokenInfo storage tokens = AllowedCrypto[_pid];
paytoken = tokens.paytoken;
uint256 listingFee;
listingFee = tokens.listingFee;
uint256 price;
price = tokens.price;
require(price > 0, "Price cannot be zero");
require(msg.value == listingFee, "Price cannot be listing fee");
_itemIds.increment();
uint256 itemId = _itemIds.current();
idToVaultItem[itemId] = VaultItem(itemId,nftContract,tokenId,payable(msg.sender),payable(address(0)),price,false);
IERC721(nftContract).transferFrom(msg.sender, address(this), tokenId);
emit VaultItemCreated(itemId,nftContract,tokenId,msg.sender,address(0),price,false);
}
function n2DMarketSale(address nftContract,uint256 itemId,uint256 _pid) public payable nonReentrant {
TokenInfo storage tokens = AllowedCrypto[_pid];
paytoken = tokens.paytoken;
uint256 listingFee;
listingFee = tokens.listingFee;
uint price = tokens.price; //idToVaultItem[itemId].price;
uint tokenId = idToVaultItem[itemId].tokenId;
paytoken.approve(msg.sender, price);
require(msg.value == paytoken.balanceOf(address(this)), "Not enough balance to complete transaction");
idToVaultItem[itemId].seller.transfer(msg.value);
IERC721(nftContract).transferFrom(address(this), msg.sender, tokenId);
idToVaultItem[itemId].holder = payable(msg.sender);
idToVaultItem[itemId].sold = true;
_itemsSold.increment();
payable(holder).transfer(listingFee);
}
function getAvailableNft() public view returns (VaultItem[] memory) {
uint itemCount = _itemIds.current();
uint unsoldItemCount = _itemIds.current() - _itemsSold.current();
uint currentIndex = 0;
VaultItem[] memory items = new VaultItem[](unsoldItemCount);
for (uint i = 0; i < itemCount; i++) {
if (idToVaultItem[i + 1].holder == address(0)) {
uint currentId = i + 1;
VaultItem storage currentItem = idToVaultItem[currentId];
items[currentIndex] = currentItem;
currentIndex += 1;
}
}
return items;
}
function getMyNft() public view returns (VaultItem[] memory) {
uint totalItemCount = _itemIds.current();
uint itemCount = 0;
uint currentIndex = 0;
for (uint i = 0; i < totalItemCount; i++) {
if (idToVaultItem[i + 1].holder == msg.sender) {
itemCount += 1;
}
}
VaultItem[] memory items = new VaultItem[](itemCount);
for (uint i = 0; i < totalItemCount; i++) {
if (idToVaultItem[i + 1].holder == msg.sender) {
uint currentId = i + 1;
VaultItem storage currentItem = idToVaultItem[currentId];
items[currentIndex] = currentItem;
currentIndex += 1;
}
}
return items;
}
function getMyMarketNfts() public view returns (VaultItem[] memory) {
uint totalItemCount = _itemIds.current();
uint itemCount = 0;
uint currentIndex = 0;
for (uint i = 0; i < totalItemCount; i++) {
if (idToVaultItem[i + 1].seller == msg.sender) {
itemCount += 1;
}
}
VaultItem[] memory items = new VaultItem[](itemCount);
for (uint i = 0; i < totalItemCount; i++) {
if (idToVaultItem[i + 1].seller == msg.sender) {
uint currentId = i + 1;
VaultItem storage currentItem = idToVaultItem[currentId];
items[currentIndex] = currentItem;
currentIndex += 1;
}
}
return items;
}
function withdraw(uint256 _pid) public payable onlyOwner() {
TokenInfo storage tokens = AllowedCrypto[_pid];
paytoken = tokens.paytoken;
require(msg.sender.balance == paytoken.balanceOf(address(this)));
paytoken.transfer(msg.sender, paytoken.balanceOf(address(this)));
}
}
the approval must be called directly from the token contract, as the approval is performed through the msg.sender (the user).
the approval is used to give permission for an address to use a maximum of X tokens from another address, so the approval should have as input the marketplace contract address and the maximum amount of tokens that will be spent.
through the UIX, first make the user approve by calling the approve function (address marketplace, amount) directly from the token contract, then you will enable the button that calls the function that needs the tokens
My contract for ERC1155 marketplace to mint buy and sell the NFT.
The nft is getting minted , However the NFT is not showing in market place and not able to purchase. I am facing this error.I have also applied setApprovedforAll method while minting still no help.
Should create and execute market sales:
Error: VM Exception while processing transaction: reverted with reason string 'ERC1155: caller is not owner nor approved'
at NFT1155.balanceOf (#openzeppelin/contracts/token/ERC1155/ERC1155.sol:71)
at NFT1155.isApprovedForAll (#openzeppelin/contracts/token/ERC1155/ERC1155.sol:110)
at NFT1155.createMarketSale (contracts/NFT1155.sol:165)
at async HardhatNode._mineBlockWithPendingTxs (node_modules/hardhat/src/internal/hardhat-network/provider/node.ts:1772:23)
at async HardhatNode.mineBlock (node_modules/hardhat/src/internal/hardhat-network/provider/node.ts:466:16)
at async EthModule._sendTransactionAndReturnHash (node_modules/hardhat/src/internal/hardhat-network/provider/modules/eth.ts:1496:18)
at async HardhatNetworkProvider.request (node_modules/hardhat/src/internal/hardhat-network/provider/provider.ts:118:18)
at async EthersProviderWrapper.send (node_modules/#nomiclabs/hardhat-ethers/src/internal/ethers-provider-wrapper.ts:13:20)
My contract for ERC1155 marketplace to mint buy and sell the NFT.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;
import "#openzeppelin/contracts/token/ERC1155/ERC1155.sol";
import "#openzeppelin/contracts/access/Ownable.sol";
import "#openzeppelin/contracts/token/ERC1155/extensions/ERC1155Supply.sol";
import "#openzeppelin/contracts/utils/Counters.sol";
import "hardhat/console.sol";
contract NFT1155 is ERC1155, Ownable, ERC1155Supply {
//contract address goes here and id will be dynamic and will be passed in the _mint function calls
//example https://ipfs.io/ipfs/QmT51bbxTbSiYGcF2X39sG6DGYyAX2413A1sZfiACMgJGP?filename={id}.json
//if the if id 1 then https://ipfs.io/ipfs/QmT51bbxTbSiYGcF2X39sG6DGYyAX2413A1sZfiACMgJGP?filename=1.json will return the data that needs to be minted
constructor() ERC1155("") {}
mapping(uint256 => string) internal _tokenURIs;
mapping(uint256 => MarketItem) private idToMarketItem;
Counters.Counter private _itemsSold;
struct MarketItem {
uint256 tokenId;
address payable seller;
address payable owner;
uint256 price;
bool sold;
}
event MarketItemCreated(
uint256 indexed tokenId,
address seller,
address owner,
uint256 price,
bool sold
);
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
//To chnage the URL String after the contract is deployed
function setURI(string memory newuri) public onlyOwner {
_setURI(newuri);
}
function mintToken(
string memory tokenURI,
uint256 amount,
uint256 price
) public returns (uint256) {
uint256 newItemId = _tokenIds.current();
_mint(address(this), newItemId, amount, "");
_setTokenUri(newItemId, tokenURI);
//createMarketItem(newItemId, price, amount);
_tokenIds.increment();
return newItemId;
}
function createMarketItem(
uint256 tokenId,
uint256 price,
uint256 amount
) private {
require(price > 0, "Price must be at least 1 wei");
idToMarketItem[tokenId] = MarketItem(
tokenId,
payable(msg.sender),
payable(address(this)),
price,
false
);
setApprovalForAll(address(this), true);
safeTransferFrom(msg.sender, address(this), tokenId, amount, "");
emit MarketItemCreated(
tokenId,
msg.sender,
address(this),
price,
false
);
}
function onERC1155Received(
address _operator,
address _from,
uint256 _id,
uint256 _value,
bytes calldata _data
) external returns (bytes4) {
return
bytes4(
keccak256(
"onERC1155Received(address,address,uint256,uint256,bytes)"
)
);
}
function _setTokenUri(uint256 tokenId, string memory tokenURI) private {
_tokenURIs[tokenId] = tokenURI;
}
function mintBatch(
address to,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
) public onlyOwner {
_mintBatch(to, ids, amounts, data);
}
// The following functions are overrides required by Solidity.
function _beforeTokenTransfer(
address operator,
address from,
address to,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
) internal override(ERC1155, ERC1155Supply) {
super._beforeTokenTransfer(operator, from, to, ids, amounts, data);
}
/* allows someone to resell a token they have purchased */
function resellToken(
uint256 tokenId,
uint256 price,
uint256 amount
) public payable {
require(
idToMarketItem[tokenId].owner == msg.sender,
"Only item owner can perform this operation"
);
idToMarketItem[tokenId].sold = false;
idToMarketItem[tokenId].price = price;
idToMarketItem[tokenId].seller = payable(msg.sender);
idToMarketItem[tokenId].owner = payable(address(this));
_itemsSold.decrement();
safeTransferFrom(msg.sender, address(this), tokenId, amount, "");
}
/* Creates the sale of a marketplace item */
/* Transfers ownership of the item, as well as funds between parties */
function createMarketSale(uint256 tokenId, uint256 amount) public payable {
uint256 price = idToMarketItem[tokenId].price;
address seller = idToMarketItem[tokenId].seller;
console.log(
" ~ file: NFT1155.sol ~ line 147 ~ createMarketSale ~ price",
msg.value,
price
);
// require(
// msg.value == price,
// "Please submit the asking price in order to complete the purchase"
// );
idToMarketItem[tokenId].owner = payable(msg.sender);
idToMarketItem[tokenId].sold = true;
idToMarketItem[tokenId].seller = payable(address(0));
_itemsSold.increment();
safeTransferFrom(address(this), msg.sender, tokenId, amount, "");
setApprovalForAll(address(this), true);
// payable(owner).transfer(listingPrice);
payable(seller).transfer(msg.value);
}
/* Returns all unsold market items */
function fetchMarketItems() public view returns (MarketItem[] memory) {
uint256 itemCount = _tokenIds.current();
uint256 unsoldItemCount = _tokenIds.current() - _itemsSold.current();
uint256 currentIndex = 0;
MarketItem[] memory items = new MarketItem[](unsoldItemCount);
for (uint256 i = 0; i < itemCount; i++) {
if (idToMarketItem[i + 1].owner == address(this)) {
uint256 currentId = i + 1;
MarketItem storage currentItem = idToMarketItem[currentId];
items[currentIndex] = currentItem;
currentIndex += 1;
}
}
return items;
}
/* Returns only items that a user has purchased */
function fetchMyNFTs() public view returns (MarketItem[] memory) {
uint256 totalItemCount = _tokenIds.current();
uint256 itemCount = 0;
uint256 currentIndex = 0;
for (uint256 i = 0; i < totalItemCount; i++) {
if (idToMarketItem[i + 1].owner == msg.sender) {
itemCount += 1;
}
}
MarketItem[] memory items = new MarketItem[](itemCount);
for (uint256 i = 0; i < totalItemCount; i++) {
if (idToMarketItem[i + 1].owner == msg.sender) {
uint256 currentId = i + 1;
MarketItem storage currentItem = idToMarketItem[currentId];
items[currentIndex] = currentItem;
currentIndex += 1;
}
}
return items;
}
/* Returns only items a user has listed */
function fetchItemsListed() public view returns (MarketItem[] memory) {
uint256 totalItemCount = _tokenIds.current();
uint256 itemCount = 0;
uint256 currentIndex = 0;
for (uint256 i = 0; i < totalItemCount; i++) {
if (idToMarketItem[i + 1].seller == msg.sender) {
itemCount += 1;
}
}
MarketItem[] memory items = new MarketItem[](itemCount);
for (uint256 i = 0; i < totalItemCount; i++) {
if (idToMarketItem[i + 1].seller == msg.sender) {
uint256 currentId = i + 1;
MarketItem storage currentItem = idToMarketItem[currentId];
items[currentIndex] = currentItem;
currentIndex += 1;
}
}
return items;
}
}
The problem is that because of the way you are minting, you don't own the token, the contract does.
Since you don't own the token, it doesn't appear in marketplaces and you can't call approve or transfer because you are not the owner of the token.
Here is the culprit:
_mint(address(this), newItemId, amount, "");
You are minting to address(this) which is the address of the contract itself.
You'll need a way to send the token from the contract to whomever you want using a custom function, or the probably better solution, you can just mint to the address calling your mintToken function by doing:
_mint(msg.sender, newItemId, amount, "");
Best of luck!!
When You call safeTransferFrom, require statement is not passing
function safeTransferFrom(address from,address to,uint256 id,uint256 amount,bytes memory data
) public virtual override {
require(
from == _msgSender() || isApprovedForAll(from, _msgSender()),
"ERC1155: caller is not owner nor approved"
);
_safeTransferFrom(from, to, id, amount, data);
}
Most likely you are transferring a token from the account that does not own the token,
You are using safeTransferFrom which is a public function. Try using _safeTransferFrom.You should add a custom function users can call, and internally use _safeTransferFrom
Overview
I am working on a project for a Blockchain Developer Course, the project must be tested with Ganache because the Reviewer Team for the course will use Ganache.
The Smart Contract that I am working right now works with two different smart contracts, one is for data storage and the other is for the application purposes (user interaction).
When an user interacts with the registerOracle function the smart contract will ask for 1 ether to generate 1 oracle (this oracle will have an ID and 3 Indexes).
I tested the Smart Contract on Remix and everything works find but when I try to test the oracle generator function in Ganache it gives me a revert error.
Project URL
The project's files are in my Github's repository along with some screenshot I took showcasting the procedure to test the smart contract on Remix and the error I get on Ganache.
The code is too extend, but I will be posting here the relevant parts that are giving me the errors. The complete code can be seen on my repository.
TestingEnv.js File
const Web3 = require('web3');
const fs = require('fs').promises;
async function main () {
/**
* IGNORE FROM HERE
*/
const web3 = new Web3(new Web3.providers.WebsocketProvider("HTTP://127.0.0.1:8545"));
const appABI = (JSON.parse(await fs.readFile("./bin/appAbi.json", "utf8"))).abi;
const appBYTECODE = (JSON.parse(await fs.readFile("./bin/appByteCode.json", "utf8"))).bytecode;
const dataABI = (JSON.parse(await fs.readFile("./bin/dataAbi.json", "utf8"))).abi;
const dataBYTECODE = (JSON.parse(await fs.readFile("./bin/dataByteCode.json", "utf8"))).bytecode;
const owner = (await web3.eth.getAccounts())[0];
const appContract = await new web3.eth.Contract(appABI).deploy({data: '0x' + appBYTECODE})
.send({from: owner, gasLimit: 6000000, gasPrice: web3.utils.toWei('5', 'gwei')});
const dataContract = await new web3.eth.Contract(dataABI).deploy({data: '0x' + dataBYTECODE})
.send({from: owner, gasLimit: 6000000, gasPrice: web3.utils.toWei('5', 'gwei')});
const appAddress = appContract._address;
const dataAddress = dataContract._address;
await appContract.methods.registerDataContract(dataAddress).send({from: owner});
await dataContract.methods.registerApplicationContract(appAddress).send({from: owner});
/**
* TO HERE
*/
/**
* REVERT ERROR - STARTS
*/
const oracleFee = await web3.utils.toWei("1", "ether");
const server = (await web3.eth.getAccounts())[2];
// I AM REGISTERING 20 ORACLES
for (var i=0; i<20; i++) {
await appContract.methods.registerOracle().send({from: server, value: oracleFee});
await new Promise(resolve => setTimeout(resolve, 2000));
};
const totalOracles = await appContract.methods.getTotalOracles().call({from: server});
console.log(`Total Numbers of Oracles Registered: ${totalOracles}`);
for (var i = 0; i<totalOracles; i++) {
var indexes = await appContract.methods.getOraceIndexes(i).call({from: server});
if (i >= 0 && i <= 9) {
console.log(`Oracle 0${i} indexes: ${indexes._index1.toString()} ${indexes._index2.toString()} ${indexes._index2.toString()}`);
} else {
console.log(`Oracle ${i} indexes: ${indexes._index1.toString()} ${indexes._index2.toString()} ${indexes._index2.toString()}`);
};
};
};
main();
FlightSuretyApp.sol Contract
pragma solidity ^0.4.24;
interface iFlightSuretyData {
function registerOracle(address server, uint8 index1, uint8 index2, uint8 index3) external payable;
}
import "./SafeMath.sol";
contract FlightSuretyApp {
using SafeMath for uint256;
bool operationalStatus;
address owner;
iFlightSuretyData FlightSecuretyData;
constructor() public {
operationalStatus = true;
owner = msg.sender;
}
modifier requireOwner() {
require(msg.sender == owner, "Owner is required");
_;
}
modifier requireOperational() {
require(operationalStatus == true, "Contract is not operational");
_;
}
function registerDataContract(address dataContract) external
requireOwner {
FlightSecuretyData = iFlightSuretyData(dataContract);
}
uint256 constant ORACLE_REGISTRATION_FEE = 1 ether;
modifier requireOracleRegistrationFee() {
require(msg.value == ORACLE_REGISTRATION_FEE, "Oracle Registration Cost 1 ether");
_;
}
function registerOracle() external payable
requireOperational
requireOracleRegistrationFee {
(uint8 index1, uint8 index2, uint8 index3) = indexesThrown();
FlightSecuretyData.registerOracle.value(msg.value)(msg.sender, index1, index2, index3);
}
function indexesThrown() private view returns(uint8 _index1, uint8 _index2, uint8 _index3)
{
uint8 index1 = generateIndex1();
uint8 index2 = generateIndex2(index1);
uint8 index3 = generateIndex3(index1, index2);
return (index1, index2, index3);
}
function generateIndex1() private view returns(uint8 _index) {
uint256 mod = 10;
uint256 time = block.timestamp;
uint256 difficulty = block.difficulty;
uint8 value = uint8(SafeMath.mod(uint256(keccak256(abi.encodePacked(time, difficulty, msg.sender))), mod));
return value;
}
function generateIndex2(uint8 _index1) private view returns(uint8 _index) {
uint256 mod = 10;
uint256 time = block.timestamp;
uint256 difficulty = block.difficulty;
uint8 value = uint8(SafeMath.mod(uint256(keccak256(abi.encodePacked(time, difficulty, msg.sender))), mod));
while(value == _index1) {
time = SafeMath.add(time, 500);
difficulty = SafeMath.add(difficulty, 700);
value = uint8(SafeMath.mod(uint256(keccak256(abi.encodePacked(time, difficulty, msg.sender))), mod));
}
return value;
}
function generateIndex3(uint8 _index1, uint8 _index2) private view returns(uint8 _index) {
uint256 mod = 10;
uint256 time = block.timestamp;
uint256 difficulty = block.difficulty;
uint8 value = uint8(SafeMath.mod(uint256(keccak256(abi.encodePacked(time, difficulty, msg.sender))), mod));
while((value == _index1) || (value == _index2)) {
time = SafeMath.add(time, 500);
difficulty = SafeMath.add(difficulty, 700);
value = uint8(SafeMath.mod(uint256(keccak256(abi.encodePacked(time, difficulty, msg.sender))), mod));
}
return value;
}
}
FlightSuretyData.sol
import "./SafeMath.sol";
contract FlightSuretyData {
using SafeMath for uint256;
bool operationalStatus;
address owner;
address appContract;
constructor() public {
operationalStatus = true;
owner = msg.sender;
airlines[owner].registrationStatus = true;
}
modifier requireOwner() {
require(msg.sender == owner, "Require contract owner");
_;
}
modifier requireApplication() {
require(msg.sender == appContract, "Require application");
_;
}
modifier requireOperational() {
require(operationalStatus == true, "Contract is not operational");
_;
}
function registerApplicationContract(address application) external
requireOwner
requireOperational {
appContract = application;
}
struct oracle {
uint8 index1;
uint8 index2;
uint8 index3;
mapping(bytes32 => bool) voteState;
}
struct oracleServer {
mapping(uint256 => oracle) oracles;
uint256 numberOfOracles;
}
mapping(address => oracleServer) private oracleServers;
function registerOracle(address server, uint8 index1, uint8 index2, uint8 index3) external payable
requireOperational
requireApplication {
uint256 counter = oracleServers[server].numberOfOracles;
oracleServers[server].oracles[counter].index1 = index1;
oracleServers[server].oracles[counter].index2 = index2;
oracleServers[server].oracles[counter].index3 = index3;
counter = SafeMath.add(counter, 1);
oracleServers[server].numberOfOracles = counter;
}
}
How to Run the Project?
It is easy, just install the dependencies and run the TestingEnv.js file.
My Environment Specifications
Windows 10 Home version 21H1
Ganache version 2.5.4 (Use the Ganache Desktop Application from Ganache and not some dependencies)
Node version 11.0.0
Solidity version 0.4.24
Proof that Works on Remix
Proof that Doesn't Work on Ganache
Request
How can I solve it? I am stuck on my project because I can make the project work, I've teste others functions of my smart contracts and they are working fine, but when I try to test the oracle generator it gives an error and doesn't work.
What am I missing?
I am working on a little "Betting Dapp" contract on solidity. I have a function called addContractFunds which allows me to send ethereum to the contract balance. However, when I call this function in the truffle console on the Ropsten test network, I get the following error:
TypeError: Cannot read property 'address' of undefined
Inside truffle console:
The interesting thing is, when I run this contract and test it in Remix(on ropsten), or locally using ganache everything works fine. Anyone have any idea what is happening?
Here is the code to my contract
import "./provableAPI.sol";
import "./Ownable.sol";
contract Coinflip is usingProvable, Ownable {
struct CoinflipSession {
uint betAmount;
}
uint public balance;
uint256 constant NUM_RANDOM_BYTES_REQUESTED = 1;
uint256 public latestNumber;
event LogNewProvableQuery(string description);
event generateRandomNumber(uint256 randomNumber);
event betTaken(uint betAmount);
event winPayout(uint betAmount);
constructor() public {
update();
}
mapping (address => CoinflipSession) private session;
modifier costs(uint cost){
require(msg.value >= cost);
_;
}
function __callback(bytes32 queryId, string memory _result, bytes memory _proof) public {
require(msg.sender == provable_cbAddress());
uint256 randomNumber = uint256(keccak256(abi.encodePacked(_result))) % 2;
latestNumber = randomNumber;
emit generateRandomNumber(randomNumber);
}
function update() payable public {
uint256 QUERY_EXECUTION_DELAY = 0;
uint256 GAS_FOR_CALLBACK = 200000;
provable_newRandomDSQuery(
QUERY_EXECUTION_DELAY,
NUM_RANDOM_BYTES_REQUESTED,
GAS_FOR_CALLBACK
);
}
function addContractFunds() public payable{
balance += msg.value;
}
function intakeToken() public payable costs(1 wei){
//require(session[msg.sender] == null || session[msg.sender].betAmount == 0);
balance += msg.value;
CoinflipSession memory newSession;
newSession.betAmount = msg.value;
address creator = msg.sender;
session[creator] = newSession;
emit betTaken(newSession.betAmount);
}
function flipCoin() public returns(uint) {
if (random() == 0) {
return 0;
}
resetBalance();
return 1;
}
function resetBalance() private {
session[msg.sender].betAmount = 0;
}
function handleWinPayout() public payable {
balance -= session[msg.sender].betAmount *2;
uint toTransfer = session[msg.sender].betAmount*2;
msg.sender.transfer(toTransfer);
emit winPayout(session[msg.sender].betAmount * 2);
resetBalance();
}
function random() public view returns (uint) {
return now % 2;
}
}