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 3 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
12 changes: 6 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 @@ -113,7 +113,7 @@ contract PlatformVoter is ControllableV3, IPlatformVoter {
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 +126,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 +159,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
18 changes: 9 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 @@ -192,6 +192,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 +208,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 +225,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 +253,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 +282,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
129 changes: 129 additions & 0 deletions scripts/gov/simulate-remove-all-platform-votes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import {PlatformVoter, PlatformVoter__factory, VeTetu__factory} from "../../typechain";
import {Misc} from "../utils/Misc";
import {Addresses} from "../addresses/addresses";
import {TimeUtils} from "../../test/TimeUtils";
import {BigNumber} from "ethers";

// 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 Misc.impersonate('0xcc16d636dD05b52FF1D8B9CE09B09BC62b11412B')
const core = Addresses.getCore();

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

const data = await request('https://api.thegraph.com/subgraphs/name/tetu-io/tetu-v2', gql`
query {
platformVoterEntities {
votes(first: 1000) {
desiredValue
date
newValue
percent
target
voteType
veWeightedValue
vePower
veNFT {
veNFTId
}
}
}
}
`);
const votes = data.platformVoterEntities[0].votes;

// before total: 2 0x0000000000000000000000000000000000000000 22309135493267960537693747 2041902115655696862561336030000
// total: 2 0x0000000000000000000000000000000000000000 5113545723033076067993240 435594938160431577170782625000
const newTotalWeight = BigNumber.from('22309135493267960537693747').sub('5113545723033076067993240');
const newTotalValues = BigNumber.from('2041902115655696862561336030000').sub('435594938160431577170782625000');
console.log('set new values', newTotalWeight.toString(), newTotalValues.toString(), 'value: ', newTotalValues.div(newTotalWeight).toString());
// await voter.emergencyAdjustWeights(2, Misc.ZERO_ADDRESS, newTotalWeight, newTotalValues)

// total: 3 0xa14dea6e48b3187c5e637c88b84d5dfc701edeb7 8330810085607459913799050 416540504280372995689952500000
// total: 3 0xa14dea6e48b3187c5e637c88b84d5dfc701edeb7 8330810085607459913799050 416540504280372995689952500000
// await voter.emergencyAdjustWeights(3, '0xa14dea6e48b3187c5e637c88b84d5dfc701edeb7', 0, 0);

await TimeUtils.advanceBlocksOnTs(60 * 60 * 24 * 14);

const targets = new Set<string>();
const veIds = new Set<number>();

for (const vote of votes) {
const veId = vote.veNFT.veNFTId;
// console.log(vote)
// console.log(new Date(vote.date * 1000), 'value: ', vote.newValue, 'power: ', Number(vote.vePower).toFixed(), 've: ', veId);
targets.add(vote.target);
veIds.add(+veId);
}

console.log('---------------------------------------------');

for (const _type of [1, 2, 3]) {
if (_type === 3) {
for (const t of targets) {
await checkW(_type, t, voter);
}
} else {
await checkW(_type, Misc.ZERO_ADDRESS, voter);
}
}

console.log('---------------------------------------------');

for (const vote of votes) {
const veId = vote.veNFT.veNFTId;
const ownerAdr = await VeTetu__factory.connect(core.ve, signer).ownerOf(veId);
const owner = await Misc.impersonate(ownerAdr)
await voter.connect(owner).reset(veId, [vote.voteType], [vote.target]);
}

for (const veId of veIds) {
const voted = await voter.veVotesLength(veId);
if (voted.gt(0)) {

const vote = await voter.votes(veId, 0);
console.log('voted, try to reset vote', veId, vote);

const ownerAdr = await VeTetu__factory.connect(core.ve, signer).ownerOf(veId);
const owner = await Misc.impersonate(ownerAdr)
try {
await voter.connect(owner).reset(veId, [vote._type], [vote.target]);
} catch (e) {
console.log('vote can not be removed!', veId, voted.toNumber());

await voter.emergencyResetVote(veId, 0, false);
}
}
}

for (const _type of [1, 2, 3]) {
if (_type === 3) {
for (const t of targets) {
await checkW(_type, t, voter);
}
} else {
await checkW(_type, Misc.ZERO_ADDRESS, voter);
}
}


}

async function checkW(_type: number, target: string, voter: PlatformVoter) {
const totalWeight = (await voter.attributeWeights(_type, target)).toString();
const totalValues = (await voter.attributeValues(_type, target)).toString();
console.log('total: ', _type, target, totalWeight, totalValues);

}

main()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});
2 changes: 1 addition & 1 deletion scripts/utils/Misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class Misc {
method: "hardhat_setBalance",
params: [address, "0x1431E0FAE6D7217CAA0000000"],
});
console.log('address impersonated', address);
// console.log('address impersonated', address);
return ethers.getSigner(address);
}

Expand Down
Loading
Loading