Source Code
Overview
ETH Balance
0 ETH
More Info
ContractCreator
Multi Chain
Multichain Addresses
N/ALoading...
Loading
Similar Match Source Code This contract matches the deployed Bytecode of the Source Code for Contract 0x0063Fc...1a95B37a The constructor portion of the code might be different and could alter the actual behaviour of the contract
Contract Name:
XykCurve
Compiler Version
v0.8.20+commit.a1b79de6
Optimization Enabled:
Yes with 1000000 runs
Other Settings:
default evmVersion
Contract Source Code (Solidity Standard Json-Input format)
// SPDX-License-Identifier: AGPL-3.0 pragma solidity ^0.8.0; import {FixedPointMathLib} from "solmate/utils/FixedPointMathLib.sol"; import {ICurve} from "./ICurve.sol"; import {CurveErrorCodes} from "./CurveErrorCodes.sol"; /** * @author 0xacedia * @notice Bonding curve logic for an x*y=k curve using virtual reserves. * @dev The virtual token reserve is stored in `spotPrice` and the virtual nft reserve is stored in `delta`. * An LP can modify the virtual reserves by changing the `spotPrice` (tokens) or `delta` (nfts). */ contract XykCurve is ICurve, CurveErrorCodes { using FixedPointMathLib for uint256; /** * @dev See {ICurve-validateDelta} */ function validateDelta(uint128 /*delta*/ ) external pure override returns (bool) { // all values are valid return true; } /** * @dev See {ICurve-validateSpotPrice} */ function validateSpotPrice(uint128 /*newSpotPrice*/ ) external pure override returns (bool) { // all values are valid return true; } /** * @dev See {ICurve-getBuyInfo} */ function getBuyInfo( uint128 spotPrice, uint128 delta, uint256 numItems, uint256 feeMultiplier, uint256 protocolFeeMultiplier ) external pure override returns ( Error error, uint128 newSpotPrice, uint128 newDelta, uint256 inputValue, uint256 tradeFee, uint256 protocolFee ) { if (numItems == 0) { return (Error.INVALID_NUMITEMS, 0, 0, 0, 0, 0); } // Get the pair's virtual nft and token reserves uint256 nftBalance = delta; // If numItems is too large, we will get a divide by zero error if (numItems >= nftBalance) { return (Error.INVALID_NUMITEMS, 0, 0, 0, 0, 0); } // Calculate new nft balance uint256 newNftBalance; unchecked { newNftBalance = nftBalance - numItems; } // Calculate the amount to send in. spotPrice is the virtual reserve. uint256 inputValueWithoutFee = (numItems * spotPrice) / newNftBalance; // Add the fees to the amount to send in protocolFee = inputValueWithoutFee.mulWadUp(protocolFeeMultiplier); tradeFee = inputValueWithoutFee.mulWadUp(feeMultiplier); inputValue = inputValueWithoutFee + tradeFee + protocolFee; // Set the new virtual reserves uint256 newSpotPrice_ = spotPrice + inputValueWithoutFee; if (newSpotPrice_ > type(uint128).max) { return (Error.SPOT_PRICE_OVERFLOW, 0, 0, 0, 0, 0); } newSpotPrice = uint128(newSpotPrice_); // token reserve newDelta = uint128(newNftBalance); // nft reserve // If we got all the way here, no math errors happened error = Error.OK; } /** * @dev See {ICurve-getSellInfo} */ function getSellInfo( uint128 spotPrice, uint128 delta, uint256 numItems, uint256 feeMultiplier, uint256 protocolFeeMultiplier ) external pure override returns ( Error error, uint128 newSpotPrice, uint128 newDelta, uint256 outputValue, uint256 tradeFee, uint256 protocolFee ) { if (numItems == 0) { return (Error.INVALID_NUMITEMS, 0, 0, 0, 0, 0); } // Get the pair's virtual nft and eth/erc20 balance uint256 tokenBalance = spotPrice; uint256 nftBalance = delta; // Return early if new nft balance is too high uint256 newBalance = nftBalance + numItems; if (newBalance > type(uint128).max) { return (Error.DELTA_OVERFLOW, 0, 0, 0, 0, 0); } // Calculate the amount to send out uint256 outputValueWithoutFee = (numItems * tokenBalance) / newBalance; // Subtract fees from amount to send out protocolFee = outputValueWithoutFee.mulWadUp(protocolFeeMultiplier); tradeFee = outputValueWithoutFee.mulWadUp(feeMultiplier); outputValue = outputValueWithoutFee - tradeFee - protocolFee; // Set new nft balance newDelta = uint128(newBalance); // Set the new virtual reserves newSpotPrice = uint128(spotPrice - outputValueWithoutFee); // token reserve // If we got all the way here, no math errors happened error = Error.OK; } }
// SPDX-License-Identifier: AGPL-3.0-only pragma solidity >=0.8.0; /// @notice Arithmetic library with operations for fixed-point numbers. /// @author Solmate (https://github.com/transmissions11/solmate/blob/main/src/utils/FixedPointMathLib.sol) /// @author Inspired by USM (https://github.com/usmfum/USM/blob/master/contracts/WadMath.sol) library FixedPointMathLib { /*////////////////////////////////////////////////////////////// SIMPLIFIED FIXED POINT OPERATIONS //////////////////////////////////////////////////////////////*/ uint256 internal constant MAX_UINT256 = 2**256 - 1; uint256 internal constant WAD = 1e18; // The scalar of ETH and most ERC20s. function mulWadDown(uint256 x, uint256 y) internal pure returns (uint256) { return mulDivDown(x, y, WAD); // Equivalent to (x * y) / WAD rounded down. } function mulWadUp(uint256 x, uint256 y) internal pure returns (uint256) { return mulDivUp(x, y, WAD); // Equivalent to (x * y) / WAD rounded up. } function divWadDown(uint256 x, uint256 y) internal pure returns (uint256) { return mulDivDown(x, WAD, y); // Equivalent to (x * WAD) / y rounded down. } function divWadUp(uint256 x, uint256 y) internal pure returns (uint256) { return mulDivUp(x, WAD, y); // Equivalent to (x * WAD) / y rounded up. } /*////////////////////////////////////////////////////////////// LOW LEVEL FIXED POINT OPERATIONS //////////////////////////////////////////////////////////////*/ function mulDivDown( uint256 x, uint256 y, uint256 denominator ) internal pure returns (uint256 z) { /// @solidity memory-safe-assembly assembly { // Equivalent to require(denominator != 0 && (y == 0 || x <= type(uint256).max / y)) if iszero(mul(denominator, iszero(mul(y, gt(x, div(MAX_UINT256, y)))))) { revert(0, 0) } // Divide x * y by the denominator. z := div(mul(x, y), denominator) } } function mulDivUp( uint256 x, uint256 y, uint256 denominator ) internal pure returns (uint256 z) { /// @solidity memory-safe-assembly assembly { // Equivalent to require(denominator != 0 && (y == 0 || x <= type(uint256).max / y)) if iszero(mul(denominator, iszero(mul(y, gt(x, div(MAX_UINT256, y)))))) { revert(0, 0) } // If x * y modulo the denominator is strictly greater than 0, // 1 is added to round up the division of x * y by the denominator. z := add(gt(mod(mul(x, y), denominator), 0), div(mul(x, y), denominator)) } } function rpow( uint256 x, uint256 n, uint256 scalar ) internal pure returns (uint256 z) { /// @solidity memory-safe-assembly assembly { switch x case 0 { switch n case 0 { // 0 ** 0 = 1 z := scalar } default { // 0 ** n = 0 z := 0 } } default { switch mod(n, 2) case 0 { // If n is even, store scalar in z for now. z := scalar } default { // If n is odd, store x in z for now. z := x } // Shifting right by 1 is like dividing by 2. let half := shr(1, scalar) for { // Shift n right by 1 before looping to halve it. n := shr(1, n) } n { // Shift n right by 1 each iteration to halve it. n := shr(1, n) } { // Revert immediately if x ** 2 would overflow. // Equivalent to iszero(eq(div(xx, x), x)) here. if shr(128, x) { revert(0, 0) } // Store x squared. let xx := mul(x, x) // Round to the nearest number. let xxRound := add(xx, half) // Revert if xx + half overflowed. if lt(xxRound, xx) { revert(0, 0) } // Set x to scaled xxRound. x := div(xxRound, scalar) // If n is even: if mod(n, 2) { // Compute z * x. let zx := mul(z, x) // If z * x overflowed: if iszero(eq(div(zx, x), z)) { // Revert if x is non-zero. if iszero(iszero(x)) { revert(0, 0) } } // Round to the nearest number. let zxRound := add(zx, half) // Revert if zx + half overflowed. if lt(zxRound, zx) { revert(0, 0) } // Return properly scaled zxRound. z := div(zxRound, scalar) } } } } } /*////////////////////////////////////////////////////////////// GENERAL NUMBER UTILITIES //////////////////////////////////////////////////////////////*/ function sqrt(uint256 x) internal pure returns (uint256 z) { /// @solidity memory-safe-assembly assembly { let y := x // We start y at x, which will help us make our initial estimate. z := 181 // The "correct" value is 1, but this saves a multiplication later. // This segment is to get a reasonable initial estimate for the Babylonian method. With a bad // start, the correct # of bits increases ~linearly each iteration instead of ~quadratically. // We check y >= 2^(k + 8) but shift right by k bits // each branch to ensure that if x >= 256, then y >= 256. if iszero(lt(y, 0x10000000000000000000000000000000000)) { y := shr(128, y) z := shl(64, z) } if iszero(lt(y, 0x1000000000000000000)) { y := shr(64, y) z := shl(32, z) } if iszero(lt(y, 0x10000000000)) { y := shr(32, y) z := shl(16, z) } if iszero(lt(y, 0x1000000)) { y := shr(16, y) z := shl(8, z) } // Goal was to get z*z*y within a small factor of x. More iterations could // get y in a tighter range. Currently, we will have y in [256, 256*2^16). // We ensured y >= 256 so that the relative difference between y and y+1 is small. // That's not possible if x < 256 but we can just verify those cases exhaustively. // Now, z*z*y <= x < z*z*(y+1), and y <= 2^(16+8), and either y >= 256, or x < 256. // Correctness can be checked exhaustively for x < 256, so we assume y >= 256. // Then z*sqrt(y) is within sqrt(257)/sqrt(256) of sqrt(x), or about 20bps. // For s in the range [1/256, 256], the estimate f(s) = (181/1024) * (s+1) is in the range // (1/2.84 * sqrt(s), 2.84 * sqrt(s)), with largest error when s = 1 and when s = 256 or 1/256. // Since y is in [256, 256*2^16), let a = y/65536, so that a is in [1/256, 256). Then we can estimate // sqrt(y) using sqrt(65536) * 181/1024 * (a + 1) = 181/4 * (y + 65536)/65536 = 181 * (y + 65536)/2^18. // There is no overflow risk here since y < 2^136 after the first branch above. z := shr(18, mul(z, add(y, 65536))) // A mul() is saved from starting z at 181. // Given the worst case multiplicative error of 2.84 above, 7 iterations should be enough. z := shr(1, add(z, div(x, z))) z := shr(1, add(z, div(x, z))) z := shr(1, add(z, div(x, z))) z := shr(1, add(z, div(x, z))) z := shr(1, add(z, div(x, z))) z := shr(1, add(z, div(x, z))) z := shr(1, add(z, div(x, z))) // If x+1 is a perfect square, the Babylonian method cycles between // floor(sqrt(x)) and ceil(sqrt(x)). This statement ensures we return floor. // See: https://en.wikipedia.org/wiki/Integer_square_root#Using_only_integer_division // Since the ceil is rare, we save gas on the assignment and repeat division in the rare case. // If you don't care whether the floor or ceil square root is returned, you can remove this statement. z := sub(z, lt(div(x, z), z)) } } function unsafeMod(uint256 x, uint256 y) internal pure returns (uint256 z) { /// @solidity memory-safe-assembly assembly { // Mod x by y. Note this will return // 0 instead of reverting if y is zero. z := mod(x, y) } } function unsafeDiv(uint256 x, uint256 y) internal pure returns (uint256 r) { /// @solidity memory-safe-assembly assembly { // Divide x by y. Note this will return // 0 instead of reverting if y is zero. r := div(x, y) } } function unsafeDivUp(uint256 x, uint256 y) internal pure returns (uint256 z) { /// @solidity memory-safe-assembly assembly { // Add 1 to x * y if x % y > 0. Note this will // return 0 instead of reverting if y is zero. z := add(gt(mod(x, y), 0), div(x, y)) } } }
// SPDX-License-Identifier: AGPL-3.0 pragma solidity ^0.8.0; import {CurveErrorCodes} from "./CurveErrorCodes.sol"; interface ICurve { /** * @notice Validates if a delta value is valid for the curve. The criteria for * validity can be different for each type of curve, for instance ExponentialCurve * requires delta to be greater than 1. * @param delta The delta value to be validated * @return valid True if delta is valid, false otherwise */ function validateDelta(uint128 delta) external pure returns (bool valid); /** * @notice Validates if a new spot price is valid for the curve. Spot price is generally assumed to be the immediate sell price of 1 NFT to the pool, in units of the pool's paired token. * @param newSpotPrice The new spot price to be set * @return valid True if the new spot price is valid, false otherwise */ function validateSpotPrice(uint128 newSpotPrice) external view returns (bool valid); /** * @notice Given the current state of the pair and the trade, computes how much the user * should pay to purchase an NFT from the pair, the new spot price, and other values. * @param spotPrice The current selling spot price of the pair, in tokens * @param delta The delta parameter of the pair, what it means depends on the curve * @param numItems The number of NFTs the user is buying from the pair * @param feeMultiplier Determines how much fee the LP takes from this trade, 18 decimals * @param protocolFeeMultiplier Determines how much fee the protocol takes from this trade, 18 decimals * @return error Any math calculation errors, only Error.OK means the returned values are valid * @return newSpotPrice The updated selling spot price, in tokens * @return newDelta The updated delta, used to parameterize the bonding curve * @return inputValue The amount that the user should pay, in tokens * @return tradeFee The amount that is sent to the trade fee recipient * @return protocolFee The amount of fee to send to the protocol, in tokens */ function getBuyInfo( uint128 spotPrice, uint128 delta, uint256 numItems, uint256 feeMultiplier, uint256 protocolFeeMultiplier ) external view returns ( CurveErrorCodes.Error error, uint128 newSpotPrice, uint128 newDelta, uint256 inputValue, uint256 tradeFee, uint256 protocolFee ); /** * @notice Given the current state of the pair and the trade, computes how much the user * should receive when selling NFTs to the pair, the new spot price, and other values. * @param spotPrice The current selling spot price of the pair, in tokens * @param delta The delta parameter of the pair, what it means depends on the curve * @param numItems The number of NFTs the user is selling to the pair * @param feeMultiplier Determines how much fee the LP takes from this trade, 18 decimals * @param protocolFeeMultiplier Determines how much fee the protocol takes from this trade, 18 decimals * @return error Any math calculation errors, only Error.OK means the returned values are valid * @return newSpotPrice The updated selling spot price, in tokens * @return newDelta The updated delta, used to parameterize the bonding curve * @return outputValue The amount that the user should receive, in tokens * @return tradeFee The amount that is sent to the trade fee recipient * @return protocolFee The amount of fee to send to the protocol, in tokens */ function getSellInfo( uint128 spotPrice, uint128 delta, uint256 numItems, uint256 feeMultiplier, uint256 protocolFeeMultiplier ) external view returns ( CurveErrorCodes.Error error, uint128 newSpotPrice, uint128 newDelta, uint256 outputValue, uint256 tradeFee, uint256 protocolFee ); }
// SPDX-License-Identifier: AGPL-3.0 pragma solidity ^0.8.0; contract CurveErrorCodes { enum Error { OK, // No error INVALID_NUMITEMS, // The numItem value is 0 SPOT_PRICE_OVERFLOW, // The updated spot price doesn't fit into 128 bits DELTA_OVERFLOW, // The updated delta doesn't fit into 128 bits SPOT_PRICE_UNDERFLOW // The updated spot price goes too low } }
{ "remappings": [ "@manifoldxyz/=lib/", "@openzeppelin/contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/contracts/", "@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/", "@prb/math/=lib/prb-math/src/", "clones-with-immutable-args/=lib/clones-with-immutable-args/src/", "create3-factory/=lib/create3-factory/src/", "ds-test/=lib/forge-std/lib/ds-test/src/", "erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/", "forge-std/=lib/forge-std/src/", "libraries-solidity/=lib/libraries-solidity/contracts/", "manifoldxyz/=lib/royalty-registry-solidity/contracts/", "openzeppelin-contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/contracts/", "openzeppelin-contracts/=lib/openzeppelin-contracts/contracts/", "royalty-registry-solidity/=lib/royalty-registry-solidity/contracts/", "solmate/=lib/solmate/src/" ], "optimizer": { "enabled": true, "runs": 1000000 }, "metadata": { "bytecodeHash": "ipfs", "appendCBOR": true }, "outputSelection": { "*": { "*": [ "evm.bytecode", "evm.deployedBytecode", "devdoc", "userdoc", "metadata", "abi" ] } }, "evmVersion": "paris", "libraries": {} }
[{"inputs":[{"internalType":"uint128","name":"spotPrice","type":"uint128"},{"internalType":"uint128","name":"delta","type":"uint128"},{"internalType":"uint256","name":"numItems","type":"uint256"},{"internalType":"uint256","name":"feeMultiplier","type":"uint256"},{"internalType":"uint256","name":"protocolFeeMultiplier","type":"uint256"}],"name":"getBuyInfo","outputs":[{"internalType":"enum CurveErrorCodes.Error","name":"error","type":"uint8"},{"internalType":"uint128","name":"newSpotPrice","type":"uint128"},{"internalType":"uint128","name":"newDelta","type":"uint128"},{"internalType":"uint256","name":"inputValue","type":"uint256"},{"internalType":"uint256","name":"tradeFee","type":"uint256"},{"internalType":"uint256","name":"protocolFee","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"uint128","name":"spotPrice","type":"uint128"},{"internalType":"uint128","name":"delta","type":"uint128"},{"internalType":"uint256","name":"numItems","type":"uint256"},{"internalType":"uint256","name":"feeMultiplier","type":"uint256"},{"internalType":"uint256","name":"protocolFeeMultiplier","type":"uint256"}],"name":"getSellInfo","outputs":[{"internalType":"enum CurveErrorCodes.Error","name":"error","type":"uint8"},{"internalType":"uint128","name":"newSpotPrice","type":"uint128"},{"internalType":"uint128","name":"newDelta","type":"uint128"},{"internalType":"uint256","name":"outputValue","type":"uint256"},{"internalType":"uint256","name":"tradeFee","type":"uint256"},{"internalType":"uint256","name":"protocolFee","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"uint128","name":"","type":"uint128"}],"name":"validateDelta","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"uint128","name":"","type":"uint128"}],"name":"validateSpotPrice","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"pure","type":"function"}]
Deployed Bytecode
0x608060405234801561001057600080fd5b506004361061004c5760003560e01c8063097cc63d146100515780630ae67ccc1461007f5780637ca542ac146100a3578063a1bbb2e81461007f575b600080fd5b61006461005f366004610375565b6100b6565b604051610076969594939291906103c2565b60405180910390f35b61009361008d366004610436565b50600190565b6040519015158152602001610076565b6100646100b1366004610375565b6101bb565b600080600080600080886000036100df57506001945060009350839250829150819050806101ae565b6fffffffffffffffffffffffffffffffff808c16908b1660006101028c83610480565b90506fffffffffffffffffffffffffffffffff81111561013a57600360008060008060009850985098509850985098505050506101ae565b600081610147858f610493565b61015191906104aa565b905061015d818c6102ee565b9450610169818d6102ee565b95508461017687836104e5565b61018091906104e5565b9650819750808f6fffffffffffffffffffffffffffffffff166101a391906104e5565b985060009950505050505b9550955095509550955095565b600080600080600080886000036101e457506001945060009350839250829150819050806101ae565b6fffffffffffffffffffffffffffffffff8a16808a1061021a5760016000806000806000965096509650965096509650506101ae565b60008a820390506000818e6fffffffffffffffffffffffffffffffff168d6102429190610493565b61024c91906104aa565b9050610258818b6102ee565b9350610264818c6102ee565b9450836102718683610480565b61027b9190610480565b95506000818f6fffffffffffffffffffffffffffffffff1661029d9190610480565b90506fffffffffffffffffffffffffffffffff8111156102d65760026000806000806000995099509950995099509950505050506101ae565b60009950975090955050509550955095509550955095565b60006103038383670de0b6b3a764000061030c565b90505b92915050565b6000827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff048411830215820261034157600080fd5b50910281810615159190040190565b80356fffffffffffffffffffffffffffffffff8116811461037057600080fd5b919050565b600080600080600060a0868803121561038d57600080fd5b61039686610350565b94506103a460208701610350565b94979496505050506040830135926060810135926080909101359150565b60c08101600588106103fd577f4e487b7100000000000000000000000000000000000000000000000000000000600052602160045260246000fd5b9681526fffffffffffffffffffffffffffffffff95861660208201529390941660408401526060830191909152608082015260a0015290565b60006020828403121561044857600080fd5b61030382610350565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b8082018082111561030657610306610451565b808202811582820484141761030657610306610451565b6000826104e0577f4e487b7100000000000000000000000000000000000000000000000000000000600052601260045260246000fd5b500490565b818103818111156103065761030661045156fea264697066735822122084487e11fde19000a51a020b324997ad02561436211f18f13e5bb1fc8033b78a64736f6c63430008140033
Loading...
Loading
[ Download: CSV Export ]
A contract address hosts a smart contract, which is a set of code stored on the blockchain that runs when predetermined conditions are met. Learn more about addresses in our Knowledge Base.