Skip to content

Commit

Permalink
supported batch requests from the lounge
Browse files Browse the repository at this point in the history
  • Loading branch information
YohanTz committed Apr 8, 2024
1 parent 9138b57 commit a4169ef
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 37 deletions.
2 changes: 0 additions & 2 deletions apps/web/src/app/(routes)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ function RootLayout({ children }: { children: React.ReactNode }) {
<Providers>
<Header />
<div className="hidden min-h-screen md:block">{children}</div>
{/* <EthereumSwitchNetwork />
<StarknetSwitchNetwork /> */}
</Providers>
<div className="block h-screen md:hidden">
<MobilePlaceholder />
Expand Down
31 changes: 12 additions & 19 deletions apps/web/src/app/(routes)/providers.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
"use client";

import {
type Chain,
// type Chain,
// goerli as starknetGoerli,
mainnet as starknetMainnet,
sepolia as starknetSepolia,
} from "@starknet-react/chains";
import {
StarknetConfig,
jsonRpcProvider,
// publicProvider,
// publicProvider,
} from "@starknet-react/core";
import { StarknetConfig, jsonRpcProvider } from "@starknet-react/core";
import { ThemeProvider } from "next-themes";
import { WagmiProvider, createConfig, http } from "wagmi";
import { mainnet } from "wagmi/chains";
import { mainnet, sepolia } from "wagmi/chains";

import { WalletModalsProvider } from "../_components/WalletModals/WalletModalsContext";
import {
Expand All @@ -23,21 +16,21 @@ import {
} from "../_lib/utils/connectors";

const wagmiConfig = createConfig({
// chains: [goerli],
chains: [mainnet],
chains: [sepolia],
// chains: [mainnet],
connectors: ethereumConnectors,
ssr: false,
transports: {
[mainnet.id]: http(
[sepolia.id]: http(
process.env.NEXT_PUBLIC_ALCHEMY_ETHEREUM_RPC_ENDPOINT ?? ""
),
// [mainnet.id]: http(
// process.env.NEXT_PUBLIC_ALCHEMY_ETHEREUM_RPC_ENDPOINT ?? ""
// ),
},
});

function starknetRpc(chain: Chain) {
if (chain.network === "goerli")
return { nodeUrl: `https://juno.testnet.arkproject.dev/` };

function starknetRpc() {
return {
nodeUrl: process.env.NEXT_PUBLIC_ALCHEMY_STARKNET_RPC_ENDPOINT ?? "",
};
Expand All @@ -53,8 +46,8 @@ export default function Providers({ children }: ProvidersProps) {
return (
<StarknetConfig
autoConnect
// chains={[starknetGoerli]}
chains={[starknetMainnet]}
chains={[starknetSepolia]}
// chains={[starknetMainnet]}
connectors={starknetConnectors}
provider={jsonRpcProvider({ rpc: starknetRpc })}
// provider={starknetProvider}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export function WalletModalsProvider({ children }: PropsWithChildren) {
void router.push("/bridge");
return;
}
}, [starknetAddress, ethereumAddress, userOpenedModal]);
}, [starknetAddress, ethereumAddress, userOpenedModal, router]);

useEffect(() => {
if (
Expand Down
66 changes: 55 additions & 11 deletions apps/web/src/server/api/routers/bridgeRequest.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import { Alchemy, Network, type Nft } from "alchemy-sdk";
import { Alchemy, Network } from "alchemy-sdk";
import { z } from "zod";

import { type Chain } from "~/app/_types";
import { createTRPCRouter, publicProcedure } from "~/server/api/trpc";

import { getMediaObjectFromAlchemyMedia } from "../helpers/l1nfts";
import {
getL2NftsMetadataBatch,
getMediaObjectFromUrl,
} from "../helpers/l2nfts";
import { type NftMedia } from "../types";

const alchemy = new Alchemy({
apiKey: process.env.ALCHEMY_API_KEY,
network: Network.ETH_MAINNET,
// network: Network.ETH_GOERLI,
// network: Network.ETH_MAINNET,
network: Network.ETH_SEPOLIA,
});

export type BridgeRequestEventStatus =
Expand Down Expand Up @@ -96,27 +101,66 @@ export const bridgeRequestRouter = createTRPCRouter({
};
}

let requestMetadata: Array<Nft> = [];
let requestMetadataByReqHash: Record<
string,
{ contractName: string; media: NftMedia }
> = {};

if (bridgeRequests[0]?.req.chain_src === "eth") {
requestMetadata = await alchemy.nft.getNftMetadataBatch(
const requestMetadatas = await alchemy.nft.getNftMetadataBatch(
bridgeRequests.reverse().map((bridgeRequest) => ({
contractAddress: bridgeRequest.req.collection_src,
tokenId: bridgeRequest.token_ids[0],
}))
);

requestMetadataByReqHash = requestMetadatas.reduce((acc, current) => {
return {
...acc,
[`${current.tokenId}`]: {
contractName: current.contract.name,
media: getMediaObjectFromAlchemyMedia(current.media[0]),
},
};
}, {});
} else {
const requestMetadatas = await getL2NftsMetadataBatch(
bridgeRequests.map((bridgeRequest) => ({
contract_address: bridgeRequest.req.collection_src,
token_id: bridgeRequest.token_ids[0],
}))
);
requestMetadataByReqHash = requestMetadatas.result.reduce(
(acc, current) => {
return {
...acc,
[`${current.token_id}`]: {
contractName: current.contract_name,
media: getMediaObjectFromUrl(
current.metadata?.normalized.image
),
},
};
},
{}
);
}

const bridgeRequestsWithMetadata = bridgeRequests.map(
(bridgeRequest, index) => {
(bridgeRequest) => {
const lastBridgeRequestEvent =
bridgeRequest.events[bridgeRequest.events.length - 1];
const isArrived =
lastBridgeRequestEvent?.label === "withdraw_available_l1" ||
lastBridgeRequestEvent?.label === "withdraw_completed_l1" ||
lastBridgeRequestEvent?.label === "withdraw_completed_l2";
const media = requestMetadata[index]?.media[0];
const mediaSrc = media?.gateway ?? media?.thumbnail ?? media?.raw;
const mediaFormat = media?.format === "mp4" ? "video" : "image";
const requestMetadata =
requestMetadataByReqHash[`${bridgeRequest.token_ids[0]}`];
const collectionMedia = requestMetadata?.media ?? {
format: "image",
src: undefined,
};
const collectionName = requestMetadata?.contractName ?? "Unknown";

return {
arrivalAddress: bridgeRequest.req.to,
Expand All @@ -127,8 +171,8 @@ export const bridgeRequestRouter = createTRPCRouter({
arrivalTimestamp: isArrived
? lastBridgeRequestEvent?.block_timestamp
: undefined,
collectionMedia: { format: mediaFormat, src: mediaSrc },
collectionName: requestMetadata[index]?.contract.name ?? "",
collectionMedia,
collectionName,
collectionSourceAddress: bridgeRequest.req.collection_src,
requestContent: JSON.parse(
bridgeRequest.req.content
Expand Down
9 changes: 5 additions & 4 deletions apps/web/src/server/api/routers/l1Nfts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Alchemy, Network, NftFilters } from "alchemy-sdk";
import { createClient, getContract, http } from "viem";
import { mainnet } from "viem/chains";
import { mainnet, sepolia } from "viem/chains";
import { z } from "zod";

import { createTRPCRouter, publicProcedure } from "~/server/api/trpc";
Expand All @@ -10,7 +10,8 @@ import { getMediaObjectFromAlchemyMedia } from "../helpers/l1nfts";
import { type Collection, type Nft } from "../types";

const viemClient = createClient({
chain: mainnet,
// chain: mainnet,
chain: sepolia,
transport: http(process.env.NEXT_PUBLIC_ALCHEMY_ETHEREUM_RPC_ENDPOINT ?? ""),
});

Expand All @@ -22,8 +23,8 @@ const bridgeL1Contract = getContract({

const alchemy = new Alchemy({
apiKey: process.env.ALCHEMY_API_KEY,
network: Network.ETH_MAINNET,
// network: Network.ETH_GOERLI,
// network: Network.ETH_MAINNET,
network: Network.ETH_SEPOLIA,
});

export const l1NftsRouter = createTRPCRouter({
Expand Down

0 comments on commit a4169ef

Please sign in to comment.