1.1.0 • Published 7 months ago

dapp-connect v1.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
7 months ago

@okxweb3/connect-kit

Chinese / English

The OKX Web3 Connect Kit is a lightweight SDK designed to help developers quickly integrate OKX Connect across multiple ecosystems, including Web, Mobile, and Telegram. It supports mainstream blockchain standards such as EIP-6963 and wallet-standard for wallet provider injection and detection, enabling seamless DApp integration with minimal effort and significantly reducing development and integration costs. Existing DApps can be adapted with minimal code changes, often requiring just two lines of code to complete the integration.

npm i @okxweb3/connect-kit

DApp Use Cases

Identify the specific scenario for your DApp. Existing DApps can quickly integrate functionality, while new DApps can refer to the comprehensive integration examples and API documentation below.

1. In Telegram | Mobile Browser

In mobile browsers and Telegram, the Connect Kit automatically injects providers for chains like Ethereum and Solana, broadcasting EIP-6963 and wallet-standard events. No additional code changes are required for DApps to function.

Quick Integration for Existing DApps

import { init } from '@okxweb3/connect-kit';
await init();

Integration for New DApps

// Step 1: Initialize
import { init } from '@okxweb3/connect-kit';
await init();

// Step 2: Add events
window.ethereum.on('connect', (result) => {
  console.log('chainId: ', result.chainId);
});
window.ethereum.on('accountsChanged', (result) => {
  console.log('address: ', result[0]);
});

// Step 3: Connect
// Option 1: Use window.ethereum to connect
const result = await window.ethereum.request({ method: 'eth_requestAccounts' });
console.log('address: ', result[0]);

// Option 2: Connect to OKX Mini Wallet
import { connectOKXMiniWallet } from '@okxweb3/connect-kit';
connectOKXMiniWallet();

// Option 3: Connect to OKX App Wallet
import { connectOKXAppWallet } from '@okxweb3/connect-kit';
connectOKXAppWallet();

2. In PC Browser

In PC environments, Connect Kit supports both automatic and manual provider switching:

  • For DApps supporting EIP-6963 or wallet-standard, initialization is sufficient.
  • For DApps not supporting these standards or integrating multiple wallets, the setProvider method can be used for manual provider switching.

Quick Integration for Existing DApps (Supporting 6963 or Wallet Standard)

// Initialize
import { init } from '@okxweb3/connect-kit';
await init();

Quick Integration for Existing DApps (Not Supporting 6963 or Wallet Standard)

Supports provider switching:

// Step 1: Initialize
import { init } from '@okxweb3/connect-kit';
await init();

// Step 2: Get provider import { getProvider, SupportedNetworks } from '@okxweb3/connect-kit'; const provider = getProvider(SupportedNetworks.ETHEREUM);

// Refer to web3.js for setProvider implementation setProvider(provider);

**Full Example for New DApps Supporting 6963**
```javascript
// Step 1: Initialize
import { init } from '@okxweb3/connect-kit';
await init();

// Step 2: Get provider
window.addEventListener('eip6963:announceProvider', (event) => {
  setProvider(event.detail.provider);
  console.log('provider:', event.detail.provider);
});

// Step 3: Use provider to connect
const result = await provider.request({ method: 'eth_requestAccounts' });
console.log('address: ', result[0]);

Full Example for New DApps not Supporting 6963

// step1 init 
import { init, getProvider, SupportedNetworks } from '@okxweb3/connect-kit';
await init();

// step2 get provider
const provider = getProvider(SupportedNetworks.ETHEREUM);
setProvider(provider);

// step3 add ethereum events
provider.on('connect', function(result){
  console.log('chainId: ', result.chainId)
});
provider.on('accountsChanged', function(result){
  console.log('address: ', result[0]);
})

// step4 connect
// option 1. use window.ethereum to connect 
const result  = await window.ethereum.request({method: 'eth_requestAccounts'});
console.log('address: ', result[0]);

// option 2. connect to OKX mini wallet
import { connectOKXMiniWallet } from '@okxweb3/connect-kit';
connectOKXMiniWallet();

// option 3. connect to OKX App Wallet
import { connectOKXAppWallet } from '@okxweb3/connect-kit';
connectOKXAppWallet();

3. Scan QR Code to Connect

import { getConnectKit, getUri } from '@okxweb3/connect-kit';

// Subscribe to QR Code events
const connectKit = getConnectKit();

connectKit.once('session_connecting', () => {
  console.log('session_connecting, QR Code');
});
connectKit.once('connect', () => {
  console.log('Connected');
});
connectKit.once('connect_error', () => {
  console.log('Connection error');
});

const uri = await getUri();
console.log('Display URI for QR Code scan: ', uri);

React Example

import { useState } from 'react';
import { getConnectKit, getUri } from '@okxweb3/connect-kit';

const [connecting, setConnecting] = useState(false);
const [uri, setUri] = useState('');
const getUriForQrCode = async () => {
  const connectKit = getConnectKit();

  connectKit.once('session_connecting', () => {
    setConnecting(true);
    console.log('session_connecting, QR Code');
  });
  connectKit.once('connect', () => {
    setConnecting(false);
    console.log('Connected');
  });
  connectKit.once('connect_error', () => {
    setConnecting(false);
    console.log('Connection error');
  });

  const newUri = await getUri();
  setUri(newUri);
  console.log('Display URI for QR Code scan: ', uri);
};

<button onClick={getUriForQrCode}>Get QR Code</button>;

API

import { xxx } from '@okxweb3/connect-kit';
MethodReturn TypeDescription
async init(config)ConnectKit objectInitializes the provider and establishes connection to OKX Connect server.
async getConnectKit()ConnectKit objectGets the ConnectKit
async getUri()uriGets the URI for the App Wallet or Mini Wallet QR Code.
connectOKXAppWallet()voidConnects directly to the App Wallet for mobile web or Telegram.
connectOKXMiniWallet()voidConnects directly to the Mini Wallet for PC and mobile environments.
getProvider(network)providerReturns the wallet provider for the specified network.
disconnect()voidDisconnects the wallet.

Supported Networks

EnumValueDescriptionStatus
ETHEREUMethereumEVM compatibleDone
SOLANAsolanaSolana compatibleDone
BITCOINbitcoinBitcoin compatibleDeveloping
TONtonTON networkDeveloping
APTOSaptosAptos networkDeveloping
SUIsuiSUI networkDeveloping
COSMOScosmosCosmos compatibleDeveloping

Error Codes

Conforms to EIP-1193 and EIP-1474 standards.

EnumValueDescription
USER_REJECTS_ERROR4001The user rejected the request.
UNAUTHORIZED_ERROR4100The method/account is not authorized.
UNSUPPORTED_METHOD_ERROR4200The provider does not support the method.
DISCONNECTED_ERROR4900Provider disconnected from all chains.
CHAIN_DISCONNECTED_ERROR4901Provider not connected to the requested chain.
INTERNAL_ERROR-32603Internal JSON-RPC error.
1.1.0

7 months ago

0.0.10

7 months ago

0.0.11

7 months ago

0.0.12

7 months ago

0.0.13

7 months ago

0.0.14

7 months ago

0.0.15

7 months ago

0.0.9

7 months ago

0.0.16

7 months ago

0.0.8

7 months ago

0.0.17

7 months ago

0.0.18

7 months ago

0.0.19

7 months ago

0.0.7

7 months ago

0.0.6

7 months ago

0.0.5

7 months ago

0.0.4

7 months ago

0.0.3

7 months ago

0.0.2

7 months ago

0.0.1

7 months ago