> ## Documentation Index
> Fetch the complete documentation index at: https://docs.polygon.technology/llms.txt
> Use this file to discover all available pages before exploring further.

# Run a contract call on arrival

> Run a contract call, such as a vault deposit or a purchase, with the funds as they arrive, so the customer confirms once.

This guide shows you how to attach a contract call to an intent. When funds reach the destination, OMS calls your contract with them in the same intent. The customer confirms once, and there is no second transaction for the deposit, mint, or purchase. You need an OMS publishable key (`pk_...`) and the [SDK installed](https://docs.polygon.technology/cross-chain/sdk/index.md).

## Encode the call with the arrived amount

The amount that arrives can differ slightly from the quote because of fees and price movement. Put `TRAILS_HYDRATE_PLACEHOLDER_AMOUNT` wherever the amount belongs in your call. At execution, OMS replaces it with the exact amount that arrived.

```typescript theme={null}
import { TRAILS_HYDRATE_PLACEHOLDER_AMOUNT } from "0xtrails";
import { encodeFunctionData, erc4626Abi } from "viem";

const calldata = encodeFunctionData({
  abi: erc4626Abi,
  functionName: "deposit",
  args: [TRAILS_HYDRATE_PLACEHOLDER_AMOUNT, customerAddress],
});
```

If your call takes a fixed amount, such as a purchase at a set price, encode the real value and use an exact-output flow so that amount arrives.

## Attach it in the widget

Pass the calldata as `to.calldata`, with your contract as `to.recipient`. `Fund`, `Pay`, `Earn`, and `Swap` all accept it.

```tsx theme={null}
import { Fund } from "0xtrails/widget";

<Fund
  apiKey="pk_..."
  to={{
    recipient: "0xVAULT_ADDRESS",
    token: "USDC",
    chain: "polygon",
    calldata,
  }}
  onFundingSuccess={({ intentId }) => recordDeposit(intentId)}
/>
```

## From your server

Set `destinationToAddress` to your contract and `destinationCallData` to the encoded call. If the contract pulls tokens with `transferFrom`, also set `destinationApproveAddress` to the contract.

```typescript theme={null}
import { TradeType } from "@0xtrails/api";

const { intent } = await intents.quoteIntent({
  ownerAddress: customerAddress,
  originChainId: 1,
  originTokenAddress: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
  originTokenAmount: 100_000_000n,
  destinationChainId: 137,
  destinationTokenAddress: "0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359",
  destinationToAddress: "0xVAULT_ADDRESS",
  destinationApproveAddress: "0xVAULT_ADDRESS",
  destinationCallData: calldata,
  tradeType: TradeType.EXACT_INPUT,
});
```

Then deposit and execute as in [quotes and execution](https://docs.polygon.technology/cross-chain/api-reference/quotes.md).

## Common destinations

* **Vault deposits**: ERC-4626 `deposit(assets, receiver)`, with the customer as `receiver`. See [earn](https://docs.polygon.technology/cross-chain/earn.md).
* **Lending supply**: a lending pool's `supply` call on behalf of the customer.
* **Purchases and mints**: a contract call priced in the destination token, used with [checkout](https://docs.polygon.technology/cross-chain/checkout.md).

If the destination call fails, inspect the intent receipt for a `refundTransaction`. When funds remain at an intent address without a refund, the owner can use the [recovery flow](https://docs.polygon.technology/cross-chain/sdk/hooks.md#recover-stuck-funds).
