Skip to content

Commit

Permalink
Merge pull request #87 from gammaswap/feat/proxy-positionmanager
Browse files Browse the repository at this point in the history
publish package
  • Loading branch information
Kalesberg committed Dec 29, 2023
2 parents 85cc83c + f5e0755 commit f34b1b2
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 58 deletions.
32 changes: 19 additions & 13 deletions contracts/PositionManager.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity 0.8.21;

import "@openzeppelin/contracts/proxy/utils/UUPSUpgradeable.sol";
import "@openzeppelin/contracts/proxy/utils/Initializable.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

import "@gammaswap/v1-core/contracts/utils/TwoStepOwnable.sol";
Expand All @@ -18,11 +20,11 @@ import "./base/GammaPoolQueryableLoans.sol";
/// @notice Periphery contract used to aggregate function calls to a GammaPool and give NFT (ERC721) functionality to loans
/// @notice Loans created through PositionManager become NFTs and can only be managed through PositionManager
/// @dev PositionManager is owner of loan and user is owner of NFT that represents loan in a GammaPool
contract PositionManager is TwoStepOwnable, IPositionManager, Transfers, GammaPoolQueryableLoans {
contract PositionManager is Initializable, UUPSUpgradeable, TwoStepOwnable, IPositionManager, Transfers, GammaPoolQueryableLoans {

error Forbidden();
error Expired();
error AmountsMin();
error AmountsMin(uint8 id);

string constant private _name = "PositionManager";
string constant private _symbol = "PM-V1";
Expand All @@ -32,6 +34,17 @@ contract PositionManager is TwoStepOwnable, IPositionManager, Transfers, GammaPo

address public priceStore;

/// @dev Initializes the contract by setting `factory`, `WETH`.
constructor(address _factory, address _WETH) TwoStepOwnable(msg.sender) Transfers(_WETH) {
factory = _factory;
}

function initialize(address _dataStore, address _priceStore) public initializer {
owner = msg.sender;
dataStore = _dataStore;
priceStore = _priceStore;
}

modifier isAuthorizedForToken(uint256 tokenId) {
checkAuthorization(tokenId);
_;
Expand All @@ -58,15 +71,6 @@ contract PositionManager is TwoStepOwnable, IPositionManager, Transfers, GammaPo
}
}

/// @dev Initializes the contract by setting `factory`, `WETH`, and `dataStore`.
constructor(address _factory, address _WETH, address _dataStore, address _priceStore)
TwoStepOwnable(msg.sender)
Transfers(_WETH)
GammaPoolQueryableLoans(_dataStore) {
factory = _factory;
priceStore = _priceStore;
}

/// @dev See {IERC721Metadata-name}.
function name() public view virtual override returns (string memory) {
return _name;
Expand Down Expand Up @@ -152,7 +156,7 @@ contract PositionManager is TwoStepOwnable, IPositionManager, Transfers, GammaPo
if(len!=len2) return;
for (uint256 i; i < len;) {
if(amounts[i] < amountsMin[i]) {
revert AmountsMin();
revert AmountsMin(1);
}
unchecked {
++i;
Expand All @@ -170,7 +174,7 @@ contract PositionManager is TwoStepOwnable, IPositionManager, Transfers, GammaPo
if(len!=len2) return;
for (uint256 i; i < len;) {
if(amounts[i] < amountsMin[i]) {
revert AmountsMin();
revert AmountsMin(2);
}
unchecked {
++i;
Expand Down Expand Up @@ -382,4 +386,6 @@ contract PositionManager is TwoStepOwnable, IPositionManager, Transfers, GammaPo
}
_logPrice(gammaPool);
}

function _authorizeUpgrade(address) internal override onlyOwner {}
}
4 changes: 1 addition & 3 deletions contracts/PositionManagerWithStaking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ contract PositionManagerWithStaking is PositionManager, IAutoStakable {
/// @dev Constructs the PositionManagerWithStaking contract.
/// @param _factory Address of the contract factory.
/// @param _WETH Address of the Wrapped Ether (WETH) contract.
/// @param _dataStore Address of the data store contract.
/// @param _priceStore Address of the price store contract.
constructor(address _factory, address _WETH, address _dataStore, address _priceStore) PositionManager(_factory, _WETH, _dataStore, _priceStore) {}
constructor(address _factory, address _WETH) PositionManager(_factory, _WETH) {}

/// @dev See {IAutoStakable-setStakingRouter}
function setStakingRouter(address _stakingRouter) external onlyOwner {
Expand Down
5 changes: 0 additions & 5 deletions contracts/base/GammaPoolQueryableLoans.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ abstract contract GammaPoolQueryableLoans is GammaPoolERC721 {
/// @dev Database where it will store loan information. dataStore has to know this address though to accept messages
address public dataStore;

/// @dev Initializes the contract by setting `dataStore`.
constructor(address _dataStore) {
dataStore = _dataStore;
}

/// @dev Mint tokenId of loan as ERC721 NFT and store in mappings so that it can be queried
/// @param pool - pool loan identified by `tokenId` belongs to
/// @param tokenId - unique identifier of loan
Expand Down
8 changes: 3 additions & 5 deletions contracts/test/TestPositionManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,18 @@ import "../PositionManager.sol";

contract TestPositionManager is PositionManager {

constructor(address _factory, address _WETH, address _dataStore, address _priceStore)
PositionManager( _factory, _WETH, _dataStore, _priceStore) {
}
constructor(address _factory, address _WETH) PositionManager( _factory, _WETH) {}

function createTestLoan(address to) external virtual returns(uint256 tokenId) {
tokenId = 1;
_safeMint(to, tokenId);
}

function testCheckMinReserves(uint256[] calldata amounts, uint256[] calldata amountsMin) external virtual pure {
function checkMinReservesTest(uint256[] calldata amounts, uint256[] calldata amountsMin) external virtual pure {
checkMinReserves(amounts, amountsMin);
}

function testCheckMinCollateral(uint128[] memory amounts, uint128[] memory amountsMin) external virtual pure {
function checkMinCollateralTest(uint128[] memory amounts, uint128[] memory amountsMin) external virtual pure {
checkMinCollateral(amounts, amountsMin);
}

Expand Down
3 changes: 1 addition & 2 deletions contracts/test/TestTransfers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import "../base/Transfers.sol";
import "../interfaces/ITransfers.sol";

contract TestTransfers is Transfers {
constructor(address _WETH) Transfers(_WETH) {
}
constructor(address _WETH) Transfers(_WETH) {}


function getGammaPoolAddress(address cfmm, uint16) internal virtual override view returns(address) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gammaswap/v1-periphery",
"version": "1.1.8",
"version": "1.1.9",
"description": "Periphery contracts for the GammaSwap V1 protocol",
"homepage": "https://gammaswap.com",
"scripts": {
Expand Down
59 changes: 30 additions & 29 deletions test/PositionManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ describe("PositionManager", function () {

await (await factory.addProtocol(implementation.address)).wait();

posMgr = await TestPositionManager.deploy(factory.address, WETH.address, store.address, priceStore.address);
posMgr = await TestPositionManager.deploy(factory.address, WETH.address);
await posMgr.initialize(store.address, priceStore.address)

await (await store.setSource(posMgr.address)).wait();

Expand Down Expand Up @@ -199,121 +200,121 @@ describe("PositionManager", function () {
it("#checkMinReserves should revert when Amounts < AmountsMin", async function () {
const amounts = [90000, 1000];
const amountsMin = [90000, 1001];
await expect(posMgr.testCheckMinReserves(amounts, amountsMin)).to.be.revertedWith("AmountsMin");
await expect(posMgr.checkMinReservesTest(amounts, amountsMin)).to.be.revertedWith("AmountsMin");

const amounts0 = [90000, 1000];
const amountsMin0 = [90001, 1000];
await expect(posMgr.testCheckMinReserves(amounts0, amountsMin0)).to.be.revertedWith("AmountsMin");
await expect(posMgr.checkMinReservesTest(amounts0, amountsMin0)).to.be.revertedWith("AmountsMin");

const amounts1 = [90000, 1000];
const amountsMin1 = [90001, 1001];
await expect(posMgr.testCheckMinReserves(amounts1, amountsMin1)).to.be.revertedWith("AmountsMin");
await expect(posMgr.checkMinReservesTest(amounts1, amountsMin1)).to.be.revertedWith("AmountsMin");

const amounts2 = [1, 1];
const amountsMin2 = [1, 2];
await expect(posMgr.testCheckMinReserves(amounts2, amountsMin2)).to.be.revertedWith("AmountsMin");
await expect(posMgr.checkMinReservesTest(amounts2, amountsMin2)).to.be.revertedWith("AmountsMin");

const amounts3 = [0, 1];
const amountsMin3 = [1, 1];
await expect(posMgr.testCheckMinReserves(amounts3, amountsMin3)).to.be.revertedWith("AmountsMin");
await expect(posMgr.checkMinReservesTest(amounts3, amountsMin3)).to.be.revertedWith("AmountsMin");

const amounts4 = [1, 0];
const amountsMin4 = [1, 1];
await expect(posMgr.testCheckMinReserves(amounts4, amountsMin4)).to.be.revertedWith("AmountsMin");
await expect(posMgr.checkMinReservesTest(amounts4, amountsMin4)).to.be.revertedWith("AmountsMin");

const amounts5 = [0, 0];
const amountsMin5 = [1, 1];
await expect(posMgr.testCheckMinReserves(amounts5, amountsMin5)).to.be.revertedWith("AmountsMin");
await expect(posMgr.checkMinReservesTest(amounts5, amountsMin5)).to.be.revertedWith("AmountsMin");
});

it("#checkMinReserves should not revert when Amounts >= AmountsMin", async function () {
const amounts = [90000, 1000];
const amountsMin = [90000, 1000];
posMgr.testCheckMinReserves(amounts, amountsMin);
posMgr.checkMinReservesTest(amounts, amountsMin);

const amounts0 = [90001, 1000];
const amountsMin0 = [90000, 1000];
posMgr.testCheckMinReserves(amounts0, amountsMin0);
posMgr.checkMinReservesTest(amounts0, amountsMin0);

const amounts1 = [90000, 1001];
const amountsMin1 = [90000, 1000];
posMgr.testCheckMinReserves(amounts1, amountsMin1);
posMgr.checkMinReservesTest(amounts1, amountsMin1);

const amounts2 = [90001, 1001];
const amountsMin2 = [90000, 1000];
posMgr.testCheckMinReserves(amounts2, amountsMin2);
posMgr.checkMinReservesTest(amounts2, amountsMin2);

const amounts3 = [1, 1];
const amountsMin3 = [1, 1];
posMgr.testCheckMinReserves(amounts3, amountsMin3);
posMgr.checkMinReservesTest(amounts3, amountsMin3);

const amounts4 = [1, 1];
const amountsMin4 = [0, 0];
posMgr.testCheckMinReserves(amounts4, amountsMin4);
posMgr.checkMinReservesTest(amounts4, amountsMin4);

const amounts5 = [0, 0];
const amountsMin5 = [0, 0];
posMgr.testCheckMinReserves(amounts5, amountsMin5);
posMgr.checkMinReservesTest(amounts5, amountsMin5);
});

it("#checkMinCollateral should revert when Amounts < AmountsMin", async function () {
const amounts = [90000, 1000];
const amountsMin = [90000, 1001];
await expect(posMgr.testCheckMinCollateral(amounts, amountsMin)).to.be.revertedWith("AmountsMin");
await expect(posMgr.checkMinCollateralTest(amounts, amountsMin)).to.be.revertedWith("AmountsMin");

const amounts0 = [90000, 1000];
const amountsMin0 = [90001, 1000];
await expect(posMgr.testCheckMinCollateral(amounts0, amountsMin0)).to.be.revertedWith("AmountsMin");
await expect(posMgr.checkMinCollateralTest(amounts0, amountsMin0)).to.be.revertedWith("AmountsMin");

const amounts1 = [90000, 1000];
const amountsMin1 = [90001, 1001];
await expect(posMgr.testCheckMinCollateral(amounts1, amountsMin1)).to.be.revertedWith("AmountsMin");
await expect(posMgr.checkMinCollateralTest(amounts1, amountsMin1)).to.be.revertedWith("AmountsMin");

const amounts2 = [1, 1];
const amountsMin2 = [1, 2];
await expect(posMgr.testCheckMinCollateral(amounts2, amountsMin2)).to.be.revertedWith("AmountsMin");
await expect(posMgr.checkMinCollateralTest(amounts2, amountsMin2)).to.be.revertedWith("AmountsMin");

const amounts3 = [0, 1];
const amountsMin3 = [1, 1];
await expect(posMgr.testCheckMinCollateral(amounts3, amountsMin3)).to.be.revertedWith("AmountsMin");
await expect(posMgr.checkMinCollateralTest(amounts3, amountsMin3)).to.be.revertedWith("AmountsMin");

const amounts4 = [1, 0];
const amountsMin4 = [1, 1];
await expect(posMgr.testCheckMinCollateral(amounts4, amountsMin4)).to.be.revertedWith("AmountsMin");
await expect(posMgr.checkMinCollateralTest(amounts4, amountsMin4)).to.be.revertedWith("AmountsMin");

const amounts5 = [0, 0];
const amountsMin5 = [1, 1];
await expect(posMgr.testCheckMinCollateral(amounts5, amountsMin5)).to.be.revertedWith("AmountsMin");
await expect(posMgr.checkMinCollateralTest(amounts5, amountsMin5)).to.be.revertedWith("AmountsMin");
});

it("#checkMinCollateral should not revert when Amounts >= AmountsMin", async function () {
const amounts = [90000, 1000];
const amountsMin = [90000, 1000];
posMgr.testCheckMinCollateral(amounts, amountsMin);
posMgr.checkMinCollateralTest(amounts, amountsMin);

const amounts0 = [90001, 1000];
const amountsMin0 = [90000, 1000];
posMgr.testCheckMinCollateral(amounts0, amountsMin0);
posMgr.checkMinCollateralTest(amounts0, amountsMin0);

const amounts1 = [90000, 1001];
const amountsMin1 = [90000, 1000];
posMgr.testCheckMinCollateral(amounts1, amountsMin1);
posMgr.checkMinCollateralTest(amounts1, amountsMin1);

const amounts2 = [90001, 1001];
const amountsMin2 = [90000, 1000];
posMgr.testCheckMinCollateral(amounts2, amountsMin2);
posMgr.checkMinCollateralTest(amounts2, amountsMin2);

const amounts3 = [1, 1];
const amountsMin3 = [1, 1];
posMgr.testCheckMinCollateral(amounts3, amountsMin3);
posMgr.checkMinCollateralTest(amounts3, amountsMin3);

const amounts4 = [1, 1];
const amountsMin4 = [0, 0];
posMgr.testCheckMinCollateral(amounts4, amountsMin4);
posMgr.checkMinCollateralTest(amounts4, amountsMin4);

const amounts5 = [0, 0];
const amountsMin5 = [0, 0];
posMgr.testCheckMinCollateral(amounts5, amountsMin5);
posMgr.checkMinCollateralTest(amounts5, amountsMin5);
});
});

Expand Down

0 comments on commit f34b1b2

Please sign in to comment.