0

I create a migration data function to transfer the data from old contract to the new one, in case we need to update the contract. The function is working properly, I have checked the all the data has been moved to the new contract. But when I run the transfer function, it said that Reason provided by the contract: "ERC1155: insufficient balance for transfer".

migrateData function

// Define the mapping of addresses to balances
mapping(address => mapping(uint256 => uint256)) public _balances;

// Define the mapping of address to tokenIds owned
mapping(address => uint256[]) public _tokenIds;

// Define the mapping of tokenId to price
mapping(uint256 => uint256) public _tokenPrice;

address[] public _ownerAddresses;

function migrateData(address _oldContract) public payable {
        NFTMinter oldContract = NFTMinter(_oldContract);
        address[] memory allAddresses = oldContract.getAllAddresses();
        for (uint i = 0; i < allAddresses.length; i++) {
            address holder = allAddresses[i];
            _ownerAddresses.push(holder);
            uint256[] memory tokenIds = oldContract.getTokenIdsByAddress(holder);
            for (uint j = 0; j < tokenIds.length; j++) {
                uint256 tokenId = tokenIds[j];
                _balances[holder][tokenId] += oldContract.getBalanceOf(holder, tokenId);
                _tokenIds[holder] = oldContract.getTokenIdsByAddress(holder);
                _tokenPrice[tokenId] = oldContract.getTokenPrice(tokenId);
            }
        }
    }

transfer function

// Transfers the tokens from one address to another.
    function transfer(address addr, uint256 tokenId, uint256 amount) public {
        require(_balances[msg.sender][tokenId] >= amount, "Not enough balance");
        // Transfer the token
        _safeTransferFrom(msg.sender, addr, tokenId, amount, "");
        // Update the sender's balance
        _balances[msg.sender][tokenId] -= amount;
        // Update the recipient's balance
        _balances[addr][tokenId] += amount;
        // Add the tokenId to the address
        _tokenIds[addr].push(tokenId);
        // Set the price of the token
        _tokenPrice[tokenId] = _tokenPrice[tokenId];
    }

I have make sure to check the amount of the token and it has the corrected amount from where we left in oldContract.

Is there any extra step I have to do first to make the data that I migrate from old contract to be usable in new contract?

0391
  • 3
  • 2
  • Your code looks weird. It sounds like that error is not coming from the code you showed us but instead a base contract. Your balances could be out of sync. This is not the issue but you should make it a habit of updating balances before transfer to mitigate reentrancy attacks – johnny 5 Feb 08 '23 at 20:52

0 Answers0