Contract 0xfb1Fc21D2937bF5a49D480189e7FEd42bF8282aD 1

 
Txn Hash Method
Index
From
To
Value
0xa11564e3bda8c4d4b85822c67740603ca8c4d7f2a0a81e35cf32fd7748a7f8fe0x60a06040197027122022-08-19 3:27:0542 days 19 hrs ago0x725bd51dccd08272c63efdc1a5112002da0c5540 IN  Create: VelodromeLibrary0 Ether0.0005400534210.001
[ Download CSV Export 
Parent Txn Hash Block From To Value
Loading

Contract Source Code Verified (Exact Match)

Contract Name:
VelodromeLibrary

Compiler Version
v0.8.13+commit.abaa5c0e

Optimization Enabled:
Yes with 200 runs

Other Settings:
default evmVersion
File 1 of 3 : VelodromeLibrary.sol
// SPDX-License-Identifier: MIT

pragma solidity 0.8.13;

import "contracts/interfaces/IPair.sol";
import "contracts/interfaces/IRouter.sol";

contract VelodromeLibrary {
    IRouter internal immutable router;

    constructor(address _router) {
        router = IRouter(_router);
    }

    function _f(uint x0, uint y) internal pure returns (uint) {
        return x0*(y*y/1e18*y/1e18)/1e18+(x0*x0/1e18*x0/1e18)*y/1e18;
    }

    function _d(uint x0, uint y) internal pure returns (uint) {
        return 3*x0*(y*y/1e18)/1e18+(x0*x0/1e18*x0/1e18);
    }

    function _get_y(uint x0, uint xy, uint y) internal pure returns (uint) {
        for (uint i = 0; i < 255; i++) {
            uint y_prev = y;
            uint k = _f(x0, y);
            if (k < xy) {
                uint dy = (xy - k)*1e18/_d(x0, y);
                y = y + dy;
            } else {
                uint dy = (k - xy)*1e18/_d(x0, y);
                y = y - dy;
            }
            if (y > y_prev) {
                if (y - y_prev <= 1) {
                    return y;
                }
            } else {
                if (y_prev - y <= 1) {
                    return y;
                }
            }
        }
        return y;
    }

    function getTradeDiff(uint amountIn, address tokenIn, address tokenOut, bool stable) external view returns (uint a, uint b) {
        (uint dec0, uint dec1, uint r0, uint r1, bool st, address t0,) = IPair(router.pairFor(tokenIn, tokenOut, stable)).metadata();
        uint sample = tokenIn == t0 ? r0*dec1/r1 : r1*dec0/r0;
        a = _getAmountOut(sample, tokenIn, r0, r1, t0, dec0, dec1, st) * 1e18 / sample;
        b = _getAmountOut(amountIn, tokenIn, r0, r1, t0, dec0, dec1, st) * 1e18 / amountIn;
    }

    function getTradeDiff(uint amountIn, address tokenIn, address pair) external view returns (uint a, uint b) {
        (uint dec0, uint dec1, uint r0, uint r1, bool st, address t0,) = IPair(pair).metadata();
        uint sample = tokenIn == t0 ? r0*dec1/r1 : r1*dec0/r0;
        a = _getAmountOut(sample, tokenIn, r0, r1, t0, dec0, dec1, st) * 1e18 / sample;
        b = _getAmountOut(amountIn, tokenIn, r0, r1, t0, dec0, dec1, st) * 1e18 / amountIn;
    }

    function getSample(address tokenIn, address tokenOut, bool stable) external view returns (uint) {
        (uint dec0, uint dec1, uint r0, uint r1, bool st, address t0,) = IPair(router.pairFor(tokenIn, tokenOut, stable)).metadata();
        uint sample = tokenIn == t0 ? r0*dec1/r1 : r1*dec0/r0;
        return _getAmountOut(sample, tokenIn, r0, r1, t0, dec0, dec1, st) * 1e18 / sample;
    }

    function getMinimumValue(address tokenIn, address tokenOut, bool stable) external view returns (uint, uint, uint) {
        (uint dec0, uint dec1, uint r0, uint r1,, address t0,) = IPair(router.pairFor(tokenIn, tokenOut, stable)).metadata();
        uint sample = tokenIn == t0 ? r0*dec1/r1 : r1*dec0/r0;
        return (sample, r0, r1);
    }

    function getAmountOut(uint amountIn, address tokenIn, address tokenOut, bool stable) external view returns (uint) {
        (uint dec0, uint dec1, uint r0, uint r1, bool st, address t0,) = IPair(router.pairFor(tokenIn, tokenOut, stable)).metadata();
        return _getAmountOut(amountIn, tokenIn, r0, r1, t0, dec0, dec1, st) * 1e18 / amountIn;
    }

    function _getAmountOut(uint amountIn, address tokenIn, uint _reserve0, uint _reserve1, address token0, uint decimals0, uint decimals1, bool stable) internal pure returns (uint) {
        if (stable) {
            uint xy =  _k(_reserve0, _reserve1, stable, decimals0, decimals1);
            _reserve0 = _reserve0 * 1e18 / decimals0;
            _reserve1 = _reserve1 * 1e18 / decimals1;
            (uint reserveA, uint reserveB) = tokenIn == token0 ? (_reserve0, _reserve1) : (_reserve1, _reserve0);
            amountIn = tokenIn == token0 ? amountIn * 1e18 / decimals0 : amountIn * 1e18 / decimals1;
            uint y = reserveB - _get_y(amountIn+reserveA, xy, reserveB);
            return y * (tokenIn == token0 ? decimals1 : decimals0) / 1e18;
        } else {
            (uint reserveA, uint reserveB) = tokenIn == token0 ? (_reserve0, _reserve1) : (_reserve1, _reserve0);
            return amountIn * reserveB / (reserveA + amountIn);
        }
    }

    function _k(uint x, uint y, bool stable, uint decimals0, uint decimals1) internal pure returns (uint) {
        if (stable) {
            uint _x = x * 1e18 / decimals0;
            uint _y = y * 1e18 / decimals1;
            uint _a = (_x * _y) / 1e18;
            uint _b = ((_x * _x) / 1e18 + (_y * _y) / 1e18);
            return _a * _b / 1e18;  // x3y+y3x >= k
        } else {
            return x * y; // xy >= k
        }
    }
    
}

File 2 of 3 : IPair.sol
pragma solidity 0.8.13;

interface IPair {
    function metadata() external view returns (uint dec0, uint dec1, uint r0, uint r1, bool st, address t0, address t1);
    function claimFees() external returns (uint, uint);
    function tokens() external returns (address, address);
    function transferFrom(address src, address dst, uint amount) external returns (bool);
    function permit(address owner, address spender, uint value, uint deadline, uint8 v, bytes32 r, bytes32 s) external;
    function swap(uint amount0Out, uint amount1Out, address to, bytes calldata data) external;
    function burn(address to) external returns (uint amount0, uint amount1);
    function mint(address to) external returns (uint liquidity);
    function getReserves() external view returns (uint _reserve0, uint _reserve1, uint _blockTimestampLast);
    function getAmountOut(uint, address) external view returns (uint);
}

File 3 of 3 : IRouter.sol
pragma solidity 0.8.13;

interface IRouter {
    function pairFor(address tokenA, address tokenB, bool stable) external view returns (address pair);
}

Settings
{
  "remappings": [
    "@ensdomains/=node_modules/@ensdomains/",
    "LayerZero/=lib/LayerZero/contracts/",
    "contracts/=contracts/",
    "ds-test/=lib/ds-test/src/",
    "forge-std/=lib/forge-std/src/",
    "hardhat/=node_modules/hardhat/",
    "openzeppelin-contracts/=lib/openzeppelin-contracts/",
    "script/=script/",
    "solmate/=lib/solmate/src/",
    "test/=test/",
    "utils/=test/utils/",
    "contracts/=contracts/",
    "test/=test/",
    "script/=script/"
  ],
  "optimizer": {
    "enabled": true,
    "runs": 200
  },
  "metadata": {
    "bytecodeHash": "ipfs"
  },
  "outputSelection": {
    "*": {
      "*": [
        "evm.bytecode",
        "evm.deployedBytecode",
        "devdoc",
        "userdoc",
        "metadata",
        "abi"
      ]
    }
  },
  "evmVersion": "london",
  "libraries": {}
}

Contract Security Audit

Contract ABI

[{"inputs":[{"internalType":"address","name":"_router","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"address","name":"tokenIn","type":"address"},{"internalType":"address","name":"tokenOut","type":"address"},{"internalType":"bool","name":"stable","type":"bool"}],"name":"getAmountOut","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"tokenIn","type":"address"},{"internalType":"address","name":"tokenOut","type":"address"},{"internalType":"bool","name":"stable","type":"bool"}],"name":"getMinimumValue","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"tokenIn","type":"address"},{"internalType":"address","name":"tokenOut","type":"address"},{"internalType":"bool","name":"stable","type":"bool"}],"name":"getSample","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"address","name":"tokenIn","type":"address"},{"internalType":"address","name":"tokenOut","type":"address"},{"internalType":"bool","name":"stable","type":"bool"}],"name":"getTradeDiff","outputs":[{"internalType":"uint256","name":"a","type":"uint256"},{"internalType":"uint256","name":"b","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"address","name":"tokenIn","type":"address"},{"internalType":"address","name":"pair","type":"address"}],"name":"getTradeDiff","outputs":[{"internalType":"uint256","name":"a","type":"uint256"},{"internalType":"uint256","name":"b","type":"uint256"}],"stateMutability":"view","type":"function"}]

60a060405234801561001057600080fd5b5060405161100238038061100283398101604081905261002f91610040565b6001600160a01b0316608052610070565b60006020828403121561005257600080fd5b81516001600160a01b038116811461006957600080fd5b9392505050565b608051610f626100a06000396000818161010c015281816102730152818161043701526105cb0152610f626000f3fe608060405234801561001057600080fd5b50600436106100575760003560e01c8063525007331461005c578063865db5e21461008f578063be3b428e146100b7578063da214231146100d8578063ec323c00146100eb575b600080fd5b61006f61006a366004610d03565b6100fe565b604080519384526020840192909252908201526060015b60405180910390f35b6100a261009d366004610d4e565b610265565b60408051928352602083019190915201610086565b6100ca6100c5366004610d03565b61042a565b604051908152602001610086565b6100ca6100e6366004610d4e565b6105be565b6100a26100f9366004610da1565b610704565b6000806000806000806000807f00000000000000000000000000000000000000000000000000000000000000006001600160a01b0316634c1ee03e8c8c8c6040518463ffffffff1660e01b815260040161015a93929190610dd8565b602060405180830381865afa158015610177573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061019b9190610dfc565b6001600160a01b031663392f37e96040518163ffffffff1660e01b815260040160e060405180830381865afa1580156101d8573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906101fc9190610e19565b5095505094509450945094506000816001600160a01b03168c6001600160a01b03161461023d578361022e8785610ea3565b6102389190610ec2565b610252565b826102488686610ea3565b6102529190610ec2565b9c939b5091995091975050505050505050565b6000806000806000806000807f00000000000000000000000000000000000000000000000000000000000000006001600160a01b0316634c1ee03e8c8c8c6040518463ffffffff1660e01b81526004016102c193929190610dd8565b602060405180830381865afa1580156102de573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906103029190610dfc565b6001600160a01b031663392f37e96040518163ffffffff1660e01b815260040160e060405180830381865afa15801561033f573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906103639190610e19565b509550955095509550955095506000816001600160a01b03168c6001600160a01b0316146103a557846103968886610ea3565b6103a09190610ec2565b6103ba565b836103b08787610ea3565b6103ba9190610ec2565b9050806103cd828e8888878d8d8b610838565b6103df90670de0b6b3a7640000610ea3565b6103e99190610ec2565b98508c6103fc8e8e8888878d8d8b610838565b61040e90670de0b6b3a7640000610ea3565b6104189190610ec2565b97505050505050505094509492505050565b60008060008060008060007f00000000000000000000000000000000000000000000000000000000000000006001600160a01b0316634c1ee03e8b8b8b6040518463ffffffff1660e01b815260040161048593929190610dd8565b602060405180830381865afa1580156104a2573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906104c69190610dfc565b6001600160a01b031663392f37e96040518163ffffffff1660e01b815260040160e060405180830381865afa158015610503573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906105279190610e19565b509550955095509550955095506000816001600160a01b03168b6001600160a01b031614610569578461055a8886610ea3565b6105649190610ec2565b61057e565b836105748787610ea3565b61057e9190610ec2565b905080610591828d8888878d8d8b610838565b6105a390670de0b6b3a7640000610ea3565b6105ad9190610ec2565b9750505050505050505b9392505050565b60008060008060008060007f00000000000000000000000000000000000000000000000000000000000000006001600160a01b0316634c1ee03e8b8b8b6040518463ffffffff1660e01b815260040161061993929190610dd8565b602060405180830381865afa158015610636573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061065a9190610dfc565b6001600160a01b031663392f37e96040518163ffffffff1660e01b815260040160e060405180830381865afa158015610697573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906106bb9190610e19565b509550955095509550955095508a6106d98c8c8787868c8c8a610838565b6106eb90670de0b6b3a7640000610ea3565b6106f59190610ec2565b9b9a5050505050505050505050565b600080600080600080600080886001600160a01b031663392f37e96040518163ffffffff1660e01b815260040160e060405180830381865afa15801561074e573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906107729190610e19565b509550955095509550955095506000816001600160a01b03168b6001600160a01b0316146107b457846107a58886610ea3565b6107af9190610ec2565b6107c9565b836107bf8787610ea3565b6107c99190610ec2565b9050806107dc828d8888878d8d8b610838565b6107ee90670de0b6b3a7640000610ea3565b6107f89190610ec2565b98508b61080b8d8d8888878d8d8b610838565b61081d90670de0b6b3a7640000610ea3565b6108279190610ec2565b975050505050505050935093915050565b6000811561097e57600061084f88888588886109d8565b90508461086489670de0b6b3a7640000610ea3565b61086e9190610ec2565b97508361088388670de0b6b3a7640000610ea3565b61088d9190610ec2565b9650600080876001600160a01b03168b6001600160a01b0316146108b257888a6108b5565b89895b91509150876001600160a01b03168b6001600160a01b0316146108f457856108e58d670de0b6b3a7640000610ea3565b6108ef9190610ec2565b610911565b866109078d670de0b6b3a7640000610ea3565b6109119190610ec2565b9b506000610929610922848f610ee4565b8584610ac9565b6109339083610efc565b9050670de0b6b3a7640000896001600160a01b03168d6001600160a01b03161461095d578861095f565b875b6109699083610ea3565b6109739190610ec2565b9450505050506109cc565b600080866001600160a01b03168a6001600160a01b0316146109a15787896109a4565b88885b90925090506109b38b83610ee4565b6109bd828d610ea3565b6109c79190610ec2565b925050505b98975050505050505050565b60008315610ab3576000836109f588670de0b6b3a7640000610ea3565b6109ff9190610ec2565b9050600083610a1688670de0b6b3a7640000610ea3565b610a209190610ec2565b90506000670de0b6b3a7640000610a378385610ea3565b610a419190610ec2565b90506000670de0b6b3a7640000610a588480610ea3565b610a629190610ec2565b670de0b6b3a7640000610a758680610ea3565b610a7f9190610ec2565b610a899190610ee4565b9050670de0b6b3a7640000610a9e8284610ea3565b610aa89190610ec2565b945050505050610ac0565b610abd8587610ea3565b90505b95945050505050565b6000805b60ff811015610bcf57826000610ae38783610bd8565b905085811015610b33576000610af98887610c75565b610b038389610efc565b610b1590670de0b6b3a7640000610ea3565b610b1f9190610ec2565b9050610b2b8187610ee4565b955050610b75565b6000610b3f8887610c75565b610b498884610efc565b610b5b90670de0b6b3a7640000610ea3565b610b659190610ec2565b9050610b718187610efc565b9550505b81851115610b9e576001610b898387610efc565b11610b99578493505050506105b7565b610bba565b6001610baa8684610efc565b11610bba578493505050506105b7565b50508080610bc790610f13565b915050610acd565b50909392505050565b6000670de0b6b3a764000082818581610bf18280610ea3565b610bfb9190610ec2565b610c059190610ea3565b610c0f9190610ec2565b610c199190610ea3565b610c239190610ec2565b670de0b6b3a7640000808481610c398280610ea3565b610c439190610ec2565b610c4d9190610ea3565b610c579190610ec2565b610c619086610ea3565b610c6b9190610ec2565b6105b79190610ee4565b6000670de0b6b3a76400008381610c8c8280610ea3565b610c969190610ec2565b610ca09190610ea3565b610caa9190610ec2565b670de0b6b3a764000080610cbe8580610ea3565b610cc89190610ec2565b610cd3866003610ea3565b610c619190610ea3565b6001600160a01b0381168114610cf257600080fd5b50565b8015158114610cf257600080fd5b600080600060608486031215610d1857600080fd5b8335610d2381610cdd565b92506020840135610d3381610cdd565b91506040840135610d4381610cf5565b809150509250925092565b60008060008060808587031215610d6457600080fd5b843593506020850135610d7681610cdd565b92506040850135610d8681610cdd565b91506060850135610d9681610cf5565b939692955090935050565b600080600060608486031215610db657600080fd5b833592506020840135610dc881610cdd565b91506040840135610d4381610cdd565b6001600160a01b039384168152919092166020820152901515604082015260600190565b600060208284031215610e0e57600080fd5b81516105b781610cdd565b600080600080600080600060e0888a031215610e3457600080fd5b875196506020880151955060408801519450606088015193506080880151610e5b81610cf5565b60a0890151909350610e6c81610cdd565b60c0890151909250610e7d81610cdd565b8091505092959891949750929550565b634e487b7160e01b600052601160045260246000fd5b6000816000190483118215151615610ebd57610ebd610e8d565b500290565b600082610edf57634e487b7160e01b600052601260045260246000fd5b500490565b60008219821115610ef757610ef7610e8d565b500190565b600082821015610f0e57610f0e610e8d565b500390565b600060018201610f2557610f25610e8d565b506001019056fea26469706673582212204ae8b2e06c6bbdf0afe0e7befd071409b85f463bf66b94fbdd6d5a96723cade964736f6c634300080d00330000000000000000000000009c12939390052919af3155f41bf4160fd3666a6f

Constructor Arguments (ABI-Encoded and is the last bytes of the Contract Creation Code above)

0000000000000000000000009c12939390052919af3155f41bf4160fd3666a6f

-----Decoded View---------------
Arg [0] : _router (address): 0x9c12939390052919af3155f41bf4160fd3666a6f

-----Encoded View---------------
1 Constructor Arguments found :
Arg [0] : 0000000000000000000000009c12939390052919af3155f41bf4160fd3666a6f


Block Transaction Difficulty Gas Used Reward
Block Uncle Number Difficulty Gas Used Reward
Loading
Loading
Make sure to use the "Vote Down" button for any spammy posts, and the "Vote Up" for interesting conversations.