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
4 changes: 3 additions & 1 deletion voting/v2/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ ARBITRUM_API_KEY=
OPTIMISM_API_KEY=
MOONRIVER_API_KEY=

SKIP_PREFLIGHT_CHECK=true
DEFENDER_TEAM_API_KEY=
DEFENDER_TEAM_API_SECRET_KEY=

VOTING_ADDRESS=
PROD_MODE=true
55 changes: 52 additions & 3 deletions voting/v2/contracts/Voting.sol
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,10 @@ contract Voting is Initializable, OwnableUpgradeable, ReentrancyGuardUpgradeable
totalUserWeight[_owner] = totalUserWeight[_owner] + _farmWeight;

// Store all voting infos
farmInfos[totalFarmVoteCount] = FarmInfo(_owner, _farm, _farmWeight, block.timestamp);

totalFarmVoteCount++;
if (_farmWeight != 0) {
farmInfos[totalFarmVoteCount] = FarmInfo(_owner, _farm, _farmWeight, block.timestamp);
totalFarmVoteCount++;
}
}

for (uint256 j = 0; j < _fnftIds.length; j++) {
Expand Down Expand Up @@ -376,6 +377,54 @@ contract Voting is Initializable, OwnableUpgradeable, ReentrancyGuardUpgradeable
return totalWeightByUser;
}

// Get weight for voting by lockFarm, time
function getWeightByTime(LockFarm _lockFarm, uint256 _startTime) public view returns (uint256) {
uint256 weightByTime = 0;
// Calculate user's voting weights in customized time
uint256 i = totalFarmVoteCount - 1;
do {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@xiangdev1016 I am curious your thought process behind using the do-while loop while you can do make things cleaner with a simple for loop.

Copy link
Copy Markdown
Contributor Author

@xdev1016 xdev1016 Jan 31, 2023

Choose a reason for hiding this comment

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

We should consider i value range till 0 in this loop. But in my past experience, when I set index range to 0 in for loop, I got revert error while execution loop. That's why I used do while rather than for. Here is 0 value index range revert error from remix.
image

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

what caused the revert? and the solution to fix it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

to avoid this, we can use do while instead of for loop

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

if we use for, while loop then math error will be happened.
ex:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

contract test {
function getWeightByTime() public view returns (uint256) {
uint256 weightByTime = 0;
for (uint256 i = 3; i >= 0; i--) {
if (i < 0) break;
weightByTime += i;
}
return weightByTime;
}
}

here: i = -1 can be occurred when i = 0, so this value can not be acceptable in solidity language cause of uint type.
that's why I used do while loop.

if (_startTime >= farmInfos[i].time) break;
if (_lockFarm == farmInfos[i]._lockFarm) {
weightByTime += farmInfos[i]._farmWeight;
}
if (i > 0) i--;
else break;
} while (i >= 0);
return weightByTime;
}

// Get total weights for voting by time
function getTotalWeightsByTime(uint256 _startTime) public view returns (uint256) {
uint256 totalWeightByTime = 0;
LockFarm[] memory _validFarms = getFarms();
for (uint256 i = 0; i < _validFarms.length; i++) {
uint256 weight = getWeightByTime(_validFarms[i], _startTime);
totalWeightByTime += weight;
}
return totalWeightByTime;
}

// Get total HEC amount to participate in voting system by lockFarm, time
function getTotalHecForVoting() public view returns (uint256) {
uint256 totalHecAmount = 0;
LockFarm[] memory _validFarms = getFarms();
for (uint256 i = 0; i < _validFarms.length; i++) {
uint256 tokenSupplyForLockFarm = _validFarms[i].totalTokenSupply();
uint256 caculatedTokenSupplyForLockFarm = convertToHEC(
address(stakingToken[_validFarms[i]]),
tokenSupplyForLockFarm
);
totalHecAmount += caculatedTokenSupplyForLockFarm;
}
return totalHecAmount;
}

// Get available total HEC amount to participate in voting system by lockFarm, time
function getAvlTotalHecForVoting(uint256 _startTime) public view returns (uint256) {
uint256 amount = getTotalHecForVoting() - getTotalWeightsByTime(_startTime);
return amount;
}

// Return HEC amount of the ERC20 token converted
function convertToHEC(address _stakingToken, uint256 _amount) public view returns (uint256) {
uint256 hecWeight = 0;
Expand Down
5 changes: 5 additions & 0 deletions voting/v2/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import '@nomiclabs/hardhat-etherscan';
import 'hardhat-gas-reporter';

require('@openzeppelin/hardhat-upgrades');
require('@openzeppelin/hardhat-defender');
require('dotenv').config();

export default {
Expand Down Expand Up @@ -88,4 +89,8 @@ export default {
mocha: {
timeout: 100000000,
},
defender: {
apiKey: process.env.DEFENDER_TEAM_API_KEY,
apiSecret: process.env.DEFENDER_TEAM_API_SECRET_KEY,
},
};
2 changes: 2 additions & 0 deletions voting/v2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"update": "hardhat run ./scripts/upgradeVoting.ts --network",
"verify": "hardhat run ./scripts/verifyVoting.ts",
"migrate": "hardhat run ./scripts/migrationVoting.ts --network",
"propose": "hardhat run scripts/propose-upgrade.ts --network",
"test": "hardhat test --network ftmtest"
},
"devDependencies": {
Expand Down Expand Up @@ -44,6 +45,7 @@
"@nomiclabs/hardhat-etherscan": "^3.0.3",
"@openzeppelin/contracts": "4.1.0",
"@openzeppelin/contracts-upgradeable": "4.1.0",
"@openzeppelin/hardhat-defender": "^1.8.1",
"@uniswap/lib": "4.0.1-alpha",
"@uniswap/v2-core": "1.0.1",
"abi-decoder": "^2.4.0"
Expand Down
33 changes: 18 additions & 15 deletions voting/v2/scripts/deployVoting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,21 @@ async function main() {

const lockFarm = prod_mode
? [
'0x80993B75e38227f1A3AF6f456Cf64747F0E21612',
'0xd7faE64DD872616587Cc8914d4848947403078B8',
'0xB13610B4e7168f664Fcef2C6EbC58990Ae835Ff1',
]
'0x80993B75e38227f1A3AF6f456Cf64747F0E21612',
'0xd7faE64DD872616587Cc8914d4848947403078B8',
'0xB13610B4e7168f664Fcef2C6EbC58990Ae835Ff1',
]
: [
'0xC464e6d45004Bf56772E70e22d9cF61C5Ae63970',
'0x55869De94AB1F18295C1C5aC3C1c80995F2D5a2E',
'0x9DF988299260F5A21C3b903630cF53e1C5688990',
'0xE54C5c3C00Ca22c7Bf471923F17f41Fc94a8F31c',
'0x6B047365B1C75772f7CaF922FD71c8106F2B0c71',
'0xea08E048643Bf498741774348Ae7aFb16B9DbA40',
'0x9391abd498Ecb2Be226e446a76a8b9C61932856C',
'0x0112F57a5EF77b7D074D7213127Df8E907D017bE',
'0xDb798136b7Eb1167fe3242cdb34af0f1a890EC20',
];

'0xC464e6d45004Bf56772E70e22d9cF61C5Ae63970',
'0x55869De94AB1F18295C1C5aC3C1c80995F2D5a2E',
'0x9DF988299260F5A21C3b903630cF53e1C5688990',
'0xE54C5c3C00Ca22c7Bf471923F17f41Fc94a8F31c',
'0x6B047365B1C75772f7CaF922FD71c8106F2B0c71',
'0xea08E048643Bf498741774348Ae7aFb16B9DbA40',
'0x9391abd498Ecb2Be226e446a76a8b9C61932856C',
'0x0112F57a5EF77b7D074D7213127Df8E907D017bE',
'0xDb798136b7Eb1167fe3242cdb34af0f1a890EC20',
];
const stakingToken = prod_mode
? [_hec, _hecUsdc, _hecTor]
: [_hec, _hec, _hec, _hec, _hec, _sHec, _wsHec, _hecUsdc, _hecTor];
Expand All @@ -72,20 +71,24 @@ async function main() {
console.log('Voting contract deployed to:', votingContract.address);

// Add LockFarms
console.log("Add LockFarms");
for (let i = 0; i < lockFarm.length; i++) {
const txAddLockFarm = await votingContract.addLockFarmForOwner(
lockFarm[i],
stakingToken[i],
_lockAddressRegistry
);
await txAddLockFarm.wait();
console.log("Done: #", i + 1);
}

// Add LPTokens
console.log("Add LPTokens");
const txAddLPHECUSDC = await votingContract.addLPTokens(_hecUsdc, true);
await txAddLPHECUSDC.wait();
const txAddLPHECTOR = await votingContract.addLPTokens(_hecTor, true);
await txAddLPHECTOR.wait();

}

main().catch((error) => {
Expand Down
16 changes: 16 additions & 0 deletions voting/v2/scripts/propose-upgrade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { BigNumber } from 'ethers';
const { defender } = require("hardhat");

async function main() {
const proxyAddress = process.env.VOTING_ADDRESS;

const VotingContractFactory = await ethers.getContractFactory("Voting");
console.log("Preparing proposal...");
const proposal = await defender.proposeUpgrade(proxyAddress, VotingContractFactory);
console.log("Upgrade proposal created at:", proposal.url);
}

main().catch((error) => {
console.error(error);
process.exitCode = 1;
});