Solana Provider

You can interact with Solana blockchains using @solana/web3.js together with the Wepin Provider.

Supported Networks

Chain IDNetwork NameNetwork Variable

solana:mainnet

Solana Mainnet

solana

solana:devnet

Solana Devnet

solana-devnet

Installation

npm install @wepin/provider-js

Once the installation is complete, initialize the WepinProvider instance using the app ID and app key assigned after registering the app. This will enable the use of WepinProvider.

// 1. Import the package
import { WepinProvider } from '@wepin/provider-js'

// 2. Initialization
const WepinProvider = new WepinProvider({
    appId: 'your-wepin-app-id',
    appKey: 'your-wepin-api-key',
})

Initialization

Here's how to initialize the Wepin Provider.

init

await wepinProvider.init(attributes?)

Parameters

  • attributes <object> optional

    • defaultLanguage: The language to be displayed on the widget (default: 'ko') Currently, only 'ko' , 'en' and 'ja' are supported.

    • defaultCurrency: The currency to be displayed on the widget (default: 'KRW') Currently, only 'KRW', 'USD' and 'JPY' are supported.

Return value

  • Promise<void>

Example

await wepinProvider.init({
    defaultLanguage: 'ko',
    defaultCurrency: 'KRW',
})

isInitialized

It checks if WepinProvider is initialized properly.

wepinProvider.isInitialized()

Parameters

  • <void>

Return value

  • <boolean> Returns true if init was successful, otherwise returns false.

Example

if(wepinProvider.isInitialized()) {
  console.log('wepinProvider is initialized!')
}

changeLanguage

It allows changing the language and currency of the widget.

wepinProvider.changeLanguage(attributes)

Parameters

  • attributes <object>

    • language <string> It specifies the language to be displayed on the widget. Currently supported languages are en , ko and ja

    • currency <string> It specifies the currency to be displayed on the widget. Currently supported currencies are USD , KRWand JPY

Return value

  • <boolean> It returns true if the change was successful, otherwise returns false.

Example

if(wepinProvider.isInitialized()) {
  console.log('wepinProvider is initialized!')
}

Methods

Methods can be used after initializing the Wepin Provider.

getProvider

It returns the provider for the specified network.

await wepinProvider.getProvider(network)

Parameters

  • network <string> The Network Variable value for providers supported by Wepin, such as "solana" for Solana Mainnet, must be entered in lowercase. For the complete list, please refer to the Supported Networks for Solana Provider.

Return value

  • Promise<BaseProvider> - A solana provider

Example

const provider = await wepinProvider.getProvider('solana')

finalize

It terminates the use of WepinProvider.

wepinProvider.finalize()

Parameters

  • <void>

Return value

  • <void>

Example

wepinProvider.finalize()

request

You can send JSON-RPC requests to interact with the Solana blockchain.

connect

Connects to the Wepin Wallet and requests permission to share the user's account address (Public Key). Once the connection is approved, the application can make further requests, such as signing messages or transactions.

Parameters

  • <void>

Return value

  • Promise<object>

    • publicKey<string> - Solana Account's Address(Public Key)

Example

await wepinProvider.request({
    method: 'connect',
    params: [],
})

signTransaction

Signs a serialized transaction. Takes the transaction as a hex string and returns the signed transaction.

Parameters

  • <object>

    • message <string> - A transaction serialized and converted to a hex string.

Return value

  • Promise <Transaction> - A Transaction that includes a signature.

Example

import { Transaction, SystemProgram, PublicKey } from '@solana/web3.js'

const transaction = new Transaction()
const { value } = await getBlock()
transaction.add(
  SystemProgram.transfer({
    fromPubkey: new PublicKey(selectedAccount),
    toPubkey: new PublicKey(toAddress),
    lamports: parseFloat(toAmount) * 1000000000,
  })
)

transaction.recentBlockhash = value.blockhash
transaction.feePayer = new PublicKey(selectedAccount.value)

await wepinProvider.request({
    method: 'signTransaction',
    params: { message: _toHexString(transaction.serializeMessage()) },
})

signAndSendTransaction

Signs and submits the serialized transaction to the Solana network. Returns the signature of the transaction.

Parameters

  • <object>

    • message <string> - A transaction serialized and converted to a hex string.

Return value

  • Promise <object>

    • signature <TransactionSignature> - The signature of the Transaction.

Example

import { Transaction, SystemProgram, PublicKey } from '@solana/web3.js'

const transaction = new Transaction()
const { value } = await getBlock()
transaction.add(
  SystemProgram.transfer({
    fromPubkey: new PublicKey(selectedAccount),
    toPubkey: new PublicKey(toAddress),
    lamports: parseFloat(toAmount) * 1000000000,
  })
)

transaction.recentBlockhash = value.blockhash
transaction.feePayer = new PublicKey(selectedAccount.value)

await wepinProvider.request({
    method: 'signAndSendTransaction',
    params: { message: _toHexString(transaction.serializeMessage()) },  //The return value of transaction.serializeMessage() is a Buffer, which needs to be converted to a hex string.
})

signMessage

Signs a message with a specified account's address (Public Key). Takes the account address and the message to be signed as input.

Parameters

  • <array>

    • <string> - The address (Public Key) of the account that signs the transaction.

    • <string> - The message to be signed.

Return value

  • Promise <Transaction> - A Transaction that includes a signature.

Example

await wepinProvider.request({
    method: 'signMessage',
    params: [selectedAccount, data],
})

changeNetwork

Switches the network. Supports switching between Solana Mainnet and Devnet, and returns the address, network name, and chain ID of the switched network.

Parameters

  • <object>

    • chainId <string> - The chainId of the network to switch to. Only Solana chains (solana , solana ) are supported.

Return value

  • Promise <object>

    • address <string> - The account address (Public Key) on the switched network.

    • network <string> - The name of the switched network.

    • chainId <string> - The chain ID of the switched network.

Example

await wepinProvider.request({
    method: 'changeNetwork',
    params: [{ chainId: 'solana:devnet' }],
})

Other Method Example

In addition to the methods provided by Wepin, Solana RPC HTTP methods are also available.

Returns all information associated with the account of provided Pubkey

Parameters

  • PubKey <string> - Address of the account to retrieve (encoded in base-58).

  • <object>

    • commitment <string> optional

      • Default Commitment - finalize

      • processed - Fetches the latest block processed by the node, which is not yet finalized and may change.

      • confirmed - Fetches the latest block approved by a majority in the cluster.

      • finalized - Fetches the latest block finalized by a majority in the cluster.

    • encoding <string> optional

      • Encoding format for account data.

      • base58, base64, base64+zstd, jsonParsed

    • dataSlice <object> optional - Request a slice of the account's data. Used only with base58, base64, or base64+zstd encoding.

      • length <usize> - Number of bytes to return.

      • offset <usize> - Byte offset to start reading from.

    • minContextSlot <number> optional

      • Minimum slot for executing the request.

Return value

  • context <object>

    • apiVersion <string> - solana-core version

    • slot <number> - Slot at which the operation was executed.

  • value <object> nullable

    • lamports <u64> - Account balance in lamports.

    • owner <string> - Address of the program managing this account (encoded in base-58).

    • data <[string, encoding] | object> - Data associated with the account.

    • executable <boolean> - Boolean indicating if the account contains a program (read-only).

    • rentEpoch <u64> - the epoch at which this account will next owe rent, as u64

    • size <u64> - Size of the account data.

Example

await wepinProvider.request({
    method: 'getAccountInfo',
    params: [
        "vines1vzrYbzLMRdu58ou5XTby4qAqVRLmqo36NKPTg"
    ],
})
//return data
{
  "context": {
    "apiVersion": "2.0.14",
    "slot": 338510838
  },
  "value": {
    "data": "",
    "executable": false,
    "lamports": 14000000000,
    "owner": "11111111111111111111111111111111",
    "rentEpoch": 18446744073709552000,
    "space": 0
  }
}

Returns the lamport balance of the account of provided Pubkey.

Parameters

  • Pubkey <string> - Address of the account to check balance (encoded in base-58).

  • <object>

    • commitment <string> optional

      • Default Commitment - finalize

      • processed - Fetches the latest block processed by the node, which is not yet finalized and may change.

      • confirmed - Fetches the latest block approved by a majority in the cluster.

      • finalized - Fetches the latest block finalized by a majority in the cluster.

    • minContextSlot <number> optional

      • Minimum slot for executing the request.

Return value

  • context <object>

    • apiVersion <string> - solana-core version

    • slot <number> - Slot at which the operation was executed.

  • value <number> - Account balance.

Example

await wepinProvider.request({
    method: 'getAccountInfo',
    params: [
        "vines1vzrYbzLMRdu58ou5XTby4qAqVRLmqo36NKPTg"
    ],
})
//return data
{
  "context": {
    "apiVersion": "2.0.14",
    "slot": 338529622
  },
  "value": 14000000000
}

Returns the latest blockhash.

Parameters

  • <object> optional

    • commitment <string> optional

      • Default Commitment - finalize

      • processed - Fetches the latest block processed by the node, which is not yet finalized and may change.

      • confirmed - Fetches the latest block approved by a majority in the cluster.

      • finalized - Fetches the latest block finalized by a majority in the cluster.

    • minContextSlot <number> optional

      • Minimum slot for executing the request.

Return value

  • context <object>

    • apiVersion <string> - solana-core version

    • slot <number> - Slot at which the operation was executed.

  • value <object>

    • blockhash <string> - a Hash as base-58 encoded string

    • lastValidBlockHeight <number> - last block height at which the blockhash will be valid

Example

await wepinProvider.request({
    method: 'getAccountInfo',
    params: [
        "vines1vzrYbzLMRdu58ou5XTby4qAqVRLmqo36NKPTg"
    ],
})
//return data
{
    "context": {
        "apiVersion": "2.0.14",
        "slot": 338534308
    },
    "value": {
        "blockhash": "8uJtPoFrdEqxFCA4zaBxBQXosZKvWWQYcWb9kZbF2hDW",
        "lastValidBlockHeight": 326729023
    }
}

Returns all SPL Token accounts by token owner.

Parameters

  • PubKey <string> - the account address (Pubkey as base-58 encoded string)

  • <object> - Filter for SPL Token accounts to query; requires either mint or programId.

    • mint <string> - Address of a specific token’s Mint (encoded in base-58).

    • programId <string> - ID of a specific program (encoded in base-58).

  • <object>

    • commitment <string> optional

      • Default Commitment - finalize

      • processed - Fetches the latest block processed by the node, which is not yet finalized and may change.

      • confirmed - Fetches the latest block approved by a majority in the cluster.

      • finalized - Fetches the latest block finalized by a majority in the cluster.

    • minContextSlot <number> optional

      • Minimum slot for executing the request.

    • encoding <string> optional

      • Encoding format for account data.

      • base58, base64, base64+zstd, jsonParsed.

    • dataSlice <object> optional - Used only with base58, base64, or base64+zstd encoding.

      • length <usize> - Number of bytes to return.

      • offset <usize> - Byte offset to start reading from.

Return value

  • context <object>

    • apiVersion <string> - solana-core version.

    • slot <number> - Slot at which the operation was executed.

  • value <Array<object>> - Array of SPL Token account objects.

    • pubkey <string> - Address of the retrieved account.

    • account <object> - Account details.

      • lamports <u64> - Account balance in lamports.

      • data <object> - Token state data associated with the account.

        • parsed <object>

          • info <object> - Account information.

            • isNative <boolean> - Indicates if the account is native; usually false.

            • mint <string> - Address of the token’s mint associated with the account.

            • owner <string> - Owner of the token account.

            • state <string> - Token account status.

            • tokenAmount <object> - Token balance information.

              • amount <string> - Raw amount of tokens as a string, ignoring decimals.

              • decimals <number> - Number of decimals configured for token's mint.

              • uiAmount <number> - Token amount as a float, accounting for decimals. DEPRECATED

              • uiAmountString <string> - Token amount as a string, accounting for decimals.

          • type <string> - Type of info; usually account.

        • program <string> - Program to which the account belongs.

        • space <number> - Account data size in bytes.

      • executable <boolean> - Indicates if the account is executable.

      • owner <string> - Program address that owns the account.

      • rentEpoch <u64> - the epoch at which this account will next owe rent, as u64

      • size <u64> - Data size of the account.

Example

await wepinProvider.request({
    method: 'getTokenAccountsByOwner',
    params: [
      "A1TMhSGzQxMr1TboBKtgixKz1sS6REASMxPo1qsyTSJd",
      {
        "mint": "BejB75Gmq8btLboHx7yffWcurHVBv5xvKcnY1fBYxnvf"
      },
      {
        "encoding": "jsonParsed"
      }
    ]
})
//return data
{
    "context": {
      "apiVersion": "2.0.8",
      "slot": 329669901
    },
    "value": [
      {
        "account": {
          "data": {
            "parsed": {
              "info": {
                "isNative": false,
                "mint": "BejB75Gmq8btLboHx7yffWcurHVBv5xvKcnY1fBYxnvf",
                "owner": "A1TMhSGzQxMr1TboBKtgixKz1sS6REASMxPo1qsyTSJd",
                "state": "initialized",
                "tokenAmount": {
                  "amount": "10000000000000",
                  "decimals": 9,
                  "uiAmount": 10000,
                  "uiAmountString": "10000"
                }
              },
              "type": "account"
            },
            "program": "spl-token",
            "space": 165
          },
          "executable": false,
          "lamports": 2039280,
          "owner": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
          "rentEpoch": 18446744073709551615,
          "space": 165
        },
        "pubkey": "5HvuXcy57o41qtGBBJM7dRN9DS6G3jd9KEhHt4eYqJmB"
      }
    ]
}

Returns the token balance of an SPL Token account.

Parameters

  • PubKey <string> - ATA(Associated Token Account) address (encoded in base-58).

Return value

  • context <object>

    • apiVersion <string> - solana-core version

    • slot <number> - Slot at which the operation was executed.

  • value <object>

    • amount <string> - Raw amount of tokens as a string, ignoring decimals.

    • decimals <number> - Number of decimals configured for token's mint.

    • uiAmount <number> nullable - Token amount as a float, accounting for decimals. DEPRECATED

    • uiAmountString <string> - Token amount as a string, accounting for decimals.

Example

await wepinProvider.request({
    method: 'getTokenAccountBalance',
    params: [
        "7fUAJdStEuGbc3sM84cKRL6yYaaSstyLSU4ve5oovLS7"
    ],
})
{
    "context": {
      "slot": 1114
    },
    "value": {
      "amount": "9864",
      "decimals": 2,
      "uiAmount": 98.64,
      "uiAmountString": "98.64"
    }
}

For more details on Solana network providers, please refer to the link below.

Last updated