← All articles

How to trigger a transaction automatically when an event happens on another chain

Watch for the event with Lit Triggers, check it against the chain yourself inside a Lit Action, and sign the follow-up transaction with a wallet whose key stays in a secure enclave. Here is how to act on a Base event from Arbitrum.

Lit Protocol

To trigger a transaction automatically when an event happens on another chain, have a service watch for the event and run code that sends the follow-up transaction. With Lit Triggers, you name the chain, the contract, and the event signature, and every matching log runs a Lit Action. Triggers waits for confirmations before it runs the action. The action reads the log again from a chain endpoint it trusts, checks the confirmations itself, and then signs the transaction on the other chain with a Lit wallet whose key stays inside a secure enclave. A contract on the destination chain refuses to act twice on the same event.

As an example, when a Deposit event fires on Base, the action calls a contract on Arbitrum that credits the depositor.

Do not trust the event payload

Triggers passes the decoded event to the action. But anyone with the usage key can also run the action directly with a made-up event, so the action must not sign based on the payload alone. It uses the payload only to find the log, then fetches the transaction receipt from an RPC host written into its code and checks the log there.

The action

// on-deposit.js
const PKP = "0xYourLitWallet...";
const SOURCE_RPC = "https://mainnet.base.org";      // pinned source endpoint
const VAULT = "0xYourBaseDepositContract...".toLowerCase();
const DEPOSIT_TOPIC = ethers.utils.id("Deposit(address,uint256)");
const MIN_CONFIRMATIONS = 10;
const CREDITS = "0xYourArbitrumCreditsContract...";
const MAX_CREDIT = ethers.BigNumber.from("10000000000"); // 10,000 USDC

const main = async (params) => {
  const { transaction_hash, log_index } = params.event;
  const source = new ethers.providers.StaticJsonRpcProvider(SOURCE_RPC);
  const receipt = await source.getTransactionReceipt(transaction_hash);
  if (!receipt || receipt.status !== 1) return { credited: false, reason: "no receipt" };
  if ((await source.getBlockNumber()) - receipt.blockNumber < MIN_CONFIRMATIONS) throw new Error("not enough confirmations yet");

  const log = receipt.logs.find(l => l.logIndex === Number(log_index));
  if (!log || log.address.toLowerCase() !== VAULT || log.topics[0] !== DEPOSIT_TOPIC) return { credited: false, reason: "not a deposit" };
  const user = ethers.utils.getAddress("0x" + log.topics[1].slice(26));
  const amount = ethers.BigNumber.from(log.data);
  if (amount.gt(MAX_CREDIT)) return { credited: false, reason: "over the limit" };

  const dest = new ethers.providers.StaticJsonRpcProvider("https://arb1.arbitrum.io/rpc");
  const wallet = new ethers.Wallet(await Lit.Actions.getPrivateKey({ pkpId: PKP }), dest);
  const credits = new ethers.Contract(CREDITS, ["function credit(bytes32 sourceEvent, address user, uint256 amount)"], wallet);
  const sourceEvent = ethers.utils.solidityKeccak256(["uint256", "bytes32", "uint256"], [8453, transaction_hash, log.logIndex]);
  const tx = await credits.credit(sourceEvent, user, amount);
  return { credited: true, user, amount: amount.toString(), hash: tx.hash };
};

This assumes Deposit has an indexed user and a non-indexed amount. If the deposit is not deep enough yet, the action throws, and Triggers retries the run a few times before it marks it failed. To credit it later, call the Lit API with the same code and event. For large amounts, compare against the source chain's finalized block instead of a fixed count. The limit per credit is a constant, so a bug elsewhere cannot make the action credit more than that at once.

Stopping double credits

A run can be retried after a temporary error, and anyone with the usage key can run the action again with the same event. The action does not remember past runs, so the Arbitrum contract records each source event it has credited and reverts on a repeat.

mapping(bytes32 => bool) public done;
function credit(bytes32 sourceEvent, address user, uint256 amount) external onlyCreditor {
    require(!done[sourceEvent], "already credited");
    done[sourceEvent] = true;
    balances[user] += amount;
}

Set the creditor address that onlyCreditor checks to the Lit wallet's address. The source event ID includes Base's chain ID, 8453, so it stays unique if you add a second source chain later.

Set it up

Create an account at the Lit dashboard by clicking Create an account. Create a Lit wallet, put it and this action in a group, make a usage key that can only run that group, and send the wallet a little ETH on Arbitrum for gas. Authorize Lit Triggers by following the pairing steps in the Triggers docs, then create the chain-event trigger.

jq -n --rawfile code on-deposit.js --arg key "$USAGE_KEY" \
  '{name: "base-deposits", kind: "chain_event", action_code: $code, usage_api_key: $key,
    config: {chain: "base", contract_address: "0xYourBaseDepositContract...", event_signature: "Deposit(address,uint256)"}}' \
  | curl -fsS -X POST https://triggers.litprotocol.com/api/triggers \
      -H "authorization: Bearer $TOKEN" -H "content-type: application/json" -d @-

Chain-event triggers can watch Ethereum, Base, Arbitrum, BNB Chain, and Polygon. The action can send its transaction to any chain it can reach over RPC.

What the setup protects

The action credits only deposits it has checked on the source chain itself, after enough confirmations, and only up to the limit in its code. The destination contract stops double credits, and the signing key never leaves the enclave. You still rely on the source RPC host you pinned, so for large amounts check the receipt against more than one host.

Get started

To create an account, go to the Lit dashboard and click Create an account. Chain-event triggers are documented at developer.litprotocol.com/lit-triggers/triggers.