Generate Wallet

Before getting around to generating wallets for your users, it is assumed you already have your platform's authentication system and user management system in place. This is important as you will need to associate the wallets you create with your users using, in this case, an appID that serves as a linker to a specific user and an auth-token assigned after user authentication.

Generating Wallets

To generate a new wallet for a user, you will need to make a request to the following GraphQL endpoint:

https://graph.100pay.co/graphql

The request to be constructed will be in the form of the following GraphQL query:

const WALLET_QUERY = `
mutation CreateUserWallet($symbol: String!, $appId: String) {
  createUserWallet(symbol: $symbol, appId: $appId) {
    name
    symbol
    status
    decimals
    logo
    account {
      address
    }
    balance {
      available
      locked
    }
  }
}
`;

In the query above, the symbol field represents the cryptocurrency wallet you want to create. The appId field is the unique identifier of the user you want to create the wallet for.

Required Fields

FieldTypeDescription
symbolStringThe symbol of the cryptocurrency you want to create a wallet for.
appIdStringThe unique identifier of the user you want to create the wallet for.

Sending the Request

To send the request, you can create a function that executes the query in any language of your choice. Below is an example of how you can do this in TypeScript:

// Wallet state
import { Wallet } from "@/types/Wallet";
// helper function to make graphql requests
import graphqlRequest from "@/utils/graphqlRequest";

// function to get wallet
const createUserWallet = async ({
  token,
  symbol,
  appId,
}: {
  token?: string;
  symbol: string;
  appId: string;
}) => {

  const headers = token ? { "auth-token": `${token}` } : undefined;

  // make a request to the graphql server
  const data = await graphqlRequest<{ createUserWallet: Wallet }>(
    GRAPHQL_API || "",
    {
      query: WALLET_QUERY,
      variables: { symbol, appId },
    },
    headers
  );

  return data;
};

In the code block above, the createUserWallet function is defined to make a request to the GraphQL server using the query variable we created earlier. The function takes in the auth-token of the user making the request, the symbol of the cryptocurrency wallet to be created, and the appId of the user the wallet is being created for. Using these, a wallet of choice is created for the user.

If you are also using TypeScript/JavaScript, and you are curious about the graphqlRequest helper function, here is a glimpse of what it contains:

// utils/graphqlRequest.ts
import { GraphQLRequestOptions, GraphQLResponse } from "@/types/graphql";

// Function to make a GraphQL request
const graphqlRequest = async <T>(
  url: string,
  options: GraphQLRequestOptions,
  headers?: Record<string, string>
): Promise<GraphQLResponse<T>> => {
  try {
    // Send a POST request to the specified URL with the provided options
    const response = await fetch(url, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        ...headers,
      },
      body: JSON.stringify(options),
    });

    // Check if the response is not successful (status code other than 2xx)
    if (!response.ok) {
      throw new Error(
        `Network error: ${response.status} - ${response.statusText}`
      );
    }

    // Parse the response body as JSON
    const responseBody: GraphQLResponse<T> = await response.json();

    // Return the parsed response body
    return responseBody;
  } catch (error: any) {
    // Handle any errors that occur during the request
    console.error(error || "An error occurred");

    // Return a GraphQLResponse object with undefined data and the error message
    return {
      data: undefined,
      errors: [error?.message || "An error occurred"],
    };
  }
};

export default graphqlRequest;

The graphqlRequest function is a helper function that sends a POST request to the specified URL with the provided options. It also handles any errors that may occur during the request and returns a GraphQLResponse object with the data and any errors encountered.

The type definition for GraphQLRequestOptions and GraphQLResponse are shown below:

// graphql request options type
// this is the type of the options parameter in the graphqlRequest function
export interface GraphQLRequestOptions {
  query: string;
  variables?: Record<string, any>;
}

// graphql response type
// this is the type of the response body in the graphqlRequest function
export interface GraphQLResponse<T> {
  data?: T;
  errors?: GraphQLError[];
}

Summary

Completato. With this you should be able to generate crypto wallets for your platform users. Creating wallets for your users is a crucial part of your platform's functionality. By following the steps outlined above, you can easily generate wallets for your users and associate them with their accounts using the appId and auth-token fields. This way, users can have a seamless experience managing their cryptocurrency assets on your platform.