Hi,
I have a use case to replace the way balances are calculated. In fact to replace _balances mapping and some logic with my own.
Found out that I couldn’t do it without modifying your code for ERC777.sol (and I guess ERC20.sol if I need that as well).
The required changes to your contract were minimal. The transferFrom function is fine because it has a _move function that I can override. Unfortunately, this isn’t the case for your _burn and _mint functions. Here’s my amendment to your contract for _burn
function _burn(
address from,
uint256 amount,
bytes memory data,
bytes memory operatorData
) internal virtual {
.
.
_beforeTokenTransfer(operator, from, address(0), amount);
_burnState(from, amount);
emit Burned(operator, from, amount, data, operatorData);
.
.
}
function _burnState(
address from,
uint256 amount
)
internal virtual {
// Update state variables
uint256 fromBalance = _balances[from];
require(fromBalance >= amount, "ERC777: burn amount exceeds balance");
unchecked {
_balances[from] = fromBalance - amount;
}
_totalSupply -= amount;
}
I did something the same for _mint.
So these minor changes, might be useful for others as well? And allow your contracts to be used without modification.
Jules