import { ChipiServerSDK } from "@chipi-stack/backend";
const serverClient = new ChipiServerSDK({
apiPublicKey: process.env.CHIPI_PUBLIC_KEY!,
apiSecretKey: process.env.CHIPI_SECRET_KEY!,
});
async function executeContractCall(
wallet: { publicKey: string; encryptedPrivateKey: string },
contractAddress: string,
methodName: string,
parameters: any[],
userPin: string
) {
try {
const result = await serverClient.callAnyContract({
params: {
encryptKey: userPin,
wallet: wallet,
calls: [
{
contractAddress: contractAddress,
entrypoint: methodName,
calldata: parameters,
}
],
},
});
console.log('Contract call successful!');
console.log('Transaction hash:', result);
return result;
} catch (error) {
console.error('Contract call failed:', error);
throw error;
}
}
// Usage example - Approve tokens for spending
async function approveTokenSpending(
userId: string,
tokenContract: string,
spenderAddress: string,
amount: string,
pin: string
) {
// Get user's wallet
const wallet = await serverClient.getWallet({
externalUserId: userId,
});
// Call the approve method on the token contract
const txHash = await executeContractCall(
wallet,
tokenContract,
"approve",
[spenderAddress, amount, "0"], // StarkNet uses felt252 for amounts
pin
);
console.log(`Approved ${amount} tokens for ${spenderAddress}`);
return txHash;
}