|
| 1 | +# Using EVM-Based Smart Contracts on Linera |
| 2 | + |
| 3 | +It is possible to use Ethereum Virtual Machine (EVM)-based smart contracts on |
| 4 | +Linera. These contracts are typically written in Solidity. |
| 5 | + |
| 6 | +EVM smart contracts have access Linera-specific functionality of Linera through |
| 7 | +the `Linera.sol` library which exposes necessary interfaces for integration. |
| 8 | + |
| 9 | +This allows smart contracts originally deployed on Ethereum to be migrated to |
| 10 | +Linera and progressively adapted to Linera’s architecture and features. |
| 11 | + |
| 12 | +## Publishing EVM Smart Contracts |
| 13 | + |
| 14 | +The process for publishing EVM smart contracts is similar to that for Wasm smart |
| 15 | +contracts, with the key difference being the need to specify the virtual machine |
| 16 | +used (the default is Wasm). |
| 17 | + |
| 18 | +For EVM contracts, there is only one bytecode file (unlike Wasm, which requires |
| 19 | +separate `contract` and `service` binaries). Therefore, the same file must be |
| 20 | +passed twice: |
| 21 | + |
| 22 | +```bash |
| 23 | +linera publish-and-create \ |
| 24 | + counter.bytecode counter.bytecode \ |
| 25 | + --vm-runtime evm \ |
| 26 | + --json-parameters "42" |
| 27 | +``` |
| 28 | + |
| 29 | +Here, `counter.bytecode` contains the compiled contract, and "42" is passed as a |
| 30 | +constructor argument via the `--json-parameters` flag. |
| 31 | + |
| 32 | +Constructor arguments for the EVM contract are passed through application |
| 33 | +parameters. Instantiation-specific arguments are provided separately. |
| 34 | + |
| 35 | +## Calling other smart contracts. |
| 36 | + |
| 37 | +EVM smart contracts on Linera can invoke other EVM contracts using standard |
| 38 | +Solidity syntax. |
| 39 | + |
| 40 | +### EVM Contracts calling Wasm smart contracts. |
| 41 | + |
| 42 | +To call a Wasm smart contract from an EVM contract, use the following Solidity |
| 43 | +command: |
| 44 | + |
| 45 | +```solidity |
| 46 | + bytes memory return_value = Linera.try_call_application(address, input); |
| 47 | +``` |
| 48 | + |
| 49 | +- `address`: the address of the Wasm smart contract, as a `bytes32`. |
| 50 | +- `input`: the bytes representing the BCS-serialized `ContractAbi::Operation` |
| 51 | + input. |
| 52 | + |
| 53 | +The serialization code can be generated using the `serde-reflection` crate. |
| 54 | + |
| 55 | +### Wasm Smart Contracts calling EVM Contracts. |
| 56 | + |
| 57 | +Wasm smart contracts can call EVM contracts using the `alloy-sol-types` crate. |
| 58 | +This crate enables construction of Solidity-compatible types and supports RLP |
| 59 | +serialization/deserialization. |
| 60 | + |
| 61 | +The Wasm contract call-evm-counter demonstrates this functionality. |
| 62 | + |
| 63 | +- For operations, the input type is `Vec<u8>`. |
| 64 | + |
| 65 | +- For service calls, the input type is `EvmQuery`. |
| 66 | + |
| 67 | +Note: Linera distinguishes between contract and service code execution contexts. |
| 68 | + |
| 69 | +## Multichain EVM applications. |
| 70 | + |
| 71 | +To operate across multiple chains, an EVM application must implement the |
| 72 | +following functions: |
| 73 | + |
| 74 | +```solidity |
| 75 | + function instantiate(bytes memory input) external |
| 76 | + function execute_message(bytes memory input) external |
| 77 | +``` |
| 78 | + |
| 79 | +- `instantiate` is called on the creator chain. |
| 80 | + |
| 81 | +- `execute_message` handles incoming cross-chain messages. |
| 82 | + |
| 83 | +Additional SDK functions available include: |
| 84 | + |
| 85 | +```solidity |
| 86 | + Linera.chain_ownership() |
| 87 | + Linera.read_data_blob() |
| 88 | + Linera.assert_data_blob_exists() |
| 89 | + Linera.validation_round() |
| 90 | + Linera.message_id() |
| 91 | + Linera.message_is_bouncing() |
| 92 | +``` |
| 93 | + |
| 94 | +## Difference between EVM applications in Ethereum and Linera. |
| 95 | + |
| 96 | +- `Reentrancy`: Reentrancy is not supported on Linera. Contracts relying on it |
| 97 | + will fail with a clean error. |
| 98 | + |
| 99 | +- `Address Computation`: Contract addresses are computed differently from |
| 100 | + Ethereum. |
| 101 | + |
| 102 | +- `Gas Limits`: Following Infura's practice, Linera imposes a gas limit of |
| 103 | + 20,000,000 for service calls in EVM contracts. Contract execution is similarly |
| 104 | + constrained. |
0 commit comments