From a1f79ff0c0c05aa316b140778835ba308ffcc0b4 Mon Sep 17 00:00:00 2001 From: Willian Mitsuda Date: Mon, 6 Sep 2021 18:32:11 -0300 Subject: [PATCH] Toggle ipfs on/off --- src/AddressTransactions.tsx | 6 +++++- src/address/Contract.tsx | 10 +++++++++- src/address/Contracts.tsx | 22 +++++++++++++++++++++- src/url.ts | 16 +++++++++++----- src/useSourcify.ts | 16 ++++++++++------ 5 files changed, 56 insertions(+), 14 deletions(-) diff --git a/src/AddressTransactions.tsx b/src/AddressTransactions.tsx index 659c1eb..371ff92 100644 --- a/src/AddressTransactions.tsx +++ b/src/AddressTransactions.tsx @@ -181,9 +181,11 @@ const AddressTransactions: React.FC = () => { const [feeDisplay, feeDisplayToggler] = useFeeToggler(); const selectionCtx = useSelection(); + const [useIPFS, setUseIPFS] = useState(true); const rawMetadata = useSourcify( checksummedAddress, - provider?.network.chainId + provider?.network.chainId, + useIPFS ); return ( @@ -291,6 +293,8 @@ const AddressTransactions: React.FC = () => { diff --git a/src/address/Contract.tsx b/src/address/Contract.tsx index 1a30583..0fd7c89 100644 --- a/src/address/Contract.tsx +++ b/src/address/Contract.tsx @@ -12,6 +12,7 @@ type ContractProps = { networkId: number; filename: string; source: any; + useIPFS: boolean; }; const Contract: React.FC = ({ @@ -19,8 +20,15 @@ const Contract: React.FC = ({ networkId, filename, source, + useIPFS, }) => { - const content = useContract(checksummedAddress, networkId, filename, source); + const content = useContract( + checksummedAddress, + networkId, + filename, + source, + useIPFS + ); return ( void; }; const Contracts: React.FC = ({ checksummedAddress, rawMetadata, + useIPFS, + setUseIPFS, }) => { const { provider } = useContext(RuntimeContext); @@ -30,6 +34,21 @@ const Contracts: React.FC = ({ return ( + + + + + {rawMetadata && ( <> @@ -95,6 +114,7 @@ const Contracts: React.FC = ({ networkId={provider!.network.chainId} filename={selected} source={rawMetadata.sources[selected]} + useIPFS={useIPFS} /> )} diff --git a/src/url.ts b/src/url.ts index 8aaa6f0..e2a191d 100644 --- a/src/url.ts +++ b/src/url.ts @@ -17,20 +17,26 @@ export const blockTxsURL = (blockNum: BlockTag) => `/block/${blockNum}/txs`; const sourcifyRootHash = "k51qzi5uqu5dll0ocge71eudqnrgnogmbr37gsgl12uubsinphjoknl6bbi41p"; const ipfsGatewayPrefix = `https://ipfs.io/ipns/${sourcifyRootHash}`; -// const ipfsGatewayPrefix = `https://repo.sourcify.dev`; +const sourcifyHttpRepoPrefix = `https://repo.sourcify.dev`; export const sourcifyMetadata = ( checksummedAddress: string, - networkId: number + networkId: number, + useIPFS: boolean ) => - `${ipfsGatewayPrefix}/contracts/full_match/${networkId}/${checksummedAddress}/metadata.json`; + `${ + useIPFS ? ipfsGatewayPrefix : sourcifyHttpRepoPrefix + }/contracts/full_match/${networkId}/${checksummedAddress}/metadata.json`; export const sourcifySourceFile = ( checksummedAddress: string, networkId: number, - filepath: string + filepath: string, + useIPFS: boolean ) => - `${ipfsGatewayPrefix}/contracts/full_match/${networkId}/${checksummedAddress}/sources/${filepath}`; + `${ + useIPFS ? ipfsGatewayPrefix : sourcifyHttpRepoPrefix + }/contracts/full_match/${networkId}/${checksummedAddress}/sources/${filepath}`; export const openInRemixURL = (checksummedAddress: string, networkId: number) => `https://remix.ethereum.org/#call=source-verification//fetchAndSave//${checksummedAddress}//${networkId}`; diff --git a/src/useSourcify.ts b/src/useSourcify.ts index 5a8d0fd..40c0bea 100644 --- a/src/useSourcify.ts +++ b/src/useSourcify.ts @@ -38,7 +38,8 @@ export type Metadata = { export const useSourcify = ( checksummedAddress: string | undefined, - chainId: number | undefined + chainId: number | undefined, + useIPFS: boolean ) => { const [rawMetadata, setRawMetadata] = useState(); @@ -51,7 +52,8 @@ export const useSourcify = ( try { const contractMetadataURL = sourcifyMetadata( checksummedAddress, - chainId + chainId, + useIPFS ); const result = await fetch(contractMetadataURL); if (result.ok) { @@ -66,7 +68,7 @@ export const useSourcify = ( } }; fetchMetadata(); - }, [checksummedAddress, chainId]); + }, [checksummedAddress, chainId, useIPFS]); return rawMetadata; }; @@ -75,7 +77,8 @@ export const useContract = ( checksummedAddress: string, networkId: number, filename: string, - source: any + source: any, + useIPFS: boolean = true ) => { const [content, setContent] = useState(source.content); @@ -89,7 +92,8 @@ export const useContract = ( const url = sourcifySourceFile( checksummedAddress, networkId, - normalizedFilename + normalizedFilename, + useIPFS ); const res = await fetch(url); if (res.ok) { @@ -98,7 +102,7 @@ export const useContract = ( } }; readContent(); - }, [checksummedAddress, networkId, filename, source.content]); + }, [checksummedAddress, networkId, filename, source.content, useIPFS]); return content; };