Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 34 additions & 34 deletions contracts/test/upgrade-savings/SavingsModuleOld.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ contract SavingsModuleOld is Module, AccessChecker, RewardDistributionsOld, Capp
event Deposit(address indexed protocol, address indexed user, uint256 nAmount, uint256 nFee);
event WithdrawToken(address indexed protocol, address indexed token, uint256 dnAmount);
event Withdraw(address indexed protocol, address indexed user, uint256 nAmount, uint256 nFee);
event UserCapEnabledChange(bool enabled);
event UserCapChanged(address indexed protocol, address indexed user, uint256 newCap);
event DefaultUserCapChanged(address indexed protocol, uint256 newCap);
event ProtocolCapEnabledChange(bool enabled);
event ProtocolCapChanged(address indexed protocol, uint256 newCap);
event VipUserEnabledChange(bool enabled);
event VipUserChanged(address indexed protocol, address indexed user, bool isVip);
// event UserCapEnabledChange(bool enabled);
// event UserCapChanged(address indexed protocol, address indexed user, uint256 newCap);
// event DefaultUserCapChanged(address indexed protocol, uint256 newCap);
// event ProtocolCapEnabledChange(bool enabled);
// event ProtocolCapChanged(address indexed protocol, uint256 newCap);
// event VipUserEnabledChange(bool enabled);
// event VipUserChanged(address indexed protocol, address indexed user, bool isVip);

using SafeERC20 for IERC20;
using SafeMath for uint256;
Expand Down Expand Up @@ -65,10 +65,10 @@ contract SavingsModuleOld is Module, AccessChecker, RewardDistributionsOld, Capp
CapperRole.initialize(_msgSender());
}

function setUserCapEnabled(bool _userCapEnabled) public onlyCapper {
userCapEnabled = _userCapEnabled;
emit UserCapEnabledChange(userCapEnabled);
}
// function setUserCapEnabled(bool _userCapEnabled) public onlyCapper {
// userCapEnabled = _userCapEnabled;
// emit UserCapEnabledChange(userCapEnabled);
// }

// function setUserCap(address _protocol, address user, uint256 cap) public onlyCapper {
// protocols[_protocol].userCap[user] = cap;
Expand All @@ -83,34 +83,34 @@ contract SavingsModuleOld is Module, AccessChecker, RewardDistributionsOld, Capp
// }
// }

function setVipUserEnabled(bool _vipUserEnabled) public onlyCapper {
vipUserEnabled = _vipUserEnabled;
emit VipUserEnabledChange(_vipUserEnabled);
}
// function setVipUserEnabled(bool _vipUserEnabled) public onlyCapper {
// vipUserEnabled = _vipUserEnabled;
// emit VipUserEnabledChange(_vipUserEnabled);
// }

function setVipUser(address _protocol, address user, bool isVip) public onlyCapper {
protocols[_protocol].isVipUser[user] = isVip;
emit VipUserChanged(_protocol, user, isVip);
}
// function setVipUser(address _protocol, address user, bool isVip) public onlyCapper {
// protocols[_protocol].isVipUser[user] = isVip;
// emit VipUserChanged(_protocol, user, isVip);
// }

function setDefaultUserCap(address _protocol, uint256 cap) public onlyCapper {
defaultUserCap[_protocol] = cap;
emit DefaultUserCapChanged(_protocol, cap);
}
// function setDefaultUserCap(address _protocol, uint256 cap) public onlyCapper {
// defaultUserCap[_protocol] = cap;
// emit DefaultUserCapChanged(_protocol, cap);
// }

function setProtocolCapEnabled(bool _protocolCapEnabled) public onlyCapper {
protocolCapEnabled = _protocolCapEnabled;
emit ProtocolCapEnabledChange(protocolCapEnabled);
}
// function setProtocolCapEnabled(bool _protocolCapEnabled) public onlyCapper {
// protocolCapEnabled = _protocolCapEnabled;
// emit ProtocolCapEnabledChange(protocolCapEnabled);
// }

function setProtocolCap(address _protocol, uint256 cap) public onlyCapper {
protocolCap[_protocol] = cap;
emit ProtocolCapChanged(_protocol, cap);
}
// function setProtocolCap(address _protocol, uint256 cap) public onlyCapper {
// protocolCap[_protocol] = cap;
// emit ProtocolCapChanged(_protocol, cap);
// }

function setWithdrawAllSlippage(address _protocol, uint256 slippageWei) public onlyOwner {
protocols[_protocol].withdrawAllSlippage = slippageWei;
}
// function setWithdrawAllSlippage(address _protocol, uint256 slippageWei) public onlyOwner {
// protocols[_protocol].withdrawAllSlippage = slippageWei;
// }

function registerProtocol(IDefiProtocol protocol, PoolToken poolToken) public onlyOwner {
uint256 i;
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@
},
"devDependencies": {
"@openzeppelin/test-helpers": "^0.5.6",
"@openzeppelin/truffle-upgrades": "^1.1.0",
"@openzeppelin/truffle-upgrades": "1.3.1",
"@truffle/hdwallet-provider": "^1.0.38",
"@typechain/truffle-v5": "^2.0.2",
"@typechain/truffle-v5": "^4.0.0",
"eth-gas-reporter": "^0.2.17",
"prettier": "^2.2.1",
"prettier-plugin-solidity": "^1.0.0-beta.2",
"solhint-plugin-prettier": "^0.0.5",
"solidity-coverage": "^0.7.9",
"truffle": "^5.1.34",
"truffle": "^5.1.58",
"truffle-contract-size": "^2.0.0",
"truffle-security": "^1.7.3",
"ts-node": "^8.10.2",
"typechain": "^2.0.0",
"typechain": "^4.0.1",
"typescript": "^3.9.7"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import {
PoolTokenContract,PoolTokenInstance,
PoolTokenOldContract,PoolTokenOldInstance,
StakingPoolContract,StakingPoolInstance,
StakingPoolAdelContract,StakingPoolAdelInstance,
FreeErc20Contract,FreeErc20Instance,
StakingPoolADELContract,StakingPoolADELInstance,
FreeERC20Contract,FreeERC20Instance,
CErc20StubContract,CErc20StubInstance,
ComptrollerStubContract,ComptrollerStubInstance
} from "../types/truffle-contracts/index";
} from "../../../types/truffle-contracts/index";


const { BN, constants, expectEvent, shouldFail, time } = require("@openzeppelin/test-helpers");
Expand All @@ -23,12 +23,12 @@ const UPGRADABLE_OPTS = {
unsafeAllowCustomTypes: true
};

import Snapshot from "./utils/snapshot";
import Snapshot from "./../../utils/snapshot";
const should = require("chai").should();
var expect = require("chai").expect;
const expectRevert= require("./utils/expectRevert");
const expectEqualBN = require("./utils/expectEqualBN");
const w3random = require("./utils/w3random");
const expectRevert= require("./../../utils/expectRevert");
const expectEqualBN = require("./../../utils/expectEqualBN");
const w3random = require("./../../utils/w3random");

const FreeERC20 = artifacts.require("FreeERC20");
const CErc20Stub = artifacts.require("CErc20Stub");
Expand All @@ -50,9 +50,9 @@ const StakingPoolADEL = artifacts.require("StakingPoolADEL");
contract("RewardDistributionModule - migration", async ([owner, user, ...otherAccounts]) => {
//let snap:Snapshot;

let dai:FreeErc20Instance;
let dai:FreeERC20Instance;
let cDai:CErc20StubInstance;
let comp:FreeErc20Instance;
let comp:FreeERC20Instance;
let comptroller:ComptrollerStubInstance;


Expand All @@ -63,10 +63,10 @@ contract("RewardDistributionModule - migration", async ([owner, user, ...otherAc
let rewardVesting:RewardVestingModuleInstance;
let compoundProtocolDai:CompoundProtocolInstance;
let poolTokenCompoundProtocolDai:PoolTokenOldInstance|PoolTokenInstance;
let akro:FreeErc20Instance;
let adel:FreeErc20Instance;
let akro:FreeERC20Instance;
let adel:FreeERC20Instance;
let stakingPoolAkro:StakingPoolInstance;
let stakingPoolAdel:StakingPoolAdelInstance;
let stakingPoolAdel:StakingPoolADELInstance;


before(async () => {
Expand Down
173 changes: 173 additions & 0 deletions test/modules/reward/RewardDistributionModule_reward_migration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import {
PoolContract, PoolInstance,
AccessModuleContract, AccessModuleInstance,
SavingsModuleContract, SavingsModuleInstance,
SavingsModuleOldContract,SavingsModuleOldInstance,
RewardDistributionModuleContract,RewardDistributionModuleInstance,
RewardVestingModuleContract, RewardVestingModuleInstance,
CompoundProtocolContract,CompoundProtocolInstance,
PoolTokenContract,PoolTokenInstance,
StakingPoolContract,StakingPoolInstance,
StakingPoolADELContract,StakingPoolADELInstance,
FreeERC20Contract,FreeERC20Instance,
CErc20StubContract,CErc20StubInstance,
ComptrollerStubContract,ComptrollerStubInstance
} from "../../../types/truffle-contracts/index";


const { BN, constants, expectEvent, shouldFail, time } = require("@openzeppelin/test-helpers");
const { deployProxy, upgradeProxy } = require('@openzeppelin/truffle-upgrades');
const UPGRADABLE_OPTS = {
unsafeAllowCustomTypes: true
};

import Snapshot from "./../../utils/snapshot";
const should = require("chai").should();
var expect = require("chai").expect;
const expectRevert= require("./../../utils/expectRevert");
const expectEqualBN = require("./../../utils/expectEqualBN");
const w3random = require("./../../utils/w3random");

const FreeERC20 = artifacts.require("FreeERC20");
const CErc20Stub = artifacts.require("CErc20Stub");
const ComptrollerStub = artifacts.require("ComptrollerStub");

const Pool = artifacts.require("Pool");
const AccessModule = artifacts.require("AccessModule");
const SavingsModule = artifacts.require("SavingsModule");
const SavingsModuleOld = artifacts.require("SavingsModuleOld");
const RewardVestingModule = artifacts.require("RewardVestingModule");
const RewardDistributionModule = artifacts.require("RewardDistributionModule");
const CompoundProtocol = artifacts.require("CompoundProtocol");
const PoolToken = artifacts.require("PoolToken");

const StakingPool = artifacts.require("StakingPool");
const StakingPoolADEL = artifacts.require("StakingPoolADEL");

contract("RewardDistributionModule - reward migration", async ([owner, user, ...otherAccounts]) => {
//let snap:Snapshot;

let dai:FreeERC20Instance;
let cDai:CErc20StubInstance;
let comp:FreeERC20Instance;
let comptroller:ComptrollerStubInstance;


let pool:PoolInstance;
let access:AccessModuleInstance;
let savings:SavingsModuleOldInstance|SavingsModuleInstance;
let rewardDistributions:RewardDistributionModuleInstance;
let rewardVesting:RewardVestingModuleInstance;
let compoundProtocolDai:CompoundProtocolInstance;
let poolTokenCompoundProtocolDai:PoolTokenInstance;
let akro:FreeERC20Instance;
let adel:FreeERC20Instance;
let stakingPoolAkro:StakingPoolInstance;
let stakingPoolAdel:StakingPoolADELInstance;


before(async () => {
//Setup external contracts
dai = await FreeERC20.new();
await dai.methods['initialize(string,string,uint8)']("Dai Stablecoin", "DAI", 18);
cDai = await CErc20Stub.new();
await cDai.methods['initialize(address)'](dai.address);
comp = await FreeERC20.new();
await comp.methods['initialize(string,string,uint8)']("Compound", "COMP", 18);
comptroller = await ComptrollerStub.new();
await comptroller.methods['initialize(address)'](comp.address);


await comptroller.setSupportedCTokens([cDai.address]);
await comp.methods['mint(address,uint256)'](comptroller.address, web3.utils.toWei('1000000000'));

//Setup system contracts
pool = await Pool.new();
await pool.methods['initialize()']();
access = await AccessModule.new();
await access.methods['initialize(address)'](pool.address);
await pool.set('access', access.address, false);

// savings = await SavingsModule.new();
// await savings.methods['initialize(address)'](pool.address);
savings = await deployProxy(SavingsModuleOld, [pool.address], UPGRADABLE_OPTS);
await pool.set('savings', savings.address, false);

akro = await FreeERC20.new();
await akro.methods['initialize(string,string)']("Akropolis", "AKRO");
await pool.set('akro', akro.address, false);
adel = await FreeERC20.new();
await adel.methods['initialize(string,string)']("Akropolis Delphi", "ADEL");
await pool.set('adel', adel.address, false);

stakingPoolAkro = await StakingPool.new();
await stakingPoolAkro.methods['initialize(address,address,uint256)'](pool.address, akro.address, '0');
await pool.set('staking', stakingPoolAkro.address, false);
stakingPoolAdel = await StakingPoolADEL.new();
await stakingPoolAdel.methods['initialize(address,address,uint256)'](pool.address, adel.address, '0');
await pool.set('stakingAdel', stakingPoolAdel.address, false);

compoundProtocolDai = await CompoundProtocol.new();
await compoundProtocolDai.methods['initialize(address,address,address,address)'](pool.address, dai.address, cDai.address, comptroller.address);
poolTokenCompoundProtocolDai = await PoolToken.new();
await poolTokenCompoundProtocolDai.methods['initialize(address,string,string)'](pool.address, "Delphi Compound DAI","dCDAI");
await savings.registerProtocol(compoundProtocolDai.address, poolTokenCompoundProtocolDai.address);
await compoundProtocolDai.addDefiOperator(savings.address);
await poolTokenCompoundProtocolDai.addMinter(savings.address);

rewardVesting = await RewardVestingModule.new();
await rewardVesting.methods['initialize(address)'](pool.address);
await pool.set('reward', rewardVesting.address, false);

rewardDistributions = await RewardDistributionModule.new();
await rewardDistributions.methods['initialize(address)'](pool.address);
await pool.set('rewardDistributions', rewardDistributions.address, false);
await rewardDistributions.registerProtocol(compoundProtocolDai.address, poolTokenCompoundProtocolDai.address);
await compoundProtocolDai.addDefiOperator(rewardDistributions.address);

//Save snapshot
//snap = await Snapshot.create(web3.currentProvider);

//Create distributions
for(let i=0; i<50; i++){
let randUser = Math.round(5*Math.random());
await deposit(otherAccounts[randUser], w3random.interval(10, 20, 'ether'));

await time.increase(7*24*60*60);
await (<any>savings).distributeRewardsForced(compoundProtocolDai.address);
}

// Upgrade Savings
savings = await upgradeProxy(savings.address, SavingsModule, UPGRADABLE_OPTS);
});

beforeEach(async () => {
//await snap.revert();
});


it('should use low gas if migrate rewards for user not participated in a pool', async () => {
let tx = await rewardDistributions.migrateRewards([user]);
//console.log(tx);
let gasUsed = tx.receipt.gasUsed;
expect(gasUsed).to.be.lt(100000);
});

it('should use low gas if withdraw rewards for user not participated in a pool', async () => {
let tx = await rewardDistributions.methods['withdrawReward()']({from:user});
//console.log(tx);
let gasUsed = tx.receipt.gasUsed;
expect(gasUsed).to.be.lt(100000);
});

async function deposit(acc:string, amount:BN){
await dai.methods['mint(address,uint256)'](acc, amount);
await dai.approve(savings.address, amount, {from:acc});
console.log('balance', (await dai.balanceOf(acc)).toString());
console.log('allowance', (await dai.allowance(acc,savings.address)).toString());
console.log('deposit', acc, amount.toString());
await savings.methods['deposit(address,address[],uint256[])'](compoundProtocolDai.address, [dai.address], [amount.divn(2)], {from:acc});
console.log('deposit-done');
}

});
Loading