Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Commit

Permalink
remove dep on ethers signer
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Jul 8, 2024
1 parent 14a0a7a commit 8070087
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 51 deletions.
4 changes: 1 addition & 3 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const universalSwapContract = "0xb459F14260D1dc6484CE56EB0826be317171e91F"
export default function IndexPage() {
const { stakingDelegations } = useStakingContext()
const { prices } = usePricesContext()
const { trackTransaction, cctxs, inbounds } = useCCTXsContext()
const { trackTransaction } = useCCTXsContext()

const { balances, balancesLoading, balancesRefreshing, fetchBalances } =
useBalanceContext()
Expand Down Expand Up @@ -105,8 +105,6 @@ export default function IndexPage() {

return (
<div>
{JSON.stringify(cctxs)}
{JSON.stringify(inbounds)}
<div className="grid sm:grid-cols-3 gap-x-10">
<div className="sm:col-span-2 overflow-x-scroll mb-20">
<div className="mt-12 mb-8">
Expand Down
101 changes: 58 additions & 43 deletions components/Swap/hooks/useSendTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ const useSendTransaction = (
addressSelected: string,
setSourceAmount: (amount: string) => void,
omnichainSwapContractAddress: string,
track: any,
bitcoinAddress: string,
client: any,
signer: any
track?: any
) => {
const { address } = useAccount()
const [isSending, setIsSending] = useState(false)
Expand Down Expand Up @@ -102,9 +101,9 @@ const useSendTransaction = (
window.xfi.bitcoin.request(
bitcoinXDEFITransfer(bitcoinAddress, bitcoinTSSAddress, a, memo),
(error: any, hash: any) => {
if (!error) {
if (!error && track) {
track({
inboundHash: hash,
hash: hash,
desc: `Sent ${sourceAmount} tBTC`,
})
}
Expand All @@ -131,9 +130,9 @@ const useSendTransaction = (
window.xfi.bitcoin.request(
bitcoinXDEFITransfer(bitcoinAddress, bitcoinTSSAddress, a, memo),
(error: any, hash: any) => {
if (!error) {
if (!error && track) {
track({
inboundHash: hash,
hash: hash,
desc: `Sent ${a} tBTC`,
})
}
Expand All @@ -142,7 +141,7 @@ const useSendTransaction = (
}

m.transferNativeEVM = async () => {
await signer?.sendTransaction({
await client.signer?.sendTransaction({
to: addressSelected,
value: parseEther(sourceAmount),
})
Expand All @@ -160,10 +159,12 @@ const useSendTransaction = (
recipient: address as string,
amount: sourceAmount,
})
track({
inboundHash: tx.hash,
desc: `Sent ${sourceAmount} ZETA from ${from} to ${to}`,
})
if (track) {
track({
hash: tx.hash,
desc: `Sent ${sourceAmount} ZETA from ${from} to ${to}`,
})
}
}

m.withdrawBTC = async () => {
Expand All @@ -179,18 +180,20 @@ const useSendTransaction = (
amount: sourceAmount,
recipient: addressSelected,
})
track({
inboundHash: tx.hash,
desc: `Sent ${sourceAmount} ${token} from ${from} to ${to}`,
})
if (track) {
track({
hash: tx.hash,
desc: `Sent ${sourceAmount} ${token} from ${from} to ${to}`,
})
}
}

m.wrapZeta = async () => {
const zetaTokenAddress = getAddress("zetaToken", "zeta_testnet")
if (!zetaTokenAddress) {
throw new Error("ZetaToken address not found.")
}
signer?.sendTransaction({
client.signer?.sendTransaction({
to: zetaTokenAddress,
value: parseEther(sourceAmount),
})
Expand All @@ -201,8 +204,12 @@ const useSendTransaction = (
if (!zetaTokenAddress) {
throw new Error("ZetaToken address not found.")
}
if (signer) {
const contract = new ethers.Contract(zetaTokenAddress, WETH9.abi, signer)
if (client.signer) {
const contract = new ethers.Contract(
zetaTokenAddress,
WETH9.abi,
client.signer
)
contract.withdraw(parseEther(sourceAmount))
}
}
Expand All @@ -214,7 +221,7 @@ const useSendTransaction = (
const contract = new ethers.Contract(
sourceTokenSelected.contract as string,
ERC20_ABI.abi,
signer
client.signer
)
const approve = await contract.approve(
addressSelected,
Expand Down Expand Up @@ -245,10 +252,12 @@ const useSendTransaction = (
const token = sourceTokenSelected.symbol
const from = sourceTokenSelected.chain_name
const dest = destinationTokenSelected.chain_name
track({
inboundHash: tx.hash,
desc: `Sent ${sourceAmount} ${token} from ${from} to ${dest}`,
})
if (track) {
track({
hash: tx.hash,
desc: `Sent ${sourceAmount} ${token} from ${from} to ${dest}`,
})
}
}

m.depositNative = async () => {
Expand All @@ -263,10 +272,12 @@ const useSendTransaction = (
amount: sourceAmount,
recipient: addressSelected,
})
track({
inboundHash: tx.hash,
desc: `Sent ${sourceAmount} ${token} from ${from} to ${to}`,
})
if (track) {
track({
hash: tx.hash,
desc: `Sent ${sourceAmount} ${token} from ${from} to ${to}`,
})
}
}

m.fromZetaChainSwapAndWithdraw = async () => {
Expand All @@ -276,7 +287,7 @@ const useSendTransaction = (
const swapContract = new ethers.Contract(
omnichainSwapContractAddress,
SwapToAnyToken.abi,
signer
client.signer
)
const amount = ethers.utils.parseUnits(
sourceAmount,
Expand All @@ -287,7 +298,7 @@ const useSendTransaction = (
const erc20Contract = new ethers.Contract(
sourceToken as string,
ERC20_ABI.abi,
signer
client.signer
)
const approve = await erc20Contract.approve(
omnichainSwapContractAddress,
Expand All @@ -302,10 +313,12 @@ const useSendTransaction = (
recipient,
true
)
track({
inboundHash: tx.hash,
desc: `Sent ${sourceAmount} ${sourceTokenSelected.symbol} from ZetaChain to ${destinationTokenSelected.chain_name}`,
})
if (track) {
track({
hash: tx.hash,
desc: `Sent ${sourceAmount} ${sourceTokenSelected.symbol} from ZetaChain to ${destinationTokenSelected.chain_name}`,
})
}
}

m.fromZetaChainSwap = async () => {
Expand All @@ -315,7 +328,7 @@ const useSendTransaction = (
const swapContract = new ethers.Contract(
omnichainSwapContractAddress,
SwapToAnyToken.abi,
signer
client.signer
)
const amount = ethers.utils.parseUnits(
sourceAmount,
Expand All @@ -326,7 +339,7 @@ const useSendTransaction = (
const erc20Contract = new ethers.Contract(
sourceToken as string,
ERC20_ABI.abi,
signer
client.signer
)
const approve = await erc20Contract.approve(
omnichainSwapContractAddress,
Expand Down Expand Up @@ -354,7 +367,7 @@ const useSendTransaction = (
const custodyContract = new ethers.Contract(
custodyAddress as string,
ERC20Custody.abi,
signer
client.signer
)
const assetAddress = sourceTokenSelected.contract
const amount = ethers.utils.parseUnits(
Expand All @@ -365,7 +378,7 @@ const useSendTransaction = (
const contract = new ethers.Contract(
assetAddress as string,
ERC20_ABI.abi,
signer
client.signer
)
await (await contract.approve(custodyAddress, amount)).wait()
const tx = await custodyContract.deposit(
Expand All @@ -378,10 +391,12 @@ const useSendTransaction = (
const token = sourceTokenSelected.symbol
const from = sourceTokenSelected.chain_name
const dest = destinationTokenSelected.chain_name
track({
inboundHash: tx.hash,
desc: `Sent ${sourceAmount} ${token} from ${from} to ${dest}`,
})
if (track) {
track({
hash: tx.hash,
desc: `Sent ${sourceAmount} ${token} from ${from} to ${dest}`,
})
}
} catch (error) {
console.error("Error during deposit: ", error)
}
Expand Down Expand Up @@ -433,9 +448,9 @@ const useSendTransaction = (
}
const tx = await client.deposit(params)

if (tx) {
if (tx && track) {
track({
inboundHash: tx.hash,
hash: tx.hash,
desc: `Sent ${sourceAmount} ${tiker} from ${from} to ${dest}`,
})
}
Expand Down
7 changes: 2 additions & 5 deletions components/Swap/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { useFeesContext } from "@/context/FeesContext"
import { useAccount, useNetwork, useSwitchNetwork } from "wagmi"

import { formatAddress } from "@/lib/utils"
import { useEthersSigner } from "@/hooks/useEthersSigner"
import { useZetaChainClient } from "@/hooks/useZetaChainClient"
import useSendType, {
computeSendType,
Expand All @@ -24,11 +23,10 @@ import useSwapErrors from "./hooks/useSwapErrors"

interface SwapProps {
contract: string
track: any
track?: any
}

const Swap: React.FC<SwapProps> = ({ contract, track }) => {
const signer = useEthersSigner()
const { client } = useZetaChainClient()
const { isLoading, pendingChainId, switchNetwork } = useSwitchNetwork()
const { chain } = useNetwork()
Expand Down Expand Up @@ -105,10 +103,9 @@ const Swap: React.FC<SwapProps> = ({ contract, track }) => {
addressSelected,
setSourceAmount,
contract,
track,
bitcoinAddress,
client,
signer
track
)

const sendDisabled =
Expand Down

0 comments on commit 8070087

Please sign in to comment.