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

273 additional changes in v25 contracts #276

Merged
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
30 changes: 14 additions & 16 deletions contracts/PushCore/PushCoreV2_Temp.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ pragma solidity ^0.8.20;
* Functionalties.
*
*/
import "./PushCoreStorageV1_5.sol";
import "./PushCoreStorageV2.sol";
import { PushCoreStorageV1_5 } from "./PushCoreStorageV1_5.sol";
import { PushCoreStorageV2 } from "./PushCoreStorageV2.sol";
import "../interfaces/IPUSH.sol";
import { IPushCoreV2 } from "../interfaces/IPushCoreV2.sol";
import { IPushCommV2 } from "../interfaces/IPushCommV2.sol";
import { Errors } from "../libraries/Errors.sol";
import { CoreTypes } from "../libraries/DataTypes.sol";

import "@openzeppelin/contracts/utils/Strings.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts-upgradeable/utils/PausableUpgradeable.sol";
import { Strings } from "@openzeppelin/contracts/utils/Strings.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import { PausableUpgradeable, Initializable } from "@openzeppelin/contracts-upgradeable/utils/PausableUpgradeable.sol";

contract PushCoreV2_Temp is Initializable, PushCoreStorageV1_5, PausableUpgradeable, PushCoreStorageV2, IPushCoreV2 {
using SafeERC20 for IERC20;
Expand Down Expand Up @@ -126,6 +126,9 @@ contract PushCoreV2_Temp is Initializable, PushCoreStorageV1_5, PausableUpgradea

function setFeeAmount(uint256 _newFees) external {
onlyGovernance();
if (_newFees == 0) {
revert Errors.InvalidArg_LessThanExpected(1, _newFees);
}
if (_newFees >= ADD_CHANNEL_MIN_FEES) {
revert Errors.InvalidArg_MoreThanExpected(ADD_CHANNEL_MIN_FEES, _newFees);
}
Expand All @@ -135,7 +138,7 @@ contract PushCoreV2_Temp is Initializable, PushCoreStorageV1_5, PausableUpgradea
function setMinPoolContribution(uint256 _newAmount) external {
onlyGovernance();
if (_newAmount == 0) {
revert Errors.InvalidArg_LessThanExpected(0, _newAmount);
revert Errors.InvalidArg_LessThanExpected(1, _newAmount);
}
MIN_POOL_CONTRIBUTION = _newAmount;
}
Expand Down Expand Up @@ -298,8 +301,10 @@ contract PushCoreV2_Temp is Initializable, PushCoreStorageV1_5, PausableUpgradea
revert Errors.Core_InvalidChannelType();
}
if (
(msg.sender != _channelAddress || channelData.expiryTime >= block.timestamp)
&& (msg.sender != pushChannelAdmin || channelData.expiryTime + 14 days >= block.timestamp)
!(
(msg.sender == _channelAddress && channelData.expiryTime < block.timestamp)
|| (msg.sender == pushChannelAdmin && channelData.expiryTime + 14 days < block.timestamp)
)
) {
revert Errors.UnauthorizedCaller(msg.sender);
}
Expand Down Expand Up @@ -824,11 +829,4 @@ contract PushCoreV2_Temp is Initializable, PushCoreStorageV1_5, PausableUpgradea

emit ChatIncentiveClaimed(msg.sender, _amount);
}

function sendFunds(address _user, uint256 _amount) external {
if (msg.sender != feePoolStakingContract) {
revert Errors.UnauthorizedCaller(msg.sender);
}
IERC20(PUSH_TOKEN_ADDRESS).transfer(_user, _amount);
}
}
221 changes: 221 additions & 0 deletions scripts/Migration/FetchCore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
require("@nomiclabs/hardhat-ethers");
require("dotenv").config();
const { BigNumber } = require("ethers");
var fs = require("fs");
const fsPromises = fs.promises;

const ABI_FILE_PATH_CORE =
"artifacts/contracts/PushCore/PushCoreV2_Temp.sol/PushCoreV2_Temp.json"
const core = "0x66329Fdd4042928BfCAB60b179e1538D56eeeeeE";

const mappingSlot = 134;

async function getAbi(file) {
const data = await fsPromises.readFile(file, "utf8");
const abi = JSON.parse(data)["abi"];
return abi;
}

async function getEpochToUserStakedWeight(user, epoch) {
const userFessInfoSlotHash = ethers.utils.solidityKeccak256(
["uint256", "uint256"],
[user, mappingSlot] // key, mapping slot
);

const eTUSWtMappingSlot = ethers.BigNumber.from(userFessInfoSlotHash)
.add(4)
.toHexString();

const eTUSWtSlot = ethers.utils.solidityKeccak256(
["uint256", "uint256"],
[epoch, eTUSWtMappingSlot] // key, mapping slot
);
return await provider.getStorageAt(core, eTUSWtSlot);
}

BigInt.prototype.toJSON = function () {
return FetchHelper.toString();
};

const TILL_EPOCH = 4;
const FetchHelper = {
getEpochsIdArr: n => Array.from({ length: n }, (_, i) => i + 1),

fetchEpochToTotalStakedWeight: async () => {
return await Promise.all(
FetchHelper.getEpochsIdArr(TILL_EPOCH).map(
el => EPNSCoreV1Proxy.epochToTotalStakedWeight(el)
)).then(res => res.map(el => el.toString()
));
},

fetchEpochRewards: async () =>{
return await Promise.all(
FetchHelper.getEpochsIdArr(TILL_EPOCH).map(
el => EPNSCoreV1Proxy.epochRewards(el)
)
).then(res => res.map(el => el.toString()))
},

fetchEpochInfo: async() =>{
const [_epochToTotalStakedWeight,_epochRewards] = await Promise.all([
FetchHelper.fetchEpochToTotalStakedWeight(),
FetchHelper.fetchEpochRewards(),
])

return {_epochToTotalStakedWeight, _epochRewards}
},

fetchUserFeesInfos: async(usersList) =>{
return await Promise.all(usersList.map(el => EPNSCoreV1Proxy.userFeesInfo(el)));
},

fetchUserRewardsClaimed: async(usersList) =>{
return await Promise.all(usersList.map(el => EPNSCoreV1Proxy.usersRewardsClaimed(el)));
},

fetchUserInfos:async(userList)=>{
const [
userFeesInfos,
userRewardsClaimed
] = await Promise.all([
FetchHelper.fetchUserFeesInfos(userList),
FetchHelper.fetchUserRewardsClaimed(userList)
])

let _stakedAmount = [];
let _stakedWeight = [];
let _lastStakedBlock = [];
let _lastClaimedBlock = [];
let _userRewardsClaimed = [];

for (let i = 0; i < userList.length; ++i) {
let userFeeInfo = userFeesInfos[i]
_stakedAmount.push(userFeeInfo.stakedAmount.toString());
_stakedWeight.push(userFeeInfo.stakedWeight.toString());
_lastStakedBlock.push(userFeeInfo.lastStakedBlock.toString());
_lastClaimedBlock.push(userFeeInfo.lastClaimedBlock.toString());
_userRewardsClaimed.push(
userRewardsClaimed[i].toString()
);
}

return {
_stakedAmount,
_stakedWeight,
_lastStakedBlock,
_lastClaimedBlock,
_userRewardsClaimed
}

},

getEpochToUserStakedWeight: async(users)=>{
const fetchEpochToUserStakedWeight = async (epochId, users) => {
const epochToStakedWts = await Promise.all(users.map(user => getEpochToUserStakedWeight(user, epochId)))
return epochToStakedWts.map(el => BigNumber.from(el).toString())
}

// const epochsArr = FetchHelper.getEpochsIdArr(TILL_EPOCH)
// const stakedWts = await Promise.all(epochsArr.map(el => fetchEpochToUserStakedWeight(el, users)))
const stakedWts = [];
for (let i = 1; i < TILL_EPOCH+1; i++) {
stakedWts.push(
await fetchEpochToUserStakedWeight(i, users)
)
}

let stakedWtsJSON = {}
stakedWts.map((el,idx)=>{
stakedWtsJSON[`epochToUserStakedWeight${idx+1}`] = el
})

return stakedWtsJSON
},

fetchAll: async(filteredArray)=>{
const [epochInfo, userInfo,] = await Promise.all([
FetchHelper.fetchEpochInfo(),
FetchHelper.fetchUserInfos(filteredArray),
])

return {
epochInfo,
userInfo,
}
}

}

const getUserArray = async (contract) =>{
let userArray = [];
let filteredArray = [];
let eventFilter = contract.filters.Staked();
let events = await contract.queryFilter(eventFilter);
console.log(`Total ${events.length} events found `);
for (let i = 0; i < events.length; i++) {
let user = events[i].args[0];
userArray.push(user);
}
userArray = userArray.sort();

let checkDup;
for (let i = 0; i < userArray.length; ++i) {
if (userArray[i] == checkDup) {
continue;
} else {
filteredArray.push(userArray[i]);
checkDup = userArray[i];
}
}

return filteredArray;
}


let EPNSCoreV1Proxy

async function main() {
const abi = await getAbi(ABI_FILE_PATH_CORE);
provider = ethers.provider;
EPNSCoreV1Proxy = new ethers.Contract(core, abi, provider);

// Old contract
const usersArray = await getUserArray(EPNSCoreV1Proxy);
console.log(usersArray.length, "Unique addresses found");

console.log("fetching user and epoch info");
const {
epochInfo,
userInfo,
} = await FetchHelper.fetchAll(usersArray)

const {_epochToTotalStakedWeight,_epochRewards} = epochInfo;
const {_lastClaimedBlock,_lastStakedBlock,_stakedAmount,_stakedWeight,_userRewardsClaimed} = userInfo;

console.log("fetching st wt");
const stakedWeightInfo = await FetchHelper.getEpochToUserStakedWeight(usersArray);

let obj = {
users: usersArray,
epochToTotalStakedWeight: _epochToTotalStakedWeight,
epochRewards: _epochRewards,
stakedAmount: _stakedAmount,
stakedWeight: _stakedWeight,
lastStakedBlock: _lastStakedBlock,
lastClaimedBlock: _lastClaimedBlock,
userRewardsClaimed: _userRewardsClaimed,
...stakedWeightInfo,
};

fs.writeFileSync("./OldUsers.json", JSON.stringify(obj), "utf-8", (err) => {
console.log(err);
});
}

main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
Loading