From a768f0dfae500c71a0672f27bd50551176ef8941 Mon Sep 17 00:00:00 2001 From: igorgoldobin Date: Thu, 14 Mar 2024 20:50:01 +1000 Subject: [PATCH] feat: updated for mainnet and new contract --- src/components/Content/index.tsx | 16 +- src/constants/abis/masterNode.json | 567 +++++++++++++-------------- src/constants/index.ts | 7 +- src/state/stats/hooks.ts | 16 +- src/state/user/hooks.ts | 57 ++- src/state/user/updater.tsx | 4 - src/types/index.ts | 3 +- src/web3/chains.ts | 19 +- src/web3/connection/WalletConnect.ts | 2 +- src/web3/providers.ts | 1 - 10 files changed, 309 insertions(+), 383 deletions(-) diff --git a/src/components/Content/index.tsx b/src/components/Content/index.tsx index 6dd8210..593e36d 100644 --- a/src/components/Content/index.tsx +++ b/src/components/Content/index.tsx @@ -28,7 +28,6 @@ import { import { useUserBalance, useUserRewards, - useUserBlockShares, useUserLastClaimedBlock, useUserSinceLastClaim, useUserRegistrationStatus, @@ -40,7 +39,6 @@ import { import { useContractBalance, useTotalCollateralAmount, - useTotalBlockShares, useTotalRegistrations, } from 'state/stats/hooks' @@ -59,14 +57,12 @@ const Content = () => { const balance = useContractBalance() const totalCollateralAmount = useTotalCollateralAmount() - const totalBlockShares = useTotalBlockShares() const totalRegistrations = useTotalRegistrations() const totalSeconds = useTotalSeconds() const userType = useUserType() const userBalance = useUserBalance() const userRewards = useUserRewards() - const userBlockShares = useUserBlockShares() const userLastClaimedBlock = useUserLastClaimedBlock() const userSinceLastClaim = useUserSinceLastClaim() const userRegistrationStatus = useUserRegistrationStatus() @@ -192,16 +188,12 @@ const Content = () => { value: totalRegistrations, }, { - title: 'Total block shares', - value: totalBlockShares, + title: 'Balance', + value: `${financial(formatEther(userBalance))} STRAX`, }, ] const userStatsData: StatsTileProps[] = [ - { - title: 'Balance', - value: `${financial(formatEther(userBalance))} STRAX`, - }, { title: 'Rewards', value: `${financial(formatEther(userRewards))} STRAX`, @@ -210,10 +202,6 @@ const Content = () => { title: 'Collateral amount', value: `${financial(formatEther(userCollateralAmount))} STRAX`, }, - { - title: 'Block shares', - value: userBlockShares, - }, { title: 'Last claimed block', value: userLastClaimedBlock, diff --git a/src/constants/abis/masterNode.json b/src/constants/abis/masterNode.json index 4dd1724..0f7f012 100644 --- a/src/constants/abis/masterNode.json +++ b/src/constants/abis/masterNode.json @@ -1,302 +1,269 @@ [ - { - "inputs": [ - { - "internalType": "address", - "name": "account", - "type": "address" - } - ], - "name": "AddressInsufficientBalance", - "type": "error" - }, - { - "inputs": [], - "name": "FailedInnerCall", - "type": "error" - }, - { - "anonymous": false, - "inputs": [ - { - "indexed": true, - "internalType": "address", - "name": "_from", - "type": "address" - } - ], - "name": "Deregistration", - "type": "event" - }, - { - "anonymous": false, - "inputs": [ - { - "indexed": true, - "internalType": "address", - "name": "_from", - "type": "address" - } - ], - "name": "Registration", - "type": "event" - }, - { - "inputs": [], - "name": "COLLATERAL_AMOUNT", - "outputs": [ - { - "internalType": "uint256", - "name": "", - "type": "uint256" - } - ], - "stateMutability": "view", - "type": "function" - }, - { - "inputs": [], - "name": "COLLATERAL_AMOUNT_10K", - "outputs": [ - { - "internalType": "uint256", - "name": "", - "type": "uint256" - } - ], - "stateMutability": "view", - "type": "function" - }, - { - "inputs": [], - "name": "COLLATERAL_AMOUNT_50K", - "outputs": [ - { - "internalType": "uint256", - "name": "", - "type": "uint256" - } - ], - "stateMutability": "view", - "type": "function" - }, - { - "inputs": [], - "name": "WITHDRAWAL_DELAY", - "outputs": [ - { - "internalType": "uint256", - "name": "", - "type": "uint256" - } - ], - "stateMutability": "view", - "type": "function" - }, - { - "inputs": [ - { - "internalType": "address[]", - "name": "legacy10Kaccounts", - "type": "address[]" - }, - { - "internalType": "address[]", - "name": "legacy50Kaccounts", - "type": "address[]" - } - ], - "name": "assignLegacyAccounts", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" - }, - { - "inputs": [ - { - "internalType": "address", - "name": "masternodeAccount", - "type": "address" - } - ], - "name": "checkBlockShares", - "outputs": [ - { - "internalType": "uint256", - "name": "", - "type": "uint256" - } - ], - "stateMutability": "view", - "type": "function" - }, - { - "inputs": [], - "name": "claimRewards", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" - }, - { - "inputs": [], - "name": "completeWithdrawal", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" - }, - { - "inputs": [], - "name": "initialized", - "outputs": [ - { - "internalType": "bool", - "name": "", - "type": "bool" - } - ], - "stateMutability": "view", - "type": "function" - }, - { - "inputs": [], - "name": "lastBlock", - "outputs": [ - { - "internalType": "uint256", - "name": "", - "type": "uint256" - } - ], - "stateMutability": "view", - "type": "function" - }, - { - "inputs": [ - { - "internalType": "address", - "name": "", - "type": "address" - } - ], - "name": "lastClaimedBlock", - "outputs": [ - { - "internalType": "uint256", - "name": "", - "type": "uint256" - } - ], - "stateMutability": "view", - "type": "function" - }, - { - "inputs": [ - { - "internalType": "address", - "name": "", - "type": "address" - } - ], - "name": "legacy10K", - "outputs": [ - { - "internalType": "bool", - "name": "", - "type": "bool" - } - ], - "stateMutability": "view", - "type": "function" - }, - { - "inputs": [ - { - "internalType": "address", - "name": "", - "type": "address" - } - ], - "name": "legacy50K", - "outputs": [ - { - "internalType": "bool", - "name": "", - "type": "bool" - } - ], - "stateMutability": "view", - "type": "function" - }, - { - "inputs": [], - "name": "register", - "outputs": [], - "stateMutability": "payable", - "type": "function" - }, - { - "inputs": [ - { - "internalType": "address", - "name": "", - "type": "address" - } - ], - "name": "registrationStatus", - "outputs": [ - { - "internalType": "enum MasternodeStakingContract.RegistrationStatus", - "name": "", - "type": "uint8" - } - ], - "stateMutability": "view", - "type": "function" - }, - { - "inputs": [], - "name": "startWithdrawal", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" - }, - { - "inputs": [], - "name": "totalBlockShares", - "outputs": [ - { - "internalType": "uint256", - "name": "", - "type": "uint256" - } - ], - "stateMutability": "view", - "type": "function" - }, - { - "inputs": [], - "name": "totalCollateralAmount", - "outputs": [ - { - "internalType": "uint256", - "name": "", - "type": "uint256" - } - ], - "stateMutability": "view", - "type": "function" - }, - { - "inputs": [], - "name": "totalRegistrations", - "outputs": [ - { - "internalType": "uint256", - "name": "", - "type": "uint256" - } - ], - "stateMutability": "view", - "type": "function" - } + { + "inputs": [ + { + "internalType": "address", + "name": "account", + "type": "address" + } + ], + "name": "AddressInsufficientBalance", + "type": "error" + }, + { + "inputs": [], + "name": "FailedInnerCall", + "type": "error" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "_from", + "type": "address" + } + ], + "name": "Deregistration", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "_from", + "type": "address" + } + ], + "name": "Registration", + "type": "event" + }, + { + "inputs": [], + "name": "COLLATERAL_AMOUNT", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "COLLATERAL_AMOUNT_LEGACY", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "WITHDRAWAL_DELAY", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "name": "accounts", + "outputs": [ + { + "internalType": "uint256", + "name": "balance", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "lastDividends", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "lastClaimedBlock", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address[]", + "name": "legacyAccounts", + "type": "address[]" + } + ], + "name": "assignLegacyAccounts", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "name": "claimRewards", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "name": "completeWithdrawal", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "name": "initialized", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "lastBalance", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "name": "legacy", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "register", + "outputs": [], + "stateMutability": "payable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "name": "registrationStatus", + "outputs": [ + { + "internalType": "enum MasternodeStakingContract.RegistrationStatus", + "name": "", + "type": "uint8" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "startWithdrawal", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "name": "totalCollateralAmount", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "totalDividends", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "totalRegistrations", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "withdrawingCollateralAmount", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + } ] \ No newline at end of file diff --git a/src/constants/index.ts b/src/constants/index.ts index ad23303..6593199 100644 --- a/src/constants/index.ts +++ b/src/constants/index.ts @@ -2,7 +2,6 @@ import { parseEther } from 'ethers/lib/utils' export const MASTERNODE_ADDRESS = '0x0000000000000000000000000000000000001002' -export const COLLATERAL_AMOUNT = parseEther('25000') -export const COLLATERAL_AMOUNT_10K = parseEther('100000') -export const COLLATERAL_AMOUNT_50K = parseEther('500000') -export const WITHDRAWAL_DELAY = 100800 +export const COLLATERAL_AMOUNT = parseEther('1000') // 1000000 +export const COLLATERAL_AMOUNT_LEGACY = parseEther('100') // 100000 +export const WITHDRAWAL_DELAY = 20 // 100800 diff --git a/src/state/stats/hooks.ts b/src/state/stats/hooks.ts index f09d800..78e7f14 100644 --- a/src/state/stats/hooks.ts +++ b/src/state/stats/hooks.ts @@ -61,22 +61,12 @@ export function useUpdateTotalCollateralAmount() { } export function useUpdateTotalBlockShares() { - const contract = useMasterNodeContract() const dispatch = useAppDispatch() return useCallback(async () => { - if (!contract) { - dispatch(setTotalBlockShares(0)) - return - } - - const totalRegistrations = await contract.totalRegistrations() - const lastBlockShareUpdate = await contract.lastBlock() - const blockNumber = await contract.provider.getBlockNumber() - const val = await contract.totalBlockShares() - - dispatch(setTotalBlockShares(val.toNumber() + ((blockNumber - lastBlockShareUpdate.toNumber()) * totalRegistrations.toNumber()))) - }, [contract, dispatch]) + dispatch(setTotalBlockShares(0)) + return + }, [dispatch]) } export function useContractBalance() { diff --git a/src/state/user/hooks.ts b/src/state/user/hooks.ts index 45cadf9..6945120 100644 --- a/src/state/user/hooks.ts +++ b/src/state/user/hooks.ts @@ -7,9 +7,9 @@ import { useMasterNodeContract } from 'hooks/useContract' import { useAppDispatch, useAppSelector } from 'state' import { RegistrationStatus, UserType } from 'types' -import { useContractBalance, useTotalCollateralAmount, useTotalBlockShares } from 'state/stats/hooks' +import { useContractBalance, useTotalCollateralAmount, useTotalBlockShares, useTotalRegistrations } from 'state/stats/hooks' -import { COLLATERAL_AMOUNT, COLLATERAL_AMOUNT_10K, COLLATERAL_AMOUNT_50K } from '../../constants' +import { COLLATERAL_AMOUNT, COLLATERAL_AMOUNT_LEGACY } from '../../constants' import { setBalance, @@ -18,8 +18,8 @@ import { setRegistrationStatus, setLastClaimedBlock, setSinceLastClaim, - setBlockShares, setTotalSeconds, + setBlockShares, } from './reducer' export function useUpdateBalance() { @@ -38,6 +38,8 @@ export function useUpdateBalance() { } export function useUpdateRewards() { + const { account } = useWeb3React() + const contract = useMasterNodeContract() const dispatch = useAppDispatch() const userStatus = useUserRegistrationStatus() const sinceLastClaim = useUserSinceLastClaim() @@ -45,14 +47,25 @@ export function useUpdateRewards() { const lastClaimedBlock = useUserLastClaimedBlock() const totalCollateralAmount = useTotalCollateralAmount() const totalBlockShares = useTotalBlockShares() + const totalRegistrations = useTotalRegistrations() return useCallback(async () => { - if (userStatus !== RegistrationStatus.REGISTERED || totalBlockShares === 0 || sinceLastClaim === 0) { + if (!account || !contract || userStatus !== RegistrationStatus.REGISTERED || totalBlockShares === 0 || sinceLastClaim === 0 || totalRegistrations === 0) { dispatch(setRewards('0')) return } - const value = contractBalance.sub(totalCollateralAmount).mul(sinceLastClaim).div(totalBlockShares) + const existingDividends = await contract.totalDividends() + const lastBalance = await contract.lastBalance() + const withdrawingCollateralAmount = await contract.withdrawingCollateralAmount() + + const amount = contractBalance.sub(lastBalance).sub(totalCollateralAmount).sub(withdrawingCollateralAmount) + + const newTotalDividends = existingDividends.add(amount.div(totalRegistrations)) + const userLastDividends = await (await contract.accounts(account)).lastDividends + + const value = newTotalDividends.sub(userLastDividends) + dispatch(setRewards(value.toString())) }, [ userStatus, @@ -93,26 +106,19 @@ export function useUpdateLastClaimedBlock() { const currentBlock = await provider.getBlockNumber() - const val = await contract.lastClaimedBlock(account) + const val = (await contract.accounts(account)).lastClaimedBlock dispatch(setLastClaimedBlock(val.toNumber())) dispatch(setSinceLastClaim(currentBlock - val.toNumber())) }, [account, provider, contract, dispatch]) } export function useUpdateBlockShares() { - const { account } = useWeb3React() - const contract = useMasterNodeContract() const dispatch = useAppDispatch() return useCallback(async () => { - if (!contract || !account) { - dispatch(setBlockShares(0)) - return - } - - const val = await contract.checkBlockShares(account) - dispatch(setBlockShares(val.toNumber())) - }, [account, contract]) + dispatch(setBlockShares(0)) + return + }, [dispatch]) } export function useUpdateTotalSeconds() { @@ -136,7 +142,7 @@ export function useUpdateTotalSeconds() { return } - const lastClaimedBlock = await contract.lastClaimedBlock(account) + const lastClaimedBlock = (await contract.accounts(account)).lastClaimedBlock const blockNumber = await contract.provider.getBlockNumber() const totalSeconds = (lastClaimedBlock.toNumber() + offset - blockNumber) * blockTime @@ -155,14 +161,9 @@ export function useUpdateType() { return } - const isLegacy10K = await contract.legacy10K(account) - if (isLegacy10K) { - setType(UserType.LEGACY_10K) - return - } - const isLegacy50K = await contract.legacy50K(account) - if (isLegacy50K) { - setType(UserType.LEGACY_50K) + const isLegacy = await contract.legacy(account) + if (isLegacy) { + setType(UserType.LEGACY) return } dispatch(setType(UserType.REGULAR)) @@ -205,10 +206,8 @@ export function useTotalSeconds() { export function useUserCollateralAmount() { const type = useUserType() - if (type === UserType.LEGACY_10K) { - return COLLATERAL_AMOUNT_10K - } else if (type === UserType.LEGACY_50K) { - return COLLATERAL_AMOUNT_50K + if (type === UserType.LEGACY) { + return COLLATERAL_AMOUNT_LEGACY } else if (type === UserType.REGULAR) { return COLLATERAL_AMOUNT } diff --git a/src/state/user/updater.tsx b/src/state/user/updater.tsx index 2683cb8..cc8e1fb 100644 --- a/src/state/user/updater.tsx +++ b/src/state/user/updater.tsx @@ -5,7 +5,6 @@ import useInterval from 'hooks/useInterval' import { useUpdateBalance, useUpdateRewards, - useUpdateBlockShares, useUpdateLastClaimedBlock, useUpdateRegistrationStatus, useUpdateType, @@ -15,7 +14,6 @@ import { export default function Updater() { const updateBalance = useUpdateBalance() const updateRewards = useUpdateRewards() - const updateBlockShares = useUpdateBlockShares() const updateLastClaimedBlock = useUpdateLastClaimedBlock() const updateRegistrationStatus = useUpdateRegistrationStatus() const updateType = useUpdateType() @@ -24,7 +22,6 @@ export default function Updater() { const updateData = useCallback(async () => { updateBalance() updateRewards() - updateBlockShares() updateLastClaimedBlock() updateRegistrationStatus() updateType() @@ -32,7 +29,6 @@ export default function Updater() { }, [ updateBalance, updateRewards, - updateBlockShares, updateLastClaimedBlock, updateRegistrationStatus, updateType, diff --git a/src/types/index.ts b/src/types/index.ts index 7f6764a..bbb3e06 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -6,7 +6,6 @@ export enum RegistrationStatus { export enum UserType { UNKNOWN, - LEGACY_10K, - LEGACY_50K, + LEGACY, REGULAR, } diff --git a/src/web3/chains.ts b/src/web3/chains.ts index 563a34a..32e291b 100644 --- a/src/web3/chains.ts +++ b/src/web3/chains.ts @@ -3,14 +3,13 @@ import type { AddEthereumChainParameter } from '@web3-react/types' import STRATIS_ICON from 'assets/images/networks/stratis_logo_white.svg' export enum ChainId { - STRATIS = 105105, - AURORIA = 205205, + STRATIS = 105105 } export const DEFAULT_CHAIN_ID = ChainId.STRATIS export const STRATIS_CURRENCY: AddEthereumChainParameter['nativeCurrency'] = { - name: 'Stratis', + name: 'Strax', symbol: 'STRAX', decimals: 18, } @@ -57,23 +56,13 @@ type ChainConfig = { [key in ChainId]: ChainInfo } export const CHAINS: ChainConfig = { [ChainId.STRATIS]: { id: ChainId.STRATIS, - name: 'Stratis', + name: 'Strax', icon: STRATIS_ICON, urls: ['https://rpc.stratisevm.com'], nativeCurrency: STRATIS_CURRENCY, blockExplorerUrls: ['https://explorer.stratisevm.com'], - available: false, - testnet: false, - }, - [ChainId.AURORIA]: { - id: ChainId.AURORIA, - name: 'Auroria', - icon: STRATIS_ICON, - urls: ['https://auroria.rpc.stratisevm.com'], - nativeCurrency: STRATIS_CURRENCY, - blockExplorerUrls: ['https://auroria.explorer.stratisevm.com'], available: true, - testnet: true, + testnet: false, }, } diff --git a/src/web3/connection/WalletConnect.ts b/src/web3/connection/WalletConnect.ts index 28cf0c9..9174da2 100644 --- a/src/web3/connection/WalletConnect.ts +++ b/src/web3/connection/WalletConnect.ts @@ -8,7 +8,7 @@ export class WalletConnectPopup extends WalletConnectV2 { options: { projectId: process.env.REACT_APP_WALLETCONNECT_PROJECT_ID || '', chains: [ChainId.STRATIS], - optionalChains: [ChainId.AURORIA], + optionalChains: [ChainId.STRATIS], showQrModal, }, onError, diff --git a/src/web3/providers.ts b/src/web3/providers.ts index 1a6c195..db7ef48 100644 --- a/src/web3/providers.ts +++ b/src/web3/providers.ts @@ -48,5 +48,4 @@ class AppJsonRpcProvider extends StaticJsonRpcProvider { export const RPC_PROVIDERS: { [key in ChainId]: StaticJsonRpcProvider } = { [ChainId.STRATIS]: new AppJsonRpcProvider(ChainId.STRATIS), - [ChainId.AURORIA]: new AppJsonRpcProvider(ChainId.AURORIA), } \ No newline at end of file