Overview
The Account API provides operations for managing user accounts and predicting account addresses. This is essential for getting the smart contract wallet address before any transactions are made.
Import
import { accountApi } from '@/lib/api/account';
Functions
predictAddress
Predicts the smart contract wallet address for a given session and admin address.
accountApi.predictAddress(
sessionAddress: string,
adminAddress: string
): Promise<string>
The session address from the embedded wallet
The admin address (typically the same as session address for most users)
Returns: Promise<string> - The predicted account address
Example
import { accountApi } from '@/lib/api/account';
async function getPredictedAddress(walletAddress: string) {
try {
const predictedAddress = await accountApi.predictAddress(
walletAddress,
walletAddress
);
console.log('Predicted address:', predictedAddress);
return predictedAddress;
} catch (error) {
console.error('Failed to predict address:', error);
throw error;
}
}
Usage in Application
The predicted address is used throughout the application for:
- Balance queries - Fetch user token balances before any transactions
- Quote requests - Include the account address in swap/transfer quotes
- Transaction execution - Sign and execute transactions with the predicted address
Integration with useQuotes Hook
import { useQuotes } from '@/lib/hooks';
import { accountApi } from '@/lib/api/account';
import { usePrivy } from '@privy-io/react-auth';
function SwapComponent() {
const { user } = usePrivy();
const embeddedWallet = user?.linkedAccounts?.find(
account => account.type === 'wallet' && account.walletClient === 'privy'
);
const [predictedAddress, setPredictedAddress] = useState<string>('');
useEffect(() => {
if (embeddedWallet?.address) {
accountApi.predictAddress(
embeddedWallet.address,
embeddedWallet.address
).then(setPredictedAddress);
}
}, [embeddedWallet]);
// Use predictedAddress for quotes and transactions
}
API Endpoint
POST /account/predict-address
Request Body:
{
"sessionAddress": "0x1234...",
"adminAddress": "0x1234..."
}
Response:
{
"predictedAddress": "0x5678..."
}
Error Handling
try {
const address = await accountApi.predictAddress(sessionAddress, adminAddress);
} catch (error) {
if (error.response?.status === 400) {
console.error('Invalid address format');
} else if (error.response?.status === 500) {
console.error('Server error predicting address');
}
}