Skip to content

Commit

Permalink
Merge pull request #17 from spherex-collab/spherex
Browse files Browse the repository at this point in the history
Spherex
  • Loading branch information
YouStillAlive authored Nov 8, 2023
2 parents 73e7021 + 36c91b3 commit c90ae08
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 52 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ coverage
coverage.json
LockDealNFT*
DelayVault
VaultManager*
VaultManager*

# SphereX
code_id_mapping.json
inheritance_code.md
14 changes: 8 additions & 6 deletions contracts/DelayVaultProvider.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
pragma solidity ^0.8.0;

import "./DelayVaultState.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@spherex-xyz/openzeppelin-solidity/contracts/token/ERC20/IERC20.sol";
import {SphereXProtected} from "@spherex-xyz/contracts/src/SphereXProtected.sol";


contract DelayVaultProvider is DelayVaultState {
constructor(address _token, IMigrator _migrator, ProviderData[] memory _providersData) {
Expand All @@ -20,12 +22,12 @@ contract DelayVaultProvider is DelayVaultState {
function registerPool(
uint256 poolId,
uint256[] calldata params
) public override onlyProvider validProviderId(poolId) {
) public override onlyProvider validProviderId(poolId) sphereXGuardPublic(0x952ef082, 0xe9a9fce2) {
require(params.length == currentParamsTargetLenght(), "invalid params length");
_registerPool(poolId, params);
}

function _registerPool(uint256 poolId, uint256[] calldata params) internal {
function _registerPool(uint256 poolId, uint256[] calldata params) internal sphereXGuardInternal(0x1854fc37) {
uint256 amount = params[0];
address owner = lockDealNFT.ownerOf(poolId);
_addHoldersSum(owner, amount, owner == msg.sender || msg.sender == address(migrator));
Expand All @@ -41,7 +43,7 @@ contract DelayVaultProvider is DelayVaultState {
withdrawalAmount = poolIdToAmount[poolId];
}

function upgradeType(uint8 newType) public {
function upgradeType(uint8 newType) public sphereXGuardPublic(0x0aa2846a, 0x66b564bf) {
uint8 oldType = userToType[msg.sender];
uint256 amount = getTotalAmount(msg.sender);
require(amount > 0, "amount must be bigger than 0");
Expand All @@ -50,7 +52,7 @@ contract DelayVaultProvider is DelayVaultState {
userToType[msg.sender] = newType;
}

function createNewDelayVault(address owner, uint256[] calldata params) external returns (uint256 poolId) {
function createNewDelayVault(address owner, uint256[] calldata params) external sphereXGuardExternal(0x6ea2b58a) returns (uint256 poolId) {
require(params.length == currentParamsTargetLenght(), "invalid params length");
uint256 amount = params[0];
IERC20(token).transferFrom(msg.sender, address(lockDealNFT), amount);
Expand All @@ -62,7 +64,7 @@ contract DelayVaultProvider is DelayVaultState {
address owner,
uint256[] calldata params,
bytes calldata signature
) external returns (uint256 poolId) {
) external sphereXGuardExternal(0x7055b37f) returns (uint256 poolId) {
require(params.length == currentParamsTargetLenght(), "invalid params length");
poolId = lockDealNFT.safeMintAndTransfer(owner, token, msg.sender, params[0], this, signature);
_registerPool(poolId, params);
Expand Down
16 changes: 9 additions & 7 deletions contracts/DelayVaultState.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ pragma solidity ^0.8.0;
import "./LockDealNFT/contracts/SimpleProviders/DealProvider/DealProviderState.sol";
import "./LockDealNFT/contracts/util/CalcUtils.sol";
import "./LastPoolOwnerState.sol";
import "./HoldersSum.sol";
import "./HoldersSum.sol";
import {SphereXProtected} from "@spherex-xyz/contracts/src/SphereXProtected.sol";


abstract contract DelayVaultState is DealProviderState, LastPoolOwnerState, HoldersSum {
using CalcUtils for uint256;

function beforeTransfer(address from, address to, uint256 poolId) external override onlyNFT {
function beforeTransfer(address from, address to, uint256 poolId) external override onlyNFT sphereXGuardExternal(0x8a1f7191) {
if (to == address(lockDealNFT))
// this means it will be withdraw or split
lastPoolOwner[poolId] = from; //this is the only way to know the owner of the pool
Expand All @@ -18,7 +20,7 @@ abstract contract DelayVaultState is DealProviderState, LastPoolOwnerState, Hold
}
}

function _handleTransfer(address from, address to, uint256 poolId) internal returns (uint256 amount) {
function _handleTransfer(address from, address to, uint256 poolId) internal sphereXGuardInternal(0xdb514809) returns (uint256 amount) {
amount = poolIdToAmount[poolId];
_subHoldersSum(from, amount);
_addHoldersSum(to, amount, false);
Expand All @@ -42,7 +44,7 @@ abstract contract DelayVaultState is DealProviderState, LastPoolOwnerState, Hold
}

//This need to make a new pool without transfering the token, the pool data is taken from the settings
function withdraw(uint256 tokenId) external override onlyNFT returns (uint256 withdrawnAmount, bool isFinal) {
function withdraw(uint256 tokenId) external override onlyNFT sphereXGuardExternal(0x5623236b) returns (uint256 withdrawnAmount, bool isFinal) {
address owner = lastPoolOwner[tokenId];
uint8 theType = userToType[owner];
uint256 amount = poolIdToAmount[tokenId];
Expand All @@ -53,15 +55,15 @@ abstract contract DelayVaultState is DealProviderState, LastPoolOwnerState, Hold
_resetTypeIfEmpty(owner);
}

function _createLockNFT(address owner, uint256 amount, uint8 theType, uint tokenId) internal {
function _createLockNFT(address owner, uint256 amount, uint8 theType, uint tokenId) internal sphereXGuardInternal(0xd4c9eab9) {
ProviderData memory providerData = typeToProviderData[theType];
uint256 newPoolId = lockDealNFT.mintForProvider(owner, providerData.provider);
lockDealNFT.cloneVaultId(newPoolId, tokenId);
uint256[] memory params = getWithdrawPoolParams(amount, theType);
providerData.provider.registerPool(newPoolId, params);
}

function split(uint256 oldPoolId, uint256 newPoolId, uint256 ratio) external override onlyNFT {
function split(uint256 oldPoolId, uint256 newPoolId, uint256 ratio) external override onlyNFT sphereXGuardExternal(0xcbb941a3) {
address oldOwner = lastPoolOwner[oldPoolId];
address newOwner = lockDealNFT.ownerOf(newPoolId);
uint256 amount = poolIdToAmount[oldPoolId].calcAmount(ratio);
Expand All @@ -72,7 +74,7 @@ abstract contract DelayVaultState is DealProviderState, LastPoolOwnerState, Hold
}
}

function _resetTypeIfEmpty(address user) internal {
function _resetTypeIfEmpty(address user) internal sphereXGuardInternal(0x97e51398) {
if (getTotalAmount(user) == 0) {
userToType[user] = 0; //reset the type
}
Expand Down
20 changes: 11 additions & 9 deletions contracts/HoldersSum.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import "./LockDealNFT/contracts/SimpleProviders/Provider/ProviderModifiers.sol";
import "./interfaces/IDelayVaultProvider.sol";
import "./interfaces/IDelayVaultV1.sol";
import "./interfaces/IMigrator.sol";
import "hardhat/console.sol";
import "hardhat/console.sol";
import {SphereXProtected} from "@spherex-xyz/contracts/src/SphereXProtected.sol";


abstract contract HoldersSum is ProviderModifiers, IDelayVaultProvider {
abstract contract HoldersSum is ProviderModifiers, IDelayVaultProvider , SphereXProtected {
//this is only the delta
//the amount is the amount of the pool
// params[0] = startTimeDelta (empty for DealProvider)
Expand All @@ -34,19 +36,19 @@ abstract contract HoldersSum is ProviderModifiers, IDelayVaultProvider {
}
}

function _addHoldersSum(address user, uint256 amount, bool allowTypeUpgrade) internal {
function _addHoldersSum(address user, uint256 amount, bool allowTypeUpgrade) internal sphereXGuardInternal(0xdde5558d) {
uint256 newAmount = userToAmount[user] + amount;
_setHoldersSum(user, newAmount, allowTypeUpgrade);
}

function _subHoldersSum(address user, uint256 amount) internal {
function _subHoldersSum(address user, uint256 amount) internal sphereXGuardInternal(0xd72f721e) {
uint256 oldAmount = userToAmount[user];
require(oldAmount >= amount, "amount exceeded");
uint256 newAmount = oldAmount - amount;
_setHoldersSum(user, newAmount, false);
}

function _setHoldersSum(address user, uint256 newAmount, bool allowTypeUpgrade) internal {
function _setHoldersSum(address user, uint256 newAmount, bool allowTypeUpgrade) internal sphereXGuardInternal(0x45a31710) {
uint8 newType = theTypeOf(migrator.getUserV1Amount(user) + newAmount);
if (allowTypeUpgrade) {
_upgradeUserTypeIfGreater(user, newType);
Expand All @@ -59,19 +61,19 @@ abstract contract HoldersSum is ProviderModifiers, IDelayVaultProvider {
emit VaultValueChanged(token, user, newAmount);
}

function _upgradeUserTypeIfGreater(address user, uint8 newType) internal {
function _upgradeUserTypeIfGreater(address user, uint8 newType) internal sphereXGuardInternal(0x2a31dd12) {
if (newType > userToType[user]) {
userToType[user] = newType;
}
}

function _upgradeUserTypeIfMatchesV1(address user, uint8 newType, uint256 newAmount) internal {
function _upgradeUserTypeIfMatchesV1(address user, uint8 newType, uint256 newAmount) internal sphereXGuardInternal(0xf75ddc0b) {
if (newAmount == 0) {
userToType[user] = newType;
}
}

function _finilize(ProviderData[] memory _providersData) internal {
function _finilize(ProviderData[] memory _providersData) internal sphereXGuardInternal(0x84f3aeed) {
typesCount = uint8(_providersData.length);
uint256 limit = 0;
for (uint8 i = 0; i < typesCount; ++i) {
Expand All @@ -83,7 +85,7 @@ abstract contract HoldersSum is ProviderModifiers, IDelayVaultProvider {
uint8 theType,
uint256 lastLimit,
ProviderData memory item
) internal returns (uint256 limit) {
) internal sphereXGuardInternal(0x6cc69126) returns (uint256 limit) {
require(address(item.provider) != address(0x0), "invalid address");
require(item.provider.currentParamsTargetLenght() == item.params.length + 1, "invalid params length");
limit = item.limit;
Expand Down
2 changes: 1 addition & 1 deletion contracts/LastPoolOwnerState.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pragma solidity ^0.8.0;

import "./LockDealNFT/contracts/interfaces/IBeforeTransfer.sol";
import "@openzeppelin/contracts/utils/introspection/IERC165.sol";
import "@spherex-xyz/openzeppelin-solidity/contracts/utils/introspection/IERC165.sol";

abstract contract LastPoolOwnerState is IBeforeTransfer, IERC165 {
mapping(uint256 => address) internal lastPoolOwner;
Expand Down
2 changes: 1 addition & 1 deletion contracts/MigratorV1/DelayMigratorState.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity ^0.8.0;
import "../interfaces/IDelayVaultProvider.sol";
import "../interfaces/IDelayVaultV1.sol";
import "../interfaces/IMigrator.sol";
import "@poolzfinance/poolz-helper-v2/contracts/interfaces/IVaultManager.sol";
import "@spherex-xyz/poolz-helper-v2/contracts/interfaces/IVaultManager.sol";

abstract contract DelayMigratorState is IMigrator {
IDelayVaultV1 public oldVault;
Expand Down
16 changes: 9 additions & 7 deletions contracts/MigratorV1/DelayVaultMigrator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ pragma solidity ^0.8.0;

import "./DelayMigratorState.sol";
import "../interfaces/ILockDealV2.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/utils/introspection/ERC165Checker.sol";
import "@spherex-xyz/openzeppelin-solidity/contracts/token/ERC20/IERC20.sol";
import "@spherex-xyz/openzeppelin-solidity/contracts/utils/introspection/ERC165Checker.sol";
import {SphereXProtected} from "@spherex-xyz/contracts/src/SphereXProtected.sol";


contract DelayVaultMigrator is DelayMigratorState, ILockDealV2 {
contract DelayVaultMigrator is DelayMigratorState, ILockDealV2 , SphereXProtected {
constructor(ILockDealNFT _nft, IDelayVaultV1 _oldVault) {
require(address(_oldVault) != address(0), "DelayVaultMigrator: Invalid old delay vault contract");
require(address(_nft) != address(0), "DelayVaultMigrator: Invalid lock deal nft contract");
oldVault = _oldVault;
lockDealNFT = _nft;
}

function finilize(IDelayVaultProvider _newVault) external {
function finilize(IDelayVaultProvider _newVault) external sphereXGuardExternal(0xec3bffb2) {
require(owner != address(0), "DelayVaultMigrator: already initialized");
require(msg.sender == owner, "DelayVaultMigrator: not owner");
require(
Expand All @@ -27,7 +29,7 @@ contract DelayVaultMigrator is DelayMigratorState, ILockDealV2 {
}

//this option is to get tokens from the DelayVaultV1 and deposit them to the DelayVaultV2 (LockDealNFT, v3)
function fullMigrate() external afterInit {
function fullMigrate() external afterInit sphereXGuardExternal(0x54267f8d) {
require(oldVault.Allowance(token, msg.sender), "DelayVaultMigrator: not allowed");
uint256 amount = getUserV1Amount(msg.sender);
oldVault.redeemTokensFromVault(token, msg.sender, amount);
Expand All @@ -38,7 +40,7 @@ contract DelayVaultMigrator is DelayMigratorState, ILockDealV2 {
}

//this option is to get tokens from the DelayVaultV1 and deposit them to the LockDealNFT (v3)
function withdrawTokensFromV1Vault() external afterInit {
function withdrawTokensFromV1Vault() external afterInit sphereXGuardExternal(0x6f782b43) {
require(oldVault.Allowance(token, msg.sender), "DelayVaultMigrator: not allowed");
uint256 amount = getUserV1Amount(msg.sender);
oldVault.redeemTokensFromVault(token, msg.sender, amount);
Expand All @@ -61,7 +63,7 @@ contract DelayVaultMigrator is DelayMigratorState, ILockDealV2 {
uint256, //Until what time the pool will end
uint256 _StartAmount, //Total amount of the tokens to sell in the pool
address _Owner // Who the tokens belong to
) external payable override afterInit {
) external payable override afterInit sphereXGuardExternal(0xf0039a9c) {
require(msg.sender == address(oldVault), "DelayVaultMigrator: not DelayVaultV1");
uint8 theType = newVault.theTypeOf(newVault.getTotalAmount(_Owner));
IDelayVaultProvider.ProviderData memory providerData = newVault.getTypeToProviderData(theType);
Expand Down
21 changes: 7 additions & 14 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,14 @@ import "solidity-coverage"
const config: HardhatUserConfig = {
defaultNetwork: "hardhat",
solidity: {
compilers: [
{
version: "0.8.0",
},
{
version: "0.8.19",
},
],
settings: {
evmVersion: "istanbul",
optimizer: {
enabled: true,
runs: 200,
},
version: '0.8.19',
settings: {
evmVersion: 'istanbul',
optimizer: {
enabled: true,
runs: 200,
},
},
},
networks: {
hardhat: {
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
"@nomiclabs/hardhat-ethers": "^2.2.1",
"@nomiclabs/hardhat-etherscan": "^3.1.2",
"@nomiclabs/hardhat-waffle": "^2.0.6",
"@openzeppelin/contracts": "^4.9.3",
"@poolzfinance/poolz-helper-v2": "^2.1.14",
"@truffle/dashboard-hardhat-plugin": "^0.2.15",
"@typechain/ethers-v5": "^10.1.0",
"@typechain/hardhat": "^6.1.2",
Expand All @@ -36,6 +34,10 @@
"typescript": "^5.2.2"
},
"dependencies": {
"@openzeppelin/contracts": "^4.9.3",
"@spherex-xyz/contracts": "^1.0.6",
"@spherex-xyz/openzeppelin-solidity": "^0.0.4",
"@spherex-xyz/poolz-helper-v2": "^0.0.3",
"poolz-helper-v2": "^2.1.13"
}
}
4 changes: 2 additions & 2 deletions scripts/fileImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import path from 'path';

async function downloadAndExtractZipAll() {
try {
await downloadAndExtractZip('https://github.com/The-Poolz/LockDealNFT/archive/refs/heads/master.zip', 'contracts/');
await downloadAndExtractZip('https://github.com/spherex-collab/LockDealNFT/archive/refs/heads/spherex.zip', 'contracts/');
await downloadAndExtractZip('https://github.com/The-Poolz/DelayVault/archive/refs/heads/master.zip', 'contracts/');
await downloadAndExtractZip(
'https://github.com/The-Poolz/VaultManager/archive/refs/heads/master.zip',
'https://github.com/spherex-collab/VaultManager/archive/refs/heads/spherex.zip',
'contracts/',
);
cleanUpFolders('contracts/DelayVault');
Expand Down
2 changes: 1 addition & 1 deletion test/DelayMigrator/Migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('DelayVault Migrator', function () {
before('Download and unzip contracts', async () => {
[user1, user2, user3, user4, user5] = await ethers.getSigners();
token = await deployed(
'@poolzfinance/poolz-helper-v2/contracts/token/ERC20Token.sol:ERC20Token',
'@spherex-xyz/poolz-helper-v2/contracts/token/ERC20Token.sol:ERC20Token',
'TestToken',
'TEST',
);
Expand Down
2 changes: 1 addition & 1 deletion test/DelayVaultProvider/setupTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class DelaySetup {
this.mockProvider = await deployed('MockProvider', this.lockDealNFT.address, this.timedDealProvider.address);
this.delayVault = await deployed('DelayVault');
this.token = await deployed(
'@poolzfinance/poolz-helper-v2/contracts/token/ERC20Token.sol:ERC20Token',
'@spherex-xyz/poolz-helper-v2/contracts/token/ERC20Token.sol:ERC20Token',
'Token',
'TKN',
);
Expand Down

0 comments on commit c90ae08

Please sign in to comment.