Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

ethers.js calling smart contract

import { ethers } from 'ethers';
(async () => {
  //calling smart contract which contains bigNumber
let bigNumber = await stakeContract.totalBalanceGrowRich();
 let number = ethers.utils.formatEther(bigNumber);
      setStakeInfo(prev => {
        return { ...prev, staked: Number(number).toLocaleString('en-US') };
  });
  //which return non big Number
 let currentTime = await stakeContract.getCurrentTime();
  setStakeInfo(prev => {
    return { ...prev, currentTime: Number(currentTime) };
   });
  
})()
Comment

Contract in ethers.js

Provider:
A Provider (in ethers) is a class which provides an abstraction for a connection to the Ethereum Network. 
It provides read-only access to the Blockchain and its status.

import { ethers } from "ethers";
const provider = new ethers.providers.Web3Provider(window.ethereum)

Signer:
A Signer is a class which (usually) in some way directly or indirectly has access to a private key,
which can sign messages and transactions to authorize the network to charge your account ether to perform operations.

const signer = provider.getSigner()

Contract:
A Contract is an abstraction which represents a connection to a specific contract on the Ethereum Network,
so that applications can use it like a normal JavaScript object.
Comment

calling contract in ether.js

import { ethers, BigNumber } from 'ethers'


  const [contract, setContract] = useState<any>(undefined)
  const [count, setCount] = useState(BigNumber.from(0))

  useEffect(() => {
    // @ts-ignore
    const provider = new ethers.providers.Web3Provider(window.ethereum)
    setContract(
      new ethers.Contract(
        String(process.env.NEXT_PUBLIC_CONTRACT_ADDRESS),
        contractAbi,
        provider
      )
    )
  }, [])

return (
     <main>
        <button
          className="px-4 bg-red-500"
          onClick={async () => {setCount(await contract.count())}}
        >
          Count
        </button>
        <p>{count.toString()}</p>
      </main>
)

Comment

PREVIOUS NEXT
Code Example
Typescript :: Create Type from String Enum 
Typescript :: typescript get types from arrays 
Typescript :: c# check list of objects for value 
Typescript :: import xml elements in kotlin 
Typescript :: typescript function as type 
Typescript :: how to count digits in python 
Typescript :: how to push value in empty array in typescript 
Typescript :: components of cucumber bdd framework 
Typescript :: types for array props 
Typescript :: the events calendar update the word event 
Typescript :: 2. Write a program to draw this. Assume the innermost square is 20 units per side, and each successive square is 20 units bigger, per side, than the one inside it. 
Typescript :: class-validator not working nest-typescript-starter 
Typescript :: shortid typescript 
Typescript :: java check if element exists in array 
Typescript :: how to remove second square brackets in an array 
Typescript :: linux copy all directory contents to another directory 
Typescript :: typescript compare types 
Typescript :: set constraints for UIView swift 
Typescript :: how to search for elements that are on the webpage using html 
Typescript :: python get elements from list of dictionaries 
Typescript :: dart create list from object properties 
Typescript :: arrays in typescript 
Typescript :: nuxt "AxiosRequestConfig" 
Typescript :: join elements in a list with , java 
Typescript :: declare type function typescript 
Typescript :: list elements not in indices 
Typescript :: sts is not opening in mac 
Typescript :: stipe elements angular.js 
Typescript :: interface extending mongoose document object does not contain _doc object typescript 
Typescript :: 365+6 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =