import { ChipiBrowserSDK } from "@chipi-stack/backend";
const browserClient = new ChipiBrowserSDK({
apiPublicKey: process.env.VITE_CHIPI_PUBLIC_KEY, // or your framework's env var
});
async function transferTokens(
senderWallet: { publicKey: string; encryptedPrivateKey: string },
recipientAddress: string,
amount: string,
userPin: string
) {
try {
const bearerToken = await getBearerToken(); // Your auth implementation
const transferResult = await browserClient.transfer({
params: {
encryptKey: userPin,
wallet: senderWallet,
amount: amount,
token: "USDC",
recipient: recipientAddress,
},
bearerToken: bearerToken,
});
console.log('Transfer completed successfully!');
console.log('Transaction hash:', transferResult);
return transferResult;
} catch (error) {
console.error('Transfer failed:', error);
throw error;
}
}
// Usage example
async function sendUSDC(userId: string, recipientAddress: string, amount: string, pin: string) {
// First, get the user's wallet
const bearerToken = await getBearerToken();
const wallet = await browserClient.getWallet({
externalUserId: userId,
bearerToken: bearerToken,
});
// Then perform the transfer
const txHash = await transferTokens(
wallet,
recipientAddress,
amount,
pin
);
console.log(`Sent ${amount} USDC to ${recipientAddress}`);
console.log(`Transaction: https://starkscan.co/tx/${txHash}`);
return txHash;
}