Skip to content

Commit

Permalink
Resolve address name (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementWalter authored Dec 9, 2023
1 parent dcff925 commit 840a726
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 7 deletions.
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>;
}

0 comments on commit 840a726

Please sign in to comment.