Adding ERC1155 Token to Game Discussion


#1

Welcome to the discussion about this section. Here you can ask questions or post feedback about this specific section.


#2

As soon as I click on the buy button I get this error in metamask


#3

Looks like something is wrong in your contract. Could you send me the contract and it’s address?


#4

GameToken Contract
pragma solidity ^0.5.0;

import ‘…/lib/ERC1155.sol’;
import ‘…/lib/ERC1155Mintable.sol’;

contract GameToken is ERC1155, ERC1155Mintable {
constructor() public {}
}

GameToken Contract Address: “0x88ad3ae682f0a35283610a06668B3a5ec3898403”

Marketplace Contract
pragma solidity ^0.5.0;

import ‘…/lib/IERC1155.sol’;

contract Marketplace {
IERC1155 private _token;

mapping (uint256 => uint256) price; //mapping from id of the token to its price

constructor (IERC1155 token) public {
    require(address(token) != address(0));
    _token = token;

    price[1] = 100000000000000;
    price[2] = 200000000000000;
    price[3] = 300000000000000;        
}

function () external payable {
    buyToken(0);
}

function buyToken(uint256 tokenID) public payable {
    uint256 weiAmount = msg.value;
    require(weiAmount >= price[tokenID] && price[tokenID] == 0);

    _token.safeTransferFrom(address(this), msg.sender, tokenID, 1, "");
}

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

}

Marketplace Contract Address: “0x0d94CbB328989f86c300d6A5bc58Fa1618971592”


#5

You have an error in your buyToken function.

    require(weiAmount >= price[tokenID] && price[tokenID] == 0);

should be

    require(weiAmount >= price[tokenID] && price[tokenID] != 0);

We want to check so that the price of the item is NOT 0. You’re require function failed because the item price was not zero.


#6

Thanks a lot. It worked :+1: