# Router Calls

The base `userCmd()` treates the calling address (`msg.sender`) as the owner of the call for crediting/debiting the output/input of transactions and assigning ownership of LP positions and surplus collateral. However Ambient supports allowing for a third-party smart contract (*router*) to execute arbitrary commands on the user's behalf.&#x20;

### Invocation

In most cases whatever the user wants to do can be accomplished by directly calling the `CrocSwapDex` contract. But Ambient also allows users to authorize an intermediate smart contract to call Ambient on their behalf. Router calls on behalf of users will execute as if a user had directly called `userCmd` on the Ambient contract. The method to do this is:

```
function userCmdRouter (
  uint16 callpathIdx, 
  bytes calldata cmd, 
  address client)
  returns (bytes memory)
```

`callpathIdx` and `cmd` are the same parameters that would be passed to the standard `userCmd` and will behave identically.

`client` is the address of the end user the router is calling on behalf.&#x20;

### Approval

Before a router call is invoked, the user ***must*** previously approve the specific smart contract address making the call. Otherwise the contract call will revert. The user must either directly call the approve command.

```
userCmd(0, abi.encode(
  72,         // Fixed initPool subcode  
  router,     // address
  nCalls,     // uint32
  callpaths,  // uint16[]
)
```

`nCalls` is the number of calls the router is approved to make on a user's behalf. Setting this value to the 32-bit maximum will approve the router for an unlimited number of calls (which reduces the gas overhead from decrementing a router's call count). To revoke approval, this value can be set to 0.

`callpaths` are the proxy index calls the router contract is approved for. For example to only approve the router contract to perform swaps this value would be set to `[1]` (the swap proxy contract index)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.ambient.finance/developers/dex-contract-interface/router-calls.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
