Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

voters fix for able poke properly #39

Merged
merged 4 commits into from
Jul 24, 2023
Merged
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
13 changes: 7 additions & 6 deletions contracts/infrastructure/PlatformVoter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ contract PlatformVoter is ControllableV3, IPlatformVoter {
// *************************************************************

/// @dev Version of this contract. Adjust manually on each code modification.
string public constant PLATFORM_VOTER_VERSION = "1.0.2";
string public constant PLATFORM_VOTER_VERSION = "1.0.3";
/// @dev Denominator for different ratios. It is default for the whole platform.
uint public constant RATIO_DENOMINATOR = 100_000;
/// @dev Delay between votes.
Expand Down Expand Up @@ -109,11 +109,12 @@ contract PlatformVoter is ControllableV3, IPlatformVoter {

/// @dev Resubmit exist votes for given token.
/// Need to call it for ve that did not renew votes too long.
/// Anyone can renew the votes, no restriction.
function poke(uint tokenId) external {
Vote[] memory _votes = votes[tokenId];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are no restrictions to call poke. Probably it worth to make a comment why the restrictions are not required.

for (uint i; i < _votes.length; ++i) {
Vote memory v = _votes[i];
_vote(tokenId, v._type, v.weightedValue / v.weight, v.target);
_vote(tokenId, v._type, v.weightedValue / v.weight, v.target, true);
}
}

Expand All @@ -126,17 +127,17 @@ contract PlatformVoter is ControllableV3, IPlatformVoter {
) external {
require(IVeTetu(ve).isApprovedOrOwner(msg.sender, tokenId), "!owner");
for (uint i; i < types.length; ++i) {
_vote(tokenId, types[i], values[i], targets[i]);
_vote(tokenId, types[i], values[i], targets[i], false);
}
}

/// @dev Vote for given parameter using a vote power of given tokenId. Reset previous vote.
function vote(uint tokenId, AttributeType _type, uint value, address target) external {
require(IVeTetu(ve).isApprovedOrOwner(msg.sender, tokenId), "!owner");
_vote(tokenId, _type, value, target);
_vote(tokenId, _type, value, target, false);
}

function _vote(uint tokenId, AttributeType _type, uint value, address target) internal {
function _vote(uint tokenId, AttributeType _type, uint value, address target, bool skipDelay) internal {
require(value <= RATIO_DENOMINATOR, "!value");

// load maps for reduce gas usage
Expand All @@ -159,7 +160,7 @@ contract PlatformVoter is ControllableV3, IPlatformVoter {
for (; i < length; ++i) {
Vote memory v = _votes[i];
if (v._type == _type && v.target == target) {
require(v.timestamp + VOTE_DELAY < block.timestamp, "delay");
require(skipDelay || v.timestamp + VOTE_DELAY < block.timestamp, "delay");
oldVeWeight = v.weight;
oldVeValue = v.weightedValue;
found = true;
Expand Down
19 changes: 10 additions & 9 deletions contracts/ve/TetuVoter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ contract TetuVoter is ReentrancyGuard, ControllableV3, IVoter {
// *************************************************************

/// @dev Version of this contract. Adjust manually on each code modification.
string public constant VOTER_VERSION = "1.0.1";
string public constant VOTER_VERSION = "1.0.2";
/// @dev Rewards are released over 7 days
uint internal constant _DURATION = 7 days;
/// @dev Maximum votes per veNFT
Expand Down Expand Up @@ -156,6 +156,7 @@ contract TetuVoter is ReentrancyGuard, ControllableV3, IVoter {

/// @dev Resubmit exist votes for given token.
/// Need to call it for ve that did not renew votes too long.
/// Anyone can renew the votes, no restriction.
function poke(uint _tokenId) external {
address[] memory _vaultVotes = vaultsVotes[_tokenId];
uint length = _vaultVotes.length;
Expand Down Expand Up @@ -192,6 +193,10 @@ contract TetuVoter is ReentrancyGuard, ControllableV3, IVoter {
require(length <= MAX_VOTES, "Too many votes");

int256 _weight = int256(IVeTetu(ve).balanceOfNFT(_tokenId));
if (_weight == 0) {
// if ve power is 0, no need to vote
return;
}
int256 _totalVoteWeight = 0;
int256 _totalWeight = 0;
int256 _usedWeight = 0;
Expand All @@ -204,9 +209,8 @@ contract TetuVoter is ReentrancyGuard, ControllableV3, IVoter {
address _vault = _vaultVotes[i];
require(isVault(_vault), "Invalid vault");

int256 _vaultWeight = _weights[i] * _weight / _totalVoteWeight;
int256 _vaultWeight = _totalVoteWeight != 0 ? _weights[i] * _weight / _totalVoteWeight : int256(0);
require(votes[_tokenId][_vault] == 0, "duplicate vault");
require(_vaultWeight != 0, "zero power");
_updateFor(_vault);

vaultsVotes[_tokenId].push(_vault);
Expand All @@ -222,9 +226,7 @@ contract TetuVoter is ReentrancyGuard, ControllableV3, IVoter {
_totalWeight += _vaultWeight;
emit Voted(msg.sender, _tokenId, _vaultWeight, _vault, _weights[i], _weight);
}
if (_usedWeight > 0) {
IVeTetu(ve).voting(_tokenId);
}

totalWeight += uint(_totalWeight);
usedWeights[_tokenId] = uint(_usedWeight);
}
Expand Down Expand Up @@ -252,9 +254,6 @@ contract TetuVoter is ReentrancyGuard, ControllableV3, IVoter {
totalWeight -= uint(_totalWeight);
usedWeights[_tokenId] = 0;
delete vaultsVotes[_tokenId];
if (_totalWeight > 0) {
IVeTetu(ve).abstain(_tokenId);
}
}

// *************************************************************
Expand Down Expand Up @@ -284,6 +283,8 @@ contract TetuVoter is ReentrancyGuard, ControllableV3, IVoter {
/// Need to have restrictions for max attached tokens and votes.
function detachTokenFromAll(uint tokenId, address account) external override {
require(msg.sender == ve, "!ve");
// we assume that any responsibilities should be longer than the delay
require(lastVote[tokenId] + VOTE_DELAY < block.timestamp, "delay");

_reset(tokenId);

Expand Down
9 changes: 7 additions & 2 deletions scripts/gov/execute-upgrades.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import {ethers} from "hardhat";
import {Addresses} from "../addresses/addresses";
import {ControllerV2__factory} from "../../typechain";
import {RunHelper} from "../utils/RunHelper";
import {txParams} from "../deploy/DeployContract";

// tslint:disable-next-line:no-var-requires
const hre = require("hardhat");


async function main() {
const [signer] = await ethers.getSigners();
Expand All @@ -26,8 +31,8 @@ async function main() {

console.log('Ready to Update ', proxiesReadyToUpgrade);
if (proxiesReadyToUpgrade.length !== 0) {
await RunHelper.runAndWait(() => ControllerV2__factory.connect(core.controller, signer).upgradeProxy(proxiesReadyToUpgrade)
);
const props = await txParams(hre, ethers.provider);
await RunHelper.runAndWait(() => ControllerV2__factory.connect(core.controller, signer).upgradeProxy(proxiesReadyToUpgrade, {...props}));
}


Expand Down
86 changes: 86 additions & 0 deletions scripts/gov/poke-gauge-voter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import {ethers} from "hardhat";
import {Addresses} from "../addresses/addresses";
import {ControllerV2__factory, TetuVoter__factory} from "../../typechain";
import {RunHelper} from "../utils/RunHelper";
import {deployContract, txParams} from "../deploy/DeployContract";
import {Misc} from "../utils/Misc";
import {TimeUtils} from "../../test/TimeUtils";

// tslint:disable-next-line:no-var-requires
const {request, gql} = require('graphql-request')

// tslint:disable-next-line:no-var-requires
const hre = require("hardhat");

async function main() {
const [signer] = await ethers.getSigners();
const core = Addresses.getCore();

const gov = await Misc.impersonate('0xcc16d636dD05b52FF1D8B9CE09B09BC62b11412B')
const logic = await deployContract(hre, signer, 'TetuVoter');
await ControllerV2__factory.connect(core.controller, gov).announceProxyUpgrade([core.tetuVoter], [logic.address]);
await TimeUtils.advanceBlocksOnTs(60 * 60 * 24 * 2);
await ControllerV2__factory.connect(core.controller, gov).upgradeProxy([core.tetuVoter]);

const voter = TetuVoter__factory.connect(core.tetuVoter, signer);

const data = await request('https://api.thegraph.com/subgraphs/name/tetu-io/tetu-v2', gql`
query tetuVoterUserVotes {
tetuVoterUserVotes {
date
percent
weight
user {
veNFT {
veNFTId
}
}
}
}
`);

const votes = data.tetuVoterUserVotes;

const minDate = new Map<number, number>()
for (const vote of votes) {
// console.log(vote)
const veId = vote.user.veNFT.veNFTId;
console.log(new Date(vote.date * 1000), 'percent: ', vote.percent, 'weight: ', Number(vote.weight).toFixed(), 've: ', veId);
const md = minDate.get(+veId) ?? Date.now();
if (md > +vote.date && +vote.weight > 10_000) {
minDate.set(+veId, +vote.date);
}
}

console.log('total ve voted', minDate.size);

const vePokes: number[] = [];
for (const [veId, md] of minDate.entries()) {
console.log('ve: ', veId, 'min date: ', new Date(md * 1000));
const time = Date.now() - 60 * 60 * 24 * 21 * 1000;
if (md * 1000 < time) {
vePokes.push(veId);
}
}

console.log('total ve pokes', vePokes.length);

const skipVe = new Set<number>([]);

for (const veId of vePokes) {
console.log('poke ve: ', veId);
if (skipVe.has(veId)) {
continue;
}
const params = await txParams(hre, ethers.provider);
await RunHelper.runAndWait(() => voter.poke(veId, {...params}));
}

}

main()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});
17 changes: 14 additions & 3 deletions scripts/gov/poke-platform-voter.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import {ethers} from "hardhat";
import {Addresses} from "../addresses/addresses";
import {PlatformVoter__factory} from "../../typechain";
import {ControllerV2__factory, PlatformVoter__factory} from "../../typechain";
import {RunHelper} from "../utils/RunHelper";
import {txParams} from "../deploy/DeployContract";
import {deployContract, txParams} from "../deploy/DeployContract";
import {Misc} from "../utils/Misc";
import {TimeUtils} from "../../test/TimeUtils";

// tslint:disable-next-line:no-var-requires
const {request, gql} = require('graphql-request')
Expand All @@ -14,6 +16,13 @@ async function main() {
const [signer] = await ethers.getSigners();
const core = Addresses.getCore();

// const gov = await Misc.impersonate('0xcc16d636dD05b52FF1D8B9CE09B09BC62b11412B')
// const logic = await deployContract(hre, signer, 'PlatformVoter');
// await ControllerV2__factory.connect(core.controller, gov).announceProxyUpgrade([core.platformVoter], [logic.address]);
// await TimeUtils.advanceBlocksOnTs(60 * 60 * 24 * 2);
// await ControllerV2__factory.connect(core.controller, gov).upgradeProxy([core.platformVoter]);


const voter = PlatformVoter__factory.connect(core.platformVoter, signer);

const data = await request('https://api.thegraph.com/subgraphs/name/tetu-io/tetu-v2', gql`
Expand Down Expand Up @@ -60,9 +69,11 @@ async function main() {

console.log('total ve pokes', vePokes.length);

const skipVe = new Set<number>([]);

for (const veId of vePokes) {
console.log('poke ve: ', veId);
if(veId === 14) {
if (skipVe.has(veId)) {
continue;
}
const params = await txParams(hre, ethers.provider);
Expand Down
Loading
Loading