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
mainnetUSCTransfer
This function coordinates a series of actions to transfer USDT, place a UBIT buy order, and withdraw funds to a user.
Steps:
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.
Transfer USDT:
Calls
USDTTransfer
to transfer the specified amount of USDT to the retrieved deposit address.
Place UBIT Buy Order:
Calls
placeUBITBuyOrder
to place a buy order for UBIT with the specified amount.
Withdraw to User:
Calls
withdrawToUser
to withdraw the specified amount to the user's address.
2. USDTTransfer
USDTTransfer
This function handles the transfer of USDT from one address to another using the Ethereum blockchain.
Steps:
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.
Define Transfer ABI:
Defines the ABI for the
transfer
function of the USDT contract.
Create Contract Instance:
Creates an instance of the USDT contract using the ABI and the provider's signer.
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
withdrawToUser
This function withdraws funds to a user's address using a specific API.
Steps:
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.
Make API Call:
Sends a POST request to the withdrawal API with the payload and headers.
Logs the response data or error.
4. getDepositAddress
getDepositAddress
This function retrieves a deposit address for the specified amount and address.
Steps:
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.
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.
Return Address:
Returns the retrieved deposit address.
5. placeUBITBuyOrder
placeUBITBuyOrder
This function places a buy order for UBIT using a specific API.
Steps:
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.
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
testnetUSCTransfer
This function handles the transfer of USC on the UBIT testnet.
Steps:
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.
Create Transaction:
Creates a transaction object with the specified address, amount (converted to the appropriate token decimals), and gas limit.
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.
Last updated