-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* create test page for using ethers in browser * small fixes * correctly export all types, errors, utils and actions * small fix
1 parent
be7f5ab
commit c1ba18e
Showing
21 changed files
with
1,245 additions
and
265 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
"use client"; | ||
import { Button } from "@/components/ui/button"; | ||
import { | ||
AcrossClient, | ||
DepositStatus, | ||
FillStatus, | ||
Quote, | ||
} from "@across-toolkit/sdk"; | ||
import { useEthers } from "@usedapp/core"; | ||
import { useEffect, useState } from "react"; | ||
import { Address, createWalletClient, custom, Hash, parseEther } from "viem"; | ||
import { toAccount } from "viem/accounts"; | ||
import { arbitrum, mainnet } from "viem/chains"; | ||
|
||
const chains = [mainnet, arbitrum]; | ||
|
||
const sdk = AcrossClient.create({ | ||
chains, | ||
useTestnet: false, | ||
integratorId: "TEST", | ||
logLevel: "DEBUG", | ||
}); | ||
|
||
async function getQuote(account: Address) { | ||
const routes = await sdk.actions.getAvailableRoutes({ | ||
originChainId: mainnet.id, | ||
destinationChainId: arbitrum.id, | ||
})!; | ||
|
||
const route = routes.find((r) => r.inputTokenSymbol === "ETH")!; | ||
|
||
// 1. get quote | ||
const bridgeQuoteRes = await sdk.actions.getQuote({ | ||
route, | ||
inputAmount: parseEther("0.01"), | ||
recipient: account, | ||
}); | ||
|
||
return bridgeQuoteRes; | ||
} | ||
|
||
async function bridge( | ||
quote: Awaited<ReturnType<typeof getQuote>>, | ||
library: any, | ||
account: string | undefined, | ||
) { | ||
if (!account) return; | ||
|
||
const walletClient = createWalletClient({ | ||
account: toAccount(account as Address), | ||
chain: mainnet, | ||
transport: custom(library.provider), | ||
}); | ||
|
||
const { request } = await sdk.actions.simulateDepositTx({ | ||
walletClient, | ||
deposit: quote.deposit, | ||
}); | ||
|
||
const transactionHash = await walletClient.writeContract(request); | ||
|
||
return transactionHash; | ||
} | ||
|
||
export function Bridge() { | ||
const { account, library } = useEthers(); | ||
const [quote, setQuote] = useState<Quote>(); | ||
const [txHash, setTxHash] = useState<Hash>(); | ||
const [destinationBlock, setDestinationBlock] = useState<bigint>(); | ||
const [depositData, setDepositData] = useState<DepositStatus>(); | ||
const [fillData, setFillData] = useState<FillStatus>(); | ||
|
||
const [loadingDeposit, setLoadingDeposit] = useState(false); | ||
const [loadingFill, setLoadingFill] = useState(false); | ||
|
||
async function handleQuote() { | ||
if (!account) return; | ||
const quote = await getQuote(account as Address); | ||
setQuote(quote); | ||
} | ||
|
||
async function handleBridge() { | ||
if (!quote || !account) return; | ||
const destinationBlock = await sdk | ||
.getPublicClient(quote.deposit.destinationChainId) | ||
.getBlockNumber(); | ||
|
||
const hash = await bridge(quote, library as any, account); | ||
setTxHash(hash); | ||
|
||
setDestinationBlock(destinationBlock); | ||
} | ||
|
||
const waitForDeposit = async (txHash: Hash, quote: Quote) => { | ||
setLoadingDeposit(true); | ||
// wait for tx to be mined | ||
const data = await sdk.waitForDepositTx({ | ||
transactionHash: txHash, | ||
chainId: quote.deposit.originChainId, | ||
}); | ||
setLoadingDeposit(false); | ||
setDepositData(data); | ||
}; | ||
|
||
useEffect(() => { | ||
if (txHash && quote) { | ||
waitForDeposit(txHash, quote); | ||
} | ||
}, [txHash, quote]); | ||
|
||
const waitForFill = async ( | ||
deposit: DepositStatus, | ||
quote: Quote, | ||
destinationBlock: bigint, | ||
) => { | ||
setLoadingFill(true); | ||
// wait for tx to be filled | ||
const data = await sdk.actions.waitForFillTx({ | ||
depositId: deposit.depositId, | ||
deposit: quote.deposit, | ||
fromBlock: destinationBlock, | ||
}); | ||
setLoadingFill(false); | ||
setFillData(data); | ||
}; | ||
|
||
useEffect(() => { | ||
if (depositData && quote && destinationBlock) { | ||
waitForFill(depositData, quote, destinationBlock); | ||
} | ||
}, [depositData, quote, destinationBlock]); | ||
|
||
return ( | ||
<> | ||
<Button variant="filled" onClick={handleQuote}> | ||
get Quote | ||
</Button> | ||
{quote && ( | ||
<details> | ||
<summary>Quote</summary> | ||
<pre> | ||
{JSON.stringify( | ||
quote, | ||
(_, v) => (typeof v === "bigint" ? v.toString() : v), | ||
2, | ||
)} | ||
</pre> | ||
</details> | ||
)} | ||
<Button variant="filled" onClick={handleBridge}> | ||
Bridge | ||
</Button> | ||
{!depositData && loadingDeposit && <h3>Waiting for deposit...</h3>} | ||
{depositData && ( | ||
<details className="break-words max-w-full"> | ||
<summary>Deposit Data</summary> | ||
<pre> | ||
{JSON.stringify( | ||
depositData, | ||
(_, v) => (typeof v === "bigint" ? v.toString() : v), | ||
2, | ||
)} | ||
</pre> | ||
</details> | ||
)} | ||
{!fillData && loadingFill && <h3>Waiting for fill...</h3>} | ||
{fillData && ( | ||
<details> | ||
<summary>Fill Data</summary> | ||
<pre> | ||
{JSON.stringify( | ||
fillData, | ||
(_, v) => (typeof v === "bigint" ? v.toString() : v), | ||
2, | ||
)} | ||
</pre> | ||
</details> | ||
)} | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
"use client"; | ||
import { Button, ButtonProps } from "@/components/ui/button"; | ||
import { Icon } from "@/components/Icon"; | ||
import { cn } from "@/lib/utils"; | ||
import { shortenIfAddress, useEthers } from "@usedapp/core"; | ||
|
||
type ConnectButton = { | ||
className?: string; | ||
}; | ||
|
||
export const ConnectButton = ({ className }: ConnectButton) => { | ||
const { account, active, activateBrowserWallet, deactivate } = useEthers(); | ||
const connected = !!account && !!active; | ||
|
||
return ( | ||
<div className={cn(className)}> | ||
{(() => { | ||
if (!connected) { | ||
return <HamburgerButton onClick={activateBrowserWallet} />; | ||
} | ||
// if (connected) { | ||
// return ( | ||
// <Button variant="bordered" onClick={openChainModal} type="button"> | ||
// Wrong network | ||
// </Button> | ||
// ); | ||
// } | ||
return ( | ||
<div className="flex items-center gap-2"> | ||
<Button | ||
className="flex gap-1" | ||
variant="bordered" | ||
onClick={deactivate} | ||
> | ||
<span className="max-w-[120px] truncate"> | ||
{shortenIfAddress(account)} | ||
</span> | ||
</Button> | ||
</div> | ||
); | ||
})()} | ||
</div> | ||
); | ||
}; | ||
|
||
const HamburgerButton = (props: ButtonProps) => { | ||
return ( | ||
<Button | ||
size="icon" | ||
variant="bordered" | ||
className="h-[40px] w-[40px]" | ||
{...props} | ||
> | ||
<Icon name="hamburger" className="h-[20px] w-[20px] text-text/75" /> | ||
</Button> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Providers } from "./providers"; | ||
import { Header } from "./components/Header"; | ||
import { Bridge } from "./components/Bridge"; | ||
|
||
export default function Ethers() { | ||
return ( | ||
<Providers> | ||
<Header /> | ||
<main className="flex gap-2 text-sm min-h-screen max-w-[800px] min-w-[600px] mx-auto flex-col items-center justify-start p-24"> | ||
<Bridge /> | ||
</main> | ||
</Providers> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
"use client"; | ||
import { | ||
useEthers, | ||
useEtherBalance, | ||
DAppProvider, | ||
Arbitrum, | ||
Config, | ||
Mainnet, | ||
} from "@usedapp/core"; | ||
import * as React from "react"; | ||
import { ThemeProvider } from "next-themes"; | ||
|
||
export function Providers({ children }: { children: React.ReactNode }) { | ||
return ( | ||
<ThemeProvider attribute="class" defaultTheme="dark" enableSystem={true}> | ||
<DAppProvider config={config}>{children}</DAppProvider> | ||
</ThemeProvider> | ||
); | ||
} | ||
|
||
const config: Config = { | ||
readOnlyChainId: Mainnet.chainId, | ||
readOnlyUrls: { | ||
[Mainnet.chainId]: "https://eth.llamarpc.com", | ||
[Arbitrum.chainId]: "https://arbitrum.llamarpc.com", | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
import { Header } from "./viem/components"; | ||
import { Providers } from "./viem/providers"; | ||
|
||
export default function Home() { | ||
return ( | ||
<main className="flex min-h-screen flex-col items-center justify-between p-24"> | ||
<div></div> | ||
</main> | ||
<Providers> | ||
<Header /> | ||
<main className="flex min-h-screen flex-col items-center justify-between p-24"> | ||
<div></div> | ||
</main> | ||
</Providers> | ||
); | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { cn } from "@/lib/utils"; | ||
import { Icon } from "@/components/Icon"; | ||
import { ConnectButton } from "./ConnectButton"; | ||
|
||
export const Header = ({ | ||
className, | ||
...props | ||
}: React.ComponentPropsWithoutRef<"div">) => { | ||
return ( | ||
<div | ||
className={cn( | ||
"fixed top-0 z-20 flex w-full items-center justify-between bg-transparent p-4 md:p-5", | ||
className, | ||
)} | ||
{...props} | ||
> | ||
<div className="relative flex items-center justify-start gap-2"> | ||
<Icon name="across-logo" className="h-8 w-8 text-accent" /> | ||
<h2 className="text-gradient-oval text-2xl">SDK Example - Viem</h2> | ||
</div> | ||
|
||
<ConnectButton className="relative" /> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./Header"; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
"use client"; | ||
|
||
import { useEtherBalance, useEthers } from "@usedapp/core"; | ||
|
||
export function useGetBalance() { | ||
const { account } = useEthers(); | ||
const userBalance = useEtherBalance(account); | ||
return userBalance; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
export * from "./client"; | ||
export * from "./types"; | ||
export * from "./actions"; | ||
export * from "./errors"; | ||
export * from "./utils"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.