0.5.2 • Published 1 year ago

volo-sdk-react-native v0.5.2

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

volo-sdk-react-native

Volo SDK

Installation

yarn add volo-sdk-react-native

Usage

Follow the instructions here to setup deeplinking in your app. Note: for the correct work of the SDK you do not have to add Universal Links/Android App Links support.

Also you need to install react-native-get-random-values.

Import the SDK and call the init function in the root of your project (App.tsx).

import {voloSDK} from 'volo-sdk-react-native'

voloSDK.init({
  deeplink: 'exampleapp://', // The deeplink your dapp can open (used to redirect back to the app from Volo Wallet)
  appName: 'Example app', // The name of your dapp
  appIcon: 'https://google.com', // The link to your dapp's icon
  storage: {
    getItem: (key) => SInfo.getItem(key, options),
    setItem: (key, value) => SInfo.setItem(key, value, options),
    deleteItem: async (key) => {
      SInfo.deleteItem(key, options)
    },
  },
})

Storage argument takes three async functions to get, set, and delete item from device storage. You need to provide the implementation yourself using storage library of your choice (e.g. AsyncStorage, MMKV, sensitive-info, etc.). This storage does not have to be secure, as we are not storing any sensitive info in it.

Now you can import and use functions and hooks.

Hooks

The SDK exposes the following hooks to connect to Volo Wallet:

HookFunctionArgumentsReturnsDescription
useConnectconnectvoidPromise<boolean> — whether the connection was acceptedUse this hook to connect to Volo Wallet and request the permissions
useExecuteMoveCallexecuteMoveCalldata: MoveCallTransactionPromise<SuiExecuteTransactionResponse> — the result of the Move CallUse this hook to execute move call
useRequestPermissionsrequestPermissionsvoidPromise<boolean> — whether the connection was acceptedThe same as connect
useSignAndExecuteTransactionsignAndExecuteTransactiontransaction: UnserializedSignableTransactionPromise<SuiExecuteTransactionResponse> — the result of Sign and Execute transactionUse this hook to sign and execute a single transaction
useSignAndExecuteMultipleTransactionssignAndExecuteMultipleTransactionstransactions: UnserializedSignableTransaction[]Promise<SuiExecuteTransactionResponse[]> — the array the results of Sign and Execute transactionsThe same as signAndExecuteTransaction, but works for multiple transactions
import {Sui} from 'volo-sdk-react-native'
// In any component
// ...
const {
  connect, // returns Promise
  cancel, // You can cancel the listener and the promise by calling this
} = Sui.useConnect()

Errors

This is a list of errors functions from hooks can throw.

import {
  CancelledError,
  ExternalError,
  InvalidUrlError,
  OpeningWalletDeeplinkError,
  SDKNotInitiatedError,
} from 'volo-sdk-react-native'
Error nameDescription
CancelledErrorThis error is thrown if you call cancel function
ExternalErrorThis error is thrown if there's an error in Volo Wallet. It contains a message with more details
InvalidUrlErrorThis error is thrown if the parsing of redirect URL failed (either JSON.parse, or some mandatory parameter is missing)
OpeningWalletDeeplinkErrorThis error is thrown if Linking.canOpenURL returns false for the link to Volo Wallet
SDKNotInitiatedErrorThis error is thrown if the SDK has not been initiated before calling the function

Functions

import {Sui} from 'volo-sdk-react-native'

const {
  getBalance,
  signMessage,
  verifyMessage,
  hasPermissions,
  getAccounts,
  disconnect,
} = Sui
FunctionArgumentsReturnsDescription
signMessagesecretKey: Uint8Array, message: stringstringSign a message with the given secret key
verifyMessagemessage: Hex, signature: Hex, publicKey: HexbooleanVerify that the given message hash corresponds to the signature and the public key
hasPermissionsvoidPromise<boolean>After you first call connect and until you call disconnect, this function will return true. Otherwise — false.
getAccountsvoidPromise<string[]>After the successful connection and after every call of the hook that redirects to Volo Wallet and back to your app, an array of user's addresses is passed and saved. This function returns this array of addresses
disconnectvoidPromise<void>Reset the saved addresses and the connection
getBalanceaddress?: stringPromise<{tokensBalance: SuiTokenDisplayObject[]; nfts: NFT[]}>Fetch the balance for the given address on the Sui network. If the address is not provided, empty arrays will be returned

For the getBalance function, the SDK provides a hook that refetches the balance every 5000 ms:

import {Sui} from 'volo-sdk-react-native'
// In any component
// ...
const {
  balance: SuiTokenDisplayObject[];
  nft: NFT[];
  isLoading: boolean;
  error: unknown;
} = Sui.useBalanceSynced(address)

If the address is not provided (or it is undefined), empty arrays for nfts and balance will be returned.


Made with create-react-native-library