Wallet Transfer API
This API endpoint allows users to transfer funds from their wallet to another address. It handles the authentication, retrieves the user's wallet information, and executes the transaction on the Polygon network.
Endpointβ
POST /api/wallet/transfer
Authenticationβ
This endpoint requires authentication. Ensure that the user is logged in and has a valid session.
Request Bodyβ
Parameter | Type | Description |
---|---|---|
toAddress | string | The recipient's wallet address |
amount | string | The amount of MATIC to transfer |
Responseβ
The API returns a JSON object with the following structure:
{
txHash: string;
}
Where txHash
is the transaction hash of the completed transfer.
Usage Example
Transfer 1 MATIC to another address:
const response = await fetch('/api/wallet/transfer', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
toAddress: '0x742d35Cc6634C0532925a3b844Bc454e4438f44e',
amount: '1',
}),
});
const data = await response.json();
console.log('Transaction hash:', data.txHash);
Error Responsesβ
Usage Notes
- The
amount
should be specified in MATIC, not in Wei. The API will convert it to Wei before sending the transaction. - Ensure that the user has sufficient balance to cover both the transfer amount and the gas fees.
- The API uses the user's encrypted private key stored in the database to sign the transaction.
- The transaction is sent to the Polygon network using the RPC URL specified in the environment variables.
Best Practices
- Always validate the
toAddress
on the client-side to ensure it's a valid Ethereum address. - Implement a confirmation step in your UI before sending the transaction to prevent accidental transfers.
- Display a loading indicator while waiting for the transaction to be mined, as it may take several seconds.
- Store the returned transaction hash and use it to check the transaction status or provide a link to a block explorer.
Limitations
- This API currently only supports transfers on the Polygon network.
- The maximum amount that can be transferred is limited by the user's balance and the network's transaction size limits.
- Gas fees are automatically calculated and deducted from the user's balance.
Security Considerations
- Never expose the user's private key in logs, error messages, or client-side code.
- Implement rate limiting to prevent abuse of the API.
- Consider implementing additional security measures such as 2FA or email confirmations for large transfers.
Stay tuned for updates and feel free to provide feedback for improvements!