Connect to Mobile Wallet
Solana

Solana#

Solana is a high-performance blockchain platform committed to providing fast, secure, and scalable solutions for decentralized applications and cryptocurrencies. The platform utilizes an innovative consensus algorithm called Proof of History (PoH) that can handle tens of thousands of transactions per second (TPS) while maintaining decentralization and security. Overall, Solana aims to achieve mass adoption of blockchain through its unique technological advantages, catering to various complex decentralized applications and global financial systems.

Sonic is the first atomic SVM chain built to enable sovereign game economics on Solana.

Installation and Initialization#

Make sure to update the OKX App to version 6.90.1 or later to start accessing it:

To integrate OKX Connect into your DApp, you can use npm:

npm install @okxconnect/solana-provider

Before connecting the wallet, you need to create an object for subsequent wallet connections, transaction submissions, and other operations.

OKXUniversalProvider.init({metaData: {name, icon}})

*Request Parameters

  • metaData - object
    • name - string: The name of the application, will not be used as a unique representation.
    • icon - string: URL of the application icon, must be in PNG, ICO, etc. SVG icons are not supported. SVG icons are not supported. It is best to pass a url pointing to a 180x180px PNG icon.

Return Value

  • OKXUniversalProvider

Example

import { OKXUniversalProvider } from "@okxconnect/universal-provider";

const okxUniversalProvider = await OKXUniversalProvider.init({
    dappMetaData: {
        name: "application name",
        icon: "application icon url"
    },
})

Connect to Wallet#

Connect the wallet to obtain the wallet address, which serves as an identifier and is necessary for signing transactions.

okxUniversalProvider.connect(connectParams: ConnectParams);

Request Parameters

  • connectParams - ConnectParams

    • namespaces - [namespace: string]: ConnectNamespace ; information necessary to request a connection, the key is “eip155” for EVM and “solana” for Solana. If any of the requested chains are not supported by the wallet, the wallet will reject the connection;
      • chains: string[]; Chain id information, the
      • rpcMap?: [chainId: string]: string; rpc information, configure the rpc url to request rpc information on the chain;
      • defaultChain?: string; default chain
    • optionalNamespaces - [namespace: string]: ConnectNamespace; optional information for requesting a connection, the key for EVM is “eip155” and for Solana is “solana”. If the corresponding chain information is not supported by the wallet, the connection can still be made;
      • chains: string[]; Chain id information, chain information, chain information, chain information.
        • rpcMap?: [chainId: string]: string; rpc info;
        • defaultChain?: string; default chain
    • sessionConfig: object
      • redirect: string Jump parameter after successful connection, if it is Mini App in Telegram, here can be set deeplink: "tg://resolve" in Telegram
  • Promise <SessionTypes.Struct | undefined>

    • topic: string; The session identifier;
    • namespaces: Record<string, Namespace>; namespace information for a successful connection;
      • chains: string[]; Chain information for the connection;
      • accounts: string[]; accounts information for the connection;
      • methods: string[]; methods supported by the wallet under the current namespace;
      • rpcMap?: [chainId: string]: string; rpc info;
      • defaultChain?: string; default chain for the current session
    • sessionConfig?: SessionConfig
      • dappInfo: object DApp information;
        • name:string
        • icon:string
      • redirect?: string, the redirect parameter after successful connection;

Example

var session = await okxUniversalProvider.connect({
    namespaces: {
        solana: {
            chains: ["solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp", //solana mainnet
                "solana:4uhcVJyU9pJkvQyS88uRDiswHXSCkY3z",//solana testnet
                "sonic:4uhcVJyU9pJkvQyS88uRDiswHXSCkY3z"// sonic testnet
            ],
        }
    },
    sessionConfig: {
        redirect: "tg://resolve"
    }
})

Sending Signature and Transactions#

This method allows sending messages to the wallet, supporting signatures, transactions, and RPC requests.

First create an OKXSolanaProvider object, passing OKXUniversalProvider into the constructor.

import { OKXSolanaProvider } from "@okxconnect/solana-provider/OKXSolanaProvider";
let okxSolanaProvider = new OKXSolanaProvider(okxUniversalProvider)

Signature

okxSolanaProvider.signMessage(message, chain);

Request Parameters

  • message - string, the message to be signed
  • chain: string, the chain for which the signature is requested, recommended to pass this parameter; mandatory when connecting multiple chains; if a chain is not passed, it will be treated as solan:mainnet or sonic:testnet;

Return Value.

  • Promise - object
    • publicKey:string wallet address
    • signature:Uint8Array The result of the signature.

Signature of single transaction

okxSolanaProvider.signTransaction(transaction, chain);

Request parameters

  • transaction - Transaction | VersionedTransaction transaction data object.
  • chain: string, the chain for which the signature execution is requested, it is recommended to pass this parameter; it is mandatory to pass this parameter when connecting multiple chains; connecting a chain without passing it will be handled according to solan:mainnet or sonic:testnet;

Return value

  • Promise - Transaction | VersionedTransaction signed transaction object.

Signs multiple transactions

okxSolanaProvider.signAllTransactions(transactions, chain);

Request parameters

  • transactions - [Transaction | VersionedTransaction] Array of transaction data objects.
  • chain: string, the chain for which the signature execution is requested, it is recommended to pass this parameter; it is mandatory to pass this parameter when connecting multiple chains; connecting a chain without passing it will be handled according to solan:mainnet or sonic:testnet;

Return value

  • Promise - [Transaction | VersionedTransaction] An array of signed transaction objects.

Signs a transaction and broadcasts onchain

okxSolanaProvider.signAndSendTransaction(transaction, chain);

**Request Parameters

  • transactions - Transaction | VersionedTransaction transaction data object
  • chain: string, the chain for which the signature execution is requested, it is recommended to pass this parameter; it is mandatory to pass this parameter when connecting multiple chains; connecting a chain without passing it will be handled according to solan:mainnet or sonic:testnet;

**Return Value

  • Promise - string transactionhash.

get wallet address and pubKey

okxSolanaProvider.getAccount(chain);

Request Parameters

  • chain: string, get the chain id of the wallet address, if not passed then the first connected svm address will be taken by default.

**Return Value

  • Object
    • address: string The address of the wallet.
    • publicKey: PublicKey

Example

// Signing a transfer transaction on solana mainnet
let provider = new OKXSolanaProvider(okxUniversalProvider)
const transaction = new Transaction({
    feePayer: new PublicKey(provider.getAccount().address),
    recentBlockhash: "xNWbUfdEPktMsZQHY6Zk5RJqamWFcTKasekjr7c3wFX",
}).add(SystemProgram.transfer(
    {
        fromPubkey: new PublicKey(provider.getAccount().address),
        toPubkey: new PublicKey(provider.getAccount().address),
        lamports: 1000,
    }
))

let result = await provider.signTransaction(transaction, "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp")


Disconnect wallet#

Disconnect from a connected wallet and delete the current session. If you want to switch wallets, disconnect from the current wallet first.

okxUniversalProvider.disconnect();