Skip to content

Commit

Permalink
custom useBalance() implementation (from starknet-react)
Browse files Browse the repository at this point in the history
until @starknet-react/core migrates to v6
apibara/starknet-react#451
  • Loading branch information
rsodre committed Jun 7, 2024
1 parent 045363c commit 9fc29b2
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 13 deletions.
4 changes: 2 additions & 2 deletions client/src/lib/dojo/hooks/useDojoContractWrite.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useCallback, useState } from 'react'
import { BigNumberish, Abi, Account } from 'starknet'
import { useWaitForTransaction } from '@starknet-react/core'
import { useDojoAccount } from '@/lib/dojo/DojoContext'
import { execute } from '@/lib/utils/starknet'
import { bigintToHex } from '@/lib/utils/types'
import { BigNumberish, Abi, Account } from 'starknet'
import { execute } from '@/lib/utils/starknet'

export function useDojoContractWrite(contractAddress: BigNumberish, abi: Abi, functionName: string, callData: BigNumberish[], fromAccount: Account = null) {
const { account } = useDojoAccount()
Expand Down
4 changes: 2 additions & 2 deletions client/src/lib/dojo/hooks/useDojoERC20.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { useMemo } from 'react'
import { useDojoContractWrite } from '@/lib/dojo/hooks/useDojoContractWrite'
import { splitU256 } from '@/lib/utils/starknet'
import { bigintToUint256 } from '@/lib/utils/starknet'
import { bigintToHex } from '@/lib/utils/types'
import { erc20_abi } from '@/lib/abi'
import { BigNumberish, Account } from 'starknet'

export function useDojoERC20Transfer(contractAddress: BigNumberish, toAddress: BigNumberish, amount: bigint, fromAccount: Account = null) {
const { low, high } = useMemo(() => splitU256(amount), [amount])
const { low, high } = useMemo(() => bigintToUint256(amount), [amount])
const callData = useMemo(() => {
return [
bigintToHex(toAddress),
Expand Down
47 changes: 42 additions & 5 deletions client/src/lib/utils/hooks/useERC20.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { useMemo } from 'react'
import { useAccount, useBalance, useContractRead } from '@starknet-react/core'
import { useEffect, useMemo } from 'react'
import { useContractRead,
// useBalance,
UseBalanceProps, Balance,
} from '@starknet-react/core'
import { bigintToHex } from '@/lib/utils/types'
import { BigNumberish } from 'starknet'
import { BigNumberish, Uint256 } from 'starknet'
import { erc20_abi } from '@/lib/abi'
import { feltToString } from '../starknet'
import { Uint256ToBigint, feltToString, weiToEth } from '../starknet'

export const useERC20Balance = (contractAddress: BigNumberish, ownerAddress: BigNumberish, fee: BigNumberish = 0n) => {
const { data: balance } = useBalance({
// const { data: balance } = useBalance({
const balance = _useBalance({
token: bigintToHex(contractAddress),
address: bigintToHex(ownerAddress),
watch: true,
Expand Down Expand Up @@ -52,3 +56,36 @@ export const useERC20TokenName = (contractAddress: BigNumberish) => {
}
}


//--------------------------------------
// TEMP
// until @starknet-react/core v3 + starknet v6
// https://github.com/apibara/starknet-react/issues/451
//
enum BlockTag {
pending = "pending",
latest = "latest"
}
export const _useBalance = (props: UseBalanceProps): Balance => {
const { data, isError, isLoading, error } = useContractRead({
functionName: "balanceOf",
args: [bigintToHex(props.address)],
abi: erc20_abi,
address: bigintToHex(props.token),
enabled: (BigInt(props.address || 0) > 0n && BigInt(props.token || 0) > 0n),
// refetchInterval: 2000, // update every 2 seconds
// update every block
watch: true,
blockIdentifier: BlockTag.pending,
})
useEffect(() => { if (error) console.warn(`_useBalance() ERROR:`, error) }, [error])
//@ts-ignore
const value = useMemo<bigint>(() => (data?.balance ? Uint256ToBigint(data.balance as Uint256) : 0n), [data])
const formatted = useMemo(() => Number(weiToEth(value)).toString(), [value])
return {
decimals: 18,
symbol: '$?',
formatted,
value,
}
}
10 changes: 6 additions & 4 deletions client/src/lib/utils/starknet.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import {
ec,
shortString,
Account,
BigNumberish,
AccountInterface,
InvocationsDetails,
InvokeFunctionResponse,
RpcProvider,
shortString,
BigNumberish,
Uint256,
Abi,
ec,
} from 'starknet'
import { bigintToHex } from './types'

Expand All @@ -22,7 +23,8 @@ export const ethToWei = (v: BigNumberish): bigint => (BigInt(v) * ETH_TO_WEI)
export const weiToEth = (v: BigNumberish): bigint => (BigInt(v) / ETH_TO_WEI)
export const dummyAccount = (rpc?: RpcProvider): Account => (new Account(rpc ?? {}, '0x0', '0x0'))

export const splitU256 = (v: BigNumberish): { low: bigint, high: bigint } => ({
export const Uint256ToBigint = (v: Uint256): bigint => ((BigInt(v.high) << 128n) + BigInt(v.low))
export const bigintToUint256 = (v: BigNumberish): Uint256 => ({
low: BigInt(v) & 0xffffffffffffffffffffffffffffffffn,
high: BigInt(v) >> 128n,
})
Expand Down

0 comments on commit 9fc29b2

Please sign in to comment.