ABOUT ME

Today
Yesterday
Total
  • 이더 전송 모든네트워크 가능 메타마스크
    코인/코인현황 2022. 11. 11. 14:39
    반응형

    import logo from './logo.svg';
    import './App.css';
    import Web3 from 'web3';
    import { useEffect,useState } from 'react';
    
    function App() {
      const [web3, setWeb3] = useState();
      const [account,setAccount] = useState('');
      const [hash , setHash] = useState('');
      const [fromAccount,setFromAccount] = useState('');
      const [toAccount,setToAccount] = useState('');
      const [ethValue,setEthValue] = useState('');
    
      useEffect(()=>{
          if(typeof window.ethereum !== "undefined"){
            try{
              const web = new Web3(window.ethereum);
              setWeb3(web);
            }catch(err){
              console.log(err);
            }
          }
      },[]);
    
      const connectWallet = async ()=>{
         const  accounts = await window.ethereum.request({
            method: "eth_requestAccounts",
         }); 
    
         setAccount(accounts[0]);
      }
    
      const inputChangeFrom=(e)=>{
        setFromAccount(e.target.value);
      }
    
      const inputChangeTo=(e)=>{
        setToAccount(e.target.value);
      }
    
      const inputChangeEth=(e)=>{
        setEthValue(e.target.value);
      }
    
      const SendEth = async()=>{
           // using the promise
         await web3.eth.sendTransaction({
            from: fromAccount,
            to: toAccount,
            value: web3.utils.toWei(ethValue,'ether')
          })
          .then(function(receipt){
            console.log(receipt.blockHash);
            setHash(receipt.blockHash);
          });
      }
    
    
    
      return (
        <div className='App'>
          <button onClick={connectWallet} >지갑연결</button>
             <div>주소 : {account}</div>
             <br/>
             <input type="text"  onChange={inputChangeFrom} placeholder ="보낼주소"/>
             <br/>
             <input type="text" onChange={inputChangeTo}  placeholder ="받을주소"/>
             <br/>
             <input type="text" onChange={inputChangeEth}  placeholder ="eth"/>
             <br/>
             <button className="ethBtn" onClick={SendEth}>이더 전송</button>
             <br/>
             
             <br/>
             <div className='tran'>트랜잭션 : {hash}</div>
        </div>
             
       
      );
    }
    
    export default App;

    App.js

    반응형
Designed by Tistory.