import { useEffect, useState, useRef } from "react";
import { IncentivResolver, IncentivSigner } from "@incentiv/dapp-sdk";
import { parseEther, formatEther } 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.JsonRpcProvider(
Config.Environments.Mainnet.RPC
);
const signer = new IncentivSigner({
address: userAddress,
provider: provider,
environment: Config.Environments.Mainnet.Portal,
entryPoint: "0x3eC61c5633BBD7Afa9144C6610930489736a72d4",
});
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 {
const userAddress = await IncentivResolver.getAccountAddress(
Config.Environments.Mainnet.Portal
);
if (!userAddress || userAddress === "null" || userAddress === null) {
throw new Error(
"No wallet address received. The popup may have been closed or the connection was rejected."
);
}
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 = 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 = parseEtheramount.toString());
const data = contractInterface.encodeFunctionData("deposit", []);
const tx = await signer.sendTransaction({
to: Config.SC.address,
data,
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 = parseEtheramount.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;