Skip to content

Commit

Permalink
Search ens (#954)
Browse files Browse the repository at this point in the history
* ENS Searching

* Revert "ENS Searching"

This reverts commit 710e262.

* search by ens

* copy fix on address input

* add EOA address and ENS

* fix EOA calc from kwenta and poly, show ENS on top

* Resolve ens optionally

* Renames

* Fix deps

* Simplify useEnsName

* Lint harder

---------

Co-authored-by: Steve Rogers <[email protected]>
  • Loading branch information
noisekit and Steve-Rog authored Sep 7, 2023
1 parent 232951e commit 467df74
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 5 deletions.
1 change: 1 addition & 0 deletions v2/perps-v2/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@graphql-typed-document-node/core": "^3.1.2",
"@pythnetwork/pyth-evm-js": "^1.1.0",
"@snx-v2/formatters": "workspace:*",
"@snx-v2/useGlobalProvidersWithFallback": "workspace:*",
"@snx-v2/v3Theme": "workspace:*",
"@synthetixio/contracts": "workspace:*",
"@synthetixio/wei": "^2.74.4",
Expand Down
21 changes: 17 additions & 4 deletions v2/perps-v2/ui/src/components/AddressInput/AddressInput.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
import { ethers } from 'ethers';
import { SearchIcon } from '@chakra-ui/icons';
import { Button, Flex, Input } from '@chakra-ui/react';
import { useForm } from 'react-hook-form';
import { useNavigate } from 'react-router-dom';
import { useGlobalProvidersWithFallback } from '@snx-v2/useGlobalProvidersWithFallback';

export const AddressInput = () => {
const { globalProviders } = useGlobalProvidersWithFallback();
const L1DefaultProvider = globalProviders.mainnet;
const navigate = useNavigate();

const { register, getValues } = useForm({
defaultValues: { address: '' },
});

const onSubmit = () => {
if (getValues('address')) {
navigate(`/${getValues('address')}`);
const onSubmit = async () => {
const address = getValues('address');
if (address) {
// Try to resolve ENS
if (!ethers.utils.isAddress(address)) {
const ens: string | null = await L1DefaultProvider.resolveName(address);
if (ens) {
navigate(`/${ens}`);
return;
}
}
navigate(`/${address}`);
}
};

return (
<Flex alignSelf="end" width="50%" justifyContent="flex-end" alignItems="center" mb="3px">
<Input
placeholder="Search by address"
placeholder="Search by ENS / address"
w="38%"
minW={{ base: '180px', md: '250px' }}
{...register('address')}
Expand Down
22 changes: 22 additions & 0 deletions v2/perps-v2/ui/src/hooks/useEnsName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { useEffect, useState } from 'react';
import { useGlobalProvidersWithFallback } from '@snx-v2/useGlobalProvidersWithFallback';

export const useEnsName = (address?: string | null) => {
const { globalProviders } = useGlobalProvidersWithFallback();

const [addressEnsName, setAddressEnsName] = useState<string | null>(null);

useEffect(() => {
async function resolve() {
if (!address || !globalProviders.mainnet) {
setAddressEnsName(null);
return;
}
const name = await globalProviders.mainnet.lookupAddress(address);
setAddressEnsName(name);
}
resolve();
}, [address, globalProviders]);

return { addressEnsName };
};
81 changes: 81 additions & 0 deletions v2/perps-v2/ui/src/hooks/useOwnerBySmartId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { z } from 'zod';
import { KWENTA_SUBGRAPH_URL, POLYNOMIAL_SUBGRAPH_URL } from '../utils';
import { ApolloClient, gql, InMemoryCache, useQuery } from '@apollo/client';

const kwentaClient = new ApolloClient({
uri: KWENTA_SUBGRAPH_URL,
cache: new InMemoryCache(),
});

const polyClient = new ApolloClient({
uri: POLYNOMIAL_SUBGRAPH_URL,
cache: new InMemoryCache(),
});

const AccountBySmartIdQuery = gql`
query Account($id: String!) {
smartMarginAccount(id: $id) {
owner
}
}
`;

const AccountQuery = gql`
query SmAccounts($account: String) {
logAccountCreateds(where: { account: $account }) {
owner
account
}
}
`;

const SmartMarginAccountSchema = z.object({
owner: z.string(),
});

const ResponseSchema = z
.object({
smartMarginAccount: SmartMarginAccountSchema,
})
.optional();

const OwnerByPolyAccountSchema = z.array(
z.object({
owner: z.string(),
account: z.string(),
})
);

const ResponseSchemaOwnerByPoly = z
.object({
logAccountCreateds: OwnerByPolyAccountSchema,
})
.optional();

export const useOwnerKwenta = (smartAccountId?: string) => {
const { data, ...rest } = useQuery(AccountBySmartIdQuery, {
client: kwentaClient,
variables: { id: smartAccountId },
skip: !smartAccountId,
});

const parsedResult = ResponseSchema.safeParse(data);
const kwentaOwner = parsedResult.success ? parsedResult.data?.smartMarginAccount?.owner : null;

return { ...rest, kwentaOwner };
};

export const useOwnerPolynomial = (polynomialAccount?: string) => {
const { data, ...rest } = useQuery(AccountQuery, {
client: polyClient,
variables: { account: polynomialAccount },
skip: !polynomialAccount,
});

const parsedResult = ResponseSchemaOwnerByPoly.safeParse(data);
const polynomialOwner = parsedResult.success
? parsedResult.data?.logAccountCreateds?.[0]?.owner
: null;

return { ...rest, polynomialOwner };
};
24 changes: 23 additions & 1 deletion v2/perps-v2/ui/src/pages/Account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,24 @@ import { PositionsTable } from '../components/Positions';
import { AccountActionsTable } from '../components/Actions';
import { useKwentaAccount } from '../hooks/useKwentaAccount';
import { usePolynomialAccount } from '../hooks/usePolynomialAccount';
import { useOwnerKwenta, useOwnerPolynomial } from '../hooks/useOwnerBySmartId';
import { useEnsName } from '../hooks/useEnsName';
import { SmartWallet } from '../components/Shared';

export const Account: FC = () => {
const params = useParams();
const navigate = useNavigate();

const { data: kwentaAccount } = useKwentaAccount(params?.walletAddress);
const { data: polynomialAccount } = usePolynomialAccount(params?.walletAddress);

const { kwentaOwner } = useOwnerKwenta(params?.walletAddress);
const { polynomialOwner } = useOwnerPolynomial(params?.walletAddress);

const { addressEnsName: addressEnsName } = useEnsName(params?.walletAddress);
const { addressEnsName: kwentaEnsName } = useEnsName(kwentaOwner);
const { addressEnsName: polynomialEnsName } = useEnsName(polynomialOwner);

return (
<Flex flexDir="column" px={{ base: '16px', md: '40px' }} py={2}>
<Box mt={12}>
Expand All @@ -36,14 +46,26 @@ export const Account: FC = () => {
flexWrap={{ base: 'wrap', md: 'nowrap' }}
>
<Heading fontSize={{ base: '14px', md: '24px' }} p={0} mr={2}>
Account: {params?.walletAddress}
Account: {addressEnsName ? addressEnsName : params?.walletAddress}
</Heading>
<ExternalLinkIcon color="cyan.500" />
</Link>
<Flex mt={8} wrap="wrap">
{kwentaAccount && (
<SmartWallet label="Kwenta Smart Account" account={kwentaAccount.account} />
)}
{kwentaOwner && (
<SmartWallet
label={kwentaEnsName ? kwentaEnsName : 'EOA Account'}
account={kwentaOwner}
/>
)}
{polynomialOwner && (
<SmartWallet
label={polynomialEnsName ? polynomialEnsName : 'EOA Account'}
account={polynomialOwner}
/>
)}
{polynomialAccount && (
<SmartWallet
ml="30px"
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8974,6 +8974,7 @@ __metadata:
"@pmmmwh/react-refresh-webpack-plugin": ^0.5.10
"@pythnetwork/pyth-evm-js": ^1.1.0
"@snx-v2/formatters": "workspace:*"
"@snx-v2/useGlobalProvidersWithFallback": "workspace:*"
"@snx-v2/v3Theme": "workspace:*"
"@synthetixio/contracts": "workspace:*"
"@synthetixio/wei": ^2.74.4
Expand Down

9 comments on commit 467df74

@vercel
Copy link

@vercel vercel bot commented on 467df74 Sep 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

v2-storybook – ./v2/ui

v2-storybook-synthetixio.vercel.app
staking-storybook.vercel.app
v2-storybook-git-master-synthetixio.vercel.app

@Steve-Rog
Copy link
Contributor

@Steve-Rog Steve-Rog commented on 467df74 Sep 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @noisekit

It seems that search by ENS does not work.
I saw that some components code, compared to my PR, has been modified (as you had commented to me).
How can we coordinate to resolve?

The search bar in the test site with the PR code works. You can see it from here:

I am available to give support if needed.

@Steve-Rog
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@noisekit It was also reported to me that "Largest open position" is not working correctly. But if I'm not mistaken, no part of the code that has been merged pertains to that.

@noisekit
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @noisekit

It seems that search by ENS does not work. I saw that some components code, compared to my PR, has been modified (as you had commented to me). How can we coordinate to resolve?

The search bar in the test site with the PR code works. You can see it from here:

I am available to give support if needed.

Do you have more info? When I checked search it worked fine. Where are you testing right now? The change is not released yet

@noisekit
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@noisekit It was also reported to me that "Largest open position" is not working correctly. But if I'm not mistaken, no part of the code that has been merged pertains to that.

Yeah those things are going to be fixed soon.

@Steve-Rog
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @noisekit
It seems that search by ENS does not work. I saw that some components code, compared to my PR, has been modified (as you had commented to me). How can we coordinate to resolve?
The search bar in the test site with the PR code works. You can see it from here:
I am available to give support if needed.

Do you have more info? When I checked search it worked fine. Where are you testing right now? The change is not released yet

I'm testing here: https://watcher.synthetix.io/

@noisekit
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm testing here: https://watcher.synthetix.io/

yeah. Change was not released to main site yet. Just merged.

@Steve-Rog
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I'm doing some testing locally as well and it seems to work even with these changes and it actually seems to work :).
I was told it was not working, and I reported what I was told.

@noisekit
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries. We will release some time later. More fixes incoming.

Please sign in to comment.