What Are EVM Opcodes?

Opcodes are the individual low-level instructions that make up a smart contract on the Ethereum Virtual Machine (EVM). Each opcode corresponds to a specific operation that the EVM can perform, such as adding or comparing values in memory, or interacting with the blockchain. Opcodes are executed one at a time in the order they appear in a contract’s bytecode. The set of opcodes available on the EVM is fixed and limited, but they provide a powerful set of building blocks for creating complex smart contracts.

Quai Specific Opcodes

Quai Network’s implementation of the EVM introduces two new opcodes for developers to utilize.

isaddrinternal

Checks if provided address is within smart contract’s context.

etx

Emits an external transaction from a smart contract.

Each of these opcodes serves a very specific purpose within Quai Network’s VM. isaddrinternal allows smart contracts to determine whether a contract interaction can occur entirely within a single context or whether an ETX needs to occur. Based on the boolean response from isaddrinternal, a smart contract can continue on as normal with a local interaction, or can emit an ETX using the etx opcode.

Assembly Implementation

Solidity does not currently have native compiler support for Quai’s additional opcodes that provide cross-chain functionality, but it does have support for inline assembly usage.

Inline assembly is typically implemented by developers for more fine-grained control over your contract. In the context of Quai, we can use it to directly call opcodes inside of contracts via Yul and Assembly. This allows us to maintain the same general structure as traditional Solidity based contract while implementing simple assembly to provide functionality for Quai specific utilities.

Inline assembly within Solidity is generally straightforward as you can access your contract’s variables via normal methods and insert directly into assembly. The syntax for inserting assembly into your contract is as follows:

function doSomethingWithAssembly() {
    assembly {...}
}

More information on case specific syntax, usage and conventions can be found on the Solidity inline assembly page.

Examples

Conclusion

The isaddrinternal and etx opcodes provide key cross-chain functionality to smart contracts deployed on Quai Network. They provide the basis for contracts in different contexts to communicate and interact with each other in a trustless fashion and allow developers to create multi-chain applications that span the entire network.