Skip to content

Commit

Permalink
Merge pull request #690 from oceanprotocol/ve
Browse files Browse the repository at this point in the history
Update veAllocate & DFStrategyV1 contracts
  • Loading branch information
alexcos20 committed Aug 30, 2022
2 parents a61587a + 89f46ba commit d7923de
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 96 deletions.
50 changes: 10 additions & 40 deletions contracts/df/DFStrategyV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "../interfaces/IDFRewards.sol";
import "../interfaces/IPool.sol";

interface IveOCEAN {
function deposit_for(address _address, uint256 _amount) external;
}

contract DFStrategyV1 is ReentrancyGuard {
using SafeERC20 for IERC20;
Expand Down Expand Up @@ -45,35 +48,21 @@ contract DFStrategyV1 is ReentrancyGuard {
return result;
}

/*
* @dev Claims rewards and stakes them into multiple pools.
* @param tokenAddress Token address to claim
* @param poolAddress Array of pool address to stake the rewards
* @param amount Array of amount to stake in each pool.
*/
function claimAndStake(
address tokenAddress,
address[] calldata poolAddress,
uint256[] calldata amount
uint256 totalAmount,
address _veOCEAN
) public nonReentrant returns (bool) {
require(poolAddress.length == amount.length, "Lengths must match");
uint256 totalAmount = 0;
uint256 i;
for (i = 0; i < amount.length; i += 1) {
totalAmount += amount[i];
}
require(
dfrewards.claimable(msg.sender, tokenAddress) > totalAmount,
dfrewards.claimable(msg.sender, tokenAddress) >= totalAmount,
"Not enough rewards"
);
uint256 balanceBefore = IERC20(tokenAddress).balanceOf(address(this));
uint256 claimed = dfrewards.claimForStrat(msg.sender, tokenAddress); // claim rewards for strategy
uint256 claimed = dfrewards.claimForStrat(msg.sender, tokenAddress); // claim rewards for the strategy
uint256 balanceAfter = IERC20(tokenAddress).balanceOf(address(this));
require(balanceAfter - balanceBefore == claimed, "Not enough rewards");

for (i = 0; i < amount.length; i += 1) {
stake(tokenAddress, poolAddress[i], amount[i], msg.sender);
}
IERC20(tokenAddress).safeApprove(_veOCEAN, totalAmount);
IveOCEAN(_veOCEAN).deposit_for(msg.sender, totalAmount);

if (claimed > totalAmount) {
IERC20(tokenAddress).safeTransfer(
Expand All @@ -84,23 +73,4 @@ contract DFStrategyV1 is ReentrancyGuard {

return true;
}

function stake(
address tokenAddress,
address poolAddress,
uint256 amount,
address _to
) internal returns (bool) {
require(
tokenAddress == IPool(poolAddress).getBaseTokenAddress(),
"Cannot stake"
);
uint256 balanceBefore = IERC20(poolAddress).balanceOf(address(this));
IERC20(tokenAddress).approve(poolAddress, amount);
IPool(poolAddress).joinswapExternAmountIn(amount, 0);
uint256 sharesBalance = IERC20(poolAddress).balanceOf(address(this)) -
balanceBefore;
IERC20(poolAddress).safeTransfer(_to, sharesBalance);
return true;
}
}
25 changes: 12 additions & 13 deletions contracts/ve/veAllocate.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ pragma solidity ^0.8.12;
// Code is Apache-2.0 and docs are CC-BY-4.0

contract veAllocate {
mapping(address => mapping(address => mapping(uint256 => uint256))) private veAllocation;
mapping(address => mapping(address => mapping(uint256 => uint256)))
private veAllocation;
mapping(address => uint256) private _totalAllocation;

event AllocationSet(
Expand All @@ -14,36 +15,34 @@ contract veAllocate {
uint256 amount
);

function getveAllocation(address _address, address _nft, uint256 chainid)
public
view
returns (uint256)
{
return veAllocation[_address][_nft][chainid];
function getveAllocation(
address user,
address nft,
uint256 chainid
) external view returns (uint256) {
return veAllocation[user][nft][chainid];
}

function getTotalAllocation(address _address)
function getTotalAllocation(address user)
public
view
returns (uint256)
{
return _totalAllocation[_address];
return _totalAllocation[user];
}


function setAllocation(
uint256 amount,
address nft,
uint256 chainId
) external {
require(amount <= 1000, "BM");

_totalAllocation[msg.sender] =
_totalAllocation[msg.sender] +
amount -
veAllocation[msg.sender][nft][chainId];

require(_totalAllocation[msg.sender] <= 10000, "Max Allocation");
veAllocation[msg.sender][nft][chainId] = amount;
emit AllocationSet(msg.sender, nft, chainId, amount);
}
}
}
88 changes: 45 additions & 43 deletions scripts/deploy-contracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ let shouldDeployOPFCommunityFeeCollector = false;
const shouldDeployOPFCommunity = true;
const logging = true;
const show_verify = true;


async function main() {
const url = process.env.NETWORK_RPC_URL;
console.log("Using RPC: " + url);
Expand Down Expand Up @@ -267,10 +269,9 @@ async function main() {
addresses.OPFCommunityFeeCollector = OPFCommunityFeeCollectorAddress;
}

// v4 contracts
if (shouldDeployV4) {
if (logging) console.info("Deploying V4 contracts");
// v4 contracts

// DEPLOY ROUTER, SETTING OWNER

/*if (logging) console.info("Deploying BPool");
Expand Down Expand Up @@ -460,45 +461,7 @@ async function main() {

}
}
if (shouldDeployDF) {
//DF contracts
if (logging) console.info("Deploying DFRewards");
const DFRewards = await ethers.getContractFactory(
"DFRewards",
owner
);
const deployedDFRewards = await DFRewards.connect(owner).deploy(options);
await deployedDFRewards.deployTransaction.wait();
addresses.DFRewards = deployedDFRewards.address;
if (show_verify) {
console.log("\tRun the following to verify on etherscan");
console.log("\tnpx hardhat verify --network " + networkName + " " + addresses.DFRewards)
}
if (sleepAmount > 0) await sleep(sleepAmount)

if (logging) console.info("Deploying DFStrategyV1");
const DFStrategyV1 = await ethers.getContractFactory(
"DFStrategyV1",
owner
);
const deployedDFStrategyV1 = await DFStrategyV1.connect(owner).deploy(addresses.DFRewards, options);
await deployedDFStrategyV1.deployTransaction.wait();
addresses.DFStrategyV1 = deployedDFStrategyV1.address;
if (show_verify) {
console.log("\tRun the following to verify on etherscan");
console.log("\tnpx hardhat verify --network " + networkName + " " + addresses.DFStrategyV1 + " " + addresses.DFRewards)
}
if (sleepAmount > 0) await sleep(sleepAmount)
//add strategy to DFRewards
const strategyTx = await deployedDFRewards.connect(owner).addStrategy(addresses.DFStrategyV1, options);
await strategyTx.wait();
if (owner.address != routerOwner) {
if (logging) console.info("Moving ownerships to " + routerOwner)
const DFRewardsOwnerTx = await deployedDFRewards.connect(owner).transferOwnership(routerOwner, options)
await DFRewardsOwnerTx.wait()
}

}
//VE contracts
if (shouldDeployVE) {
//veAllocate
Expand Down Expand Up @@ -531,8 +494,6 @@ async function main() {
}
if (sleepAmount > 0) await sleep(sleepAmount)



//veDelegation
if (logging) console.info("Deploying veDelegation");
const veDelegation = await ethers.getContractFactory(
Expand Down Expand Up @@ -590,6 +551,46 @@ async function main() {
if (sleepAmount > 0) await sleep(sleepAmount)
}

//DF contracts
if (shouldDeployDF) {
//DFRewards
if (logging) console.info("Deploying DFRewards");
const DFRewards = await ethers.getContractFactory(
"DFRewards",
owner
);
const deployedDFRewards = await DFRewards.connect(owner).deploy(options);
await deployedDFRewards.deployTransaction.wait();
addresses.DFRewards = deployedDFRewards.address;
if (show_verify) {
console.log("\tRun the following to verify on etherscan");
console.log("\tnpx hardhat verify --network " + networkName + " " + addresses.DFRewards)
}
if (sleepAmount > 0) await sleep(sleepAmount)

//DFStrategyV1
if (logging) console.info("Deploying DFStrategyV1");
const DFStrategyV1 = await ethers.getContractFactory(
"DFStrategyV1",
owner
);
const deployedDFStrategyV1 = await DFStrategyV1.connect(owner).deploy(addresses.DFRewards, options);
await deployedDFStrategyV1.deployTransaction.wait();
addresses.DFStrategyV1 = deployedDFStrategyV1.address;
if (show_verify) {
console.log("\tRun the following to verify on etherscan");
console.log("\tnpx hardhat verify --network " + networkName + " " + addresses.DFStrategyV1 + " " + addresses.DFRewards)
}
if (sleepAmount > 0) await sleep(sleepAmount)
//add strategy to DFRewards
const strategyTx = await deployedDFRewards.connect(owner).addStrategy(addresses.DFStrategyV1, options);
await strategyTx.wait();
if (owner.address != routerOwner) {
if (logging) console.info("Moving ownerships to " + routerOwner)
const DFRewardsOwnerTx = await deployedDFRewards.connect(owner).transferOwnership(routerOwner, options)
await DFRewardsOwnerTx.wait()
}
}

if (addressFile) {
// write address.json if needed
Expand All @@ -610,12 +611,13 @@ async function main() {
}



async function sleep(s) {
return new Promise((resolve) => {
setTimeout(resolve, s * 1000)
})
}


// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main()
Expand Down

0 comments on commit d7923de

Please sign in to comment.