Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
juliancwirko committed Nov 16, 2022
1 parent 8f30c88 commit 70090cf
Show file tree
Hide file tree
Showing 19 changed files with 1,432 additions and 1,387 deletions.
12 changes: 6 additions & 6 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
# Public variables (exposed on the frontend)
# =============================================

# Elrond chain (can be devnet, testnet, mainnet)
NEXT_PUBLIC_ELROND_CHAIN = devnet
# MultiversX chain (can be devnet, testnet, mainnet)
NEXT_PUBLIC_MULTIVERSX_CHAIN = devnet

# This is the masked/proxied public API endpoint
# only current instance of the Dapp can use it if only API_ALLOWED_DAPP_HOST is set
NEXT_PUBLIC_ELROND_API = /api/elrond
NEXT_PUBLIC_MULTIVERSX_API = /api/multiversx

# This is basically the main domain of your dapp
NEXT_PUBLIC_DAPP_HOST = http://localhost:3000
Expand All @@ -16,9 +16,9 @@ NEXT_PUBLIC_DAPP_HOST = http://localhost:3000
# Private variables (used on backend)
# =============================================

# Your main Elrond API can be a custom one. There won't be a possibility
# to reveal this endpoint, it will be masked by NEXT_PUBLIC_ELROND_API
ELROND_CUSTOM_API = https://devnet-api.elrond.com
# Your main MultiversX API can be a custom one. There won't be a possibility
# to reveal this endpoint, it will be masked by NEXT_PUBLIC_MULTIVERSX_API
MULTIVERSX_CUSTOM_API = https://devnet-api.elrond.com

# Only this host will be allowed to consume the API (optional)
# It will work only inside the Dapp, no one will be able to use the endpoint, even in browser
Expand Down
21 changes: 13 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
### [2.2.2](https://github.com/ElrondDevGuild/nextjs-dapp-template/releases/tag/v2.2.2) (2022-10-30)
### [3.0.0](https://github.com/xdevguild/nextjs-dapp-template/releases/tag/v2.3.0) (2022-11-16)
- dependencies updates
- first phase of 'rebranding' into MultiversX ;)
- **Breaking**: `useElrondNetworkSync` is now `useNetworkSync`

### [2.2.2](https://github.com/xdevguild/nextjs-dapp-template/releases/tag/v2.2.2) (2022-10-30)
- dependencies updates, also Next 13, changes in the routing will be introduced later. Waits for new Next 13 docs to be completed.

### [2.2.1](https://github.com/ElrondDevGuild/nextjs-dapp-template/releases/tag/v2.2.1) (2022-10-10)
### [2.2.1](https://github.com/xdevguild/nextjs-dapp-template/releases/tag/v2.2.1) (2022-10-10)
- bugfix for the wrong usage of the Chakra Factory on CardWrapper and FlexCardWrapper components. Thanks to @janniksam for reporting that

### [2.2.0](https://github.com/ElrondDevGuild/nextjs-dapp-template/releases/tag/v2.2.0) (2022-10-09)
### [2.2.0](https://github.com/xdevguild/nextjs-dapp-template/releases/tag/v2.2.0) (2022-10-09)
- dependencies updates (Next, erdjs, etc.)

### [2.1.0](https://github.com/ElrondDevGuild/nextjs-dapp-template/releases/tag/v2.1.0) (2022-09-04)
### [2.1.0](https://github.com/xdevguild/nextjs-dapp-template/releases/tag/v2.1.0) (2022-09-04)
- new `useApiCall` hook, check the readme for more info

### [2.0.1](https://github.com/ElrondDevGuild/nextjs-dapp-template/releases/tag/v2.0.1) (2022-08-13)
### [2.0.1](https://github.com/xdevguild/nextjs-dapp-template/releases/tag/v2.0.1) (2022-08-13)
- fix problems with rerendering (wrong memo usage)
- update dependencies

### [2.0.0](https://github.com/ElrondDevGuild/nextjs-dapp-template/releases/tag/v2.0.0) (2022-08-01)
### [2.0.0](https://github.com/xdevguild/nextjs-dapp-template/releases/tag/v2.0.0) (2022-08-01)
- Nextjs update
- React update
- erdjs libraries update
Expand All @@ -25,8 +30,8 @@
- ts types improvements
- switched to MIT license (erdjs libs are now MIT too)

### [1.1.0](https://github.com/ElrondDevGuild/nextjs-dapp-template/releases/tag/v1.1.0) (2022-06-19)
### [1.1.0](https://github.com/xdevguild/nextjs-dapp-template/releases/tag/v1.1.0) (2022-06-19)
- added HW provider

### [1.0.0](https://github.com/ElrondDevGuild/nextjs-dapp-template/releases/tag/v1.0.0) (2022-05-15)
### [1.0.0](https://github.com/xdevguild/nextjs-dapp-template/releases/tag/v1.0.0) (2022-05-15)
- initial code
36 changes: 18 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
### NextJS Dapp Template

- [elrond-nextjs-dapp.netlify.com](https://elrond-nextjs-dapp.netlify.com)
- [multiversx-nextjs-dapp.netlify.com](https://multiversx-nextjs-dapp.netlify.com)

Nextjs alternative to the [dapp-core](https://github.com/ElrondNetwork/dapp-core).
Based on [Elven Tools Dapp](https://www.elven.tools/docs/minter-dapp-introduction.html).
Expand All @@ -12,7 +12,7 @@ It has straightforward and complete functionality.

- it works on Nextjs
- it uses erdjs 11.* without the dapp-core library.
- it uses backed side redirections to hide the API endpoint. The only exposed one is `/api/elrond` and it is used only be the dapp internally
- it uses backed side redirections to hide the API endpoint. The only exposed one is `/api/multiversx` and it is used only be the dapp internally
- it uses the .env file - there is an example in the repo (for all configuration, also for the demo config)
- it uses chakra-ui

Expand All @@ -35,7 +35,7 @@ There are much more hooks and tools, but most of them are already used in the on

The code samples are not ready to copy and paste. Please search them in the code.

#### useElrondNetworkSync()
#### useNetworkSync()

The hook is responsible for synchronizing the network on each refresh. It should be used in the root component. Here is the `_app.tsx`.

Expand All @@ -44,10 +44,10 @@ Why not the context wrapper? Because context wrappers with auth state data check
This way, you can check the auth state in chosen places. You are not forced to do this constantly for the whole document tree.

```jsx
import { useElrondNetworkSync } from '../hooks/auth/useElrondNetworkSync';
import { useNetworkSync } from '../hooks/auth/useNetworkSync';

const NextJSDappTemplate = ({ Component, pageProps }: AppProps) => {
useElrondNetworkSync();
useNetworkSync();
return (
<ChakraProvider theme={theme}>
<Component {...pageProps} />
Expand Down Expand Up @@ -186,7 +186,7 @@ const { loginMethod, expires, loginToken, signature } = useLoginInfo();

#### useApiCall()

The hook provides a convenient way of doing custom API calls unrelated to transactions or smart contract queries. By default it will use Elrond API endpoint. But it can be any type of API, not only Elrond API. In that case you would need to pass the `{ baseEndpoint: "https://some-api.com" }` in options
The hook provides a convenient way of doing custom API calls unrelated to transactions or smart contract queries. By default it will use MultiversX API endpoint. But it can be any type of API, not only MultiversX API. In that case you would need to pass the `{ baseEndpoint: "https://some-api.com" }` in options

```jsx
const { data, isLoading, isValidating, fetch, error } = useApiCall<Token[]>({
Expand All @@ -203,9 +203,9 @@ The hook uses `swr` and native `fetch` under the hood.

### Working with the API

The API endpoint is proxied on the backend side. The only public API endpoint is `/api/elrond`. This is useful when you don't want to show the API endpoint because, for example, you use the paid ones. Also, there is an option to block the `/api/elrond` endpoint to be used only within the Dapp, even previewing it in the browser won't be possible.
The API endpoint is proxied on the backend side. The only public API endpoint is `/api/multiversx`. This is useful when you don't want to show the API endpoint because, for example, you use the paid ones. Also, there is an option to block the `/api/multiversx` endpoint to be used only within the Dapp, even previewing it in the browser won't be possible.

You can use `API_ALLOWED_DAPP_HOST` in the .env file to enable `/api/elrond` restrictions. If you don't want to restrict it, you can remove that variable.
You can use `API_ALLOWED_DAPP_HOST` in the .env file to enable `/api/multiversx` restrictions. If you don't want to restrict it, you can remove that variable.

In the `middleware.ts`, you'll find the logic for the API restrictions. And in the `next.config.js`, you'll find the configuration for rewrites of the API.

Expand All @@ -224,12 +224,12 @@ Here are all variables:
# Public variables (exposed on the frontend)
# =============================================

# Elrond chain (can be devnet, testnet, mainnet)
NEXT_PUBLIC_ELROND_CHAIN = devnet
# MultiversX chain (can be devnet, testnet, mainnet)
NEXT_PUBLIC_MULTIVERSX_CHAIN = devnet

# This is the masked/proxied public API endpoint
# only current instance of the Dapp can use it if only API_ALLOWED_DAPP_HOST is set
NEXT_PUBLIC_ELROND_API = /api/elrond
NEXT_PUBLIC_MULTIVERSX_API = /api/multiversx

# This is basically the main domain of your dapp
NEXT_PUBLIC_DAPP_HOST = http://localhost:3000
Expand All @@ -238,9 +238,9 @@ NEXT_PUBLIC_DAPP_HOST = http://localhost:3000
# Private variables (used on the backend)
# =============================================

# Your main Elrond API can be a custom one. There won't be a possibility
# to reveal this endpoint, it will be masked by NEXT_PUBLIC_ELROND_API
ELROND_CUSTOM_API = https://devnet-api.elrond.com
# Your main MultiversX API can be a custom one. There won't be a possibility
# to reveal this endpoint, it will be masked by NEXT_PUBLIC_MULTIVERSX_API
MULTIVERSX_CUSTOM_API = https://devnet-api.elrond.com

# Only this host will be allowed to consume the API (optional)
# It will work only inside the Dapp, no one will be able to use the endpoint, even in browser
Expand Down Expand Up @@ -272,7 +272,7 @@ NEXT_PUBLIC_EGLD_TRANSFER_AMOUNT = 0.001

All variables which start with `NEXT_PUBLIC_` will be readable on the frontend side of the dapp. So please don't use them for any secret keys and data. If you need something to be available only on the backend side, don't use the `NEXT_PUBLIC_` prefix.

You can set up the chain type here. Use `NEXT_PUBLIC_ELROND_CHAIN` to set devnet, testnet or mainnet.
You can set up the chain type here. Use `NEXT_PUBLIC_MULTIVERSX_CHAIN` to set devnet, testnet or mainnet.

Each hosting provider will have a different way of doing that. We will take a look at how Netlify is doing that below.

Expand Down Expand Up @@ -306,12 +306,12 @@ If you would like to test other templates:
Dapps using it (send the links if you used it, use issues here on GitHub):

- [Elven Tools Dapp](https://dapp-demo.elven.tools/)
- [Elrond ESDT Faucet Dapp](https://devnet-elrond-esdt-faucet.netlify.app/)
- [MultiversX ESDT Faucet Dapp](https://devnet-elrond-esdt-faucet.netlify.app/)

Compact Elrond SDK for browsers (no build steps required)
Compact MultiversX SDK for browsers (no build steps required)

- [Elven.js](https://www.elvenjs.com)

### Contact

- [Elrond's Dev Guild](https://github.com/ElrondDevGuild)
- [xDevGuild](https://github.com/xdevguild)
2 changes: 1 addition & 1 deletion components/demo/GetUserDataDemo.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TokenPayment } from '@elrondnetwork/erdjs/out';
import { TokenPayment } from '@elrondnetwork/erdjs';
import { Text, Link } from '@chakra-ui/react';
import { shortenHash } from '../../utils/shortenHash';
import { useAccount } from '../../hooks/auth/useAccount';
Expand Down
2 changes: 1 addition & 1 deletion components/tools/LedgerAccountsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const LedgerAccountsList: FC<LedgerAccountsListProps> = ({
mounted.current
) {
setError(
'Not connected, please check the connection and make sure that you have the Elrond app opened on your Ledger device.'
'Not connected, please check the connection and make sure that you have the MultiversX app opened on your Ledger device.'
);
} else {
setError(`Error: ${errorParse(e)}`);
Expand Down
6 changes: 3 additions & 3 deletions components/ui/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const Footer = () => {
fontWeight="normal"
textAlign="center"
>
<Box>Elrond NextJS Dapp Template (v{`${packageJson.version}`})</Box>
<Box>MultiversX NextJS Dapp Template (v{`${packageJson.version}`})</Box>
<Box fontSize="xs" fontWeight="hairline">
All for free. Please support the project. Give it credit and tell the
world about it. Attribution is not required but welcomed in the form
Expand All @@ -31,11 +31,11 @@ export const Footer = () => {
<Text
as="a"
color="dappTemplate.color3.base"
href="https://github.com/ElrondDevGuild"
href="https://github.com/zdevguild"

This comment has been minimized.

Copy link
@icegriffinguru

icegriffinguru Nov 17, 2022

Member

xdevguild :)

This comment has been minimized.

Copy link
@juliancwirko

juliancwirko Nov 18, 2022

Author Member

thanks :)

This comment has been minimized.

Copy link
@icegriffinguru

icegriffinguru via email Nov 18, 2022

Member

This comment has been minimized.

Copy link
@juliancwirko

juliancwirko Nov 18, 2022

Author Member

devs never sleep ;)

target="_blank"
rel="noopener noreferrer nofollow"
>
{"Elrond's Dev Guild"}
{'xDevGuild'}
</Text>
<Text ml={2} mr={2}>
{' '}
Expand Down
2 changes: 1 addition & 1 deletion components/ui/Logo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const Logo = () => {
fontWeight="black"
color="dappTemplate.white"
>
Elrond Dapp Template
MultiversX Dapp Template
</Text>
</Box>
</NextLink>
Expand Down
2 changes: 1 addition & 1 deletion components/ui/MetaHead.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const MetaHead: FC<MetaHeadProps> = memo(
name="description"
content={metaDescription || defaultMetaTags.description}
/>
<meta name="author" content="Elrond NextJS Dapp Template"></meta>
<meta name="author" content="MultiversX NextJS Dapp Template"></meta>
<meta property="og:type" content="website" />
<meta
property="og:title"
Expand Down
2 changes: 1 addition & 1 deletion components/ui/SocialMediaIcons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const SocialMediaIcons = () => {
return (
<Box display="flex" alignItems="center" gap={3}>
<SocialIcon
url="https://github.com/ElrondDevGuild/nextjs-dapp-template"
url="https://github.com/xdevguild/nextjs-dapp-template"
bgColor="#fff"
style={{ width: 30, height: 30 }}
/>
Expand Down
4 changes: 2 additions & 2 deletions config/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const dappHostname = process.env.NEXT_PUBLIC_DAPP_HOST;

// HTML metata and og tags, default values for MetaHead.tsx component
export const defaultMetaTags = {
title: 'Elrond NextJS dapp demo - Elrond blockchain',
description: 'Open source Dapp template for the Elrond blockchain.',
title: 'MultiversX NextJS dapp demo - MultiversX blockchain',
description: 'Open source Dapp template for the MultiversX blockchain.',
image: `${dappHostname}/og-image.png`,
};
12 changes: 7 additions & 5 deletions config/network.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { NetworkType } from '../types/network';

// Default Elrond network configuration (constants).
// Default MultiversX network configuration (constants).
// Change if you need, but by default, you shouldn't have to do that.

export const DEFAULT_MIN_GAS_LIMIT = 50_000;

export const DAPP_CONFIG_ENDPOINT = '/dapp/config';
export const DAPP_INIT_ROUTE = '/dapp/init';

export const chainType = process.env.NEXT_PUBLIC_ELROND_CHAIN || 'devnet';
export const chainType = process.env.NEXT_PUBLIC_MULTIVERSX_CHAIN || 'devnet';

export const networkConfig: Record<string, NetworkType> = {
devnet: {
Expand All @@ -24,7 +24,7 @@ export const networkConfig: Record<string, NetworkType> = {
walletConnectBridgeAddresses: ['https://bridge.walletconnect.org'],
walletAddress: 'https://devnet-wallet.elrond.com',
apiAddress:
process.env.NEXT_PUBLIC_ELROND_API || 'https://devnet-api.elrond.com',
process.env.NEXT_PUBLIC_MULTIVERSX_API || 'https://devnet-api.elrond.com',
explorerAddress: 'https://devnet-explorer.elrond.com',
apiTimeout: '4000',
},
Expand All @@ -42,7 +42,8 @@ export const networkConfig: Record<string, NetworkType> = {
walletConnectBridgeAddresses: ['https://bridge.walletconnect.org'],
walletAddress: 'https://testnet-wallet.elrond.com',
apiAddress:
process.env.NEXT_PUBLIC_ELROND_API || 'https://testnet-api.elrond.com',
process.env.NEXT_PUBLIC_MULTIVERSX_API ||
'https://testnet-api.elrond.com',
explorerAddress: 'https://testnet-explorer.elrond.com',
apiTimeout: '4000',
},
Expand All @@ -59,7 +60,8 @@ export const networkConfig: Record<string, NetworkType> = {
'https://maiar.page.link/?apn=com.elrond.maiar.wallet&isi=1519405832&ibi=com.elrond.maiar.wallet&link=https://maiar.com/',
walletConnectBridgeAddresses: ['https://bridge.walletconnect.org'],
walletAddress: 'https://wallet.elrond.com',
apiAddress: process.env.NEXT_PUBLIC_ELROND_API || 'https://api.elrond.com',
apiAddress:
process.env.NEXT_PUBLIC_MULTIVERSX_API || 'https://api.elrond.com',
explorerAddress: 'https://explorer.elrond.com',
apiTimeout: '4000',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { clearAuthStates } from '../../store/auth';
import { DappProvider } from '../../types/network';
import { errorParse } from '../../utils/errorParse';

export const useElrondNetworkSync = () => {
export const useNetworkSync = () => {
const { logout } = useLogout();
const [accountDone, setAccountDone] = useState(false);
const [loginInfoDone, setLoginInfoDone] = useState(false);
Expand All @@ -44,7 +44,7 @@ export const useElrondNetworkSync = () => {
const apiNetworkProviderRef = useRef<ApiNetworkProvider>();

useEffect(() => {
const accountStorage = localStorage.getItem('elrond_dapp__account');
const accountStorage = localStorage.getItem('multiversx_dapp__account');
const parsedStorage = accountStorage ? JSON.parse(accountStorage) : null;

if (!parsedStorage?.address) {
Expand All @@ -60,7 +60,7 @@ export const useElrondNetworkSync = () => {
}, []);

useEffect(() => {
const loginInfoStorage = localStorage.getItem('elrond_dapp__loginInfo');
const loginInfoStorage = localStorage.getItem('multiversx_dapp__loginInfo');
if (loginInfoStorage) {
const parsedStorage = JSON.parse(loginInfoStorage);
setLoginInfoState('loginMethod', parsedStorage.loginMethod);
Expand All @@ -72,7 +72,10 @@ export const useElrondNetworkSync = () => {
}, []);

useEffectOnlyOnUpdate(() => {
localStorage.setItem('elrond_dapp__account', JSON.stringify(accountSnap));
localStorage.setItem(
'multiversx_dapp__account',
JSON.stringify(accountSnap)
);
}, [
accountSnap.address,
accountSnap.nonce,
Expand All @@ -82,7 +85,7 @@ export const useElrondNetworkSync = () => {

useEffectOnlyOnUpdate(() => {
localStorage.setItem(
'elrond_dapp__loginInfo',
'multiversx_dapp__loginInfo',
JSON.stringify(loginInfoSnap)
);
}, [
Expand All @@ -97,7 +100,7 @@ export const useElrondNetworkSync = () => {
const askForApiNetworkProvider = async () => {
let apiNetworkProvider = apiNetworkProviderRef?.current;
if (!apiNetworkProvider) {
const publicApiEndpoint = process.env.NEXT_PUBLIC_ELROND_API;
const publicApiEndpoint = process.env.NEXT_PUBLIC_MULTIVERSX_API;
if (publicApiEndpoint) {
apiNetworkProvider = new ApiNetworkProvider(publicApiEndpoint, {
timeout: Number(networkConfig[chainType].apiTimeout),
Expand Down
6 changes: 2 additions & 4 deletions middleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-disable @next/next/no-server-import-in-page */

// This middleware is to protect the proxied api to be called only by the same host
// In the future it could get more logic, for example JWT tokens, etc.

Expand All @@ -17,9 +15,9 @@ interface CustomNextRequest extends NextRequest {
export function middleware(req: CustomNextRequest) {
const res = NextResponse.next();

if (!process.env.NEXT_PUBLIC_ELROND_API) return res;
if (!process.env.NEXT_PUBLIC_MULTIVERSX_API) return res;

if (req.nextUrl.pathname.startsWith(process.env.NEXT_PUBLIC_ELROND_API)) {
if (req.nextUrl.pathname.startsWith(process.env.NEXT_PUBLIC_MULTIVERSX_API)) {
const definedHost = process.env.API_ALLOWED_DAPP_HOST;

if (!definedHost) return res;
Expand Down
4 changes: 2 additions & 2 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const nextConfig = {
async rewrites() {
return [
{
source: `${process.env.NEXT_PUBLIC_ELROND_API}/:path*`,
destination: `${process.env.ELROND_CUSTOM_API}/:path*`,
source: `${process.env.NEXT_PUBLIC_MULTIVERSX_API}/:path*`,
destination: `${process.env.MULTIVERSX_CUSTOM_API}/:path*`,
},
];
},
Expand Down
Loading

0 comments on commit 70090cf

Please sign in to comment.