Using Solong in Dapp

Dapps can use ‘solong’ object which injected to window to interact with solong extension

A typical process acts like this:

  • S1: Check user has installed SolongExtension

1. Check user has installed SolongExtension

Dapps can directly check the global “solong” object is existed or not,if yes,indicates that the user has installed solong,otherwise, can take the user to chrome extension store for install.

if (!solong) { console.log("please install Solong Extension from chrome") }

solong equals to window.solong in explorer, we suggested always do the check before your actions,

2. Authorize user’s account

Dapps can provide a button,like “Connect” then call API ‘selectAccount’, like the following code:

solong.selectAccount().then(account) => { console.log("connect account with ", account) } )

if this site is already authorized for the current account,then can get the account right after the call, or solong will pop up a window for user’s authorization:

After the authorization,we get the account:

select account: 9HAjDw4onTxxxxxxxxxxxxxxxxHNigen

The “selectAccount” api will return a Promise ,which resolve with the user’s account

3. Transfer

For Dapp want to do transfer,it can call api “transfer” like:

async transfer(to, amount)

it also returns a Promise ,if it resolves with no error, the transfer is successful. Param ‘to’ is the receiver’s address, ‘amount’ is the amount of assets you want to transfer

Attention: amount is not lamport here

solong.transfer('HSfwVfB7RUF1SKCd4yrz8KZp7TU262Y5BeZZN1tdCTVk', 0.1).then((err)=>{ if (!err){ console.log("transfer error:", err) } else { console.log("transfer success") } })

After the api call,solong will pop up this window to user for transfer confirmation:

After the confirmation,the transfer is done

4. Sign Transaction

For most use cases for dapps is to use solong’s api to signTransaction to sign transaction,after the sign. You can call api sendTransaction of "@solana/web3.js"' to Solana nodes.

async signTransaction(transaction)

Before the api call,you should construct a transaction first, take the following code as an example.

const trxi = Token.createTransferInstruction( new PublicKey('TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA'), new PublicKey('6xe4WTMLrcrxae196V4PLhNpqWgzmFqJw1CLVvWHkw4U'), new PublicKey('CMaGy8RNmNx5zZ1cdaV4ajtSKNZQKQz2VqAGdmkBdz1B'), new PublicKey('HSfwVfB7RUF1SKCd4yrz8KZp7TU262Y5BeZZN1tdCTVk'), [], 100000 ) const trx= new Transaction().add(trxi); trx.recentBlockhash = ( await connection.getRecentBlockhash('max') ).blockhash trx.signPartial(new PublicKey(this.currentAccount)) let signedTrx = await solong.signTransaction(trx) connection.sendRawTransaction(signedTrx.serialize()).then((rst) => { console.log('send raw transaction:', rst) })

We also make use of Token from spl-token:

import {Token} from '@solana/spl-token'

We use Token to construct a TransferInstruction, then add it to Transaction . After setting recentBlockhash and signPartial,we can call solong.signTransaction to pop up the window for user to sign:

After confirmation,we can call transaction’s serialize() function to generate RawTransaction for sending to api node.

solong wallet