# FFI

{% embed url="<https://youtu.be/DTyn5ShI2vQ>" %}
FFI
{% endembed %}

## Setup

Test file:

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

import "forge-std/Test.sol";
import "forge-std/console.sol";

// forge test --match-path test/FFI.t.sol --ffi -vvvv

contract FFITest is Test {
    function testFFI() public {
        string[] memory cmds = new string[](2);
        cmds[0] = "cat";
        cmds[1] = "ffi_test.txt";
        bytes memory res = vm.ffi(cmds);
        console.log(string(res));
    }
}
```

## vm.ffi()

`vm.ffi()` lets you execute arbitrary command. For example, if we want to cat a file:

```solidity
string[] memory cmds = new string[](2);
cmds[0] = "cat";
cmds[1] = "ffi_test.txt";
bytes memory res = vm.ffi(cmds);
console.log(string(res));
```

To run FFI, we must specify it in the terminal when doing `forge test`:

```bash
forge test --match-path test/FFI.t.sol --ffi -vvv
```
