Skip to content

Commit

Permalink
refactor: returning weight and sum of weights
Browse files Browse the repository at this point in the history
  • Loading branch information
kupermind committed Apr 16, 2024
1 parent 4e0ae66 commit 39af7cd
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 36 deletions.
68 changes: 42 additions & 26 deletions contracts/VoteWeighting.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ pragma solidity ^0.8.23;
import "./interfaces/IErrors.sol";

Check warning on line 4 in contracts/VoteWeighting.sol

View workflow job for this annotation

GitHub Actions / build

global import of path ./interfaces/IErrors.sol is not allowed. Specify names to import individually or bind all exports of the module into a name (import "path" as Name)

interface IVEOLAS {
// Structure for voting escrow points
// The struct size is two storage slots of 2 * uint256 (128 + 128 + 64 + 64 + 128)
struct PointVoting {
// w(i) = at + b (bias)
int128 bias;
// dw / dt = a (slope)
int128 slope;
// Timestamp. It will never practically be bigger than 2^64 - 1
uint64 ts;
// Block number. It will not be bigger than the timestamp
uint64 blockNumber;
// Token amount. It will never practically be bigger. Initial OLAS cap is 1 bn tokens, or 1e27.
// After 10 years, the inflation rate is 2% per year. It would take 1340+ years to reach 2^128 - 1
uint128 balance;
}

/// @dev Gets the `account`'s lock end time.
/// @param account Account address.
/// @return unlockTime Lock end time.
Expand All @@ -30,22 +46,6 @@ struct VotedSlope {
uint256 end;
}

// Structure for voting escrow points
// The struct size is two storage slots of 2 * uint256 (128 + 128 + 64 + 64 + 128)
struct PointVoting {
// w(i) = at + b (bias)
int128 bias;
// dw / dt = a (slope)
int128 slope;
// Timestamp. It will never practically be bigger than 2^64 - 1
uint64 ts;
// Block number. It will not be bigger than the timestamp
uint64 blockNumber;
// Token amount. It will never practically be bigger. Initial OLAS cap is 1 bn tokens, or 1e27.
// After 10 years, the inflation rate is 2% per year. It would take 1340+ years to reach 2^128 - 1
uint128 balance;
}

contract VoteWeighting is IErrors {
event NewNomineeWeight(address indexed nominee, uint256 chainId, uint256 weight, uint256 totalWeight);
event VoteForNominee(address indexed user, address indexed nominee, uint256 chainId, uint256 weight);
Expand Down Expand Up @@ -231,15 +231,20 @@ contract VoteWeighting is IErrors {
_getSum();
}

/// @dev Get Nominee relative weight (not more than 1.0) normalized to 1e18 (e.g. 1.0 == 1e18).
/// @dev Get Nominee relative weight (not more than 1.0) normalized to 1e18 (e.g. 1.0 == 1e18) and a sum of weights.
/// Inflation which will be received by it is inflation_rate * relativeWeight / 1e18.
/// @param nominee Address of the nominee.
/// @param chainId Chain Id.
/// @param time Relative weight at the specified timestamp in the past or present.
/// @return weight Value of relative weight normalized to 1e18.
function _nomineeRelativeWeight(address nominee, uint256 chainId, uint256 time) internal view returns (uint256 weight) {
/// @return totalSum Sum of nominee weights.
function _nomineeRelativeWeight(
address nominee,
uint256 chainId,
uint256 time
) internal view returns (uint256 weight, uint256 totalSum) {
uint256 t = time / WEEK * WEEK;
uint256 totalSum = pointsSum[t].bias;
totalSum = pointsSum[t].bias;

// Push a pair of key defining variables into one key
// nominee occupies first 160 bits
Expand All @@ -253,27 +258,38 @@ contract VoteWeighting is IErrors {
}
}

/// @dev Get Nominee relative weight (not more than 1.0) normalized to 1e18.
/// @dev Get Nominee relative weight (not more than 1.0) normalized to 1e18 and the sum of weights.
/// (e.g. 1.0 == 1e18). Inflation which will be received by it is
/// inflation_rate * relativeWeight / 1e18.
/// @param nominee Address of the nominee.
/// @param chainId Chain Id.
/// @param time Relative weight at the specified timestamp in the past or present.
/// @return Value of relative weight normalized to 1e18.
function nomineeRelativeWeight(address nominee, uint256 chainId, uint256 time) external view returns (uint256) {
return _nomineeRelativeWeight(nominee, chainId, time);
/// @return weight Value of relative weight normalized to 1e18.
/// @return totalSum Sum of nominee weights.
function nomineeRelativeWeight(
address nominee,
uint256 chainId,
uint256 time
) external view returns (uint256 weight, uint256 totalSum) {
(weight, totalSum) = _nomineeRelativeWeight(nominee, chainId, time);
}

/// @dev Get nominee weight normalized to 1e18 and also fill all the unfilled values for type and nominee records.
/// Also, get the total sum of all the nominee weights.
/// @notice Any address can call, however nothing is recorded if the values are filled already.
/// @param nominee Address of the nominee.
/// @param chainId Chain Id.
/// @param time Relative weight at the specified timestamp in the past or present.
/// @return Value of relative weight normalized to 1e18.
function nomineeRelativeWeightWrite(address nominee, uint256 chainId, uint256 time) external returns (uint256) {
/// @return weight Value of relative weight normalized to 1e18.
/// @return totalSum Sum of nominee weights.
function nomineeRelativeWeightWrite(
address nominee,
uint256 chainId,
uint256 time
) external returns (uint256 weight, uint256 totalSum) {
_getWeight(nominee, chainId);
_getSum();
return _nomineeRelativeWeight(nominee, chainId, time);
(weight, totalSum) = _nomineeRelativeWeight(nominee, chainId, time);
}

/// @dev Allocate voting power for changing pool weights.
Expand Down
20 changes: 10 additions & 10 deletions test/VoteWeighting.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,9 @@ describe("Voting Escrow OLAS", function () {

// Make sure the initial weight is zero
let weight = await vw.nomineeRelativeWeight(nominee, chainId, block.timestamp);
expect(weight).to.equal(0);
expect(weight.weight).to.equal(0);
weight = await vw.nomineeRelativeWeight(nominee, chainId, nextTime);
expect(weight).to.equal(0);
expect(weight.weight).to.equal(0);

// Vote for the nominee
await vw.voteForNomineeWeights(nominee, chainId, maxVoteWeight / 2);
Expand All @@ -253,7 +253,7 @@ describe("Voting Escrow OLAS", function () {

// Make sure the initial weight is zero
weight = await vw.nomineeRelativeWeight(nominee2, chainId, nextTime);
expect(weight).to.equal(0);
expect(weight.weight).to.equal(0);

// Vote for another nominee
await vw.voteForNomineeWeights(nominee2, chainId, maxVoteWeight / 2);
Expand All @@ -268,13 +268,13 @@ describe("Voting Escrow OLAS", function () {

// Check relative weights that must represent a half for each
weight = await vw.nomineeRelativeWeight(nominee, chainId, nextTime);
expect(Number(weight) / E18).to.equal(0.5);
expect(Number(weight.weight) / E18).to.equal(0.5);
weight = await vw.nomineeRelativeWeight(nominee2, chainId, nextTime);
expect(Number(weight) / E18).to.equal(0.5);
expect(Number(weight.weight) / E18).to.equal(0.5);

// Write nominee weight and try to get one from the distant future
weight = await vw.callStatic.nomineeRelativeWeightWrite(nominee, chainId, nextTime * 2);
expect(weight).to.equal(0);
expect(weight.weight).to.equal(0);

// Checkpoint and checkpoint nominee
await vw.checkpoint();
Expand Down Expand Up @@ -305,7 +305,7 @@ describe("Voting Escrow OLAS", function () {

// Check relative weights that must represent a half for each
const weight = await vw.nomineeRelativeWeight(nominee, chainId, nextTime);
expect(Number(weight) / E18).to.equal(1);
expect(Number(weight.weight) / E18).to.equal(1);

// Restore to the state of the snapshot
await snapshot.restore();
Expand Down Expand Up @@ -335,7 +335,7 @@ describe("Voting Escrow OLAS", function () {
// Check weights that must represent a half for each
for (let i = 0; i < numNominees; i++) {
const weight = await vw.nomineeRelativeWeight(nominees[i], chainIds[i], nextTime);
expect(Number(weight) / E18).to.equal(0.5);
expect(Number(weight.weight) / E18).to.equal(0.5);
}
});

Expand Down Expand Up @@ -366,7 +366,7 @@ describe("Voting Escrow OLAS", function () {

// Check relative weights that must represent a half for each
const weight = await vw.nomineeRelativeWeight(nominee, chainId, nextTime);
expect(Number(weight) / E18).to.equal(1);
expect(Number(weight.weight) / E18).to.equal(1);

// Restore to the state of the snapshot
await snapshot.restore();
Expand Down Expand Up @@ -418,7 +418,7 @@ describe("Voting Escrow OLAS", function () {
await vw.nomineeRelativeWeight(nominees[1], chainId, nextTime)
];
// nominees[0] weight: 666666666680682666, nominees[1] weight: 333333333319317333; the ratio is 2:1
expect(Number(weights[0]) / E18).to.be.greaterThan(Number(weights[1]) / E18);
expect(Number(weights[0].weight) / E18).to.be.greaterThan(Number(weights[1].weight) / E18);

// Restore to the state of the snapshot
await snapshot.restore();
Expand Down

0 comments on commit 39af7cd

Please sign in to comment.