From 9d6e55902e0345d4ca4379483f9e0e226d848669 Mon Sep 17 00:00:00 2001 From: defi-dev Date: Tue, 10 May 2022 21:53:27 +0800 Subject: [PATCH 1/4] remove approve from torn connector reinvest block, and test --- contracts/connectors/TornPowerIndexConnector.sol | 1 - test/implementations/TornConnector.unit.js | 14 ++++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/contracts/connectors/TornPowerIndexConnector.sol b/contracts/connectors/TornPowerIndexConnector.sol index 53b6b83..b0cddaf 100644 --- a/contracts/connectors/TornPowerIndexConnector.sol +++ b/contracts/connectors/TornPowerIndexConnector.sol @@ -46,7 +46,6 @@ contract TornPowerIndexConnector is AbstractConnector { if (receivedReward > 0) { uint256 rewardsToReinvest; (rewardsToReinvest, stakeData) = _distributeReward(_distributeData, PI_TOKEN, UNDERLYING, receivedReward); - _approveToStaking(rewardsToReinvest); _stakeImpl(rewardsToReinvest); return stakeData; } diff --git a/test/implementations/TornConnector.unit.js b/test/implementations/TornConnector.unit.js index 5b9f634..798d394 100644 --- a/test/implementations/TornConnector.unit.js +++ b/test/implementations/TornConnector.unit.js @@ -334,6 +334,14 @@ describe('PancakeMasterChefRouter Tests', () => { assert.equal(await myRouter.getUnderlyingAvailable(), ether('22050')); assert.equal(await myRouter.getUnderlyingTotal(), ether('22050')); + 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('57100')); + assert.equal(await piTorn.balanceOf(bob), ether('900.001086099314865775')); + assert.equal(await torn.balanceOf(piTorn.address), '0'); + assert.equal(await governance.lockedBalance(piTorn.address), ether(22150)); + async function getPermitVrs(value, owner) { const deadline = (await latestBlockTimestamp()) + 10; if(!nonce[owner]) { @@ -449,6 +457,12 @@ describe('PancakeMasterChefRouter 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]) { From b35977d943b2a80b2256ec0d660687bbc248b769 Mon Sep 17 00:00:00 2001 From: defi-dev Date: Tue, 10 May 2022 22:20:50 +0800 Subject: [PATCH 2/4] update redeployTornRouter.js --- package.json | 2 +- tasks/redeployTornRouter.js | 81 +++++++++++-------------------------- 2 files changed, 25 insertions(+), 58 deletions(-) diff --git a/package.json b/package.json index 92c3e29..ce94dad 100644 --- a/package.json +++ b/package.json @@ -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 .", diff --git a/tasks/redeployTornRouter.js b/tasks/redeployTornRouter.js index 3b83a7a..a9cd7ef 100644 --- a/tasks/redeployTornRouter.js +++ b/tasks/redeployTornRouter.js @@ -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'); @@ -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'); @@ -75,30 +63,6 @@ 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}, @@ -106,11 +70,9 @@ task('redeploy-torn-router', 'Redeploy TornRouter').setAction(async (__, {ethers 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))); @@ -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); } From 68555a848d6960c42ff4992754df755409ea2218 Mon Sep 17 00:00:00 2001 From: defi-dev Date: Mon, 8 Aug 2022 16:11:58 +0800 Subject: [PATCH 3/4] fix tests --- test/helpers/index.js | 7 +++---- test/implementations/TornConnector.unit.js | 6 +++--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/test/helpers/index.js b/test/helpers/index.js index 4c8b19e..1ecc1bb 100644 --- a/test/helpers/index.js +++ b/test/helpers/index.js @@ -463,14 +463,13 @@ function getFileContent(fileName) { } async function deployContractWithBytecode(name, web3, args) { - const CrvStackingContract = await TruffleContract({ + const Contract = await TruffleContract({ abi: getFileContent(name + 'Abi.json'), bytecode: '0x' + getFileContent(name, { encoding: 'utf8' }).replace(/(?:\r\n|\r|\n)/g, ''), }); - CrvStackingContract.setProvider(web3.currentProvider); - console.log('name', name); - return CrvStackingContract.new.apply(CrvStackingContract, args); + Contract.setProvider(web3.currentProvider); + return Contract.new.apply(Contract, args); } module.exports = { diff --git a/test/implementations/TornConnector.unit.js b/test/implementations/TornConnector.unit.js index 2fb1077..c4057c3 100644 --- a/test/implementations/TornConnector.unit.js +++ b/test/implementations/TornConnector.unit.js @@ -20,7 +20,7 @@ const TornStaking = artifacts.require('TornStaking'); MockERC20.numberFormat = 'String'; TornPowerIndexConnector.numberFormat = 'String'; WrappedPiErc20.numberFormat = 'String'; -PowerIndexRouter.numberFormat = 'String'; +PowerIndexVaultRouter.numberFormat = 'String'; TornGovernance.numberFormat = 'String'; const { web3 } = MockERC20; @@ -321,7 +321,7 @@ describe('TornConnector Tests', () => { assert.equal(await torn.balanceOf(governance.address), ether(64250)); assert.equal(await governance.lockedBalance(piTorn.address), ether(22250)); assert.equal(await myRouter.getUnderlyingStaked(), ether(22250)); - assert.equal(await myRouter.getUnderlyingReserve(), ether('0')); + assert.equal(await myRouter.getAssetsHolderUnderlyingBalance(), ether('0')); assert.equal(await myRouter.getUnderlyingAvailable(), ether(18000)); assert.equal(await myRouter.getUnderlyingTotal(), ether('22250')); assert.equal(await myRouter.calculateLockedProfit(), ether('4250')); @@ -444,7 +444,7 @@ describe('TornConnector Tests', () => { assert.equal(await torn.balanceOf(governance.address), ether(62025)); assert.equal(await governance.lockedBalance(piTorn.address), ether(20025)); assert.equal(await myRouter.getUnderlyingStaked(), ether(20025)); - assert.equal(await myRouter.getUnderlyingReserve(), ether(1800)); + assert.equal(await myRouter.getAssetsHolderUnderlyingBalance(), ether(1800)); assert.equal(await myRouter.getUnderlyingAvailable(), ether(18000)); assert.equal(await myRouter.getUnderlyingTotal(), ether('21825')); assert.equal(await myRouter.calculateLockedProfit(), ether('3825')); From b17ab65c367266be43dc2c4d7a74ce7e7e56f745 Mon Sep 17 00:00:00 2001 From: defi-dev Date: Mon, 8 Aug 2022 16:14:00 +0800 Subject: [PATCH 4/4] lint fix --- contracts/connectors/TornPowerIndexConnector.sol | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/contracts/connectors/TornPowerIndexConnector.sol b/contracts/connectors/TornPowerIndexConnector.sol index 10c8e7d..2997a82 100644 --- a/contracts/connectors/TornPowerIndexConnector.sol +++ b/contracts/connectors/TornPowerIndexConnector.sol @@ -46,7 +46,12 @@ contract TornPowerIndexConnector is AbstractProfitDistributionConnector { uint256 receivedReward = UNDERLYING.balanceOf(address(PI_TOKEN)).sub(tokenBefore); if (receivedReward > 0) { uint256 rewardsToReinvest; - (rewardsToReinvest, stakeData) = _distributeReward(_distributeData, address(PI_TOKEN), UNDERLYING, receivedReward); + (rewardsToReinvest, stakeData) = _distributeReward( + _distributeData, + address(PI_TOKEN), + UNDERLYING, + receivedReward + ); _stakeImpl(rewardsToReinvest); return stakeData; }