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

Resolve address name #88

Merged
merged 1 commit into from
Dec 9, 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BoxProps } from "@mui/material";
import { useContext } from "react";
import { useContext, useMemo } from "react";
import { AccountContext } from "../../contexts/AccountContext";
import Button from "../Button/Button";

Expand All @@ -8,17 +8,28 @@ export type ConnectButtonProps = {
};

function ConnectButton({ sx }: ConnectButtonProps) {
const { accountAddress, connect } = useContext(AccountContext);
const { accountDomain, connect } = useContext(AccountContext);

const displayedName = useMemo(() => {
if (!accountDomain) {
return "Connect";
}
if (accountDomain.length <= 8) {
return accountDomain;
}
return `${accountDomain.substring(0, 8)}...`;
}, [accountDomain]);

return (
<Button
onClick={connect}
sx={{
color: "#0000FF",
width: "210px",
...sx,
}}
>
{accountAddress ? accountAddress.substring(0, 8) : "Connect"}
{displayedName}
</Button>
);
}
Expand Down
4 changes: 1 addition & 3 deletions packages/starksheet-webapp/src/components/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,7 @@ function Header() {
{!!sheet && copySheetButton}
{!!sheet && fillSheetButton}
{learnMoreButton}
<ConnectButton
sx={{ width: "174px", marginLeft: `-${CELL_BORDER_WIDTH}px` }}
/>
<ConnectButton sx={{ marginLeft: `-${CELL_BORDER_WIDTH}px` }} />
</Box>
);
}
Expand Down
11 changes: 10 additions & 1 deletion packages/starksheet-webapp/src/contexts/AccountContext.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { useSnackbar } from "notistack";
import React, { PropsWithChildren, useMemo, useState } from "react";
import React, { PropsWithChildren, useEffect, useMemo, useState } from "react";
import { BigNumberish } from "starknet";
import { useChainProvider } from "../hooks";
import { ContractCall, TransactionResponse } from "../types";

export const AccountContext = React.createContext<{
accountAddress: string;
accountDomain: string;
setAccountAddress: (address: string) => void;
connect: () => Promise<void>;
execute: (
Expand All @@ -15,6 +16,7 @@ export const AccountContext = React.createContext<{
proof: string[];
}>({
accountAddress: "",
accountDomain: "",
setAccountAddress: () => {},
connect: async () => {},
execute: async () => ({ transaction_hash: "" }),
Expand All @@ -23,11 +25,17 @@ export const AccountContext = React.createContext<{

export const AccountContextProvider = ({ children }: PropsWithChildren<{}>) => {
const [accountAddress, setAccountAddress] = useState<string>("");
const [accountDomain, setAccountDomain] = useState<string>("");
const { enqueueSnackbar } = useSnackbar();

const proof = useMemo(() => [], []);

const provider = useChainProvider();
useEffect(() => {
provider.resolveAddress(accountAddress).then((resolved) => {
setAccountDomain(resolved);
});
}, [provider, accountAddress]);

const connect = () =>
provider
Expand All @@ -47,6 +55,7 @@ export const AccountContextProvider = ({ children }: PropsWithChildren<{}>) => {
<AccountContext.Provider
value={{
accountAddress,
accountDomain,
setAccountAddress,
connect,
execute,
Expand Down
4 changes: 4 additions & 0 deletions packages/starksheet-webapp/src/provider/EVMProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export class EVMProvider implements ChainProvider {
private config: ChainConfig,
) {}

resolveAddress(address: string): Promise<string> {
throw new Error("Method not implemented.");
}

async addressAlreadyDeployed(address: string) {
return (await this.provider.getCode(address)).length > 2;
}
Expand Down
11 changes: 11 additions & 0 deletions packages/starksheet-webapp/src/provider/StarknetProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ export class StarknetProvider implements ChainProvider {
);
}

async resolveAddress(address: string): Promise<string> {
try {
const response = await fetch(
`https://api.starknet.id/addr_to_domain?addr=${address}`,
);
return (await response.json()).domain;
} catch (error) {
return address;
}
}

async addressAlreadyDeployed(address: string) {
try {
await this.provider.getClassAt(address, "latest");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,9 @@ export interface ChainProvider {
* Create a send ETH transaction
*/
sendEthTxBuilder(recipientAddress: bigint, amount: bigint): ContractCall;

/**
* Resolve an address to a name
*/
resolveAddress(address: string): Promise<string>;
}
Loading