Integrating OKX Connect for Cosmos DApps: A Developer's Guide

ยท

This guide provides a comprehensive overview for developers looking to integrate OKX Connect into their Cosmos-based decentralized applications (DApps). It covers everything from installation and initialization to handling transactions and managing wallet connections.

Installation and Initialization

To begin integrating OKX Connect into your DApp, ensure you have the OKX App updated to version 6.94.0 or later. You can install the necessary package using npm.

Before connecting to a wallet, you must create an object that provides a user interface for subsequent operations like wallet connection and transaction signing.

Request Parameters

Return Value

The function returns an OKXUniversalConnectUI object, which is your primary interface for managing wallet connections.

Example Code Snippet

Establishing a Wallet Connection

Connecting to a wallet is a fundamental step to obtain a wallet address for user identification and to gather necessary parameters for signing transactions.

Request Parameters

Return Value

Example Implementation

๐Ÿ‘‰ Explore more connection strategies

Connecting and Signing in a Single Request

This method allows you to connect to a wallet and request a signature in one action. The result is returned via the 'connect_signResponse' event.

Request Parameters

Return Value

Checking Wallet Connection Status

A simple function to determine if a wallet is currently connected to your DApp.

Return Value

Usage Example

Preparing and Signing Transactions

After successful connection and initialization, you can create an OKXCosmosProvider object by passing your OKXUniversalConnectUI instance to its constructor. This provider is your gateway to interacting with the Cosmos blockchain.

Retrieving Account Information

Request Parameters

Return Value

Signing a Message

Request Parameters

Return Value

Using SignAmino

This method signs a transaction using the Amino JSON format.

Request Parameters

Return Value

Using SignDirect

This method signs a transaction using Protobuf (Direct).

Request Parameters

Return Value

Managing Wallet Sessions

Disconnecting a Wallet

This function disconnects the currently connected wallet and deletes the active session. If you need to switch wallets, you must disconnect the current one first.

Handling Events and Errors

The integration emits various events for connection, signing, and session management. It's crucial to handle these events to manage the user flow effectively.

Common Error Codes

The following exceptions may be thrown during connection, transaction, or disconnection processes.

Error CodeDescription
OKX_CONNECT_ERROR_CODES.UNKNOWN_ERRORAn unknown, unexpected error occurred.
OKX_CONNECT_ERROR_CODES.ALREADY_CONNECTED_ERRORThe wallet is already connected.
OKX_CONNECT_ERROR_CODES.NOT_CONNECTED_ERRORThe wallet is not connected.
OKX_CONNECT_ERROR_CODES.USER_REJECTS_ERRORThe user rejected the connection or signing request.
OKX_CONNECT_ERROR_CODES.METHOD_NOT_SUPPORTEDThe requested method is not supported by the wallet.
OKX_CONNECT_ERROR_CODES.CHAIN_NOT_SUPPORTEDThe requested chain is not supported by the wallet.
OKX_CONNECT_ERROR_CODES.WALLET_NOT_SUPPORTEDThe wallet is not supported.
OKX_CONNECT_ERROR_CODES.CONNECTION_ERRORA general connection error occurred.

๐Ÿ‘‰ Get advanced error handling methods

Frequently Asked Questions

What is the minimum OKX App version required for integration?
You must ensure your OKX App is updated to version 6.94.0 or later to access all the functionalities required for a successful OKX Connect integration with your DApp.

What image formats are supported for the DApp icon?
The DApp icon must be provided as a PNG or ICO file. SVG format is not supported. For the best visual results, use a URL that points to a high-quality PNG image sized at 180x180 pixels.

How do I handle a user rejecting a connection request?
Your code should catch the OKX_CONNECT_ERROR_CODES.USER_REJECTS_ERROR exception. This allows you to gracefully inform the user that the request was canceled and perhaps prompt them to try again without causing application errors.

What is the difference between namespaces and optionalNamespaces?
The namespaces parameter defines the essential chains required for your DApp to function. If the wallet doesn't support them, the connection will fail. optionalNamespaces specifies additional chains your DApp can use but doesn't require; the connection will succeed even if these are unsupported.

Can I request a connection and a signature simultaneously?
Yes, you can use the 'Connect to wallet and sign' method to combine these steps. This is efficient for workflows where user authentication immediately requires a signature, reducing the number of modal pop-ups a user sees.

How do I switch between connected wallets?
To switch wallets, you must first programmatically disconnect the current wallet session using the disconnect function. This clears the active session, allowing the user to initiate a new connection and choose a different wallet.