Skip to content
Verified contractExactUNLICENSED

Ktv2

Address0x0b0b9dcac89cb585fac7fc7f7a625870ac8d9693
Balance

0.002413 ETH

 

Transactions

26

 

Compiler

solc 0.8.16

 

Same code

1 more contract

 

Details

Compiler
solc 0.8.16+commit.07a7930e
Language
solidity
Optimization
Yes, 1,000,000 runs
License
UNLICENSED
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
23,293,224
Creation transaction
0x8fbcf84dfb…14efceb68b

contracts/Ktv2.sol

// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.16;

import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/interfaces/IERC20.sol";
// import "hardhat/console.sol";

interface TPI {
    function price(address poolAddr) external view returns (uint);
    function priceV2(address pairAddr) external view returns (uint256);
}

contract Ktv2 is Ownable {
    event Staked(address, uint);
    event Withdrew(address, uint);
    event Gave(address, uint);
    event Rwd(address, uint);
    event Voted(uint, address, string);

    IERC20 token;
    address public tokenAddr;
    address payable public dest;

    address public burnDest;
    address public pool;
    bool public v2;

    TPI public tp;

    mapping(address => uint) public userStks;
    mapping(address => bool) public declines;

    uint public totalStk;
    uint public totalGvn;
    uint public totalBurned;
    uint16 public maxBrnPrc = 20;
    uint16 public donationPrc = 500;
    uint16 public burnFactor = 20;

    uint16 internal constant P_FCTR = 10;
    uint32 internal constant P_DEN = 100 * P_FCTR;

    uint public startBlock;
    uint16 public epochInterval = 44444;
    uint16 public consensusReq = 1;
    uint16 public ocFee = 2 * P_FCTR;

    // block => address => cnt
    mapping(uint => mapping(address => uint16)) public blockRwd;
    // ocRwdr => block => destVoted
    mapping(address => mapping(uint => address)) public ocRwdrVote;

    mapping(address => bool) public ocRwdrs;
    mapping(address => mapping(uint => uint)) public ocFees;
    uint public tlOcFees;

    // ------------------------------------------------------------------------
    // OC Management
    uint16 public totalOC = 1;
    // newOC => vote count
    mapping(address => uint16) public addVotes;
    // existingOC => vote count
    mapping(address => uint16) public removeVotes;
    // voter => target => voted
    mapping(address => mapping(address => bool)) public hasVoted;

    // OC Management Events
    event VotedToAdd(address indexed voter, address indexed newOC, string data);
    event VotedToRemove(address indexed voter, address indexed existingOC, string data);
    event NodeAdded(address indexed newOC);
    event NodeRemoved(address indexed oldOC);

    /** -----------------------------------------------------------------------
     *
     */
    constructor(
        address _burnDest,
        address _token,
        address payable _dest,
        address _pool,
        address _ocPrcAddr,
        address _tp,
        bool _v2
    ) Ownable() {
        burnDest = _burnDest;
        tokenAddr = _token;
        token = IERC20(_token);
        dest = _dest;
        tp = TPI(_tp);
        startBlock = block.number;
        v2 = _v2;

        ocRwdrs[_ocPrcAddr] = true;
        setPool(_pool);
    }

    receive() external payable {}

    /** -----------------------------------------------------------------------
     * 
     */
    function rwd(address payable _to, uint _amt) external onlyOC notDeclined(_to) epochComplete {
        require(blockRwd[startBlock][_to] >= consensusReq, "No consensus");

        startBlock = startBlock + epochInterval;
        uint _rwd = _amt - recordOCFee();

        if(_rwd > P_DEN) {
            (bool sent, ) = _to.call{value: _rwd}("");
            require(sent, "Transfer failed");
        }

        emit Rwd(_to, _rwd);
    }

    /** -----------------------------------------------------------------------
     *
     */
    function vote(address payable _to, string calldata data) external onlyOC notDeclined(_to) epochComplete {
        require(ocRwdrVote[msg.sender][startBlock] == address(0), "Already voted");

        blockRwd[startBlock][_to]++;
        ocRwdrVote[msg.sender][startBlock] = _to;

        recordOCFee();

        emit Voted(startBlock, _to, data);
    }

    /** -----------------------------------------------------------------------
     *
     */
    function resetVote(address _to) external onlyOC notDeclined(_to) epochComplete {
        require(blockRwd[startBlock][_to] > 0, "Invalid dest");
        require(ocRwdrVote[msg.sender][startBlock] != address(0), "Vote missing");

        blockRwd[startBlock][_to]--;
        ocRwdrVote[msg.sender][startBlock] = address(0);
        resetOCFee();
    }

    /** -----------------------------------------------------------------------
     * 
     */
    function resetOCFee() private {
        tlOcFees -= ocFees[msg.sender][startBlock];
        ocFees[msg.sender][startBlock] = 0;
    }

    /** -----------------------------------------------------------------------
     * 
     */
    function recordOCFee() private returns (uint) {
        
        if(address(this).balance > tlOcFees) {
            uint amt = (((address(this).balance - tlOcFees) * ocFee) / P_DEN);
            ocFees[msg.sender][startBlock] += amt;
            tlOcFees += amt;
        }
        
        return tlOcFees;
    }

    /** -----------------------------------------------------------------------
     *
     */
    function withdrawOCFee(uint32[] calldata blocks) external {

        uint amt;
        for(uint i = 0; i < blocks.length; i++) {
            if(blocks[i] >= startBlock) continue;
            uint epochFee = ocFees[msg.sender][blocks[i]];
            tlOcFees -= epochFee;
            ocFees[msg.sender][blocks[i]] = 0;

            amt += epochFee;
        }

        if(address(this).balance >= amt) {
            (bool sent, ) = msg.sender.call{value: amt}("");
            require(sent, "Failed");
        }
    }

    /** -----------------------------------------------------------------------
     *
     */
    modifier epochComplete() {
        require(block.number > startBlock + epochInterval, "Epoch incomplete");
        _;
    }


    /** -----------------------------------------------------------------------
     *
     */
    modifier onlyOC() {
        require(ocRwdrs[msg.sender], "Not authorized");
        _;
    }

    /** -----------------------------------------------------------------------
     *
     */
    modifier notDeclined(address _to) {
        require(!declines[_to], "Declined");
        _;
    }

    /** -----------------------------------------------------------------------
     *
     */
    function setEpochInterval(uint16 interval) public onlyOwner {
        epochInterval = interval;
    }

    /** -----------------------------------------------------------------------
     *
     */
    function setConsensusReq(uint16 req) public onlyOwner {
        consensusReq = req;
    }

    /** -----------------------------------------------------------------------
     *
     */
    function setOCFee(uint16 fee) public onlyOwner {
        ocFee = fee;
    }

    /** -----------------------------------------------------------------------
     *
     */
    function addOCRwdr(address addr) public onlyOwner {
        require(!ocRwdrs[addr], 'Already set');
        ocRwdrs[addr] = true;
    }

    /** -----------------------------------------------------------------------
     *
     */
    function removeOCRwdr(address addr) public onlyOwner {
        require(ocRwdrs[addr], 'Already removed');
        ocRwdrs[addr] = false;
    }

    /** -----------------------------------------------------------------------
     *
     */
    function setDest(address addr) public onlyOwner {
        dest = payable(addr);
    }

    /** -----------------------------------------------------------------------
     *
     */
    function setMaxBurnPrc(uint16 amt) public onlyOwner {
        require(amt < 50 * P_FCTR);
        maxBrnPrc = amt;
    }

    /** -----------------------------------------------------------------------
     *
     */
    function setBurnFactor(uint16 amt) public onlyOwner {
        require(amt < 50 * P_FCTR);
        burnFactor = amt;
    }

    /** -----------------------------------------------------------------------
     *
     */
    function setDonationPrc(uint16 amt) public onlyOwner {
        require(amt <= 100 * P_FCTR);
        donationPrc = amt;
    }

    /** -----------------------------------------------------------------------
     *
     */
    function setV2(bool _v2) public onlyOwner {
        require(v2 != _v2);
        v2 = _v2;
    }

    /** -----------------------------------------------------------------------
     *
     */
    function getTokenPrice() private view returns (uint) {
        
        if (v2) {
            return tp.priceV2(pool);
        }
        else {
            return tp.price(pool);
        }
    }

    /** -----------------------------------------------------------------------
     *
     */
    function setPool(address _pool) public onlyOwner {
        pool = _pool;
        uint price = getTokenPrice();       
        require(price > 0);
    }

    /** -----------------------------------------------------------------------
     *
     */
    function decline() public {
        declines[msg.sender] = true;
    }

    /** -----------------------------------------------------------------------
     *
     */
    function allow() public {
        declines[msg.sender] = false;
    }

    /** -----------------------------------------------------------------------
     *
     */
    function voteToAdd(address newOC, string calldata data) external onlyOC {
        require(!hasVoted[msg.sender][newOC], "Already voted for this add");
        require(!ocRwdrs[newOC], "Already an OC node");
        addVotes[newOC]++;
        hasVoted[msg.sender][newOC] = true;
        emit VotedToAdd(msg.sender, newOC, data);
        
        uint16 required = (totalOC + 1) / 2;
        if (addVotes[newOC] >= required) {
            ocRwdrs[newOC] = true;
            totalOC++;
            consensusReq = (totalOC + 1) / 2;
            emit NodeAdded(newOC);
        }
    }

    /** -----------------------------------------------------------------------
     *
     */
    function voteToRemove(address existingOC, string calldata data) external onlyOC {
        require(!hasVoted[msg.sender][existingOC], "Already voted for this remove");
        require(ocRwdrs[existingOC], "Not an OC node");
        removeVotes[existingOC]++;
        hasVoted[msg.sender][existingOC] = true;
        emit VotedToRemove(msg.sender, existingOC, data);
        
        uint16 required = (totalOC + 1) / 2;
        if (removeVotes[existingOC] >= required && totalOC > 1) {
            ocRwdrs[existingOC] = false;
            totalOC--;
            consensusReq = (totalOC + 1) / 2;
            emit NodeRemoved(existingOC);
        }
    }

    /** -----------------------------------------------------------------------
     *
     */
    function resetVoteToAdd(address newOC) external onlyOC {
        require(hasVoted[msg.sender][newOC], "No add vote to reset for this target");
        addVotes[newOC]--;
        hasVoted[msg.sender][newOC] = false;
    }

    /** -----------------------------------------------------------------------
     *
     */
    function resetVoteToRemove(address existingOC) external onlyOC {
        require(hasVoted[msg.sender][existingOC], "No remove vote to reset for this target");
        removeVotes[existingOC]--;
        hasVoted[msg.sender][existingOC] = false;
    }

    /** -----------------------------------------------------------------------
     *
     */
    function stake(uint256 amt) external {
        require(amt > 0, "Amount must be greater than 0");

        userStks[msg.sender] += amt;
        totalStk += amt;

        require(token.transferFrom(msg.sender, address(this), amt), "Transfer failed");

        emit Staked(msg.sender, amt);
    }


    /** -----------------------------------------------------------------------
     *
     */
    function withdraw(uint amt) external {
        require(amt <= userStks[msg.sender] && amt <= totalStk, "Exceeds balance");

        userStks[msg.sender] -= amt;
        totalStk -= amt;

        require(token.transfer(msg.sender, amt), "Transfer failed");
        emit Withdrew(msg.sender, amt);
    }

    /** -----------------------------------------------------------------------
     * @dev Allows users to give funds to the contract.
     */
    function give() external payable {
        uint giveAmt = (msg.value * donationPrc) / P_DEN;
        totalGvn += giveAmt;

        uint tknPrice = getTokenPrice();
        uint tknAmtGvn = (msg.value * 10**18) / tknPrice;
        uint maxBrn = totalStk;

        if (totalStk > P_DEN * 10) {
            maxBrn = (totalStk * maxBrnPrc) / P_DEN;
        }

        uint burnAmt = maxBrn;

        // Mulitply by P_FCTR because burnFactor has P_FCTR built in
        if (tknAmtGvn < (maxBrn * P_FCTR) / (2*burnFactor)) {
            burnAmt = (tknAmtGvn * burnFactor) / P_FCTR;

        } else if (tknAmtGvn < (maxBrn * P_FCTR) / burnFactor) {
            burnAmt = ((tknAmtGvn * burnFactor) / (P_FCTR * 2)) + (maxBrn / 4);

        } else if (tknAmtGvn < maxBrn) {
            burnAmt = ((tknAmtGvn * burnFactor) / (P_FCTR * 4)) + (maxBrn / 2);
        }

        if (burnAmt > 0 && totalStk >= burnAmt) {
            totalStk -= burnAmt;
            totalBurned += burnAmt;
            require(token.transfer(burnDest, burnAmt), "Burn failed");
        }

        // Donate
        (bool sent, ) = dest.call{value: giveAmt}("");
        require(sent, "Donate failed");

        emit Gave(msg.sender, giveAmt);
    }

    /** -----------------------------------------------------------------------
     *
     */
    function withdrawTkn(address _to, address addr) external onlyOwner {
        require(addr != tokenAddr, "Forbidden");

        IERC20 tkn = IERC20(addr);
        require(
            tkn.transfer(_to, tkn.balanceOf(address(this))),
            "Failed"
        );
    }
}

@openzeppelin/contracts/access/Ownable.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (access/Ownable.sol)

pragma solidity ^0.8.0;

import "../utils/Context.sol";

/**
 * @dev Contract module which provides a basic access control mechanism, where
 * there is an account (an owner) that can be granted exclusive access to
 * specific functions.
 *
 * By default, the owner account will be the one that deploys the contract. This
 * can later be changed with {transferOwnership}.
 *
 * This module is used through inheritance. It will make available the modifier
 * `onlyOwner`, which can be applied to your functions to restrict their use to
 * the owner.
 */
abstract contract Ownable is Context {
    address private _owner;

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    /**
     * @dev Initializes the contract setting the deployer as the initial owner.
     */
    constructor() {
        _transferOwnership(_msgSender());
    }

    /**
     * @dev Throws if called by any account other than the owner.
     */
    modifier onlyOwner() {
        _checkOwner();
        _;
    }

    /**
     * @dev Returns the address of the current owner.
     */
    function owner() public view virtual returns (address) {
        return _owner;
    }

    /**
     * @dev Throws if the sender is not the owner.
     */
    function _checkOwner() internal view virtual {
        require(owner() == _msgSender(), "Ownable: caller is not the owner");
    }

    /**
     * @dev Leaves the contract without owner. It will not be possible to call
     * `onlyOwner` functions. Can only be called by the current owner.
     *
     * NOTE: Renouncing ownership will leave the contract without an owner,
     * thereby disabling any functionality that is only available to the owner.
     */
    function renounceOwnership() public virtual onlyOwner {
        _transferOwnership(address(0));
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual onlyOwner {
        require(newOwner != address(0), "Ownable: new owner is the zero address");
        _transferOwnership(newOwner);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Internal function without access restriction.
     */
    function _transferOwnership(address newOwner) internal virtual {
        address oldOwner = _owner;
        _owner = newOwner;
        emit OwnershipTransferred(oldOwner, newOwner);
    }
}

@openzeppelin/contracts/interfaces/IERC20.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (interfaces/IERC20.sol)

pragma solidity ^0.8.0;

import "../token/ERC20/IERC20.sol";

@openzeppelin/contracts/token/ERC20/IERC20.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/IERC20.sol)

pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC20 standard as defined in the EIP.
 */
interface IERC20 {
    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /**
     * @dev Emitted when the allowance of a `spender` for an `owner` is set by
     * a call to {approve}. `value` is the new allowance.
     */
    event Approval(address indexed owner, address indexed spender, uint256 value);

    /**
     * @dev Returns the amount of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns the amount of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);

    /**
     * @dev Moves `amount` tokens from the caller's account to `to`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address to, uint256 amount) external returns (bool);

    /**
     * @dev Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through {transferFrom}. This is
     * zero by default.
     *
     * This value changes when {approve} or {transferFrom} are called.
     */
    function allowance(address owner, address spender) external view returns (uint256);

    /**
     * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * IMPORTANT: Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * Emits an {Approval} event.
     */
    function approve(address spender, uint256 amount) external returns (bool);

    /**
     * @dev Moves `amount` tokens from `from` to `to` using the
     * allowance mechanism. `amount` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(address from, address to, uint256 amount) external returns (bool);
}

@openzeppelin/contracts/utils/Context.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.4) (utils/Context.sol)

pragma solidity ^0.8.0;

/**
 * @dev Provides information about the current execution context, including the
 * sender of the transaction and its data. While these are generally available
 * via msg.sender and msg.data, they should not be accessed in such a direct
 * manner, since when dealing with meta-transactions the account sending and
 * paying for execution may not be the actual sender (as far as an application
 * is concerned).
 *
 * This contract is only required for intermediate, library-like contracts.
 */
abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        return msg.data;
    }

    function _contextSuffixLength() internal view virtual returns (uint256) {
        return 0;
    }
}

contracts/Ktv2Factory.sol

// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.16;
import "./Ktv2.sol";

contract Ktv2Factory {
    event Created(address created);
    Ktv2[] public created;

    function create(address _burnDest,
                    address _token,
                    address payable _dest,
                    address _pool,
                    address _ocPrcAddr,
                    address _tp,
                    bool _v2) external {

        Ktv2 newKt = new Ktv2(_burnDest, _token, _dest, _pool, _ocPrcAddr, _tp, _v2);
        newKt.transferOwnership(msg.sender);

        created.push(newKt);
        emit Created(address(newKt));
    }

    function count() public view returns (uint) {
        return created.length;
    }
}
Compiler settingsShow
{
  "optimizer": {
    "runs": 1000000,
    "enabled": true
  },
  "remappings": []
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.