import { useEffect, useState, useRef } from "react";
import { IncentivResolver, IncentivSigner } from "@incentiv/dapp-sdk";
import { ethers } from "ethers";
import ABI from "./YourContractABI.json";
import Config from "./config";
function FullIntegrationExample() {
const [address, setAddress] = useState("");
const [contract, setContract] = useState(null);
const [balance, setBalance] = useState(0);
const [isConnecting, setIsConnecting] = useState(false);
const [isProcessing, setIsProcessing] = useState(false);
const signerRef = useRef(null);
// Initialize contract and signer
async function fetchContractData(userAddress) {
try {
const provider = new ethers.providers.JsonRpcProvider(
Config.Environments.Mainnet.RPC
);
const signer = new IncentivSigner({
address: userAddress,
provider: provider,
environment: Config.Environments.Mainnet.Portal,
entryPoint: "[EntryPoint contract address]",
});
signerRef.current = signer;
const contractInstance = new ethers.Contract(
Config.GamePot.address,
ABI,
signer
);
setContract(contractInstance);
await updateBalance(userAddress, contractInstance);
} catch (err) {
console.error("Failed to initialize contract:", err);
setContract(null);
signerRef.current = null;
}
}
// Connect wallet
const handleConnect = async () => {
setIsConnecting(true);
try {
if (typeof IncentivResolver === "undefined") {
throw new Error("Incentiv wallet extension not found.");
}
const userAddress = await IncentivResolver.getAccountAddress(
Config.Environments.Mainnet.Portal
);
if (!userAddress || userAddress === "null" || userAddress === null) {
throw new Error("No wallet address received.");
}
setAddress(userAddress);
await fetchContractData(userAddress);
} catch (err) {
console.error("Connection error:", err);
alert(`Failed to connect: ${err.message}`);
} finally {
setIsConnecting(false);
}
};
// Update balance
async function updateBalance(userAddr, contractInstance) {
if (!contractInstance || !userAddr) return;
try {
const balanceWei = await contractInstance.deposits(userAddr);
const balanceCENT = ethers.utils.formatEther(balanceWei);
setBalance(parseFloat(balanceCENT));
} catch (err) {
console.error("Failed to read balance:", err);
}
}
// Deposit
const handleDeposit = async (amount) => {
if (!contract) return;
setIsProcessing(true);
try {
const value = ethers.utils.parseEther(amount.toString());
const tx = await contract.deposit({ value });
console.log("Transaction sent:", tx.hash);
await tx.wait();
console.log("Transaction confirmed");
await updateBalance(address, contract);
} catch (err) {
console.error("Deposit failed:", err);
alert(`Deposit failed: ${err.message}`);
} finally {
setIsProcessing(false);
}
};
// Withdraw
const handleWithdraw = async (amount) => {
if (!contract) return;
setIsProcessing(true);
try {
const amountWei = ethers.utils.parseEther(amount.toString());
const tx = await contract.withdraw(amountWei);
console.log("Transaction sent:", tx.hash);
await tx.wait();
console.log("Transaction confirmed");
await updateBalance(address, contract);
} catch (err) {
console.error("Withdrawal failed:", err);
alert(`Withdrawal failed: ${err.message}`);
} finally {
setIsProcessing(false);
}
};
return (
<div style={{ padding: "20px" }}>
<h1>Incentiv SDK Integration</h1>
{!address ? (
<button onClick={handleConnect} disabled={isConnecting}>
{isConnecting ? "Connecting..." : "Connect Wallet"}
</button>
) : (
<div>
<p><strong>Connected:</strong> {address}</p>
<p><strong>Balance:</strong> {balance.toFixed(4)} CENT</p>
<p><strong>Contract Status:</strong> {contract ? "Ready" : "Not initialized"}</p>
<div style={{ marginTop: "20px" }}>
<button
onClick={() => handleDeposit(10)}
disabled={isProcessing}
style={{ marginRight: "10px" }}
>
Deposit 10 CENT
</button>
<button
onClick={() => handleWithdraw(5)}
disabled={isProcessing}
>
Withdraw 5 CENT
</button>
</div>
</div>
)}
</div>
);
}
export default FullIntegrationExample;