Skip to content

Commit

Permalink
Merge branch 'master' into agent-v2
Browse files Browse the repository at this point in the history
# Conflicts:
#	contracts/AbstractPowerIndexRouter.sol
#	contracts/PowerIndexVaultRouter.sol
#	test/helpers/index.js
#	test/implementations/TornConnector.unit.js
  • Loading branch information
defi-dev committed Aug 8, 2022
2 parents 68ebfba + e4c201e commit 67a0e6b
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 81 deletions.
2 changes: 1 addition & 1 deletion .solhint.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"plugins": ["prettier"],
"rules": {
"func-visibility": ["warn", { "ignoreConstructors": true }],
"code-complexity": ["error", 11],
"code-complexity": ["error", 12],
"compiler-version": ["error", "^0.7.0"],
"constructor-syntax": "error",
"max-line-length": ["error", 120],
Expand Down
20 changes: 10 additions & 10 deletions contracts/AbstractPowerIndexRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ abstract contract AbstractPowerIndexRouter is PowerIndexRouterInterface, Ownable
uint256 internal constant COMPENSATION_PLAN_1_ID = 1;
uint256 public constant HUNDRED_PCT = 1 ether;

event SetReserveConfig(uint256 ratio, uint256 ratioLowerBound, uint256 ratioUpperBound, uint256 pokeInterval, uint256 claimRewardsInterval);
event SetReserveConfig(
uint256 ratio,
uint256 ratioLowerBound,
uint256 ratioUpperBound,
uint256 pokeInterval,
uint256 claimRewardsInterval
);
event SetPerformanceFee(uint256 performanceFee);
event SetConnector(
address indexed connector,
Expand Down Expand Up @@ -110,7 +116,7 @@ abstract contract AbstractPowerIndexRouter is PowerIndexRouterInterface, Ownable
}

modifier onlyAgent() {
require(msg.sender == address(powerAgent));
require(msg.sender == address(powerAgent), "ONLY_AGENT");
_;
}

Expand Down Expand Up @@ -278,10 +284,7 @@ abstract contract AbstractPowerIndexRouter is PowerIndexRouterInterface, Ownable
}

function agentResolver() external view returns (bool, bytes memory) {
return (
isPokeReady(),
abi.encodeWithSelector(AbstractPowerIndexRouter.pokeFromAgent.selector, true)
);
return (isPokeReady(), abi.encodeWithSelector(AbstractPowerIndexRouter.pokeFromAgent.selector, true));
}

/**
Expand Down Expand Up @@ -327,10 +330,7 @@ abstract contract AbstractPowerIndexRouter is PowerIndexRouterInterface, Ownable

_rebalance(state, _claimAndDistributeRewards);

require(
_canPoke(state.atLeastOneForceRebalance),
"INTERVAL_NOT_REACHED_OR_NOT_FORCE"
);
require(_canPoke(state.atLeastOneForceRebalance), "INTERVAL_NOT_REACHED_OR_NOT_FORCE");

lastRebalancedByPokerAt = block.timestamp;
}
Expand Down
8 changes: 6 additions & 2 deletions contracts/connectors/BakeryChefPowerIndexConnector.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ contract BakeryChefPowerIndexConnector is AbstractStakeRedeemConnector {

/*** VIEWERS ***/

function getPendingRewards() external view returns (uint256 amount) {
function getPendingRewards() public view returns (uint256 amount) {
return IBakeryMasterChef(STAKING).pendingBake(address(UNDERLYING), address(PI_TOKEN));
}

Expand Down Expand Up @@ -69,7 +69,11 @@ contract BakeryChefPowerIndexConnector is AbstractStakeRedeemConnector {
* @notice Checking: is pending rewards enough to reinvest
* @param _claimParams Claim parameters, that stored in PowerIndexRouter
*/
function isClaimAvailable(bytes calldata _claimParams, uint256, uint256) external view override virtual returns (bool) {
function isClaimAvailable(
bytes calldata _claimParams,
uint256,
uint256
) external view virtual override returns (bool) {
uint256 minClaimAmount = unpackClaimParams(_claimParams);
return getPendingRewards() >= minClaimAmount;
}
Expand Down
8 changes: 6 additions & 2 deletions contracts/connectors/MasterChefPowerIndexConnector.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ contract MasterChefPowerIndexConnector is AbstractStakeRedeemConnector {

/*** VIEWERS ***/

function getPendingRewards() external view returns (uint256 amount) {
function getPendingRewards() public view returns (uint256 amount) {
return IMasterChefV1(STAKING).pending(MASTER_CHEF_PID, address(PI_TOKEN));
}

Expand Down Expand Up @@ -72,7 +72,11 @@ contract MasterChefPowerIndexConnector is AbstractStakeRedeemConnector {
* @notice Checking: is pending rewards enough to reinvest
* @param _claimParams Claim parameters, that stored in PowerIndexRouter
*/
function isClaimAvailable(bytes calldata _claimParams, uint256, uint256) external view override virtual returns (bool) {
function isClaimAvailable(
bytes calldata _claimParams,
uint256,
uint256
) external view virtual override returns (bool) {
uint256 minClaimAmount = unpackClaimParams(_claimParams);
return getPendingRewards() >= minClaimAmount;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ contract PancakeMasterChefIndexConnector is AbstractStakeRedeemConnector {
* @notice Checking: is pending rewards enough to reinvest
* @param _claimParams Claim parameters, that stored in PowerIndexRouter
*/
function isClaimAvailable(bytes calldata _claimParams, uint256, uint256) external view override virtual returns (bool) {
function isClaimAvailable(
bytes calldata _claimParams,
uint256,
uint256
) external view virtual override returns (bool) {
uint256 minClaimAmount = unpackClaimParams(_claimParams);
return getPendingRewards() >= minClaimAmount;
}
Expand Down
1 change: 0 additions & 1 deletion contracts/connectors/TornPowerIndexConnector.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ contract TornPowerIndexConnector is AbstractProfitDistributionConnector {
UNDERLYING,
receivedReward
);
_approveToStaking(rewardsToReinvest);
_stakeImpl(rewardsToReinvest);
return stakeData;
}
Expand Down
2 changes: 1 addition & 1 deletion hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ const config = {
version: '0.7.6',
},
{
version: "0.8.15",
version: '0.8.15',
settings: {
optimizer: {
enabled: true,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"build": "yarn run compile && yarn run typechain",
"clean": "hardhat clean",
"compile": "hardhat compile",
"compile-release": "rm -rf ./artifacts && COMPILE_TARGET=release hardhat compile",
"compile-release": "rm -rf ./artifacts && rm -rf ./cache && COMPILE_TARGET=release hardhat compile",
"coverage": "hardhat coverage --show-stack-traces --solcoverjs ./.solcover.js --network coverage --temp artifacts --testfiles \"./test/**/*.js\"",
"lint:sol": "solhint --config ./.solhint.json \"contracts/**/*.sol\"",
"lint:js": "eslint --config .eslintrc.json --ignore-path ./.eslintignore --ext .js .",
Expand Down
81 changes: 24 additions & 57 deletions tasks/redeployTornRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ require('@nomiclabs/hardhat-truffle5');
require('@nomiclabs/hardhat-ethers');

task('redeploy-torn-router', 'Redeploy TornRouter').setAction(async (__, {ethers, network}) => {
const {ether, fromEther, impersonateAccount, gwei, increaseTime, advanceBlocks} = require('../test/helpers');
const {ether, fromEther, impersonateAccount, increaseTime, advanceBlocks} = require('../test/helpers');
const WrappedPiErc20 = await artifacts.require('WrappedPiErc20');
const IERC20 = await artifacts.require('WrappedPiErc20');
const PowerIndexRouter = await artifacts.require('PowerIndexRouter');
Expand All @@ -16,51 +16,39 @@ task('redeploy-torn-router', 'Redeploy TornRouter').setAction(async (__, {ethers

const [deployer] = await web3.eth.getAccounts();
console.log('deployer', deployer);
const sendOptions = { from: deployer };

const OWNER = '0xB258302C3f209491d604165549079680708581Cc';
const piTornAddress = '0xa1ebc8bde2f1f87fe24f384497b6bd9ce3b14345';
const tornConnectorAddress = '0x887d871b5aE02dFC35d1ba579461CbE4ed3D95b7';
const TORN_STAKING = '0x2fc93484614a34f26f7970cbb94615ba109bb4bf';
const TORN_GOVERNANCE = '0x5efda50f22d34f262c29268506c5fa42cb56a1ce';
const tornAddress = '0x77777feddddffc19ff86db637967013e6c6a116c';

const startBalance = fromEther(await web3.eth.getBalance(deployer));
const tornRouter = await PowerIndexRouter.new(
piTornAddress,
{
poolRestrictions: '0x698967cA2fB85A6D9a7D2BeD4D2F6D32Bbc5fCdc',
powerPoke: '0x04D7aA22ef7181eE3142F5063e026Af1BbBE5B96',
reserveRatio: '0',
reserveRatioLowerBound: '0',
reserveRatioUpperBound: '0',
claimRewardsInterval: '86400',
performanceFeeReceiver: '0xd132973eaebbd6d7ca7b88e9170f2cca058de430',
performanceFee: '0'
}
);
const tornRouter = await PowerIndexRouter.at('0xDAf584C15722cdc7E78214Fb1A4832dA6638D655');
console.log('tornRouter', tornRouter.address);
const piTorn = await WrappedPiErc20.at(piTornAddress);
const tornConnector = await TornPowerIndexConnector.at(tornConnectorAddress);
const tornConnector = await TornPowerIndexConnector.new(TORN_STAKING, tornAddress, piTorn.address, TORN_GOVERNANCE);
console.log('tornConnector', tornConnector.address);

console.log('tornConnector done');

const endBalance = fromEther(await web3.eth.getBalance(deployer));
console.log('balance spent', startBalance - endBalance);
if (network.name !== 'mainnetfork') {
return;
}

await impersonateAccount(ethers, OWNER);
await tornRouter.initRouterByConnector('0', '0x', {from: OWNER});
await tornRouter.setConnectorList([
{
connector: tornConnector.address,
share: ether(1),
callBeforeAfterPoke: false,
newConnector: true,
newConnector: false,
connectorIndex: 0,
},
]);

await tornRouter.setClaimParams('0', await tornConnector.contract.methods.packClaimParams('2592000', '564341').call({}), sendOptions);
console.log('tornConnector done');
], {from: OWNER});

await tornRouter.transferOwnership(OWNER, sendOptions);
const endBalance = fromEther(await web3.eth.getBalance(deployer));
console.log('balance spent', startBalance - endBalance);
if (network.name !== 'mainnetfork') {
return;
}
const PowerPoke = await artifacts.require('IPowerPoke');
const ITornGovernance = await artifacts.require('ITornGovernance');
const ITornStaking = await artifacts.require('ITornStaking');

Expand All @@ -75,42 +63,16 @@ task('redeploy-torn-router', 'Redeploy TornRouter').setAction(async (__, {ethers
await piTorn.deposit(amount, {from: tornHolder});
console.log('2 wrapper balance', fromEther(await torn.balanceOf(piTorn.address)));

const BONUS_NUMERATOR = '7610350076';
const BONUS_DENUMERATOR = '10000000000000000';
const MIN_REPORT_INTERVAL = 60 * 60 * 24 * 14;
const MAX_REPORT_INTERVAL = MIN_REPORT_INTERVAL + 60 * 60;
const MAX_GAS_PRICE = gwei(500);
const PER_GAS = '10000';
const MIN_SLASHING_DEPOSIT = ether(40);

await impersonateAccount(ethers, OWNER);
const oldRouter = await PowerIndexRouter.at('0x0a6AA119C58cE6e7733dA6ECe7fBa5668d897c7C');
await oldRouter.migrateToNewRouter(piTorn.address, tornRouter.address, [], {from: OWNER});

const powerPokeAddress = '0x04D7aA22ef7181eE3142F5063e026Af1BbBE5B96';
const cvpAddress = '0x38e4adb44ef08f22f5b5b76a8f0c2d0dcbe7dca1';
const powerPoke = await PowerPoke.at(powerPokeAddress);
await powerPoke.addClient(tornRouter.address, OWNER, true, MAX_GAS_PRICE, MIN_REPORT_INTERVAL, MAX_REPORT_INTERVAL, {from: OWNER});
await powerPoke.setMinimalDeposit(tornRouter.address, MIN_SLASHING_DEPOSIT, {from: OWNER});
await powerPoke.setBonusPlan(tornRouter.address, '1', true, BONUS_NUMERATOR, BONUS_DENUMERATOR, PER_GAS, {from: OWNER});
await powerPoke.setFixedCompensations(tornRouter.address, 200000, 60000, {from: OWNER});

const cvp = await IERC20.at(cvpAddress);
await cvp.approve(powerPoke.address, ether(10000), {from: OWNER});
await powerPoke.addCredit(tornRouter.address, ether(10000), {from: OWNER});

const powerPokeOpts = web3.eth.abi.encodeParameter(
{ PowerPokeRewardOpts: {to: 'address', compensateInETH: 'bool'} },
{to: pokerReporter, compensateInETH: true},
);

await impersonateAccount(ethers, pokerReporter);

await tornRouter.pokeFromReporter('1', false, powerPokeOpts, {from: pokerReporter});
// await tornRouter.pokeFromReporter('1', false, powerPokeOpts, {from: pokerReporter});

console.log('3 wrapper balance', fromEther(await torn.balanceOf(piTorn.address)));
const TORN_STAKING = '0x2fc93484614a34f26f7970cbb94615ba109bb4bf';
const TORN_GOVERNANCE = '0x5efda50f22d34f262c29268506c5fa42cb56a1ce';
const governance = await ITornGovernance.at(TORN_GOVERNANCE);
const staking = await ITornStaking.at(TORN_STAKING);
console.log('lockedBalance', fromEther(await governance.lockedBalance(piTorn.address)));
Expand Down Expand Up @@ -141,6 +103,11 @@ task('redeploy-torn-router', 'Redeploy TornRouter').setAction(async (__, {ethers

console.log('lockedBalance', fromEther(await governance.lockedBalance(piTorn.address)));

await torn.approve(piTorn.address, amount, {from: tornHolder});
await piTorn.deposit(amount, {from: tornHolder});

console.log('lockedBalance after deposit', fromEther(await governance.lockedBalance(piTorn.address)));

function getClaimParams(duration) {
return tornConnector.packClaimParams(duration, GAS_TO_REINVEST);
}
Expand Down
1 change: 0 additions & 1 deletion test/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,6 @@ async function deployContractWithBytecode(name, web3, args) {
});

Contract.setProvider(web3.currentProvider);
console.log('name', name, 'args', args);
return Contract.new.apply(Contract, args);
}

Expand Down
2 changes: 0 additions & 2 deletions test/implementations/BakeryMasterChefRouter.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,7 @@ describe('BakeryMasterChefRouter Tests', () => {
});

it('should increase reserve if required', async () => {
console.log("1 piBake.getUnderlyingBalance", await piBake.getUnderlyingBalance());
await bake.transfer(piBake.address, ether(1000), { from: deployer });
console.log("2 piBake.getUnderlyingBalance", await piBake.getUnderlyingBalance());

let stakeAndClaimStatus = await myRouter.getStakeAndClaimStatusByConnectorIndex('0', false);
assert.equal(stakeAndClaimStatus.diff, '800000000000000000000');
Expand Down
16 changes: 14 additions & 2 deletions test/implementations/TornConnector.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const TornPowerIndexConnector = artifacts.require('MockTornPowerIndexConnector')
const PowerIndexVaultRouter = artifacts.require('PowerIndexVaultRouter');
const WrappedPiErc20 = artifacts.require('WrappedPiErc20');
const MockPoolRestrictions = artifacts.require('MockPoolRestrictions');
const MockPoke = artifacts.require('MockPoke');

const TornGovernance = artifacts.require('TornGovernance');
const TornStaking = artifacts.require('TornStaking');
Expand All @@ -25,7 +24,6 @@ TornGovernance.numberFormat = 'String';

const { web3 } = MockERC20;

const REPORTER_ID = 42;
const chainId = 31337;

describe('TornConnector Tests', () => {
Expand Down Expand Up @@ -376,6 +374,14 @@ describe('TornConnector Tests', () => {
assert.equal(await myRouter.getUnderlyingAvailable(), ether('24050'));
assert.equal(await myRouter.getUnderlyingTotal(), ether('24050'));

vrs = await getPermitVrs(ether(100), bob);
await piTorn.depositWithPermit(ether(100), vrs.deadline, vrs.v, vrs.r, vrs.s, {from: bob});
// console.log('withdraw gasUsed', res.receipt.gasUsed);
assert.equal(await torn.balanceOf(bob), ether('55100'));
assert.equal(await piTorn.balanceOf(bob), ether('2900.0009774904450309'));
assert.equal(await torn.balanceOf(piTorn.address), '0');
assert.equal(await governance.lockedBalance(piTorn.address), ether(24150));

async function getPermitVrs(value, owner) {
const deadline = (await latestBlockTimestamp()) + 10;
if(!nonce[owner]) {
Expand Down Expand Up @@ -491,6 +497,12 @@ describe('TornConnector Tests', () => {
assert.equal(await myRouter.getUnderlyingAvailable(), ether('21625'));
assert.equal(await myRouter.getUnderlyingTotal(), ether('21625'));

vrs = await getPermitVrs(ether(1000), bob);
await piTorn.depositWithPermit(ether(1000), vrs.deadline, vrs.v, vrs.r, vrs.s, {from: bob});
// console.log('2 deposit by bob gasUsed', res.receipt.gasUsed);
assert.equal(await torn.balanceOf(piTorn.address), ether('2262.5'));
assert.equal(await governance.lockedBalance(piTorn.address), ether('20362.5'));

async function getPermitVrs(value, owner) {
const deadline = (await latestBlockTimestamp()) + 10;
if(!nonce[owner]) {
Expand Down

0 comments on commit 67a0e6b

Please sign in to comment.