I’m making a simple contract and trying to improve myself but I need help.
In short, what I want to do is take the average of the last 20 transfers and compare it with the smart contract without making a transfer. not allowing the transfer if it is less than the average of the last 20 transfers.
I also want to return the transfer records as public as an array. please review the codes and help me.
function transfer(address recipient, uint256 amount) public override returns (bool) {
require(amount <= _getMid(), "Require the amount must be less than mid");
_transfer(_msgSender(), recipient, amount);
_allAmountArray.push(amount);
return true;
}
function transferFrom(address sender, address recipient, uint256 amount) public override returns (bool) {
require(amount <= _getMid(), "Require the amount must be less than mid");
_transfer(sender, recipient, amount);
_allAmountArray.push(amount);
return true;
}
function _getMid() public view returns(uint256, uint256){
(uint256 mid, ) = findAmountAverage(_allAmountArray);
return mid;
}
function findAmountAverage(uint256[] storage array) internal view returns (uint256, uint256) {
if (array.length == 0 || _low == 0) {
return (0, 0);
}
uint256 high = array.length;
uint256 low = array.length.sub(_low);
uint256 totalAmount = 0;
uint256 mid = 0;
while (low < high) {
uint256 lowAmount = array[low];
totalAmount = totalAmount.add(lowAmount);
low += 1;
}
mid = totalAmount.div(_low);
return (mid, totalAmount);
}
function _getMid() public view returns(uint256, uint256){
(uint256 mid, uint256 tAmountAvarage) = findAmountAverage(_allAmountArray);
return (mid, tAmountAvarage);
}
function _getAllAmountArray(_allAmountArray[] memory) public view returns(uint256) {
return _allAmountArray;
}