Untitled
unknown
typescript
2 years ago
7.7 kB
6
Indexable
import React, { useEffect, useState } from "react"; import detectEthereumProvider from "@metamask/detect-provider"; import Web3 from "web3"; function App() { const [web3, setWeb3] = useState(null); const [account, setAccount] = useState(""); const [amount, setAmount] = useState(""); const [stakingDuration, setStakingDuration] = useState(""); useEffect(() => { const initWeb3 = async () => { if (window.ethereum) { const web3Instance = new Web3(window.ethereum); window.ethereum.enable(); setWeb3(web3Instance); const accounts = await web3Instance.eth.requestAccounts(); setAccount(accounts[0]); } else { console.error("Metamask not detected"); } }; initWeb3(); }, []); const handleStake = async () => { if (web3) { const contractAddress = "0x2750e95433b10efe9d1967052eed0e1afd300d97"; // Stake sözleşmesinin adresini buraya girin const contractABI = [ { anonymous: false, inputs: [ { indexed: false, internalType: "uint8", name: "version", type: "uint8", }, ], name: "Initialized", type: "event", }, { anonymous: false, inputs: [ { indexed: true, internalType: "address", name: "previousOwner", type: "address", }, { indexed: true, internalType: "address", name: "newOwner", type: "address", }, ], name: "OwnershipTransferred", type: "event", }, { anonymous: false, inputs: [ { indexed: false, internalType: "address", name: "account", type: "address", }, ], name: "Paused", type: "event", }, { anonymous: false, inputs: [ { indexed: true, internalType: "address", name: "_address", type: "address", }, { indexed: false, internalType: "uint256", name: "_amount", type: "uint256", }, { indexed: false, internalType: "uint256", name: "_stakingDuration", type: "uint256", }, { indexed: false, internalType: "uint256", name: "_undex", type: "uint256", }, ], name: "Staked", type: "event", }, { anonymous: false, inputs: [ { indexed: false, internalType: "address", name: "account", type: "address", }, ], name: "Unpaused", type: "event", }, { inputs: [ { internalType: "uint256", name: "_amount", type: "uint256" }, { internalType: "uint256", name: "_stakingDuration", type: "uint256", }, ], name: "addStake", outputs: [], stateMutability: "nonpayable", type: "function", }, { inputs: [ { internalType: "address", name: "_token", type: "address" }, ], name: "initialize", outputs: [], stateMutability: "nonpayable", type: "function", }, { inputs: [], name: "owner", outputs: [{ internalType: "address", name: "", type: "address" }], stateMutability: "view", type: "function", }, { inputs: [], name: "pause", outputs: [], stateMutability: "nonpayable", type: "function", }, { inputs: [], name: "paused", outputs: [{ internalType: "bool", name: "", type: "bool" }], stateMutability: "view", type: "function", }, { inputs: [], name: "renounceOwnership", outputs: [], stateMutability: "nonpayable", type: "function", }, { inputs: [ { internalType: "address", name: "", type: "address" }, { internalType: "uint256", name: "", type: "uint256" }, ], name: "stakes", outputs: [ { internalType: "uint256", name: "amount", type: "uint256" }, { internalType: "uint256", name: "stakingDuration", type: "uint256", }, { internalType: "uint256", name: "startTime", type: "uint256" }, { internalType: "bool", name: "isUnlocked", type: "bool" }, ], stateMutability: "view", type: "function", }, { inputs: [], name: "token", outputs: [ { internalType: "contract IERC20Upgradeable", name: "", type: "address", }, ], stateMutability: "view", type: "function", }, { inputs: [], name: "totalStaked", outputs: [{ internalType: "uint256", name: "", type: "uint256" }], stateMutability: "view", type: "function", }, { inputs: [ { internalType: "address", name: "newOwner", type: "address" }, ], name: "transferOwnership", outputs: [], stateMutability: "nonpayable", type: "function", }, { inputs: [ { internalType: "uint256", name: "_index", type: "uint256" }, ], name: "unlock", outputs: [], stateMutability: "nonpayable", type: "function", }, { inputs: [], name: "unpause", outputs: [], stateMutability: "nonpayable", type: "function", }, ]; const contract = new web3.eth.Contract(contractABI, contractAddress); window.contract = contract; const weiAmount = web3.utils.toWei(amount.toString(), "ether"); const duration = parseInt(stakingDuration); console.log(contract); try { const result = await contract.methods .addStake(contractAddress, weiAmount) .send({ from: account }); console.log("Stake transaction successful:", result); // Stake işlemi başarılı olduysa burada gerekli işlemleri gerçekleştirin (örneğin bildirim gösterme) } catch (error) { console.error("Stake transaction failed:", error); // Stake işlemi başarısız olduysa burada gerekli işlemleri gerçekleştirin (örneğin hata mesajı gösterme) } } }; return ( <div className="App"> <h1>Stake Sayfası</h1> <p>Account: {account}</p> <input type="number" placeholder="Amount" value={amount} onChange={(e) => setAmount(e.target.value)} /> <input type="number" placeholder="Staking Duration (in seconds)" value={stakingDuration} onChange={(e) => setStakingDuration(e.target.value)} /> <button onClick={handleStake}>Stake</button> </div> ); } export default App;
Editor is loading...