Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

interact with blockchain from nextjs

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 :: stripe create customer 
Typescript :: get type of element of array typescript 
Typescript :: angle between two vectors 
Typescript :: typeorm find with limit 
Typescript :: typescript pass a function as an argunetn 
Typescript :: typescript value in enum 
Typescript :: typescript object type 
Typescript :: isnull or empty typescript 
Typescript :: set up react with typescript 
Typescript :: react google charts x labels multiline 
Typescript :: how to read temp file in windowsnodejs 
Typescript :: auto complete of process.env in typescript 
Typescript :: vercel react redirects to index html 
Typescript :: react tailwind css components npm 
Typescript :: scripted testing and exploratory testing 
Typescript :: Warning: call_user_func_array() expects parameter 1 to be a valid callback 
Typescript :: replace floats in dataframe 
Typescript :: running tests in r 
Typescript :: typescript keyof object 
Typescript :: what is data type in data structure 
Typescript :: typescript class 
Typescript :: this typescript 
Typescript :: get number of digits in an integer python without converting to string 
Typescript :: styled components gatsby 
Typescript :: typescript object of type interface 
Typescript :: nest js joi usage 
Typescript :: tsc : File C:UsersajayAppDataRoaming pm sc.ps1 cannot be loaded because running scripts is disabled on this system. 
Typescript :: extract digits with serten lenth from string python 
Typescript :: listen to hub events asw analytics 
Typescript :: python double check if wants to execute funtion 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =