Build an App on KUB
This guide walks you through building an on-chain demo Tally app on the KUB Testnet: connecting wallets, reading and writing to a deployed counter contract, and understanding when to reach for the KUB SDK instead of standard wallet connectors.
What you will build
A Next.js app that connects EVM wallets (MetaMask and other injected wallets)
Contract reads and writes
An understanding of the two wallets on KUB: standard Web3 wallets and KUB Wallet
Start Building
Set up your project
npx create-next-app@latest my-kub-app --typescript --tailwind --app
cd my-kub-app
npm install wagmi viem @tanstack/react-queryDefine the KUB networks and configure Wagmi
viem ships chain definitions for KUB (exported as bitkub / bitkubTestnet in viem/chainsCheck your installed version. Defining the chains explicitly with defineChain also works on any version and makes the config self-documenting:
// config/chains.ts
import { defineChain } from 'viem'
export const kubTestnet = defineChain({
id: 25925,
name: 'KUB Testnet',
nativeCurrency: { name: 'tKUB', symbol: 'tKUB', decimals: 18 },
rpcUrls: {
default: { http: ['https://rpc-testnet.kubchain.io'] },
},
blockExplorers: {
default: { name: 'KUB Scan Testnet', url: 'https://testnet.kubscan.com' },
},
testnet: true,
})
export const kubMainnet = defineChain({
id: 96,
name: 'KUB Mainnet',
nativeCurrency: { name: 'KUB', symbol: 'KUB', decimals: 18 },
rpcUrls: {
default: { http: ['https://rpc.kubchain.io'] },
},
blockExplorers: {
default: { name: 'KUB Scan', url: 'https://kubscan.com' },
},
})Wrap your app in the providers
The providers component accepts an initialState prop so the layout can pass in the connection state recovered from cookies:
Wire the providers into the root layout
Wrap {children} with Providers in app/layout.tsx. This step is required: any component calling a wagmi hook outside WagmiProvider crashes at runtime with useConfig must be used within WagmiProvider. Reading the cookie here and converting it with cookieToInitialState completes the SSR setup, so a returning user's connection state is already correct on first render instead of flashing through Reconnecting...:
Connect wallets
Deploy the contract
Follow this guide to deploy Counter.sol to KUB Testnet with Foundry, funded by the KUB Faucet. Please take note of the deployed address.
Read contract data
Write to the contract
Assemble the page
Optional: Reach KUB Wallet users with the NEXT SDK
The integration differs from the wagmi flow in two important ways:
Install
@bitkub-chain/sdk.js, initialize it with a Client ID and Project ID (from the KUB Playground for KUB Testnet), and authenticate users via OAuth rather than a wallet popup
KUB Wallet users transact through the CallHelper router, so any write function they call needs an extra trailing
address _bitkubNextparameter and a guard restricting the caller to the SDK CallHelper Router
Transactions then go through sdk.sendCustomTx(toAddress, functionReadableABI, methodParams) rather than writeContract. A production KUB app typically supports both paths: wagmi/injected for MetaMask users, NEXT SDK for KUB Wallet users. See the NEXT SDK and the Code Cookbook for working examples.
Next Steps
Go to KUB Mainnet: add
kubMainnetto yourchainsarray and transports, redeploy your contract, and updateCOUNTER_ADDRESS.Scale on KUB Layer 2: the rollup-based L2 (chain ID 9601 mainnet / 259251 testnet) offers lower costs for high-frequency apps; bridge tKUB from the faucet flow to the L2 testnet to experiment.
Bridge assets in: KUB Bridge supports Ethereum, BNB Smart Chain, and JFIN Chain, so users can bring assets to your app.
Get listed: submit your dApp via the KUB Developer Center to appear in the KUB Ecosystem directory.
Last updated