Skip to content

Commit

Permalink
Merge pull request #3 from kadena-community/wc-disable-gas
Browse files Browse the repository at this point in the history
Disable Gas Station for WalletConnect
  • Loading branch information
chicodias authored Dec 10, 2024
2 parents 17ea9d1 + 12c8086 commit 71ff1dd
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 100 deletions.
2 changes: 1 addition & 1 deletion backend/API/Models/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class ClaimEvent : IBondEvent
public string RequestKey { get; set; } = "";
public string Type {get; set;} = "Claim";

public decimal Amount => TotalAmount;
public decimal Amount => OriginalAmount;
}

public class VoteEvent
Expand Down
30 changes: 9 additions & 21 deletions backend/API/Services/BondService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,30 +251,18 @@ public async Task<List<IBondEvent>> GetAllLockupsFromBond(string bondId, bool ig
var allLocks = await GetAllLockupEvents(ignoreCache);
var allClaims = await GetAllClaimEvents(ignoreCache);

var combinedEvents = new List<IBondEvent>();
var matchedLocks = new HashSet<LockEvent>();
var matchedClaims = new HashSet<ClaimEvent>();
var combinedEvents = allLocks
.Where(l => l.BondId == bondId)
.Cast<IBondEvent>()
.ToList();

foreach (var claim in allClaims.Where(c => c.BondId == bondId))
{
var matchedLock = allLocks
.Where(l => l.BondId == bondId && l.Account == claim.Account && l.Amount == claim.Amount)
.Where(l => l.Timestamp.AddSeconds((double)l.LockupLength) <= claim.Timestamp)
.Where(l => l.Rewards >= claim.Rewards)
.OrderBy(l => l.Timestamp)
.FirstOrDefault();

if (matchedLock != null)
{
combinedEvents.Add(claim);
matchedLocks.Add(matchedLock);
matchedClaims.Add(claim);
}
}
combinedEvents.AddRange(
allClaims.Where(c => c.BondId == bondId)
.Cast<IBondEvent>()
);

combinedEvents.AddRange(allLocks.Where(l => l.BondId == bondId && !matchedLocks.Contains(l)));
return combinedEvents;

return combinedEvents.Cast<IBondEvent>().ToList();
}

public async Task<decimal> GetMaxLockupReturns(decimal amount, decimal length, string bondId, bool ignoreCache = false)
Expand Down
134 changes: 80 additions & 54 deletions web/src/features/gasStation/gasModal.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useRef, useEffect, useState } from 'react';
import styles from '@/styles/main.module.css';
import React, { useRef, useEffect, useState } from "react";
import styles from "@/styles/main.module.css";
import { useAppDispatch, useAppSelector } from "@/app/hooks";
import {
selectGasConfig,
Expand All @@ -8,7 +8,7 @@ import {
setGasPrice,
toggleGasStation,
} from "./gasSlice";
import {XCircle} from 'react-feather';
import { XCircle } from "react-feather";

interface GasStationModalProps {
isOpen: boolean;
Expand All @@ -25,24 +25,30 @@ const GasStationModal: React.FC<GasStationModalProps> = ({
const gasConfig = useAppSelector(selectGasConfig);

const [localGasLimit, setLocalGasLimit] = useState(gasConfig?.LIMIT ?? 10000);
const [localGasPrice, setLocalGasPrice] = useState(gasConfig?.PRICE ?? 0.00000001);
const [localGasStationEnabled, setLocalGasStationEnabled] = useState(gasStationEnabled);
const [localGasPrice, setLocalGasPrice] = useState(
gasConfig?.PRICE ?? 0.00000001,
);
const [localGasStationEnabled, setLocalGasStationEnabled] =
useState(gasStationEnabled);

useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (modalRef.current && !modalRef.current.contains(event.target as Node)) {
if (
modalRef.current &&
!modalRef.current.contains(event.target as Node)
) {
onClose();
}
};

if (isOpen) {
document.addEventListener('mousedown', handleClickOutside);
document.addEventListener("mousedown", handleClickOutside);
} else {
document.removeEventListener('mousedown', handleClickOutside);
document.removeEventListener("mousedown", handleClickOutside);
}

return () => {
document.removeEventListener('mousedown', handleClickOutside);
document.removeEventListener("mousedown", handleClickOutside);
};
}, [isOpen, onClose]);

Expand Down Expand Up @@ -84,63 +90,83 @@ const GasStationModal: React.FC<GasStationModalProps> = ({
};

return (
<div className={`${styles.modalOverlay} ${!isOpen && 'hidden'}`}>
<div className={`${styles.modalOverlay} ${!isOpen && "hidden"}`}>
<div className={styles.modalContainer}>
<div ref={modalRef} className="modal">
<div className="flex text-justify justify-between items-center border-gray-200 border-b">
<h2 className={styles.modalHeader}>Gas Configuration</h2>
<XCircle className="ml-6 mb-3 w-6 h-6" onClick={onClose} />
</div>
<div className={styles.modalBody}>
<div className="flex items-center justify-between mb-4">
<h3>Gas Station</h3>
<label className={`${styles.switch} ml-4`}>
<div className={styles.modalBody}>
<div className="flex items-center justify-between mb-4">
<h3>Gas Station</h3>
<label className={`${styles.switch} ml-4`}>
<input
type="checkbox"
checked={localGasStationEnabled}
onChange={handleToggleChange}
/>
<span className={styles.slider}></span>
</label>
</div>
{!localGasStationEnabled && (
<>
<label>
<span>Gas Limit</span>
<input
type="checkbox"
checked={localGasStationEnabled}
onChange={handleToggleChange}
type="number"
value={localGasLimit}
onChange={handleGasLimitChange}
className="mt-1 block w-full"
/>
<span className={styles.slider}></span>
</label>
</div>
{!localGasStationEnabled && (
<>
<label>
<span>Gas Limit</span>
<input
type="number"
value={localGasLimit}
onChange={handleGasLimitChange}
className="mt-1 block w-full"
/>
</label>
<label className="mt-4">
<span>Gas Price</span>
<input
type="number"
step="0.00000001"
value={localGasPrice}
onChange={handleGasPriceChange}
className="mt-1 block w-full"
/>
</label>
<div className="flex space-x-2 mt-4">
<button className={styles.button} onClick={() => handleSpeedClick(1)}>LOW</button>
<button className={styles.button} onClick={() => handleSpeedClick(100)}>NORMAL</button>
<button className={styles.button} onClick={() => handleSpeedClick(10000)}>FAST</button>
</div>
<p className="mt-4">
Potential gas cost for transaction failure: <strong>{localGasPrice * localGasLimit} KDA</strong>
</p>
</>
)}
</div>
<div className={`${styles.modalActions} flex justify-end mt-6 space-x-2`}>
<button className={styles.button} onClick={handleSetClick}>Set</button>
</div>
<label className="mt-4">
<span>Gas Price</span>
<input
type="number"
step="0.00000001"
value={localGasPrice}
onChange={handleGasPriceChange}
className="mt-1 block w-full"
/>
</label>
<div className="flex space-x-2 mt-4">
<button
className={styles.button}
onClick={() => handleSpeedClick(1)}
>
LOW
</button>
<button
className={styles.button}
onClick={() => handleSpeedClick(100)}
>
NORMAL
</button>
<button
className={styles.button}
onClick={() => handleSpeedClick(10000)}
>
FAST
</button>
</div>
<p className="mt-4">
Potential gas cost for transaction failure:{" "}
<strong>{localGasPrice * localGasLimit} KDA</strong>
</p>
</>
)}
</div>
<div
className={`${styles.modalActions} flex justify-end mt-6 space-x-2`}
>
<button className={styles.button} onClick={handleSetClick}>
Set
</button>
</div>
</div>
</div>
</div>
);
};

Expand Down
37 changes: 31 additions & 6 deletions web/src/features/layout/Head.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ import React, { useEffect, useState } from "react";
import { Menu } from "react-feather";
import useScrollPosition from "@react-hook/window-scroll";
import { useKadenaReact } from "../../kadena/core";
import { useLoginModalToggle } from "../wallet/hooks";
import Web3Status from "../components/Web3Status";
import CabinetLogo from "@/assets/images/cabinetLogo.svg"; // Light mode SVG
import { selectIsCoreMember } from "../bond/bondSlice";
import { useAppDispatch, useAppSelector } from "../../app/hooks";
import {
CHAIN_INFO,
Expand All @@ -16,14 +14,20 @@ import GasStation from "@/features/gasStation";
import ActivityBar from "@/features/activityBar";
import SidebarModal from "./SidebarModal"; // Import SidebarModal component
import ThemeToggle from "./ThemeToggler";
import {
selectGasStationEnabled,
toggleGasStation,
} from "../gasStation/gasSlice";
import { useAddPopup } from "../main/hooks";

export default function Head() {
const dispatch = useAppDispatch();
const toggleLoginModal = useLoginModalToggle();
const { account } = useKadenaReact();
const kda = useKadenaReact();
const scrollY = useScrollPosition(60); // The value here sets when the header style changes
const isCoreMember = useAppSelector(selectIsCoreMember);
const [isSidebarOpen, setSidebarOpen] = useState(false); // State to toggle sidebar
const gasStationEnabled = useAppSelector(selectGasStationEnabled);
const addPopup = useAddPopup();
const popupShownKey = "gasStationPopupShown";

// Add hasMounted state
const [hasMounted, setHasMounted] = useState(false);
Expand All @@ -32,6 +36,27 @@ export default function Head() {
setHasMounted(true);
}, []);

useEffect(() => {
if (
kda.connector?.constructor?.name === "WalletConnect" &&
gasStationEnabled
) {
// Disable gas station for WalletConnect users if currently enabled
dispatch(toggleGasStation());

const popupShown = localStorage.getItem(popupShownKey);

if (!popupShown) {
addPopup({
reqKey: undefined,
msg: `Gas station disabled due to known issues with your provider.`,
status: "WARNING",
});
localStorage.setItem(popupShownKey, "true");
}
}
}, [kda.connector, dispatch]);

return (
<div
className={`headerWrapper headerWrapperWithBorder ${
Expand All @@ -57,7 +82,7 @@ export default function Head() {
</p>
<ThemeToggle />
<GasStation />
{account?.account && <ActivityBar accountId={account.account} />}
{kda?.account && <ActivityBar accountId={kda.account.account} />}
<Web3Status />
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion web/src/features/lockup/Lockups.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ const Lockups: React.FC = () => {
<div className="container mx-auto">
{loading && (
<div>
<AppLoader true size="96px" stroke="#E27B38" />
<AppLoader size="96px" stroke="#E27B38" />
</div>
)}
{error && <Error message={error} />}
Expand Down
2 changes: 1 addition & 1 deletion web/src/features/pollWarning/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const VoteWarning: React.FC = () => {
useEffect(() => {
dispatch(fetchActivePolls(false));
const fetchVoteStatus = async () => {
if (account && activePolls) {
if (account && activePolls.length > 0) {
console.log(JSON.stringify(activePolls));
const pollIds = activePolls.map((poll) => poll.pollId);
try {
Expand Down
Loading

0 comments on commit 71ff1dd

Please sign in to comment.