GithubHelp home page GithubHelp logo

metacraters-eth-avax_mod2-main's Introduction

METACRAFTERES PROJECT-2

To run the Project

npm run dev

SETUP

to install dependencies

npm

Functions of the project

Connect wallet

Any wallet which is used cryprocurrencies is present in the window object. that wallet can be fetched to be used in the Dapp. here is the code of how app is connect to the wallet in the metamask.

export default function ConnectWallet() {

    async function connect (){
      if (typeof window.ethereum !== "undefined") {
        
        console.log("I see a metamask")
        await window.ethereum.request({method: 'eth_requestAccounts'})
        document.getElementById('connectWalletBtn').innerHTML="Connected"
        console.log("metamask is now connected")
        
      } else {
        document.getElementById('connectWalletBtn').innerHTML="Install Metamask"
        console.log("No metamask")
      }
    }
    
  
    return (
        <button className="bg-blue-500 hover:bg-blue-700 text-white font-bold   text-center md:ml-5 sm:ml-3 ml-7 p-2 mt-32 rounded-full" onClick={()=>{connect();}} id='connectWalletBtn'>
          Connect to your metacraft wallet
        </button>
    )
  }

Get balance of the contract

any deployed contract has a property called getBalance which is used here to display the balance

async function getBal() {
  const d = new Date()
  const day = d.getDay()
  const month = d.getMonth()
  const year = d.getFullYear()
  if (typeof window.ethereum !== "undefined") {
    const provider = new ethers.providers.Web3Provider(window.ethereum);
    

    await provider.getBalance(contractAddress).then((data)=>{
      const bal=data 
      console.log(ethers.utils.formatEther(bal));
      document.getElementById('getBalButton').innerHTML = `Funds raised till ${day}-${month}-${year} is 
      ${ethers.utils.formatEther(bal)}`
    }).catch((err)=>{console.error(err);})
  }

Fund the contract with user provided number of tokens

Here is the most important function in this contract . Fund() is a function which is defined in the contract itself, this is payable function which is used to send some value to the contract; this value can be sent by anyone who uses this contract. This function is actually accessed through the ABI of the contract after its deployment.

Here is the solidity code of fund()

function fund() public payable {
        require(msg.value.getConversionRate() >= MINIMUM_USD, "You need to spend more ETH!");
        addressToAmountFunded[msg.sender] += msg.value;
        funders.push(msg.sender);
    }

And here is its javascript implementation

function Fund() {
  let amount = document.getElementById('amt').value
  console.log(`Funding the contract with ${amount}`);
  if (typeof window.ethereum !== "undefined") {
    const provider = new ethers.providers.Web3Provider(window.ethereum)
    const signer = provider.getSigner()
    
    const contract = new ethers.Contract(contractAddress, abi, signer)
    console.log(contract);
    try {
      let txRes = {}
      contract.fund({
        value: ethers.utils.parseEther(amount)
      })
      .then((data) => { 
        listenForTransactionMine(data,provider).then((data)=>{console.log(data);}).catch((err)=>{console.log(err);}); })
      .catch((err) => { console.log(err); })

      
      document.getElementById('fund').innerHTML = "Funded"
      console.log("done"); 
    } catch (err) {
      console.error(err); 
    }

    function listenForTransactionMine(transactionResponse, provider) {
  console.log(`Mining ${transactionResponse.hash}...`)
  return new Promise((resolve, reject) => {
    provider.once(transactionResponse.hash, (transactionReceipt) => {
      console.log(
        `Completed with ${transactionReceipt.confirmations}confirmations`
      );
      resolve("transaction mined")
    })

  })
}

metacraters-eth-avax_mod2-main's People

Contributors

aditya41150 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.