0

That's now my second try to create a battle Contract for ERC721 NFTs with a reward and Burn function but I'm kind of stuck, if someone has a few hints I would be very grateful. EDIT: Visibility for constructor is ignored, thats the error I get when I try to Compile

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/contracts/token/ERC721/ERC721.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/contracts/utils/math/SafeMath.sol";

contract BattleContract {
    using SafeMath for uint256;

    // Address of the ERC721 contract that holds the NFTs
    address public erc721Address = 0x8fec6549beb5EBE7dE383Ce9f6DdA42b8e6aa2c2;

    // Address of the reward coin contract
    address public rewardCoinAddress = 0x77e2DfBA9FB9EB46b3ca83CAeb31782253914a23;

    // Maximum number of losses allowed before an NFT is burned
    uint256 public maxLosses = 3;

    // Pending battles
    struct Battle {
        address player1;
        uint256 nft1;
        address player2;
        uint256 nft2;
    }
    Battle[] public battles;

    // Mapping from NFT ID to loss count
    mapping(uint256 => uint256) public nftLosses;

    // ERC721 contract instance
    ERC721 erc721;

    constructor() public {
        erc721 = ERC721(erc721Address);
    }
        
    function initiateBattle(address player1, uint256 nft1, address player2, uint256 nft2) public {
    // Check if the given NFTs exist and are owned by the given players
    require(erc721.exists(nft1) && erc721.ownerOf(nft1) == player1, "NFT1 does not exist or is not owned by player1");
    require(erc721.exists(nft2) && erc721.ownerOf(nft2) == player2, "NFT2 does not exist or is not owned by player2");

    // Add the battle to the pending battles array
    battles.push(Battle({
        player1: player1,
        nft1: nft1,
        player2: player2,
        nft2: nft2
    }));
}

function resolveBattle(uint256 battleIndex, bool player1Wins) public {
    Battle storage battle = battles[battleIndex];

    // Check if the given battle index is valid
    require(battle.player1 != address(0), "Invalid battle index");

    // Determine the winner and loser of the battle
    address winner;
    uint256 winningNFT;
    address loser;
    uint256 losingNFT;
    if (player1Wins) {
        winner = battle.player1;
        winningNFT = battle.nft1;
        loser = battle.player2;
        losingNFT = battle.nft2;
    } else {
        winner = battle.player2;
        winningNFT = battle.nft2;
        loser = battle.player1;
        losingNFT = battle.nft1;
    }

    // Update the loss count for the losing NFT
    nftLosses[losingNFT] = nftLosses[losingNFT].add(1);

    // Check if the NFT has reached the maximum number of allowed losses
    if (nftLosses[losingNFT] >= maxLosses) {
    // Burn the losing NFT
    erc721.burn(loser, losingNFT);
    } 
    // Transfer some amount of the reward coin to the winner
    rewardCoinAddress.transfer(winner, 1);
    // Clear the battle from the pending battles array
    battle.player1 = address(0);
    battle.player2 = address(0);
    battle.nft1 = 0;
    battle.nft2 = 0;
    }
}
3
  • What's the problem? We can't help if you don't know the issue.
    – Ismael
    Dec 13, 2022 at 6:51
  • Visibility for constructor is ignored, thats the error I get when I try to Compile
    – HenryHard
    Dec 13, 2022 at 12:14
  • I don't know what tool are you using but it is not an error, it is just a warning. If you search the site for the warning message you will find a previous question ethereum.stackexchange.com/questions/98453/… with the answer you are looking.
    – Ismael
    Dec 13, 2022 at 15:35

0

Browse other questions tagged or ask your own question.