# Internals

The `PaymentUtils` class provides several utility functions for handling USDT transfers, placing buy orders for UBIT, and interacting with the UBIT testnet and mainnet. Here is a detailed explanation of each function within the class:

#### 1. `mainnetUSCTransfer`

This function coordinates a series of actions to transfer USDT, place a UBIT buy order, and withdraw funds to a user.

**Steps:**

1. **Get Deposit Address:**
   * Calls `getDepositAddress` to get a deposit address for the specified amount and address.
   * If the address is `null`, it throws an exception.
2. **Transfer USDT:**
   * Calls `USDTTransfer` to transfer the specified amount of USDT to the retrieved deposit address.
3. **Place UBIT Buy Order:**
   * Calls `placeUBITBuyOrder` to place a buy order for UBIT with the specified amount.
4. **Withdraw to User:**
   * Calls `withdrawToUser` to withdraw the specified amount to the user's address.

#### 2. `USDTTransfer`

This function handles the transfer of USDT from one address to another using the Ethereum blockchain.

**Steps:**

1. **Set Up Provider and Wallet:**
   * Retrieves the private key and RPC URL from environment variables.
   * Creates a provider and a wallet using the ethers library.
2. **Define Transfer ABI:**
   * Defines the ABI for the `transfer` function of the USDT contract.
3. **Create Contract Instance:**
   * Creates an instance of the USDT contract using the ABI and the provider's signer.
4. **Transfer Tokens:**
   * Calls the `transfer` function of the USDT contract to transfer the specified amount of USDT (converted to the appropriate token decimals) to the specified address.
   * Logs the transfer result or error.

#### 3. `withdrawToUser`

This function withdraws funds to a user's address using a specific API.

**Steps:**

1. **Prepare Payload and Headers:**
   * Creates a payload with the withdrawal details.
   * Creates a signature for the payload using HMAC-SHA256 with a secret key.
   * Sets up headers with the API key, signature, expiration time, and other necessary headers.
2. **Make API Call:**
   * Sends a POST request to the withdrawal API with the payload and headers.
   * Logs the response data or error.

#### 4. `getDepositAddress`

This function retrieves a deposit address for the specified amount and address.

**Steps:**

1. **Prepare Payload and Headers:**
   * Creates a payload with the deposit details.
   * Creates a signature for the payload using HMAC-SHA256 with a secret key.
   * Sets up headers with the API key, signature, expiration time, and other necessary headers.
2. **Make API Call:**
   * Sends a POST request to the deposit address API with the payload and headers.
   * Sets the `addr` variable to the retrieved address from the response data.
   * Logs any errors.
3. **Return Address:**
   * Returns the retrieved deposit address.

#### 5. `placeUBITBuyOrder`

This function places a buy order for UBIT using a specific API.

**Steps:**

1. **Prepare Payload and Headers:**
   * Creates a payload with the buy order details.
   * Creates a signature for the payload using HMAC-SHA256 with a secret key.
   * Sets up headers with the API key, signature, expiration time, and other necessary headers.
2. **Make API Call:**
   * Sends a POST request to the order placement API with the payload and headers.
   * Logs the response data or error.

#### 6. `testnetUSCTransfer`

This function handles the transfer of USC on the UBIT testnet.

**Steps:**

1. **Set Up Provider and Wallet:**
   * Retrieves the private key and RPC URL from environment variables.
   * Creates a provider and a wallet using the ethers library.
2. **Create Transaction:**
   * Creates a transaction object with the specified address, amount (converted to the appropriate token decimals), and gas limit.
3. **Send Transaction:**
   * Sends the transaction using the wallet.
   * Waits for the transaction to be mined and logs the receipt.
   * Returns the transaction hash.

#### Error Handling

* The `HttpException` class is used to handle errors and provide appropriate status codes and messages.
* Errors are logged and rethrown where necessary to ensure proper error handling and feedback.

#### Environment Variables

* `ESCROW_PRIV_KEY`: Private key for the wallet used in transfers.
* `UBIT_TESTNET_RPC`: RPC URL for the UBIT testnet.
* `WITHDRAW_API`: API endpoint for withdrawals.
* `COINSTORE_API_KEY`: API key for the Coinstore platform.
* `COINSTORE_SECRET_KEY`: Secret key for the Coinstore platform.
* `DEPOSIT_ADDR`: API endpoint for getting deposit addresses.
* `ORDER_PLACE`: API endpoint for placing orders.
