import { useReadContract } from "wagmi"; import { readContract } from "@wagmi/core"; import toast from "react-hot-toast"; import { amountInHistory, amountOutHistory, feeIn, feeOut, stakeRatio } from "../../constants/gatekeeper"; import { STAKING_ADDRESSES } from "../../constants/addresses"; import { abi as StakingAbi } from "../../abi/GhostStaking.json"; import { abi as GatekeeperAbi } from "../../abi/GhostGatekeeper.json"; import { abi as GhostStorageHistoryAbi } from "../../abi/GhostStorageHistory.json"; import { shorten } from "../../helpers"; import { DecimalBigNumber } from "../../helpers/DecimalBigNumber"; import { executeOnChainTransaction, getTokenDecimals } from "../helpers"; import { useCirculatingSupply } from "../tokens"; export const useRotationInfoAt = (chainId, session) => { const { gatekeeperAddress } = useGatekeeperAddress(chainId); const { data: rotationInfoAt, isLoading: rotationInfoAtLoading } = useReadContract({ abi: GatekeeperAbi, address: gatekeeperAddress, args: [session], functionName: "getRotationInfoAt", scopeKey: `getRotationInfoAt-${chainId}-${session}-${gatekeeperAddress}`, chainId: chainId, }); return { rotationInfoAt, rotationInfoAtLoading }; } export const useIsTransactionExecuted = (chainId, index) => { const { gatekeeperAddress } = useGatekeeperAddress(chainId); const { data: storageHistory } = useReadContract({ abi: GatekeeperAbi, address: gatekeeperAddress, functionName: "storageHistory", scopeKey: `storageHistory-${chainId}-${gatekeeperAddress}`, chainId: chainId, }); const { data: isTransactionExecuted, isLoading: isTransactionExecutedLoading } = useReadContract({ abi: GhostStorageHistoryAbi, address: storageHistory, functionName: "isTransactionExecuted", args: [index], scopeKey: `isTransactionExecuted-${chainId}-${storageHistory}`, chainId: chainId, }); return { isTransactionExecuted, isTransactionExecutedLoading } } export const useCurrentGasPrice = (chainId) => { const { gatekeeperAddress } = useGatekeeperAddress(chainId); const { data: gasPrice } = useReadContract({ abi: GatekeeperAbi, address: gatekeeperAddress, functionName: "getCurrentGasPrice", scopeKey: `currentGasPrice-${chainId}-${gatekeeperAddress}`, chainId: chainId, }); const { data } = useReadContract({ abi: GatekeeperAbi, address: gatekeeperAddress, functionName: "MAX_ALLOWED_GAS_PRICE", scopeKey: `MAX_ALLOWED_GAS_PRICE-${chainId}-${gatekeeperAddress}`, chainId: chainId, }); const maxGasPrice = new DecimalBigNumber(data ? data : 1n, 9); const currentGasPrice = new DecimalBigNumber(gasPrice ? gasPrice : 1n, 9); return { currentGasPrice, maxGasPrice }; } export const useRegistryIndex = (chainId) => { const { gatekeeperAddress } = useGatekeeperAddress(chainId); const { data, isLoading } = useReadContract({ abi: GatekeeperAbi, address: gatekeeperAddress, functionName: "REGISTRY_INDEX", scopeKey: `REGISTRY_INDEX-${chainId}-${gatekeeperAddress}`, chainId: chainId, }); return { registryIndex: !isLoading ? data : 0n }; } export const useWeaverMaximumSlots = (chainId) => { const { gatekeeperAddress } = useGatekeeperAddress(chainId); const { data } = useReadContract({ abi: GatekeeperAbi, address: gatekeeperAddress, functionName: "SLOTS", scopeKey: `SLOTS-${chainId}-${gatekeeperAddress}`, chainId: chainId, }); return { maximumSlots: data ? data : 1n }; } export const useWeaverRoot = (chainId, blockNumber, context) => { const session = context?.session; const { gatekeeperAddress } = useGatekeeperAddress(chainId); const { data } = useReadContract({ abi: GatekeeperAbi, address: gatekeeperAddress, functionName: "getRoot", args: [session, blockNumber], scopeKey: `getRoot-${chainId}-${session}-${blockNumber}-${gatekeeperAddress}`, chainId: chainId, }); return { merkleRoot: data?.at(0), nextSession: data?.at(1) } } export const useWeaverProof = (chainId, blockNumber, context) => { const index = context?.index; const session = context?.session; const { gatekeeperAddress } = useGatekeeperAddress(chainId); const { data: merkleProofInner } = useReadContract({ abi: GatekeeperAbi, address: gatekeeperAddress, functionName: "getProof", args: [index, session, blockNumber], scopeKey: `getProof-${chainId}-${session}-${index}-${blockNumber}-${gatekeeperAddress}`, chainId: chainId, }); return { merkleProof: merkleProofInner } } export const useWeaverSlotValues = (chainId, blockNumber, context) => { const index = context?.index; const session = context?.session; const { gatekeeperAddress } = useGatekeeperAddress(chainId); const { data: slotValuesInner } = useReadContract({ abi: GatekeeperAbi, address: gatekeeperAddress, functionName: "getSlotValues", args: [index, session, blockNumber], scopeKey: `getSlotValues-${chainId}-${session}-${index}-${blockNumber}-${gatekeeperAddress}`, chainId: chainId, }); return { slotValues: slotValuesInner } } export const useGatekeeperMinimum = (chainId) => { const { gatekeeperAddress } = useGatekeeperAddress(chainId); const { data: minimum, error } = useReadContract({ abi: GatekeeperAbi, address: gatekeeperAddress, functionName: "EXISTENTIAL_DEPOSIT", scopeKey: `existentialDeposit-${chainId}-${gatekeeperAddress}`, chainId: chainId, }); const existentialDeposit = new DecimalBigNumber( minimum ? minimum : 0n, getTokenDecimals("GHST"), ); return { existentialDeposit }; } export const useGatekeeperApy = (chainId) => { const circulatingSupply = useCirculatingSupply(chainId, "STNK"); const { gatekeeperAddress } = useGatekeeperAddress(chainId); const { epoch } = useEpoch(chainId); const { data: metadata, error } = useReadContract({ abi: GatekeeperAbi, address: gatekeeperAddress, functionName: "metadata", scopeKey: `gatekeeperMetadata-${chainId}-${gatekeeperAddress}`, chainId: chainId, }); const amountIn = new DecimalBigNumber( (metadata?.amountIn ?? 0n) + (amountInHistory[chainId] ?? 0n), getTokenDecimals("GHST"), ); const amountOut = new DecimalBigNumber( (metadata?.amountOut ?? 0n) + (amountOutHistory[chainId] ?? 0n), getTokenDecimals("GHST"), ); const deployedAt = metadata?.deployedAt ?? 0; const unixSeconds = Math.floor(Date.now() / 1000); const power = 365 * 86400 / (unixSeconds - deployedAt); const feeInBig = new DecimalBigNumber((feeIn[chainId] ?? 0n), 5); const feeOutBig = new DecimalBigNumber((feeOut[chainId] ?? 0n), 5); const stakeRatioBig = new DecimalBigNumber((stakeRatio ?? 0n), 5); const numerator = amountIn.mul(feeInBig).add(amountOut.mul(feeOutBig)); const denominator = amountIn.mul(stakeRatioBig).sub(amountOut.mul(stakeRatioBig)); let apyInner = Infinity; if ((circulatingSupply?._value ?? 0n) === 0n) { return { gatekeepedApy: apyInner, apyInner}; } const value = epoch.distribute.div(circulatingSupply); apyInner = 100 * (Math.pow(1 + parseFloat(value.toString()), 1095) - 1); if (apyInner === 0) apyInner = Infinity; if (!denominator?._value || denominator._value.isZero()) { return { gatekeepedApy: apyInner, apyInner }; } const result = Number(numerator.div(denominator).toString()); const gatekeepedApy = Math.pow(1 + result, power) * 100; return { gatekeepedApy: apyInner + gatekeepedApy + (apyInner * gatekeepedApy) / 100, apyInner, } } export const useCurrentIndex = (chainId) => { const { data: index, refetch } = useReadContract({ abi: StakingAbi, address: STAKING_ADDRESSES[chainId], functionName: "index", scopeKey: `index-${chainId}`, chainId: chainId, }); const currentIndexRaw = index ? index : 1000000000n; const currentIndex = new DecimalBigNumber(currentIndexRaw, 9); return { currentIndex, refetch }; }; export const useEpoch = (chainId) => { const { data: epochRaw, refetch } = useReadContract({ abi: StakingAbi, address: STAKING_ADDRESSES[chainId], functionName: "epoch", scopeKey: `epoch-${chainId}`, chainId: chainId, }); const epoch = { length: Number(epochRaw?.at(0) ? epochRaw.at(0) : 0), number: Number(epochRaw?.at(1) ? epochRaw.at(1) : 0), end: Number(epochRaw?.at(2) ? epochRaw.at(2) : 0), distribute: new DecimalBigNumber(epochRaw?.at(3) ? epochRaw.at(3) : 0n, 9), }; return { epoch, refetch }; } export const useWarmupLength = (chainId) => { const { data: warmupLengthRaw } = useReadContract({ abi: StakingAbi, address: STAKING_ADDRESSES[chainId], functionName: "warmupPeriod", scopeKey: `warmupPeriod-${chainId}`, chainId: chainId, }); let warmupLength = new DecimalBigNumber(0n, 0); let warmupExists = false; if (warmupLengthRaw) { warmupLength = new DecimalBigNumber(warmupLengthRaw, 0); warmupExists = warmupLengthRaw > 0n; } return { warmupLength, warmupExists }; } export const useWarmupInfo = (chainId, address) => { const { data: info, refetch } = useReadContract({ abi: StakingAbi, address: STAKING_ADDRESSES[chainId], functionName: "warmupInfo", args: [address], scopeKey: `warmupInfo-${address}-${chainId}`, chainId: chainId, }); const warmupInfo = { deposit: info ? new DecimalBigNumber(info.at(0), 9) : new DecimalBigNumber(0n, 9), shares: info ? info.at(1) : 0n, expiry: info ? Number(info.at(2)) : 0, } return { warmupInfo, refetch } } export const useGatekeeperAddress = (chainId) => { const { data: info, refetch } = useReadContract({ abi: StakingAbi, address: STAKING_ADDRESSES[chainId], functionName: "gatekeeper", scopeKey: `gatekeeper-${chainId}`, chainId: chainId, }); const gatekeeperAddress = info ? info : "0x0000000000000000000000000000000000000000"; const gatekeeperIsEmpty = gatekeeperAddress === "0x0000000000000000000000000000000000000000"; return { gatekeeperAddress, gatekeeperIsEmpty, refetch }; } export const useGhostedSupply = (chainId) => { const { data: info, refetch } = useReadContract({ abi: StakingAbi, address: STAKING_ADDRESSES[chainId], functionName: "ghostedSupply", scopeKey: `ghostedSupply-${chainId}`, chainId: chainId, }); const ghostedSupply = new DecimalBigNumber( info ? info : 0n, 18 ); return ghostedSupply; } export const stake = async (chainId, account, amount, isRebase, isClaim, ftsoSymbol, stnkSymbol, ghstSymbol) => { const args = [amount, account, isRebase, isClaim]; const messages = { replacedMsg: "Staking transaction was replaced. Wait for inclusion please.", successMsg: `${ftsoSymbol} tokens staked successfully! Wait for the warm-up period to claim your ${isRebase ? stnkSymbol : ghstSymbol}.`, errorMsg: "Staking transaction failed. Check logs for error detalization.", }; const txHash = await executeOnChainTransaction({ chainId, args, messages, account, abi: StakingAbi, address: STAKING_ADDRESSES[chainId], functionName: "stake", }); return txHash; } export const unstake = async (chainId, account, amount, isTrigger, isRebase, ftsoSymbol, stnkSymbol, ghstSymbol) => { const args = [amount, account, isTrigger, isRebase]; const messages = { replacedMsg: "Unstake transaction was replaced. Wait for inclusion please.", successMsg: `${isRebase ? stnkSymbol : ghstSymbol} tokens unstaked successfully! Check your ${ftsoSymbol} balance on ${shorten(account)}.`, errorMsg: "Unstake transaction failed. Check logs for error detalization.", }; const txHash = await executeOnChainTransaction({ chainId, args, messages, account, abi: StakingAbi, address: STAKING_ADDRESSES[chainId], functionName: "unstake", }); return txHash; } export const forfeit = async (chainId, account, ftsoSymbol) => { const messages = { replacedMsg: "Forfeit transaction was replaced. Wait for inclusion please.", successMsg: `Tokens forfeited successfully! Check your ${ftsoSymbol} balance on ${shorten(account)}.`, errorMsg: "Forfeit transaction failed. Check logs for error detalization.", }; const txHash = await executeOnChainTransaction({ chainId, messages, account, args: [], abi: StakingAbi, address: STAKING_ADDRESSES[chainId], functionName: "forfeit", }); return txHash; } export const claim = async (chainId, account, isStnk, stnkSymbol, ghstSymbol) => { const args = [account, isStnk]; const messages = { replacedMsg: "Claim transaction was replaced. Wait for inclusion please.", successMsg: `${isStnk ? stnkSymbol : ghstSymbol} tokens claimed successfully to ${shorten(account)}.`, errorMsg: "Claim transaction failed. Check logs for error detalization.", }; const txHash = await executeOnChainTransaction({ chainId, args, messages, account, abi: StakingAbi, address: STAKING_ADDRESSES[chainId], functionName: "claim", }); return txHash; } export const breakout = async (chainId, account, receiver, amount) => { const args = [receiver, amount]; const messages = { replacedMsg: "Breakout transaction was replaced. Wait for inclusion please.", successMsg: `Staking breakout succesfully done. Check tx hash status or wait for slow clap finalization.`, errorMsg: "Breakout transaction failed. Check logs for error detalization.", }; const txHash = await executeOnChainTransaction({ chainId, args, messages, account, abi: StakingAbi, address: STAKING_ADDRESSES[chainId], functionName: "breakout", }); return txHash; } export const unwrap = async (chainId, account, amount, stnkSymbol) => { const args = [account, amount]; const messages = { replacedMsg: "Unwrap transaction was replaced. Wait for inclusion please.", successMsg: `Tokens unwrapped succesfully! Check your ${stnkSymbol} balance on ${shorten(account)}.`, errorMsg: "Unwrap transaction failed. Check logs for error detalization.", }; const txHash = await executeOnChainTransaction({ chainId, args, messages, account, abi: StakingAbi, address: STAKING_ADDRESSES[chainId], functionName: "unwrap", }); return txHash; } export const wrap = async (chainId, account, amount, ghstSymbol) => { const args = [account, amount]; const messages = { replacedMsg: "Wrap transaction was replaced. Wait for inclusion please.", successMsg: `Tokens wrapped successfully! Check your ${ghstSymbol} balance on ${shorten(account)}.`, errorMsg: "Wrap transaction failed. Check logs for error detalization.", }; const txHash = await executeOnChainTransaction({ chainId, args, messages, account, abi: StakingAbi, address: STAKING_ADDRESSES[chainId], functionName: "wrap", }); return txHash; } export const ghost = async (chainId, account, receiver, amount) => { const args = [receiver, amount]; const messages = { replacedMsg: "Bridge transaction was replaced. Wait for inclusion please.", successMsg: `Amount successfully bridged! Check Transaction History to trace the progress.`, errorMsg: "Bridge transaction failed. Check logs for error detalization.", }; const txHash = await executeOnChainTransaction({ chainId, args, messages, account, abi: StakingAbi, address: STAKING_ADDRESSES[chainId], functionName: "ghost", }); return txHash; } export const verifySignature = async ({ chainId, account, gatekeeper, calldata, nonce, signature }) => { const args = [calldata, nonce, signature]; const messages = { replacedMsg: "Verification transaction was replaced. Wait for inclusion please.", successMsg: `Verification succesfully passed! Check Transaction History to trace the progress.`, errorMsg: "Verification transaction failed. Check logs for error detalization.", }; const txHash = await executeOnChainTransaction({ chainId, args, messages, account, abi: GatekeeperAbi, address: gatekeeper, functionName: "verify", gasMultiplier: 145n, // 45% more }); return txHash; }