Skip to content

Commit

Permalink
fix: wrapping tokens (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxencerb authored Aug 26, 2024
1 parent 31e944b commit 645dc0f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
12 changes: 5 additions & 7 deletions components/stateful/dialogs/unwrap-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { Title } from "@/components/typography/title"
import { Button } from "@/components/ui/button"
import { Close } from "@/svgs"
import wethAbi from "../../../app/faucet/_abis/weth.json"
import { wethAdresses } from "./wrap-dialog"

type Props = {
isOpen: boolean
Expand All @@ -30,22 +31,19 @@ export default function UnWrapETHDialog({ isOpen, onClose }: Props) {
const [amount, setAmount] = React.useState("")
const [amountError, setAmountError] = React.useState("")

const wethAdresses: { [key: number]: string } = {
168587773: "0x4200000000000000000000000000000000000023",
81457: "0x4300000000000000000000000000000000000004",
}

const { data: wethBalance } = useBalance({
address,
token: wethAdresses[chain?.id as number] as Address,
token: wethAdresses[chain?.id as number],
})

const unWrapETH = () => {
try {
if (!chain?.id || !wethBalance) return
const wethAdress = wethAdresses[chain.id]
if (!wethAdress) return
const parsedAmount = parseUnits(amount, wethBalance.decimals)
writeContract({
address: wethAdresses[chain.id] as Address,
address: wethAdress,
abi: wethAbi,
functionName: "withdraw",
args: [parsedAmount],
Expand Down
15 changes: 9 additions & 6 deletions components/stateful/dialogs/wrap-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ type Props = {
onClose: () => void
}

export const wethAdresses: { [key: number]: Address | undefined } = {
168587773: "0x4200000000000000000000000000000000000023",
81457: "0x4300000000000000000000000000000000000004",
42161: "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1",
}

export default function WrapETHDialog({ isOpen, onClose }: Props) {
const { chain, address } = useAccount()
const { data: nativeBalance } = useBalance({
Expand All @@ -33,16 +39,13 @@ export default function WrapETHDialog({ isOpen, onClose }: Props) {
const [amount, setAmount] = React.useState("")
const [amountError, setAmountError] = React.useState("")

const wethAdresses: { [key: number]: string } = {
168587773: "0x4200000000000000000000000000000000000023",
81457: "0x4300000000000000000000000000000000000004",
}

const wrapETH = () => {
try {
if (!chain?.id) return
const wethAdress = wethAdresses[chain.id]
if (!wethAdress) return
sendTransaction({
to: wethAdresses[chain.id] as Address,
to: wethAdress,
value: parseEther(amount),
})
} catch (error) {
Expand Down

0 comments on commit 645dc0f

Please sign in to comment.