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.

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.

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)

Last updated