I found something that I feel is a bug, but may not be so I thought I would post here. I was working on a contract for an erc-20 token where I have to transfer ownership to a new owner after airdrop. I realized after I transfer ownership with transferOwnership() that any functions in the contract that use owner() to get the current owner still only retrieve the deployer and not the new owner. In this case I was testing a tax and it continued to tax the new owner and sending money to the deployer address rather than new owner. I am curious if this is a glitch or making sure to use another method rather than owner().
Code to reproduce
//exclude owner and this contract from fee
_isExcludedFromFee[owner()] = true;
_isExcludedFromFee[address(uniswapV2Router)] = true;
_isExcludedFromFee[feeReceiver] = true;
_isExcludedFromFee[address(this)] = true;
}
function _transfer(address from, address to, uint256 amount) internal override {
require(from != address(0), "ERC20: transfer from the zero address");
require(to != address(0), "ERC20: transfer to the zero address");
require(balanceOf(from) >= amount, "ERC20: transfer amount exceeds balance");
require(!_isRestrictedlisted[from] && !_isRestrictedlisted[to], "This address is Restricted");
if ((from != owner() || from != address(this)) && to != uniswapV2Pair){
require(balanceOf(to)+(amount) <= maxWalletBalance,"Balance is exceeding maxWalletBalance");
require(amount <= maxTxAmount,"Transfer amount exceeds the maxTxAmount.");
}
uint256 contractLiquidityBalance = balanceOf(address(this));
if ((from == uniswapV2Pair || to == uniswapV2Pair) && !inSwapAndLiquify) {
if (from != uniswapV2Pair ) {
bool overMinTokenBalance = contractLiquidityBalance >= numTokensSellToAddToLiquidity;
if (overMinTokenBalance && swapAndLiquifyEnabled) {
_swapAndLiquify(numTokensSellToAddToLiquidity);
}
}
}
uint256 transferAmount;
if (_isExcludedFromFee[from] || _isExcludedFromFee[to]) {
transferAmount = amount;
super._transfer(from, to, amount);
}
else{
if(from == uniswapV2Pair){ //buy
uint256 buyTax = ((amount * buytax) / 10000);
transferAmount = amount - buyTax;
super._transfer(from, address(this), buyTax);
taxFees = contractLiquidityBalance + buyTax;
super._transfer(from, to, transferAmount);
}else if(to == uniswapV2Pair){ //sell
uint256 sellTax = ((amount * selltax) / 10000);
transferAmount = amount - sellTax;
super._transfer(from, address(this), sellTax);
taxFees = contractLiquidityBalance + sellTax;
super._transfer(from, to, transferAmount);
} else{
super._transfer(from, to, amount);
}
}
}
function excludeFromFee(address account, bool status) public onlyOwner {
_isExcludedFromFee[account] = status;
}
function transferOwnership(address newOwner) public override onlyOwner{
require(newOwner != address(0), "Ownable: new owner is the zero address");
super._transfer(owner(), newOwner, balanceOf(owner()));
_transferOwnership(newOwner);
}
function _swapAndLiquify(uint256 contractTokenBalance) private lockTheSwap {
uint256 half = (contractTokenBalance / 2);
uint256 otherHalf = (contractTokenBalance - half);
uint256 initialBalance = address(this).balance;
_swapTokensForETH(half);
uint256 newBalance = (address(this).balance / 3 - initialBalance);
uint256 transferBalance = (address(this).balance - newBalance);
if(otherHalf > 0 && newBalance > 0){
_addLiquidity(otherHalf, newBalance);
}
transferToAddressETH(payable(feeReceiver), transferBalance);
emit SwapAndLiquify(half, newBalance, otherHalf);
}
function _swapTokensForETH(uint256 tokenAmount) private lockTheSwap {
address[] memory path = new address[](2);
path[0] = address(this);
path[1] = uniswapV2Router.WETH();
_approve(address(this), address(uniswapV2Router), tokenAmount);
uniswapV2Router.swapExactTokensForETHSupportingFeeOnTransferTokens(
tokenAmount,
0,
path,
address(this),
block.timestamp
);
}
function _addLiquidity(uint256 tokenAmount, uint256 ethAmount) private lockTheSwap {
_approve(address(this), address(uniswapV2Router), tokenAmount);
uniswapV2Router.addLiquidityETH{value: ethAmount}(
address(this),
tokenAmount,
0,
0,
owner(),
block.timestamp
);
}.
Environment
Remix solidity 0.8.17, and Hardhat in VS Code (current version)