Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show contract bytecode in Contract tab #8

Merged
merged 1 commit into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/execution/address/Contracts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { Match, MatchType } from "../../sourcify/useSourcify";
import ExternalLink from "../../components/ExternalLink";
import { openInRemixURL } from "../../url";
import ContractABI from "./contract/ContractABI";
import { useGetCode } from "../../useErigonHooks";
import StandardTextarea from "../../components/StandardTextarea";

type ContractsProps = {
checksummedAddress: string;
Expand All @@ -20,6 +22,7 @@ type ContractsProps = {

const Contracts: React.FC<ContractsProps> = ({ checksummedAddress, match }) => {
const { provider } = useContext(RuntimeContext);
const code = useGetCode(provider, checksummedAddress);

const [selected, setSelected] = useState<string>();
useEffect(() => {
Expand Down Expand Up @@ -133,6 +136,15 @@ const Contracts: React.FC<ContractsProps> = ({ checksummedAddress, match }) => {
</>
)}
</div>
<div className="py-5">
{code === undefined && <span>Getting contract bytecode...</span>}
{code && (
<>
<div className="pb-2">Contract Bytecode</div>
<StandardTextarea value={code} />
</>
)}
</div>
</ContentFrame>
);
};
Expand Down
16 changes: 16 additions & 0 deletions src/useErigonHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,22 @@ export const useHasCode = (
return data as boolean | undefined;
};

export const useGetCode = (
provider: JsonRpcProvider | undefined,
address: ChecksummedAddress | undefined,
blockTag: BlockTag = "latest"
): string | undefined => {
const fetcher = providerFetcher(provider);
const { data, error } = useSWRImmutable(
["eth_getCode", address, blockTag],
fetcher
);
if (error) {
return undefined;
}
return data as string | undefined;
};

const ERC20_PROTOTYPE = new Contract(AddressZero, erc20);

const tokenMetadataFetcher =
Expand Down