Example Codes in React & TypeScript

In this Section, you will see examples of how to use the functions provided by the SDK to integrate authentication, user management, and Movement transaction execution into your React project. All the example uses TypeScript. However, the SDK can also be used with React and JavaScript in the same way, as shown in the examples.

Functions Overview

1. Authenticating user with Google Sign-In

// Function Signature -

const signInWithGoogle = (network: MovementNetwork, apiKey: string, logEnabled?: boolean) => Promise<SignInWithGoogleReturn>;

This function allows users to sign in with Google using OAuthProvider.

Parameters:

  • network: Network type enum (MovementNetwork).

  • apiKey: Your API key from generated from LYNC dashboard.

  • logEnabled (Optional): Enable/disable logging. The default is true.

Return Type:

  • Promise<SignInWithGoogleReturn>:

    { success: true; user: ParsedUserData; } if successful.

    { success: false; message: string; } if unsuccessful.

Errors:

The function can throw an error if the sign-in operation fails. The error message is logged if logging is enabled.

Example:

Here is an example of a React component that imports and uses the signInWithGoogle function:

2. Signing Out a User

This function is used to sign a user out of the application. It uses Firebase's signOut function to sign out the user.

Parameters:

  • logEnabled (optional): Enable/disable logging. The default is true.

Return Type:

  • Promise<SignOutUserReturn>:

    { success: true; } if successful.

    { success: false; message: string; } if unsuccessful.

Errors:

The function can throw an error if the sign-out operation fails. The error message is logged if logging is enabled.

Example:

Here is an example of a React component that imports and uses the signOutUser function:

3. Get the details of a User

This function is an asynchronous function that fetches a user's profile from the server.

Parameters:

  • email: The email of the user.

  • network: Network type enum (MovementNetwork).

  • apiKey: Your API key from generated from LYNC dashboard.

  • logEnabled (Optional): Enable/disable logging. The default is true.

Return Type:

  • Promise<GetUserProfileReturn>:

    { success: true; data: ParsedUserData; } if successful.

    { success: false; data: string; } if unsuccessful.

Errors:

The function can throw an error if the fetch operation fails. The error message is logged if logging is enabled.

Example:

Here is an example of a React component that imports and uses the getUserProfile function:

4. Generic Transactions on Movement Networks

This is an asynchronous function that executes a generic Movement transaction.

Parameters:

  • args (MovementTransactionFuncArgs): The arguments for the generic Movement transaction. It is an object that includes the following properties:

    • email (string): The email of the user.

    • network (MovementNetwork): Network type enum (MovementNetwork).

    • apiKey: Your API key from generated from LYNC dashboard.

    • accountAddress (string): The wallet address of the currently signed-in user.

    • contractAddress (string): The contract address.

    • contractName (string): The contract name.

    • functionName (string): The function name.

    • arguments (Array): The arguments for the transaction. Each argument is an object that includes the following properties:

      • argument (string): The argument used by the function.

      • type (TransactionArgumentTypes): The type of the argument. It can be TransactionArgumentTypes.String, TransactionArgumentTypes.Number, TransactionArgumentTypes.Boolean, TransactionArgumentTypes.ByteArray, or TransactionArgumentTypes.Signature.

    • usePaymaster (Optional): Enable/disable the use of paymaster. The default is false. If you enable Paymaster the transaction will be sponsored. Also, if the wallet doesn't exist, it will be created automatically before the transaction.

  • apiKey: Your API key from generated from LYNC dashboard.

  • logEnabled (Optional): Enable/disable logging. The default is true.

Return Type:

  • Promise<MovementTransactionReturn>:

    { success: true; data: MovementTransactionData; } if successful.

    { success: false; data: string; } if unsuccessful.

Errors:

The function can throw an error if the transaction fails. The error message is logged if logging is enabled.

Example:

Here is an example of a React component that imports and uses the movementTransaction function to perform a simple movement transaction:

Last updated