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

(govern) fix: changes for next week start, veOlas -> veOLAS #72

Merged
merged 2 commits into from
Jul 30, 2024
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
4 changes: 2 additions & 2 deletions apps/govern/common-util/functions/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { SUPPORTED_CHAINS, wagmiConfig } from 'common-util/config/wagmi';
import { RPC_URLS } from 'common-util/constants/rpcs';

import { getAddressFromBytes32 } from './addresses';
import { getStartOfNextWeekTimestamp } from './time';
import { getUnixNextWeekStartTimestamp } from './time';
import { getVoteWeightingContract } from './web3';

type VoteForNomineeWeightsParams = {
Expand Down Expand Up @@ -109,7 +109,7 @@ export const checkLockExpired = async (account: Address) => {
args: [account],
});

const nextWeek = getStartOfNextWeekTimestamp();
const nextWeek = getUnixNextWeekStartTimestamp();

return result ? nextWeek >= (result as number) : false;
};
32 changes: 18 additions & 14 deletions apps/govern/common-util/functions/time.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
export const getStartOfNextWeekTimestamp = () => {
const date = new Date();
const dayOfWeek = date.getDay();
const daysUntilNextWeek = dayOfWeek === 1 ? 7 : (8 - dayOfWeek) % 7;
// Returns the closest Thursday in the future
// which is the start of the next week by Unix time
export const getUnixNextWeekStartTimestamp = () => {
const now = new Date();
const dayOfWeek = now.getDay();
const daysUntilNextThursday = (4 - dayOfWeek + 7) % 7;

const nextWeekStartDate = new Date(date);
nextWeekStartDate.setDate(date.getDate() + daysUntilNextWeek);
nextWeekStartDate.setHours(0, 0, 0, 0);
const result = new Date(now);
result.setDate(now.getDate() + daysUntilNextThursday);
result.setHours(0, 0, 0, 0);

return nextWeekStartDate.getTime() / 1000;
return result.getTime() / 1000;
};

export const getThisWeekMondayTimestamp = () => {
// Returns the closest Thursday in the past
// which is the start of the current week by Unix time
export const getUnixWeekStartTimestamp = () => {
const now = new Date();
const dayOfWeek = now.getDay();
const daysToMonday = (dayOfWeek + 6) % 7;
const monday = new Date(now);
const daysSinceThursday = ((dayOfWeek + 2) % 7) + 1;
const result = new Date(now);

monday.setDate(now.getDate() - daysToMonday);
monday.setHours(0, 0, 0, 0);
result.setDate(now.getDate() - daysSinceThursday);
result.setHours(0, 0, 0, 0);

return monday.getTime() / 1000;
return result.getTime() / 1000;
};
4 changes: 2 additions & 2 deletions apps/govern/components/Contracts/ContractsList.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ describe('<ContractsList />', () => {

// current weight column
expect(screen.getByText(/10.12%/)).toBeInTheDocument();
expect(screen.getByText(/298.8k veOlas/)).toBeInTheDocument();
expect(screen.getByText(/298.8k veOLAS/)).toBeInTheDocument();

// next weight column
expect(screen.getByText(/25.56%/)).toBeInTheDocument();
expect(screen.getByText(/297.4k veOlas/)).toBeInTheDocument();
expect(screen.getByText(/297.4k veOLAS/)).toBeInTheDocument();
});

describe('Already voted', () => {
Expand Down
23 changes: 10 additions & 13 deletions apps/govern/components/Contracts/ContractsList.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CheckOutlined, InfoCircleOutlined, PlusOutlined } from '@ant-design/icons';
import { Button, Card as CardAntd, Space, Table, Tooltip, Typography } from 'antd';
import { Button, Card as CardAntd, Space, Table, Typography } from 'antd';
import { ColumnsType } from 'antd/es/table';
import styled from 'styled-components';
import { Allocation, StakingContract } from 'types';
Expand All @@ -9,20 +9,12 @@ import { COLOR } from 'libs/ui-theme/src';
import { CHAIN_NAMES } from 'libs/util-constants/src';

import { formatWeiBalance } from 'common-util/functions/balance';
import { NextWeekTooltip } from 'components/NextWeekTooltip';
import { useVotingPower } from 'hooks/useVotingPower';
import { useAppSelector } from 'store/index';

const { Title, Paragraph, Text } = Typography;

const NextWeekTitle = () => (
<Tooltip
color={COLOR.WHITE}
title={<Text>Updated voting weights will take effect at the start of next week</Text>}
>
{"Next week's weight"} <InfoCircleOutlined className="ml-8" style={{ color: COLOR.GREY_2 }} />
</Tooltip>
);

const Card = styled(CardAntd)`
flex: auto;
`;
Expand Down Expand Up @@ -68,18 +60,23 @@ const getColumns = ({
render: (currentWeight) => (
<Space size={2} direction="vertical">
<Text>{`${currentWeight?.percentage.toFixed(2)}%`}</Text>
<Text type="secondary">{`${formatWeiBalance(currentWeight?.value)} veOlas`}</Text>
<Text type="secondary">{`${formatWeiBalance(currentWeight?.value)} veOLAS`}</Text>
</Space>
),
},
{
title: <NextWeekTitle />,
title: (
<NextWeekTooltip>
Next week&apos;s weight
<InfoCircleOutlined className="ml-8" style={{ color: COLOR.GREY_2 }} />
</NextWeekTooltip>
),
key: 'nextWeight',
dataIndex: 'nextWeight',
render: (nextWeight) => (
<Space size={2} direction="vertical">
<Text>{`${nextWeight?.percentage.toFixed(2)}%`}</Text>
<Text type="secondary">{`${formatWeiBalance(nextWeight?.value)} veOlas`}</Text>
<Text type="secondary">{`${formatWeiBalance(nextWeight?.value)} veOLAS`}</Text>
</Space>
),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export const MyVotingWeight = ({
return <Loader />;
}

// If the user doesn't have voting power, suggest to get veOlas
// If the user doesn't have voting power, suggest to get veOLAS
if (Number(votingPower) === 0) {
return <GetVeOlas />;
}
Expand Down
13 changes: 7 additions & 6 deletions apps/govern/components/Contracts/MyVotingWeight/Votes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { CHAIN_NAMES } from 'libs/util-constants/src';

import { RETAINER_ADDRESS } from 'common-util/constants/addresses';
import { getBytes32FromAddress } from 'common-util/functions/addresses';
import { NextWeekTooltip } from 'components/NextWeekTooltip';
import { useAppSelector } from 'store/index';

const ONE_DAY_IN_MS = 24 * 60 * 60 * 1000;
Expand Down Expand Up @@ -77,12 +78,9 @@ const columns: ColumnsType<MyVote> = [
},
{
title: (
<Tooltip
color={COLOR.WHITE}
title={<Text>Updated voting weights will take effect at the start of next week</Text>}
>
<NextWeekTooltip>
My updated weight <InfoCircleOutlined className="ml-8" style={{ color: COLOR.GREY_2 }} />
</Tooltip>
</NextWeekTooltip>
),
key: 'nextWeight',
dataIndex: 'nextWeight',
Expand Down Expand Up @@ -156,7 +154,10 @@ export const Votes = ({ setIsUpdating, setAllocations }: VotesProps) => {
isRetainer: true,
});
}
return res;
return res.sort((item) =>
// put Rollover address at the end
item.address === getBytes32FromAddress(RETAINER_ADDRESS) ? 1 : -1,
);
}, []);
}, [userVotes, stakingContracts]);

Expand Down
36 changes: 36 additions & 0 deletions apps/govern/components/NextWeekTooltip.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Space, Tooltip, Typography } from 'antd';
import { ReactNode } from 'react';
import { mainnet } from 'viem/chains';

import { COLOR } from 'libs/ui-theme/src';
import { UNICODE_SYMBOLS } from 'libs/util-constants/src';
import { VOTE_WEIGHTING } from 'libs/util-contracts/src/lib/abiAndAddresses';

const { Text } = Typography;

const TOOLTIP_STYLE = { maxWidth: '350px' };

export const NextWeekTooltip = ({ children }: { children: ReactNode }) => {
return (
<Tooltip
color={COLOR.WHITE}
overlayStyle={TOOLTIP_STYLE}
title={
<Space direction="vertical">
<Text>Updated voting weights will take effect at the start of next week Unix time.</Text>
<a
href={`https://etherscan.io/address/${
VOTE_WEIGHTING.addresses[mainnet.id]
}#readContract#F29`}
target="_blank"
rel="noopener noreferrer"
>
{`View timestamp when the last votes apply ${UNICODE_SYMBOLS.EXTERNAL_LINK}`}
</a>
</Space>
}
>
{children}
</Tooltip>
);
};
43 changes: 33 additions & 10 deletions apps/govern/hooks/useFetchStakingContractsList.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,59 @@
import { useEffect } from 'react';
import { StakingContract } from 'types';
import { useBlock } from 'wagmi';
import { Address } from 'viem';
import { mainnet } from 'viem/chains';
import { useReadContract } from 'wagmi';

import { VOTE_WEIGHTING } from 'libs/util-contracts/src/lib/abiAndAddresses';

import { RETAINER_ADDRESS } from 'common-util/constants/addresses';
import { LATEST_BLOCK_KEY, NEXT_RELATIVE_WEIGHTS_KEY } from 'common-util/constants/scopeKeys';
import { NEXT_RELATIVE_WEIGHTS_KEY } from 'common-util/constants/scopeKeys';
import { getBytes32FromAddress } from 'common-util/functions';
import { getStartOfNextWeekTimestamp } from 'common-util/functions/time';
import { setStakingContracts } from 'store/govern';
import { useAppDispatch, useAppSelector } from 'store/index';

import { useNominees } from './useNominees';
import { useNomineesMetadata } from './useNomineesMetadata';
import { useNomineesWeights } from './useNomineesWeights';

const WEEK_IN_SECONDS = 604_800;

const getCurrentWeightTimestamp = (timeSum: number | undefined) => {
if (!timeSum) return null;
// If timeSum is in the future, subtract a week from it
if (timeSum * 1000 > Date.now()) return timeSum - WEEK_IN_SECONDS;
return timeSum;
};

export const useFetchStakingContractsList = () => {
const dispatch = useAppDispatch();
const { stakingContracts } = useAppSelector((state) => state.govern);

// Get nominees list
const { data: nominees } = useNominees();

const { data: block } = useBlock({ blockTag: 'latest', scopeKey: LATEST_BLOCK_KEY });
const now = block ? Number(block.timestamp) : null;
const nextWeek = block ? getStartOfNextWeekTimestamp() : null;
// Get last scheduled time (next week) from the contract
// Has the timestamp when the last votes should be applied
const { data: timeSum } = useReadContract({
address: (VOTE_WEIGHTING.addresses as Record<number, Address>)[mainnet.id],
abi: VOTE_WEIGHTING.abi,
chainId: mainnet.id,
functionName: 'timeSum',
query: {
select: (data) => Number(data),
},
});

// Get contracts current weight
const { data: currentWeight } = useNomineesWeights(nominees || [], now);
// Get contracts current week weights
const { data: currentWeight } = useNomineesWeights(
nominees || [],
getCurrentWeightTimestamp(timeSum),
);

// Get contracts next weight
// Get contracts next week weights
const { data: nextWeight } = useNomineesWeights(
nominees || [],
nextWeek,
timeSum || null,
NEXT_RELATIVE_WEIGHTS_KEY,
);

Expand Down
9 changes: 5 additions & 4 deletions apps/govern/hooks/useFetchUserVotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { UserVotes } from 'types';
import { useAccount, useBlock } from 'wagmi';

import { LATEST_BLOCK_KEY, NEXT_USERS_SLOPES_KEY } from 'common-util/constants/scopeKeys';
import { getThisWeekMondayTimestamp } from 'common-util/functions/time';
import { getUnixWeekStartTimestamp } from 'common-util/functions/time';
import { setLastUserVote, setUserVotes } from 'store/govern';
import { useAppDispatch, useAppSelector } from 'store/index';

Expand All @@ -16,12 +16,13 @@ import { useVoteUserSlopes } from './useVoteUserSlopes';
const SECONDS_PER_BLOCK = 12;
const CONTRACT_DEPLOY_BLOCK = 20312875;

// Current votes are those that have been applied at the start of the week
// Current votes are those that have been applied at the start of the week (unix time)
const getCurrentVotesBlock = (blockNumber: bigint, blockTimestamp: bigint) => {
const mondayBlock =
Number(blockNumber) -
// Approx number of blocks between current timestamp and this Monday
Math.round((Number(blockTimestamp) - getThisWeekMondayTimestamp()) / SECONDS_PER_BLOCK);
// Approximate number of blocks between the current timestamp and
// the start of the current week by Unix time
Math.round((Number(blockTimestamp) - getUnixWeekStartTimestamp()) / SECONDS_PER_BLOCK);

return BigInt(Math.max(CONTRACT_DEPLOY_BLOCK, mondayBlock));
};
Expand Down
3 changes: 3 additions & 0 deletions libs/common-middleware/src/lib/cspHeader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ const ALLOWED_ORIGINS = [

// tenderly
'https://virtual.mainnet.rpc.tenderly.co/',
'https://virtual.gnosis.rpc.tenderly.co/',
'https://virtual.polygon.rpc.tenderly.co/',
'https://rpc.tenderly.co/fork/',

// others
'https://api.thegraph.com/',
Expand Down
Loading