@openbmp/web3
TypeScript icon, indicating that this package has built-in type declarations

0.0.5Β β€’Β PublicΒ β€’Β Published

Welcome to @openbmp/web3 πŸ‘‹

Version Documentation Maintenance

Binance Mini Program Web3 Provider

Install

pnpm add @openbmp/web3

Usage

Wrap App with Web3Provider

// app.ts
import { Web3Provider } from '@openbmp/web3'

const App = ({ children }) => {
  return <Web3Provider>{children}</Web3Provider>
}

export default App

Connect to Binance DeFi Wallet

import { Box, Button } from '@openbmp/uikit'
import { useAuth } from '@openbmp/web3'
import React, { useCallback, useState, type FC } from 'react'

// type definitions
export type Props = Record<string, unknown>

const WalletAddr: FC<Props> = () => {
  const { account, login, logOut } = useAuth()
  const [loading, setLoading] = useState(false)
  const isActive = !!account

  const handleWalletClick = useCallback(async () => {
    setLoading(true)
    if (isActive) {
      await logOut()
    } else {
      await login()
    }
    setLoading(false)
  }, [account, login, setLoading])

  return (
    <Box onClick={handleWalletClick}>
      <Button size="small" type="secondary" style={{ marginLeft: '16px' }} loading={loading}>
        {isActive ? 'Log Out' : 'Connect'}
      </Button>
    </Box>
  )
}

export default WalletAddr

Get Account Address

import { useAuth } from '@openbmp/web3'

const { account } = useAuth()

Invoke contract method

import { useContract } from '@openbmp/web3'
import { type AbiItem } from 'web3-utils'
import comptrollerAbi from 'path/to/comptroller.json'

const comptrollerContract = useContract<Comptroller>({
  abiItems: comptrollerAbi as AbiItem[],
  contractAddress: '0xfD36E2c2a6789Db23113685031d7F16329158384',
})

comptrollerContract.methods.methodName().call()

Author

πŸ‘€ Sora Liu soraliu.dev@gmail.com

Show your support

Give a ⭐️ if this project helped you!

πŸ“ License

This project is MIT licensed.

Readme

Keywords

none

Package Sidebar

Install

npm i @openbmp/web3

Weekly Downloads

0

Version

0.0.5

License

MIT

Unpacked Size

33.3 kB

Total Files

36

Last publish

Collaborators

  • rdcavanha
  • jadson666
  • soraliu