Solana Provider

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

Supported Networks

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

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.value?.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

await wepinProvider.value?.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

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

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.value?.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.value?.request({
    method: 'changeNetwork',
    params: [{ chainId: 'solana:devnet' }],
})

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

Last updated