# LibCommand

```solidity
enum MathOperation {
    None,
    Add,
    Sub,
    Mul,
    Div,
    Pow,
    Abs128,
    Abs256,
    Shr,
    Shl
}


```

<table><thead><tr><th>Field</th><th>Description</th></tr></thead><tbody><tr><td><pre><code>None
</code></pre></td><td>A default operation indicating no mathematical operation is to be performed or possibly used as a placeholder.</td></tr><tr><td><pre><code>Add
</code></pre></td><td>Represents addition, the operation of adding two numbers together to find their sum.</td></tr><tr><td><pre><code>Sub
</code></pre></td><td>Represents subtraction, the operation of taking one number away from another to find the difference.</td></tr><tr><td><pre><code>Mul
</code></pre></td><td>Represents multiplication, the operation of scaling one number by another to find the product.</td></tr><tr><td><pre><code>Div
</code></pre></td><td>Represents division, the operation of determining how many times one number is contained within another to find the quotient.</td></tr><tr><td><pre><code>Pow
</code></pre></td><td>Stands for power, an operation that raises a number to the exponent of another number, essentially multiplying a number by itself a specified number of times.</td></tr><tr><td><pre><code>Abs128
</code></pre></td><td>Likely represents an operation to find the absolute value of a number within a 128-bit context, ensuring the result is positive or zero.</td></tr><tr><td><pre><code>Abs256
</code></pre></td><td>Similar to <code>Abs128</code>, this likely refers to finding the absolute value of a number within a 256-bit context, which allows for handling larger numbers.</td></tr><tr><td><pre><code>Shr
</code></pre></td><td>Stands for "shift right", a bitwise operation that shifts all bits in a binary representation of a number to the right by a specified number of positions, effectively dividing the number by a power of two.</td></tr><tr><td><pre><code>Shl
</code></pre></td><td>Stands for "shift left", a bitwise operation that shifts all bits in a binary representation of a number to the left by a specified number of positions, effectively multiplying the number by a power of two.</td></tr></tbody></table>

```solidity
enum CommandAction {
    Call,
    Approval,
    TransferFrom,
    Transfer,
    Wrap,
    Unwrap,
    Balance,
    UniswapV2,
    UniswapV3,
    TraderJoeV2_1,
    Solidly,
    KyberSwapClassic,
    Ambient
}
```

<table><thead><tr><th>Field</th><th>Description</th></tr></thead><tbody><tr><td><pre><code>Call
</code></pre></td><td>This action representa a generic call to a function within a contract.</td></tr><tr><td><pre><code>Approval
</code></pre></td><td>Represents an approval operation.</td></tr><tr><td><pre><code>TransferFrom
</code></pre></td><td>Indicates a transfer-from operation.</td></tr><tr><td><pre><code>Transfer
</code></pre></td><td>Represents a direct transfer operation.</td></tr><tr><td><pre><code>Wrap
</code></pre></td><td>This action is used for wrapping tokens, such as converting Ether to Wrapped Ether (WETH).</td></tr><tr><td><pre><code>Unwrap
</code></pre></td><td>The opposite of Wrap, this is used for unwrapping tokens, like converting WETH back to Ether.</td></tr><tr><td><pre><code>Balance
</code></pre></td><td>to check the balance of an account or contract for a specific asset.</td></tr><tr><td><pre><code>UniswapV2
</code></pre></td><td>Represents an swap operation related to the Uniswap V2 protocol.</td></tr><tr><td><pre><code>UniswapV3
</code></pre></td><td>For swap operation specific to Uniswap V3.</td></tr><tr><td><pre><code>TraderJoeV2_1
</code></pre></td><td>Indicates swap operation related to Trader Joe V2.1.</td></tr><tr><td><pre><code>Solidly
</code></pre></td><td>Represents swap operation related to the Solidly protocol.</td></tr><tr><td><pre><code>KyberSwapClassic
</code></pre></td><td>Indicates swap operation related to KyberSwap Classic</td></tr><tr><td><pre><code>Ambient
</code></pre></td><td>Indicates swap operation related to Ambient Protocol.</td></tr></tbody></table>

```solidity
enum SequenceType {
    NativeAmount,
    Selector,
    Address,
    Amount,
    Data,
    LastAmountOut,
    RouterAddress,
    SenderAddress
}
```

<table><thead><tr><th>Field</th><th>Description</th></tr></thead><tbody><tr><td><pre><code>NativeAmount
</code></pre></td><td>The amountIn in native tokens. </td></tr><tr><td><pre><code>Selector
</code></pre></td><td>The function selector for the command.</td></tr><tr><td><pre><code>Address
</code></pre></td><td>The recipient address.</td></tr><tr><td><pre><code>Amount
</code></pre></td><td>The amount in.</td></tr><tr><td><pre><code>Data
</code></pre></td><td>The data that contains information for the command to be performed.</td></tr><tr><td><pre><code>LastAmountOut
</code></pre></td><td>The amount received after the previous swap.</td></tr><tr><td><pre><code>RouterAddress
</code></pre></td><td>The address of the protocol router used for swapping.</td></tr><tr><td><pre><code>SenderAddress
</code></pre></td><td>The address who requested the command to be performed.</td></tr></tbody></table>

```solidity
struct CommandData {
    CommandAction commandAction;
    uint8 outputType;
    uint16 inputLength;
    uint16 sequencesPosition;
    uint16 sequencesPositionEnd;
    address targetAddress;
}
```

<table><thead><tr><th>Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><pre><code>commandAction
</code></pre></td><td><pre><code>CommandAction
</code></pre></td><td>This field uses the previously defined CommandAction enum to specify the type of action this command represents.</td></tr><tr><td><pre><code>outputType
</code></pre></td><td><pre><code>uint8
</code></pre></td><td>The specific meaning of this value would depend on the context in which the CommandData struct is used.</td></tr><tr><td><pre><code>inputLength
</code></pre></td><td><pre><code>uint16
</code></pre></td><td>indicating the length of the input data for this command.</td></tr><tr><td><pre><code>sequencesPosition
</code></pre></td><td><pre><code>uint16
</code></pre></td><td>specifies the starting position of a sequence of data related to this command.</td></tr><tr><td><pre><code>sequencesPositionEnd
</code></pre></td><td><pre><code>uint16
</code></pre></td><td>marks the end position of the sequence of data.</td></tr><tr><td><pre><code>targetAddress
</code></pre></td><td><pre><code>address
</code></pre></td><td>the address of a contract or an account that is the target of this command.</td></tr></tbody></table>

### getData():

The `getData` function in Solidity is designed to extract and assemble a `CommandData` structure from transaction calldata using low-level assembly operations for efficiency. It takes a single `uint16` parameter `i`, which specifies the starting position in the calldata from where data extraction should begin.

**input**

| Field | Type   | Description                                                                    |
| ----- | ------ | ------------------------------------------------------------------------------ |
| i     | uint16 | the starting position in the calldata from where data extraction should begin. |

**output**

<table><thead><tr><th>Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td>commandData</td><td><pre><code>CommandData
</code></pre></td><td><pre><code>struct CommandData {
    CommandAction commandAction;
    uint8 outputType;
    uint16 inputLength;
    uint16 sequencesPosition;
    uint16 sequencesPositionEnd;
    address targetAddress;
}
</code></pre></td></tr></tbody></table>

### getInput():

The `getInput` function is a complex and versatile internal function designed for processing a sequence of commands encoded in a `CommandData` structure, typically used in a blockchain transaction context. It starts by initializing a dynamic `input` byte array and other local variables, including `nativeAmount`, `selector`, and various counters. The function iterates over a range of command sequences, each identified by a `SequenceType` (like `NativeAmount`, `Selector`, `Address`, etc.), and processes each sequence differently based on its type. For instance, it extracts and stores native token amounts, contract function selectors, addresses, and other data into the `input` array, adjusting the offset for each entry. The function also handles special cases, such as using the `lastAmountOut` as an input or rejecting certain types of transactions (like `transferFrom` calls in custom actions). It ensures that the length of the processed sequences matches the expected `inputLength` from `commandData`, and validates the selector for contract calls. Overall, `getInput` is a sophisticated function that dynamically constructs a transaction input from a series of encoded commands, ensuring flexibility and security in executing complex transaction sequences on the blockchain.

**Input**

<table><thead><tr><th>Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td>lastAmountOut</td><td>uint256</td><td>The amount received after the previous swap.</td></tr><tr><td>commandData</td><td>CommandData</td><td><pre><code>struct CommandData {
    CommandAction commandAction;
    uint8 outputType;
    uint16 inputLength;
    uint16 sequencesPosition;
    uint16 sequencesPositionEnd;
    address targetAddress;
}
</code></pre></td></tr></tbody></table>

**Output**

| Field        | Type    | Description                                                         |
| ------------ | ------- | ------------------------------------------------------------------- |
| nativeAmount | uint256 | The amountIn in native tokens.                                      |
| selector     | bytes4  | The function selector for the command.                              |
| input        | bytes   | The data that contains information for the command to be performed. |

### approve():

the approve function is a utility function within a smart contract to perform an ERC-20 token approval operation. It extracts the token address, spender address, and amount from a byte array and then calls the approve function on the specified ERC-20 token contract.

**input**

| Field | Type         | Description                                           |
| ----- | ------------ | ----------------------------------------------------- |
| input | bytes memory | A byte array containing necessary approve parameters. |

### transferFrom():

the transferFrom function is a utility function within a smart contract to perform an ERC-20 token transfer from one address to another. It extracts the token address, sender address, recipient address, and amount from a byte array and then calls the transferFrom function on the specified ERC-20 token contract.

**input**

| Field | Type         | Description                                                |
| ----- | ------------ | ---------------------------------------------------------- |
| input | bytes memory | A byte array containing necessary transferFrom parameters. |

### transfer():

the transfer function is a utility function within a smart contract to perform an ERC-20 token transfer to a specified address. It extracts the token address, recipient address, and amount from a byte array and then calls the transfer function on the specified ERC-20 token contract.

**input**

| Field | Type         | Description                                            |
| ----- | ------------ | ------------------------------------------------------ |
| input | bytes memory | A byte array containing necessary transfer parameters. |

### wrap():

the wrap function is a utility function within a smart contract to convert Ether into Wrapped Ether (WETH). It extracts the WETH contract address and the amount of Ether to be wrapped from a byte array and then calls the deposit function on the WETH contract, sending the specified amount of Ether.

**input**

| Field | Type         | Description                                        |
| ----- | ------------ | -------------------------------------------------- |
| input | bytes memory | A byte array containing necessary wrap parameters. |

### unwrap():

the unwrap function is a utility function within a smart contract to convert Wrapped Ether (WETH) back into Ether. It extracts the WETH contract address and the amount of WETH to be unwrapped from a byte array and then calls the withdraw function on the WETH contract.

**Input**

| Field | Type         | Description                                          |
| ----- | ------------ | ---------------------------------------------------- |
| input | bytes memory | A byte array containing necessary unwrap parameters. |

### balance():

the balance function is a utility function within a smart contract to query the balance of a specific asset (like an ERC-20 token) held by the contract. It extracts the asset's address from a byte array and then queries the balance of that asset for the contract's address.

**Input**

| Field | Type         | Description                                           |
| ----- | ------------ | ----------------------------------------------------- |
| input | bytes memory | A byte array containing necessary balance parameters. |

### execute():

the execute function is a central part of a smart contract that needs to perform a variety of operations based on dynamic inputs. It can handle standard ERC-20 operations like approvals and transfers, wrap and unwrap Ether, interact with various DeFi protocols, and make generic contract calls.

**Input**

<table><thead><tr><th>Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td>commandData</td><td>CommandData</td><td><pre><code>struct CommandData {
    CommandAction commandAction;
    uint8 outputType;
    uint16 inputLength;
    uint16 sequencesPosition;
    uint16 sequencesPositionEnd;
    address targetAddress;
}
</code></pre></td></tr><tr><td>nativeAmount</td><td>uint256</td><td>The amountIn in native tokens. </td></tr><tr><td>selector</td><td>bytes4</td><td>The function selector for the command.</td></tr><tr><td>input</td><td>bytes</td><td>The data that contains information for the command to be performed.</td></tr></tbody></table>

**Output**

| Field     | Type    | Description                         |
| --------- | ------- | ----------------------------------- |
| amountOut | uint256 | The amount received after the swap. |
