Express / Node JS

Set Up Your Project

Start by creating a new directory for your project if you haven't already:

mkdir my-web3-project
cd my-web3-project

Initialize a new Node.js project:

npm init -y

Install the Required Dependencies

Additionally, we'll use the dotenv package to manage environment variables.

Install the required dependencies:

npm install @oneramp/sdk ethers dotenv

Create a .env File

Create a .env file in your project's root directory to store sensitive information like your private key and OneRamp credentials. Replace the values with your actual credentials:

PRIVATEKEY=your_ethereum_private_key
MUMBAI_PROVIDER="MUMBAI RPC"

Write the Integration Code

Create a JavaScript file (e.g, oneramp-integration.js):

// Import necessary modules
const { OneRamp } = require('@oneramp/sdk')
const { ethers } = require('ethers')

require('dotenv').config()

// Load private key from environment variables
const { PRIVATEKEY: privateKey, MUMBAI_PROVIDER } = process.env

async function main() {
  try {
    const result = await oneRamp.offramp('usdt', 5, 'RECEIVER_MOBILE_MONEY_NUMBER')

    console.log('====================================')
    console.log(result)
    console.log('====================================')
  } catch (error) {
    console.log(error)
  }
}

Before you offramp your users with real money, it's advisable to show them a price quote of the amount they’re going to receive. Fortunately, oneramp has a .quote() returns a specific quote amount that the user will receive in their wallet.

Run the scrit using:

node oneramp-integration.js

Output:

====================================
{
  recives: 4.9,
  estimated_fee: 0.1,
  amount: 5,
  asset: 'usdt',
  memo: 'Prices may vary with local service providers'
}
====================================

This output represents the quote for offramping 5 USDT to mobile money.

Now that we've got the quote too, we can go ahead and offramp the user to mobile money.

async function main() {
  try {
    const result = await oneRamp.offramp('usdt', 5, '25677280949')

    console.log('====================================')
    console.log(result)
    console.log('====================================')
  } catch (error) {
    console.log(error)
  }
}

Output:

====================================
{
  success: true,
  response: {
    store: '650837563f03d6d25a857075',
    txHash: '0x0f63fd8ffb0ef4c26ba6752e31f102e409ea8baf7e109a976a8c128ddb74e75e',
    amount: 5,
    fiat: 18715.3,
    phone: '25677280949',
    asset: 'usdt',
    network: 'mumbai',
    status: 'Pending',
    env: 'DEV',
    createdAt: '2023-09-22T10:02:50.613Z',
  }
}
====================================

The transaction should be processed, and you will receive a response indicating the success or failure of the offramp transaction.

Full code:

// Import necessary modules
const { OneRamp } = require('@oneramp/sdk')
const { ethers } = require('ethers')

require('dotenv').config()

// Load private key from environment variables
const { PRIVATEKEY: privateKey, MUMBAI_PROVIDER } = process.env

// Create an ethers provider that connects to the Alfajores testnet
const provider = new ethers.providers.JsonRpcProvider(MUMBAI_PROVIDER)

// Create a wallet using the private key and the provider
const signer = new ethers.Wallet(privateKey, provider)

// Application keys from https://dashboard.oneramp.io
const clientPub = 'YOUR_CLIENT_PUBLIC_KEY'
const secretKey = 'YOUR_SECRET_KEY'

const oneRamp = new OneRamp('mumbai', clientPub, secretKey, provider, signer)

async function quote() {
  try {
    const quote = await oneRamp.quote(5, 'usdt')

    console.log('====================================')
    console.log(quote)
    console.log('====================================')
  } catch (error) {
    console.log(error)
  }
}

async function main() {
  try {
    const result = await oneRamp.offramp('usdt', 5, 'RECEIVER_MOBILE_MONEY_NUMBER')

    console.log('====================================')
    console.log(result)
    console.log('====================================')
  } catch (error) {
    console.log(error)
  }
}

quote();

Last updated