Usage

const { 
  callAsync, 
  callData, 
  isLoading, 
  error 
} = useCallAnyContract();

Parameters

  • encryptKey (string): User’s decryption PIN
  • wallet (WalletData): Wallet credentials
  • contractAddress (string): Target contract address
  • entrypoint (string): Contract method name
  • calldata (any[]): Arguments for the contract method

Return Value

Returns an object containing:

  • callAsync: Function to trigger contract call
  • callData: Transaction hash of the call
  • isLoading: Boolean indicating if the operation is in progress
  • error: Any error that occurred during the process

Example Implementation

'use client'

import { useCallAnyContract } from "@chipi-pay/chipi-sdk";
import { useState } from "react";

export function GenericContractForm() {
  const { callAsync, callData, isLoading, error } = useCallAnyContract();
  const [form, setForm] = useState({
    pin: '',
    contractAddress: '',
    entrypoint: '',
    calldata: ''
  });

  const handleCall = async (e: React.FormEvent) => {
    e.preventDefault();

    try {
      await callAsync({
        encryptKey: form.pin,
        wallet: {
          publicKey: "0x123...yourPublicKeyHere",
          encryptedPrivateKey: "encrypted:key:data"
        },
        contractAddress: form.contractAddress,
        entrypoint: form.entrypoint,
        calldata: JSON.parse(form.calldata || "[]")
      });
    } catch (err) {
      console.error('Contract call failed:', err);
    }
  };

  return (
    <div className="bg-white rounded-xl shadow-lg p-6">
      <h2 className="text-2xl font-bold mb-4">Contract Interaction</h2>
      
      <form onSubmit={handleCall} className="space-y-4">
        <div>
          <label className="block text-sm font-medium mb-1">Security PIN</label>
          <input
            type="password"
            value={form.pin}
            onChange={(e) => setForm({...form, pin: e.target.value})}
            className="w-full p-2 border rounded-md"
            required
          />
        </div>

        <div>
          <label className="block text-sm font-medium mb-1">Contract Address</label>
          <input
            type="text"
            value={form.contractAddress}
            onChange={(e) => setForm({...form, contractAddress: e.target.value})}
            className="w-full p-2 border rounded-md"
            required
          />
        </div>

        <div>
          <label className="block text-sm font-medium mb-1">Method Name</label>
          <input
            type="text"
            value={form.entrypoint}
            onChange={(e) => setForm({...form, entrypoint: e.target.value})}
            className="w-full p-2 border rounded-md"
            required
          />
        </div>

        <div>
          <label className="block text-sm font-medium mb-1">Call Data (JSON array)</label>
          <input
            type="text"
            value={form.calldata}
            onChange={(e) => setForm({...form, calldata: e.target.value})}
            className="w-full p-2 border rounded-md"
            placeholder='Example: [123, "0xabc...", true]'
          />
        </div>

        <button 
          type="submit" 
          disabled={isLoading}
          className="w-full bg-blue-600 text-white py-2 px-4 rounded-md hover:bg-blue-700 disabled:bg-gray-400"
        >
          {isLoading ? 'Processing...' : 'Execute'}
        </button>
      </form>

      {callData && (
        <div className="mt-4 p-3 bg-gray-50 rounded-md">
          <p className="text-sm font-mono break-all">
            TX Hash: {callData}
          </p>
        </div>
      )}

      {error && (
        <div className="mt-4 p-3 bg-red-50 text-red-700 rounded-md">
          Error: {error.message}
        </div>
      )}
    </div>
  );
}

Security Considerations

  • Verify contract addresses
  • Validate calldata format
  • Use encrypted private keys
  • Implement proper PIN validation
  • Review contract ABI before calling

Error Handling

  • Handle invalid contract addresses
  • Validate calldata format
  • Monitor gas fees
  • Implement retry logic for failed calls

Use with caution. Direct contract calls require deep understanding of the target contract’s ABI and security implications.