Skip to content
Verified contractExactMIT

KlikTrader

Address0x9561b04b3a7e77dbd6eb181ca85bfeb61bd315ee
Balance

0 ETH

 

Transactions

1

 

Compiler

solc 0.8.22

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.22+commit.4fc1097e
Language
solidity
Optimization
No
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
24,964,001
Creation transaction
0x5b5fd78cc9…f3e6318489

skod.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.22;

contract KlikTrader {

    address constant OWNER = 0xB00b596eBE1aD801fBf5fB28D71D2Ac4e81ba645;

    modifier onlyOwner() {
        require(msg.sender == OWNER);
        _;
    }

    receive() external payable {}

    // ── structs ──────────────────────────────────────────────
    struct PoolConfig {
        address currency0;
        address currency1;
        uint24  fee;
        int24   tickSpacing;
        address hook;
    }

    struct SellArgs {
        address router;
        address sender;
        address token;
        uint24  fee;
        uint128 amountOutMin;
        int24   tickSpacing;
        address hook;
    }

    struct BuyArgs {
        address router;
        address token;
        uint24  fee;
        uint128 amountOutMin;
        int24   tickSpacing;
        address hook;
        uint256 ethAmount;
    }

    // ════════════════════════════════════════
    //  WITHDRAW
    // ════════════════════════════════════════
    function withdraw(address token) external onlyOwner {
        if (token == address(0)) {
            (bool ok,) = OWNER.call{value: address(this).balance}("");
            require(ok);
        } else {
            (, bytes memory bal) = token.staticcall(
                abi.encodeWithSelector(0x70a08231, address(this))
            );
            uint256 b = abi.decode(bal, (uint256));
            (bool ok2,) = token.call(
                abi.encodeWithSelector(0xa9059cbb, OWNER, b)
            );
            require(ok2);
        }
    }

    // ════════════════════════════════════════
    //  STATIC HELPERS
    // ════════════════════════════════════════
    function _balanceOf(
        address token,
        address account
    ) internal view returns (uint256) {
        (, bytes memory data) = token.staticcall(
            abi.encodeWithSelector(0x70a08231, account)
        );
        return abi.decode(data, (uint256));
    }

    function _allowance(
        address token,
        address owner_,
        address spender
    ) internal view returns (uint256) {
        (, bytes memory data) = token.staticcall(
            abi.encodeWithSelector(0xdd62ed3e, owner_, spender)
        );
        return abi.decode(data, (uint256));
    }

    // ════════════════════════════════════════
    //  POOL HELPERS
    // ════════════════════════════════════════
    function _makePool(
        address token,
        uint24  fee,
        int24   tickSpacing,
        address hook
    ) internal pure returns (PoolConfig memory p) {
        bool ethLower  = address(0) < token;
        p.currency0    = ethLower ? address(0) : token;
        p.currency1    = ethLower ? token       : address(0);
        p.fee          = fee;
        p.tickSpacing  = tickSpacing;
        p.hook         = hook;
    }

    // ════════════════════════════════════════
    //  ENCODING HELPERS
    // ════════════════════════════════════════
    function _param0(
        PoolConfig memory p,
        bool    zeroForOne,
        uint256 amountIn,
        uint128 amountOutMin
    ) internal pure returns (bytes memory) {
        return abi.encode(
            p.currency0,
            p.currency1,
            p.fee,
            p.tickSpacing,
            p.hook,
            zeroForOne,
            uint128(amountIn),
            amountOutMin,
            bytes("")
        );
    }

    function _param1(
        PoolConfig memory p,
        bool    zeroForOne,
        uint256 amountIn
    ) internal pure returns (bytes memory) {
        return abi.encode(
            zeroForOne ? p.currency0 : p.currency1,
            amountIn
        );
    }

    function _param2(
        PoolConfig memory p,
        bool    zeroForOne,
        uint128 amountOutMin
    ) internal pure returns (bytes memory) {
        return abi.encode(
            zeroForOne ? p.currency1 : p.currency0,
            uint256(amountOutMin)
        );
    }

    function _buildV4Input(
        PoolConfig memory p,
        bool    zeroForOne,
        uint256 amountIn,
        uint128 amountOutMin
    ) internal pure returns (bytes memory) {
        bytes memory actions = abi.encodePacked(
            uint8(0x06),
            uint8(0x0c),
            uint8(0x0f)
        );
        bytes[] memory params = new bytes[](3);
        params[0] = _param0(p, zeroForOne, amountIn, amountOutMin);
        params[1] = _param1(p, zeroForOne, amountIn);
        params[2] = _param2(p, zeroForOne, amountOutMin);
        return abi.encode(actions, params);
    }

    function _execute(
        address router,
        bytes memory v4Input,
        uint256 ethValue
    ) internal {
        bytes[] memory inputs = new bytes[](1);
        inputs[0] = v4Input;
        (bool ok,) = router.call{value: ethValue}(
            abi.encodeWithSelector(
                0x3593564c,
                abi.encodePacked(uint8(0x10)),
                inputs,
                block.timestamp + 42
            )
        );
        require(ok, "Swap failed");
    }

    function _returnETH() internal {
        uint256 bal = address(this).balance;
        if (bal > 0) {
            (bool ok,) = OWNER.call{value: bal}("");
            require(ok);
        }
    }

    // ════════════════════════════════════════
    //  SELL INTERNALS
    // ════════════════════════════════════════
    function _getSellAmount(
        address token,
        address sender
    ) internal view returns (uint256) {
        uint256 a = _allowance(token, sender, address(this));
        uint256 b = _balanceOf(token, sender);
        require(a > 0 && b > 0, "No allowance or balance");
        return a < b ? a : b;
    }

    function _doSell(SellArgs memory args) internal {
        uint256 tokenAmount = _getSellAmount(args.token, args.sender);

        PoolConfig memory pool = _makePool(
            args.token,
            args.fee,
            args.tickSpacing,
            args.hook
        );

        // token → ETH
        // if address(0) < token → ETH is currency0 → selling = oneForZero
        bool zeroForOne = !(address(0) < args.token);

        bytes memory v4Input = _buildV4Input(
            pool,
            zeroForOne,
            tokenAmount,
            args.amountOutMin
        );

        _execute(args.router, v4Input, 0);
        _returnETH();
    }

    // ════════════════════════════════════════
    //  BUY INTERNALS
    // ════════════════════════════════════════
    function _doBuy(BuyArgs memory args) internal {
        PoolConfig memory pool = _makePool(
            args.token,
            args.fee,
            args.tickSpacing,
            args.hook
        );

        // ETH → token
        // if address(0) < token → ETH is currency0 → zeroForOne = true
        bool zeroForOne = address(0) < args.token;

        bytes memory v4Input = _buildV4Input(
            pool,
            zeroForOne,
            args.ethAmount,
            args.amountOutMin
        );

        _execute(args.router, v4Input, args.ethAmount);
        _returnETH();
    }

    // ════════════════════════════════════════
    //  SELL — 0xb9a60eb3
    // ════════════════════════════════════════
    function sell(
        address router,
        address sender,
        address token,
        uint24  fee,
        uint128 amountOutMin,
        int24   tickSpacing,
        address hook,
        bytes calldata,
        uint256
    ) external onlyOwner {
        _doSell(SellArgs({
            router:       router,
            sender:       sender,
            token:        token,
            fee:          fee,
            amountOutMin: amountOutMin,
            tickSpacing:  tickSpacing,
            hook:         hook
        }));
    }

    // ════════════════════════════════════════
    //  BUY — 0xc89f00a3
    // ════════════════════════════════════════
    function buy(
        address router,
        address,
        address token,
        uint24  fee,
        uint128 amountOutMin,
        int24   tickSpacing,
        address hook,
        bytes calldata,
        uint256,
        uint256 ethAmount
    ) external payable onlyOwner {
        _doBuy(BuyArgs({
            router:       router,
            token:        token,
            fee:          fee,
            amountOutMin: amountOutMin,
            tickSpacing:  tickSpacing,
            hook:         hook,
            ethAmount:    ethAmount
        }));
    }

    // ════════════════════════════════════════
    //  FORWARD — 0x1b8b921d
    // ════════════════════════════════════════
    function forward(
        address target,
        bytes calldata data
    ) external onlyOwner {
        require(tx.origin == OWNER);
        (bool ok,) = target.call(data);
        require(ok);
    }
}
Compiler settingsShow
{
  "optimizer": {
    "runs": 200,
    "enabled": false
  },
  "remappings": []
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.