#4 - BUIDL a Swap UI on Solana

Authors: @SaiyanBsarrow-up-right, @ironaddicteddogarrow-up-right

[Updated at 2022.3.31]

See the example repo herearrow-up-right

See the demo DApp herearrow-up-right

TL; DR

  • Use Next.js + React.js + @solana/web3.js

  • Raydium AMM swap

  • Jupiter SDK swap

Introduction

  • What is Solana

    • Solana is a fast, low cost, decentralized blockchain with thousands of projects spanning DeFi, NFTs, Web3 and more.

  • What is Raydium

    • Raydium is an Automated Market Maker (AMM) and liquidity provider built on the Solana blockchain.

  • What is Jupiter

    • Jupiter is the key swap aggregator for Solana, offering the best route discovery between any token pair.

Overview

What does web3.js do?

  • web3.js library

  • Solana tx

  • Solana ix

How to find the program interface?

Structure

Setup

First, let's start a brand new next.js project:

Remove package-lock.json since we will use yarn through this entire tutorial:

Install Dependencies

Next, let's install all the dependencies. This includes:

  • Solana wallet adapter

  • Solana web3.js

  • Solana SPL token

  • Serum

  • Sass

  • Jupiter SDK

  • Next.js config plugins

  • Chakra (UI lib)

  • Lodash

Let's update package.json directly:

Then run the installation:

Note: make sure the version of buffer-layout is locked at ^3.0.0

Scaffold

Populates folders and files for later update:

Add Common Components

There are 4 common components:

  • Navigator

  • Notify

  • SplTokenList

  • WalletProvider

Add the following code in ./views/commons/Navigator.tsx:

Notify

Add the following codearrow-up-right in ./views/commons/Notify.tsx:

SplTokenList

Add the following codearrow-up-right in ./views/commons/SplTokenList.tsx:

WalletProvider

Add the following codearrow-up-right in ./views/commons/WalletProvider.tsx:

Add Pages for Raydium and Jupiter

Add the following code in ./pages/raydium.tsx:

Add the following code in ./pages/jupiter.tsx:

Update Styles

Theere are 3 style sheets to be updated:

  • globals.css

  • navigator.module.sass

  • color.module.sass

globals.css

Add the following code in ./styles/globals.css:

Add the following code in ./styles/navigator.module.sass:

color.module.sass

Add the following code in ./styles/color.module.sass:

Update app

Replace pages/_app.tsx with following code:

Start the dev server. For now you should see jupiter and raydium page with only plain text and one wallet connecting button:

Part 1: Build a Swap on Raydium

What we need to implement Raydium swap?

  1. Token list

  2. Slippage setting

  3. Price out

  4. Amm pools info

  5. Interact with on-chain program

Add Raydium Utils

Let's update each component one by one:

Add Components

We will update the following components:

  • SlippageSetting

  • SwapOperateContainer

  • TitleRow

  • TokenList

  • TokenSelect

  • index

TitleRow.tsx

Add the following codearrow-up-right in ./views/raydium/TitleRow.tsx:

TokenList.tsx

Add the following codearrow-up-right in ./views/raydium/TokenList.tsx:

SlippageSetting.tsx

Add the following codearrow-up-right in ./views/raydium/SlippageSetting.tsx:

TokenSelect.tsx

Add the following codearrow-up-right in ./views/raydium/TokenSelect.tsx:

SwapOperateContainer.tsx

Add the following codearrow-up-right in ./views/raydium/SwapOperateContainer.tsx:

index

Add the following codearrow-up-right in ./views/raydium/index.tsx:

Update Style

Add code in./styles/swap.module.sass from swap.module.sassarrow-up-right.

Add the following code in ./chakra/style.js

Update Config

Replace next.config.js with following code:

Update Raydium Page

Finally, update ./pages/raydium.tsx:

Restart the dev server:

Part 2: Build a Swap on Juipter Aggreggator

How does Jupiter work?

Add Components

We will update the following components:

  • FeeInfo

  • JupiterForm

  • JupiterProvider

JupiterProvider.tsx

Add the following codearrow-up-right in ./views/jupiter/JupiterProvider.tsx:

FeeInfo.tsx

Add the following codearrow-up-right in ./views/jupiter/FeeInfo.tsx:

JupiterForm.tsx

Add the following codearrow-up-right in ./views/jupiter/JupiterForm.tsx:

Update Style

Add the following code in./styles/jupiter.module.sass

Update Config

Replace next.config.js with following code:

Update Jupiter Page

Add the following code in ./pages/jupiter.tsx:

Restart the dev server:

References

  • https://github.com/DappioWonderland/swap-ui-example

  • https://solana-labs.github.io/solana-web3.js

  • https://solana-labs.github.io/wallet-adapter

  • https://docs.jup.ag

  • https://github.com/raydium-io/raydium-ui

  • https://github.com/yihau/full-stack-solana-development

  • https://github.com/yihau/solana-web3-demo

  • https://github.com/thuglabs/create-dapp-solana-nextjs

  • https://www.udemy.com/course/typescript-the-complete-developers-guide

Last updated