Buy Tokens

Let your users purchase your app's or any other ERC-20 tokens directly inside your application. The user themselves can buy these tokens using fiat or any other token they may have, and will receive the requested token directly in their own linked wallet.

  1. First, follow the SDK setup and User Sign-in steps in the previous sections.

  2. Then, white-label the token(s) you want the users to purchase by contacting Singularity tech team and completing the integration steps. You will get the token-ids corresponding to the whitelisted tokens, which you can use in the API calls below.

Once done, you can call the following Singularity Payments APIs to initiate transactions and subscribe to the below callbacks to take action on payment completions or errors.

Buy ERC-20 token API
  1. On your UI, let the user choose the token they want to receive along with the quantity of the token desired.

  2. Important: Please make sure to mention the user's wallet address in the payload so that funds can be correctly transferred to the user's wallet. If not mentioned, the tokens will be wrongly delivered to your own registered address.

  3. Call the Singularity transaction API as follows:

const clientReferenceId = uuidv4(); //Your internal transaction reference ID

const body = {
  clientReferenceId: clientReferenceId,  
  singularityTransactionType: 'RECEIVE',
  transactionLabel: '<YOUR_LABEL>', // Short description of transaction (e.g. "Eth purchase". This will be shown to the user during payment flow.
  transactionDescription: '<Transaction_Description>', // Long description of transaction, if different from label
  transactionIconLink:
      '<URL_of_transaction_image_to_be_shown_to_the_user>', // Any public url of png or jpg image. This will be shown to the user during payment flow.
  clientReceiveObject: {
    clientRequestedAssetId: token-id,  //This is Singularity's internal ID for the particular token that the user wants to purchase. Get the full list of IDs for your app by contacting us.
    clientRequestedAssetQuantity: '5', // The number of tokens the user wants to purchase
    address: '0x17F547ae02a94a0339c4CFE034102423907c4592' // This field points to who will receive the asset. if the field is not provided, the asset will be transferred to your (merchant's) registered address, instead of the user's.
  },
  optionalAssets: [token-id2, token-id3] // Optionally, add other tokens that the user can purchase from a drop down menu that will appear during payment
};

const requestString = JSON.stringify(body)

window.SingularityEvent.transactionFlow(requestString);
Setup Subscriptions for Payments Callback

You can subscribe to the following callback events to get the status of the transaction by using the following subscribe functionality:

SingularityEvent.subscribe('SingularityEvent-onTransactionApproval', callbackFunctionWithTransactionDataAsArgument);
//eg.
//window.SingularityEvent.subscribe(
//          'SingularityEvent-onTransactionApproval',
//          data => {
//            console.log('Txn approved', JSON.parse(data));
//          }
//        );
//Event when a transaction is approved by the user but has not completed. At this time you may close the Singularity drawer (using APIs detailed above) and show something like a spinner telling the user that you are waiting for the payment confirmation. The transactionData will contain the details of the transaction approved.

SingularityEvent.subscribe('SingularityEvent-onTransactionSuccess', callbackFunctionWithTransactionDataAsArgument);
//Event when a transaction is successfully completed by the user. The transactionData will contain the details of the transaction completed.

SingularityEvent.subscribe('SingularityEvent-onTransactionFailure', callbackFunctionWithTransactionDataAsArgument);
//Event when a transaction fails. The transactionData will contain the details of the transaction failed.

Sample Implementation

https://github.com/coinbrix/demo-app/

Last updated