Skip to content

Commit

Permalink
Merge pull request #11 from numocash/robert/squared
Browse files Browse the repository at this point in the history
Rename to Square.sol
  • Loading branch information
robertleifke authored Jan 12, 2025
2 parents 48bc622 + 8b9cf79 commit ace6a18
Show file tree
Hide file tree
Showing 27 changed files with 656 additions and 656 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"scripts": {
"build": "forge build",
"clean": "rm -rf cache out",
"test": "forge test --no-match-path src/core/Lendgine.sol",
"test": "forge test --no-match-path src/core/squared.sol",
"lint": "forge fmt",
"lint:ci": "forge fmt --check",
"lint:sol": "solhint \"{src,test}/**/*.sol\"",
Expand Down
8 changes: 4 additions & 4 deletions script/Deploy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { console2 } from "forge-std/console2.sol";

import { Factory } from "src/core/Factory.sol";
import { LiquidityManager } from "src/periphery/LiquidityManager.sol";
import { LendgineRouter } from "src/periphery/LendgineRouter.sol";
import { SquaredRouter } from "src/periphery/SquaredRouter.sol";

contract Deploy is Script {
address constant create3Factory = 0x93FEC2C00BfE902F733B57c5a6CeeD7CD1384AE1;
Expand All @@ -20,7 +20,7 @@ contract Deploy is Script {
//Uniswap deployed WETH
address constant weth = 0xfFf9976782d46CC05630D1f6eBAb18b2324d6B14;

function run() external returns (address factory, address liquidityManager, address lendgineRouter) {
function run() external returns (address factory, address liquidityManager, address squaredRouter) {
// CREATE3Factory create3 = CREATE3Factory(create3Factory);

uint256 pk = vm.envUint("PRIVATE_KEY");
Expand All @@ -30,15 +30,15 @@ contract Deploy is Script {

liquidityManager = address(new LiquidityManager(factory, weth));

lendgineRouter = address(new LendgineRouter(factory, uniV2Factory, uniV3Factory, weth));
squaredRouter = address(new SquaredRouter(factory, uniV2Factory, uniV3Factory, weth));

// factory = create2.deploy(keccak256("SquaredFactory"), type(Factory).creationCode);

// liquidityManager = create2.deploy(
// keccak256("SquaredManager"), bytes.concat(type(LiquidityManager).creationCode, abi.encode(factory, weth))
// );

// lendgineRouter = create2.deploy(
// squaredRouter = create2.deploy(
// keccak256("SquaredRouter"),
// bytes.concat(type(SquaredRouter).creationCode, abi.encode(factory, uniV2Factory, uniV3Factory, weth))
// );
Expand Down
22 changes: 11 additions & 11 deletions script/SetupLocal.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { console2 } from "forge-std/console2.sol";

import { Factory } from "../src/core/Factory.sol";
import { LiquidityManager } from "../src/periphery/LiquidityManager.sol";
import { LendgineRouter } from "../src/periphery/LendgineRouter.sol";
import { SquaredRouter } from "../src/periphery/SquaredRouter.sol";
import { SwapHelper } from "../src/periphery/SwapHelper.sol";
import { ERC20 } from "../src/core/ERC20.sol";
import { IWETH9 } from "../src/periphery/interfaces/external/IWETH9.sol";
Expand Down Expand Up @@ -54,20 +54,20 @@ contract SetupLocalScript is Script {

// deploy core contracts
vm.startBroadcast(pk);
Factory factory = new Factory{salt: keccak256("NumoFactoryTest1")}();
Factory factory = new Factory{salt: keccak256("FactoryTest1")}();
LiquidityManager liquidityManager =
new LiquidityManager{salt: keccak256("NumoLiquidityManagerTest1")}(address(factory), weth);
LendgineRouter lendgineRouter = new LendgineRouter{salt: keccak256("NumoLendgineRouterTest1")}(
new LiquidityManager{salt: keccak256("SquaredLiquidityManagerTest1")}(address(factory), weth);
SquaredRouter squaredRouter = new SquaredRouter{salt: keccak256("SquaredRouterTest1")}(
address(factory),
uniV2Factory,
uniV3Factory,
weth
);

// deploy new lendgines
console2.log("usdc/weth lendgine: ", factory.createLendgine(usdc, weth, 6, 18, usdcWethBound));
console2.log("weth/uni lendgine: ", factory.createLendgine(weth, uni, 18, 18, wethUniBound));
console2.log("uni/weth lendgine: ", factory.createLendgine(uni, weth, 18, 18, uniWethBound));
// deploy new squareds
console2.log("usdc/weth squared: ", factory.createSquared(usdc, weth, 6, 18, usdcWethBound));
console2.log("weth/uni squared: ", factory.createSquared(weth, uni, 18, 18, wethUniBound));
console2.log("uni/weth squared: ", factory.createSquared(uni, weth, 18, 18, uniWethBound));

// mint tokens to addr
IWETH9(weth).deposit{ value: 100 ether }();
Expand Down Expand Up @@ -130,10 +130,10 @@ contract SetupLocalScript is Script {
);

// borrow from market
ERC20(uni).approve(address(lendgineRouter), 50 * 1e18);
ERC20(uni).approve(address(squaredRouter), 50 * 1e18);

lendgineRouter.mint(
LendgineRouter.MintParams({
squaredRouter.mint(
SquaredRouter.MintParams({
token0: weth,
token1: uni,
token0Exp: 18,
Expand Down
20 changes: 10 additions & 10 deletions src/core/Factory.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity ^0.8.4;

import { Lendgine } from "./Lendgine.sol";
import { Squared } from "./Squared.sol";

import { IFactory } from "./interfaces/IFactory.sol";

Expand All @@ -10,13 +10,13 @@ contract Factory is IFactory {
EVENTS
//////////////////////////////////////////////////////////////*/

event LendgineCreated(
event SquaredCreated(
address indexed token0,
address indexed token1,
uint256 token0Exp,
uint256 token1Exp,
uint256 indexed upperBound,
address lendgine
address squared
);

/*//////////////////////////////////////////////////////////////
Expand All @@ -38,7 +38,7 @@ contract Factory is IFactory {
/// @inheritdoc IFactory
mapping(address => mapping(address => mapping(uint256 => mapping(uint256 => mapping(uint256 => address)))))
public
override getLendgine;
override getSquared;

/*//////////////////////////////////////////////////////////////
TEMPORARY DEPLOY STORAGE
Expand All @@ -60,7 +60,7 @@ contract Factory is IFactory {
//////////////////////////////////////////////////////////////*/

/// @inheritdoc IFactory
function createLendgine(
function createSquared(
address token0,
address token1,
uint8 token0Exp,
Expand All @@ -69,21 +69,21 @@ contract Factory is IFactory {
)
external
override
returns (address lendgine)
returns (address squared)
{
if (token0 == token1) revert SameTokenError();
if (token0 == address(0) || token1 == address(0)) revert ZeroAddressError();
if (getLendgine[token0][token1][token0Exp][token1Exp][upperBound] != address(0)) revert DeployedError();
if (getSquared[token0][token1][token0Exp][token1Exp][upperBound] != address(0)) revert DeployedError();
if (token0Exp > 18 || token0Exp < 6 || token1Exp > 18 || token1Exp < 6) revert ScaleError();

parameters =
Parameters({ token0: token0, token1: token1, token0Exp: token0Exp, token1Exp: token1Exp, upperBound: upperBound });

lendgine = address(new Lendgine{ salt: keccak256(abi.encode(token0, token1, token0Exp, token1Exp, upperBound)) }());
squared = address(new Squared{ salt: keccak256(abi.encode(token0, token1, token0Exp, token1Exp, upperBound)) }());

delete parameters;

getLendgine[token0][token1][token0Exp][token1Exp][upperBound] = lendgine;
emit LendgineCreated(token0, token1, token0Exp, token1Exp, upperBound, lendgine);
getSquared[token0][token1][token0Exp][token1Exp][upperBound] = squared;
emit SquaredCreated(token0, token1, token0Exp, token1Exp, upperBound, squared);
}
}
40 changes: 20 additions & 20 deletions src/core/Lendgine.sol → src/core/Squared.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ERC20 } from "./ERC20.sol";
import { JumpRate } from "./JumpRate.sol";
import { Pair } from "./Pair.sol";

import { ILendgine } from "./interfaces/ILendgine.sol";
import { ISquared } from "./interfaces/ISquared.sol";
import { IMintCallback } from "./interfaces/callback/IMintCallback.sol";

import { Balance } from "../libraries/Balance.sol";
Expand All @@ -14,7 +14,7 @@ import { Position } from "./libraries/Position.sol";
import { SafeTransferLib } from "../libraries/SafeTransferLib.sol";
import { SafeCast } from "../libraries/SafeCast.sol";

contract Lendgine is ERC20, JumpRate, Pair, ILendgine {
contract Squared is ERC20, JumpRate, Pair, ISquared {
using Position for mapping(address => Position.Info);
using Position for Position.Info;

Expand Down Expand Up @@ -49,25 +49,25 @@ contract Lendgine is ERC20, JumpRate, Pair, ILendgine {
error InsufficientPositionError();

/*//////////////////////////////////////////////////////////////
LENDGINE STORAGE
Squared STORAGE
//////////////////////////////////////////////////////////////*/

/// @inheritdoc ILendgine
/// @inheritdoc ISquared
mapping(address => Position.Info) public override positions;

/// @inheritdoc ILendgine
/// @inheritdoc ISquared
uint256 public override totalPositionSize;

/// @inheritdoc ILendgine
/// @inheritdoc ISquared
uint256 public override totalLiquidityBorrowed;

/// @inheritdoc ILendgine
/// @inheritdoc ISquared
uint256 public override rewardPerPositionStored;

/// @inheritdoc ILendgine
/// @inheritdoc ISquared
uint256 public override lastUpdate;

/// @inheritdoc ILendgine
/// @inheritdoc ISquared
function mint(
address to,
uint256 collateral,
Expand Down Expand Up @@ -101,7 +101,7 @@ contract Lendgine is ERC20, JumpRate, Pair, ILendgine {
emit Mint(msg.sender, collateral, shares, liquidity, to);
}

/// @inheritdoc ILendgine
/// @inheritdoc ISquared
function burn(address to, bytes calldata data) external override nonReentrant returns (uint256 collateral) {
_accrueInterest();

Expand All @@ -119,7 +119,7 @@ contract Lendgine is ERC20, JumpRate, Pair, ILendgine {
emit Burn(msg.sender, collateral, shares, liquidity, to);
}

/// @inheritdoc ILendgine
/// @inheritdoc ISquared
function deposit(
address to,
uint256 liquidity,
Expand Down Expand Up @@ -148,7 +148,7 @@ contract Lendgine is ERC20, JumpRate, Pair, ILendgine {
emit Deposit(msg.sender, size, liquidity, to);
}

/// @inheritdoc ILendgine
/// @inheritdoc ISquared
function withdraw(
address to,
uint256 size
Expand Down Expand Up @@ -179,18 +179,18 @@ contract Lendgine is ERC20, JumpRate, Pair, ILendgine {
emit Withdraw(msg.sender, size, liquidity, to);
}

/// @inheritdoc ILendgine
/// @inheritdoc ISquared
function accrueInterest() external override nonReentrant {
_accrueInterest();
}

/// @inheritdoc ILendgine
/// @inheritdoc ISquared
function accruePositionInterest() external override nonReentrant {
_accrueInterest();
_accruePositionInterest(msg.sender);
}

/// @inheritdoc ILendgine
/// @inheritdoc ISquared
function collect(address to, uint256 collateralRequested) external override nonReentrant returns (uint256 collateral) {
Position.Info storage position = positions[msg.sender]; // SLOAD
uint256 tokensOwed = position.tokensOwed;
Expand All @@ -209,23 +209,23 @@ contract Lendgine is ERC20, JumpRate, Pair, ILendgine {
ACCOUNTING LOGIC
//////////////////////////////////////////////////////////////*/

/// @inheritdoc ILendgine
/// @inheritdoc ISquared
function convertLiquidityToShare(uint256 liquidity) public view override returns (uint256) {
uint256 _totalLiquidityBorrowed = totalLiquidityBorrowed; // SLOAD
return _totalLiquidityBorrowed == 0 ? liquidity : FullMath.mulDiv(liquidity, totalSupply, _totalLiquidityBorrowed);
}

/// @inheritdoc ILendgine
/// @inheritdoc ISquared
function convertShareToLiquidity(uint256 shares) public view override returns (uint256) {
return FullMath.mulDiv(totalLiquidityBorrowed, shares, totalSupply);
}

/// @inheritdoc ILendgine
/// @inheritdoc ISquared
function convertCollateralToLiquidity(uint256 collateral) public view override returns (uint256) {
return FullMath.mulDiv(collateral * token1Scale, 1e18, 2 * upperBound);
}

/// @inheritdoc ILendgine
/// @inheritdoc ISquared
function convertLiquidityToCollateral(uint256 liquidity) public view override returns (uint256) {
return FullMath.mulDiv(liquidity, 2 * upperBound, 1e18) / token1Scale;
}
Expand All @@ -234,7 +234,7 @@ contract Lendgine is ERC20, JumpRate, Pair, ILendgine {
INTERNAL INTEREST LOGIC
//////////////////////////////////////////////////////////////*/

/// @notice Helper function for accruing lendgine interest
/// @notice Helper function for accruing Squared interest
function _accrueInterest() private {
if (totalSupply == 0 || totalLiquidityBorrowed == 0) {
lastUpdate = block.timestamp;
Expand Down
20 changes: 10 additions & 10 deletions src/core/interfaces/IFactory.sol
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity >=0.5.0;

/// @notice Manages the recording and creation of Numo markets
/// @notice Manages the recording and creation of Squared positions
/// @author Kyle Scott and Robert Leifke
/// @author Modified from Uniswap (https://github.com/Uniswap/v2-core/blob/master/contracts/UniswapV2Factory.sol)
/// and Primitive (https://github.com/primitivefinance/rmm-core/blob/main/contracts/PrimitiveFactory.sol)
interface IFactory {
/// @notice Returns the lendgine address for a given pair of tokens and upper bound
/// @notice Returns the Squared address for a given pair of tokens and upper bound
/// @dev returns address 0 if it doesn't exist
function getLendgine(
function getSquared(
address token0,
address token1,
uint256 token0Exp,
Expand All @@ -17,19 +17,19 @@ interface IFactory {
)
external
view
returns (address lendgine);
returns (address squared);

/// @notice Get the parameters to be used in constructing the lendgine, set
/// transiently during lendgine creation
/// @dev Called by the immutable state constructor to fetch the parameters of the lendgine
/// @notice Get the parameters to be used in constructing the Squared, set
/// transiently during Squared position creation
/// @dev Called by the immutable state constructor to fetch the parameters of the Squared
function parameters()
external
view
returns (address token0, address token1, uint128 token0Exp, uint128 token1Exp, uint256 upperBound);

/// @notice Deploys a lendgine contract by transiently setting the parameters storage slots
/// and clearing it after the lendgine has been deployed
function createLendgine(
/// @notice Deploys a squared contract by transiently setting the parameters storage slots
/// and clearing it after the squared has been deployed
function createSquared(
address token0,
address token1,
uint8 token0Exp,
Expand Down
2 changes: 1 addition & 1 deletion src/core/interfaces/IImmutableState.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity >=0.5.0;
/// @notice Immutable state interface
/// @author Kyle Scott and Robert Leifke
interface IImmutableState {
/// @notice The contract that deployed the lendgine
/// @notice The contract that deployed the squared
function factory() external view returns (address);

/// @notice The "numeraire" or "base" token in the pair
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { IPair } from "./IPair.sol";

/// @notice Lending engine for borrowing and lending liquidity provider shares
/// @author Kyle Scott and Robert Leifke
interface ILendgine is IPair {
interface ISquared is IPair {
/// @notice Returns information about a position given the controllers address
function positions(address) external view returns (uint256, uint256, uint256);

Expand Down
4 changes: 2 additions & 2 deletions src/core/interfaces/callback/IMintCallback.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
pragma solidity >=0.5.0;

interface IMintCallback {
/// @notice Called to `msg.sender` after executing a mint via Lendgine
/// @notice Called to `msg.sender` after executing a mint via squared
/// @dev In the implementation you must pay the speculative tokens owed for the mint.
/// The caller of this method must be checked to be a Lendgine deployed by the canonical Factory.
/// The caller of this method must be checked to be a squared deployed by the canonical Factory.
/// @param data Any data passed through by the caller via the Mint call
function mintCallback(
uint256 collateral,
Expand Down
2 changes: 1 addition & 1 deletion src/core/libraries/Position.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity ^0.8.4;
import { PositionMath } from "./PositionMath.sol";
import { FullMath } from "../../libraries/FullMath.sol";

/// @notice Library for handling Lendgine liquidity positions
/// @notice Library for handling squared liquidity positions
/// @author Kyle Scott and Leifke
/// @author Modified from Uniswap (https://github.com/Uniswap/v3-core/blob/main/contracts/libraries/Position.sol)
library Position {
Expand Down
Loading

0 comments on commit ace6a18

Please sign in to comment.