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

Feat: Wallet connect logic #19

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,23 @@
},
"dependencies": {
"@apollo/client": "^3.7.14",
"@chain-registry/types": "^0.50.44",
"@chakra-ui/cli": "^3.2.3",
"@chakra-ui/progress": "^2.2.0",
"@chakra-ui/react": "^3.2.2",
"@cosmjs/encoding": "^0.30.1",
"@cosmjs/launchpad": "^0.27.1",
"@cosmjs/stargate": "^0.29.5",
"@cosmjs/math": "^0.32.4",
"@cosmjs/stargate": "^0.32.4",
"@cosmos-kit/core": "^2.15.0",
"@cosmos-kit/keplr": "^2.14.2",
"@cosmos-kit/leap": "^2.14.2",
"@cosmos-kit/ledger": "^2.13.1",
"@cosmos-kit/react": "^2.21.1",
"@emotion/react": "^11.11.0",
"@emotion/server": "^11.11.0",
"@emotion/styled": "^11.11.0",
"@interchain-ui/react": "^1.26.1",
"@keplr-wallet/types": "^0.11.59",
"@keplr-wallet/wc-client": "^0.11.59",
"@socialgouv/matomo-next": "^1.6.1",
Expand All @@ -36,6 +44,7 @@
"axios": "^0.21.4",
"bech32": "^2.0.0",
"big.js": "^6.2.1",
"chain-registry": "^1.69.85",
"chart.js": "^4.4.7",
"color": "^4.2.3",
"copy-to-clipboard": "^3.3.3",
Expand Down
162 changes: 113 additions & 49 deletions src/components/layout/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,80 +8,143 @@ import {
Flex,
useBreakpointValue,
Image,
Button,
} from '@chakra-ui/react';
import NextLink from 'next/link';
import { useRecoilValue } from 'recoil';
import { useState } from 'react';
import { WalletStatus } from '@cosmos-kit/core';
import { useChain } from '@cosmos-kit/react';

import { readMarket } from '@/recoil/market';
import { chainConfig } from '@/configs';
import { formatMarket } from '@/utils/format_market';
import { Wallet } from '../icons/wallet';
import { Search } from '../icons/search';

import { InputGroup } from '../ui/input-group';
import { Search } from '../icons/search';
import MenuDrawer from './menudrawer';
import SearchBar from './search';
import PageHeader from './page-header';

const chainNames = ['cosmoshub', 'osmosis'];

export default function Header() {
// 1. Recoil-based market data
const marketState = useRecoilValue(readMarket);
const market = formatMarket(marketState);

// 2. CosmosKit connect/disconnect logic
const {
username, connect, disconnect, wallet, status,
} = useChain(chainNames[0]);
const [globalStatus, setGlobalStatus] = useState<WalletStatus>(
WalletStatus.Disconnected,
);

// 3. Global Connect Button
const getGlobalButton = () => {
if (status === 'Connecting') {
return (
<Button
colorScheme="teal"
size="md"
mt={6}
mb={2}
disabled
/>
);
}
if (status === 'Connected') {
return (
<HStack mt={6} mb={2}>
<Button variant="outline">
{wallet?.prettyName}
</Button>
<Button variant="outline">
{username || 'My Account'}
</Button>
<Button
colorScheme="teal"
onClick={async () => {
await disconnect();
setGlobalStatus(WalletStatus.Disconnected);
}}
>
Disconnect
</Button>
</HStack>
);
}

// Default: If not connected
return (
<Button
colorScheme="teal"
size="md"
mt={6}
mb={2}
onClick={() => connect()}
>
Connect Wallet
</Button>
);
};

// 4. Responsive check
const isMobile = useBreakpointValue({
base: true, md: false,
});

return !isMobile ? (
<Flex w="full" gap="20px" align="center" pb="10" direction="column">
<HStack>
<PageHeader />
<HStack divideX="2px">
<Text fontSize="16px">
Supply:
{' '}
<Link fontWeight={600} color="black" textDecor="none">
{market.supply}
</Link>
{' '}
</Text>
<Text fontSize="16px" pl="2">
Community Pool:
{' '}
<Link fontWeight={600} color="black" textDecor="none">
{market.communityPool}
</Link>
{' '}
</Text>
<HStack w="full" justify="space-between">
<HStack>
<PageHeader />
<HStack divideX="2px">
<Text fontSize="16px">
Supply:
{' '}
<Link fontWeight={600} color="black">
{market.supply}
</Link>
</Text>
<Text fontSize="16px" pl="2">
Community Pool:
{' '}
<Link fontWeight={600} color="black">
{market.communityPool}
</Link>
</Text>
</HStack>
<SearchBar />
<Center
w="250px"
h="60px"
borderRadius="60px"
fontSize="16px"
border="1px solid #e4e4e7"
>
{chainConfig.network}
</Center>
</HStack>
<SearchBar />
<Center
w="250px"
h="60px"
borderRadius="60px"
fontSize="16px"
border="1px solid #e4e4e7"
>
{chainConfig.network}
</Center>
<IconButton
aria-label="connect wallet"
rounded="full"
bgColor="#707D8A"
w="60px"
h="60px"
>
<Wallet />
</IconButton>

{/* Right side: connect button */}
<HStack>{getGlobalButton()}</HStack>
</HStack>
</Flex>
) : (
<Flex flexDirection="column" w="100%" gap="20px" align="center" pb="10">
<Flex w="full" gap="10px" align="center" pb="2">
<Link asChild outline="none">
<NextLink href="/">
<Image w="50px" src="/images/logo.svg" />
</NextLink>
</Link>
<Text fontSize="20px" fontWeight={600} flex="1">
Realio
</Text>
<Flex w="full" gap="10px" align="center" pb="2" justify="space-between">
<HStack>
<Link asChild>
<NextLink href="/">
<Image w="50px" src="/images/logo.svg" alt="Realio Logo" />
</NextLink>
</Link>
<Text fontSize="20px" fontWeight={600}>
Realio
</Text>
</HStack>

<Center
w="200px"
Expand Down Expand Up @@ -111,6 +174,7 @@ export default function Header() {
placeholder="Search for validator / tx hash / block height / address"
/>
</InputGroup>
<HStack>{getGlobalButton()}</HStack>
</Flex>
);
}
63 changes: 55 additions & 8 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ import { ApolloProvider } from '@apollo/client';
import { useApollo } from '@/graphql/client';
import { chainConfig } from '@/configs';
import { useWindowOrigin } from '@/hooks/use_window';
import { ChainProvider } from '@cosmos-kit/react';

// CosmosKit imports:
import { Chain } from '@chain-registry/types';
import { Decimal } from '@cosmjs/math';
import { GasPrice } from '@cosmjs/stargate';
import { wallets as keplrWallets } from '@cosmos-kit/keplr';
import { wallets as leapWallets } from '@cosmos-kit/leap';
import { wallets as ledgerWallets } from '@cosmos-kit/ledger';
import {
chains, assets,
} from 'chain-registry';
import {
OPEN_GRAPH_SEO,
TWITTER_SEO,
Expand Down Expand Up @@ -46,16 +58,51 @@ function App({
additionalLinkTags={ADDITIONAL_LINK_TAGS_SEO}
additionalMetaTags={ADDITIONAL_META_TAGS}
/>
<ApolloProvider
client={apolloClient}
>
<ApolloProvider client={apolloClient}>
<RecoilRoot>
<ChakraProvider value={defaultSystem}>
<ThemeProvider attribute="class" disableTransitionOnChange>
<Layout>
<Component {...pageProps} />
</Layout>
</ThemeProvider>
<ChainProvider
chains={chains}
assetLists={assets}
wallets={[
...keplrWallets,
...leapWallets,
...ledgerWallets,
]}
walletConnectOptions={{
signClient: {
// TODO new projectId
projectId: 'a8510432ebb71e6948cfd6cde54b70f7',
relayUrl: 'wss://relay.walletconnect.org',
metadata: {
name: 'CosmosKit Template',
description: 'CosmosKit dapp template',
url: 'https://docs.cosmoskit.com/',
icons: [],
},
},
}}
// wrappedWithChakra
signerOptions={{
signingStargate: (chain: Chain) => {
switch (chain.chain_name) {
case 'realio':
return {
gasPrice: new GasPrice(Decimal.zero(1), 'ario'),
};
default:
return undefined;
}
},
}}
>

<ThemeProvider attribute="class" disableTransitionOnChange>
<Layout>
<Component {...pageProps} />
</Layout>
</ThemeProvider>
</ChainProvider>
</ChakraProvider>
</RecoilRoot>
</ApolloProvider>
Expand Down