Skip to content

Commit

Permalink
Merge pull request #1249 from scroll-tech/ipfs-subdomain
Browse files Browse the repository at this point in the history
fix: use subdomain gateway
  • Loading branch information
Holybasil authored Aug 20, 2024
2 parents c859e87 + 14325b8 commit c338d26
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/pages/canvas/Dashboard/BadgeDetailDialog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import useSnackbar from "@/hooks/useSnackbar"
import Dialog from "@/pages/canvas/components/Dialog"
import { checkBadgeUpgradable, fetchNotionBadgeByAddr, mintBadge } from "@/services/canvasService"
import useCanvasStore, { BadgeDetailDialogType, BadgesDialogType } from "@/stores/canvasStore"
import { generateShareTwitterURL, getBadgeImgURL, requireEnv, sentryDebug } from "@/utils"
import { generateShareTwitterURL, ipfsToBrowserURL, requireEnv, sentryDebug } from "@/utils"

import BadgeDesc from "../../components/BadgeDesc"
import UpgradeAction from "./UpgradeAction"
Expand Down Expand Up @@ -223,7 +223,7 @@ const BadgeDetailDialog = () => {
>
<Img
alt="img"
src={getBadgeImgURL(selectedBadge.image)}
src={ipfsToBrowserURL(selectedBadge.image)}
placeholder="/imgs/canvas/badgePlaceholder.svg"
style={{
width: isMobile ? "12rem" : "20rem",
Expand Down
4 changes: 2 additions & 2 deletions src/pages/canvas/Dashboard/BadgeWall/Badge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Img from "react-cool-img"
import { Box, Badge as MuiBadge } from "@mui/material"

import useCanvasStore, { BadgeDetailDialogType } from "@/stores/canvasStore"
import { getBadgeImgURL } from "@/utils"
import { ipfsToBrowserURL } from "@/utils"

import ToolTip from "../../components/Tooltip"

Expand Down Expand Up @@ -75,7 +75,7 @@ const Badge = ({ badge, index, badgewidth }) => {
<Img
alt={badge.metadata?.name}
style={{ width: "100%", borderRadius: "0.8rem" }}
src={getBadgeImgURL(badge.metadata?.image)}
src={ipfsToBrowserURL(badge.metadata?.image)}
placeholder="/imgs/canvas/badgePlaceholder.svg"
/>
</MuiBadge>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { tooltipClasses } from "@mui/material/Tooltip"
import { keyframes, styled } from "@mui/system"

import NameTip from "@/pages/canvas/components/NameTip"
import { getBadgeImgURL } from "@/utils/canvas"
import { ipfsToBrowserURL } from "@/utils/canvas"

// import styles from "./Item.module.css"
const CustomTooltip = styled(Tooltip)(({ theme }) => ({}))
Expand Down Expand Up @@ -249,7 +249,7 @@ export const Item = React.memo(
<Img
alt={(value as any)?.name}
style={{ borderRadius: "0.8rem" }}
src={getBadgeImgURL((value as any)?.image)}
src={ipfsToBrowserURL((value as any)?.image)}
placeholder="/imgs/canvas/badgePlaceholder.svg"
/>
</StyledItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { makeStyles } from "tss-react/mui"

import { Box } from "@mui/material"

import { getBadgeImgURL } from "@/utils"
import { ipfsToBrowserURL } from "@/utils"

const getTranslateX = transform => {
return transform ? `${Math.round(transform.x)}px` : undefined
Expand Down Expand Up @@ -106,7 +106,7 @@ const Item = forwardRef((props: any, ref) => {
}}
>
<Box className={cx(classes.item, dragging && classes.dragging, dragOverlay && "dragOverlay")} {...listeners}>
<Img alt={name} style={{ borderRadius: "0.8rem" }} src={getBadgeImgURL(image)} placeholder="/imgs/canvas/badgePlaceholder.svg" />
<Img alt={name} style={{ borderRadius: "0.8rem" }} src={ipfsToBrowserURL(image)} placeholder="/imgs/canvas/badgePlaceholder.svg" />
</Box>
</Box>
)
Expand Down
4 changes: 2 additions & 2 deletions src/pages/canvas/badge/BadgeDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Skeleton from "@/components/Skeleton"
import useCheckViewport from "@/hooks/useCheckViewport"
import UpgradeAction from "@/pages/canvas/Dashboard/BadgeDetailDialog/UpgradeAction"
import useCanvasStore from "@/stores/canvasStore"
import { getBadgeImgURL } from "@/utils"
import { ipfsToBrowserURL } from "@/utils"

import BadgeDesc from "../components/BadgeDesc"
import Statistic from "../components/Statistic"
Expand Down Expand Up @@ -148,7 +148,7 @@ const BadgeDetail = props => {
<Skeleton dark sx={{ height: "100%" }}></Skeleton>
) : (
<Img
src={getBadgeImgURL(detail.image)}
src={ipfsToBrowserURL(detail.image)}
placeholder="/imgs/canvas/badgePlaceholder.svg"
style={{ borderRadius: "0.8rem" }}
alt="badge image"
Expand Down
11 changes: 6 additions & 5 deletions src/services/canvasService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ORIGINS_NFT_BADGE } from "@/constants"
import {
checkDelegatedAttestation,
decodeBadgePayload,
ipfsToBrowserURL,
isOriginsNFTBadge,
isUserRejected,
recognizeError,
Expand Down Expand Up @@ -118,9 +119,9 @@ const queryBadgeDetailById = async badgeId => {
const getBadgeMetadata = async (provider, badgeContractAddress, badgeUID = ethers.encodeBytes32String("0x0")) => {
try {
const contract = new ethers.Contract(badgeContractAddress, BadgeABI, provider)
const badgeMetadataURI = await contract.badgeTokenURI(badgeUID)
let badgeImageURI = badgeMetadataURI.replace(/^ipfs:\/\/(.*)/, "https://ipfs.io/ipfs/$1")
const metadata = await scrollRequest(badgeImageURI, { timeout: 5e3 })
const badgeTokenURI = await contract.badgeTokenURI(badgeUID)
const badgeTokenBrowserURL = ipfsToBrowserURL(badgeTokenURI)
const metadata = await scrollRequest(badgeTokenBrowserURL, { timeout: 5e3 })
return metadata
} catch (error) {
// console.log("Failed to get badge image URI:", error)
Expand Down Expand Up @@ -401,8 +402,8 @@ const upgradeBadge = async (provider, badge) => {
const txReceipt = await tx.wait()
if (txReceipt.status === 1) {
const badgeMetadataURI = await badgeInstance.badgeTokenURI(id)
const accessableURI = badgeMetadataURI.replace(/^ipfs:\/\/(.*)/, "https://ipfs.io/ipfs/$1")
const metadata = await scrollRequest(accessableURI)
const badgeTokenBrowserURL = ipfsToBrowserURL(badgeMetadataURI)
const metadata = await scrollRequest(badgeTokenBrowserURL)
return metadata
} else {
throw new Error("due to any operation that can cause the transaction or top-level call to revert")
Expand Down
6 changes: 3 additions & 3 deletions src/utils/canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ const SCROLL_SEPOLIA_BADGE_SCHEMA = requireEnv("REACT_APP_BADGE_SCHEMA")

const SCROLL_ORIGINS_BADGE_ADDRESS = "0x2dBce60ebeAafb77e5472308f432F78aC3AE07d9"

export const getBadgeImgURL = image => {
if (!image) return ""
return image.replace(/^ipfs:\/\/(.*)/, "https://cloudflare-ipfs.com/ipfs/$1")
export const ipfsToBrowserURL = ipfsAddress => {
if (!ipfsAddress) return ""
return ipfsAddress.replace(/^ipfs:\/\/(.*)/, "https://dweb.link/ipfs/$1")
}

export const decodeBadgePayload = encodedData => {
Expand Down

0 comments on commit c338d26

Please sign in to comment.