This section focuses on understanding the import functionality in Solidity, a crucial aspect for building Solana programs.
illustration
b
Understanding Solidity Data Types

In this lesson, you will:

  • Learn about various data types in Solidity and how to use them.

Solidity supports a range of basic data types, including booleans, integers (both signed and unsigned), fixed-length byte arrays, address types, and function types. Note that the address type varies between Ethereum Virtual Machine (EVM) and Solana. You can also create fixed-length arrays with a set size.

Here's a rundown of the primary data types in Solidity:

  1. Boolean Type (bool): Represents true or false.
  2. Integers:
    • uint: An unsigned 256-bit integer. Examples include 0, 102, 0xdeadcafe, 1000_000_000_000_000.
    • uint64, uint32, uint16, uint8: Shorter unsigned integers. Using the most efficient width is recommended.
    • int: A signed 256-bit integer. Examples include -102, 0, 102, -0xdead_cafe.
    • int64, int32, int16, int8: Shorter signed integers. Opt for the most efficient width where possible.

Note: It's best to use int64, int32, uint64, or uint32 types when working with integers.

  1. Fixed-Length Byte Arrays: Unique to Solidity, these are arrays of 1 to 32 bytes, like bytes32, bytes24, bytes8, or bytes1. byte is an alias for bytes1. Initialize these arrays with a hex string (e.g. hex"414243"), a text string ("ABC"), or a hex value (0x414243).
bytes4 foo = "ABCD";
bytes4 bar = hex"41_42_43_44";
  1. Address Types:

    • Regular Address: Holds a 20-byte EVM account address or a 32-byte Solana account address.
    • Address Payable: Similar to a regular address but can receive Ether in Ethereum. In Solidity for Solana, address payable can be used as a cast.
  2. Fixed-Length Arrays: Created with [length] after the type name. They can contain any data type, including other arrays.

  3. Function Types: Refer to functions and come in two flavours: internal and external. Internal functions are within the same contract or its base contracts. External functions reference public or external functions on any contract. Function types specify parameter types, return types, and mutability.

contract ft {
    function test() public {
        // reference to an internal function with two argments, returning bool
        // with the default mutability (i.e. cannot be payable)
        function(int32, bool) internal returns (bool) x;

        // the local function func1 can be assigned to this type; mutability
        // can be more restrictive than the type.
        x = func1;

        // now you can call func1 via the x
        bool res = x(102, false);

        // reference to an internal function with no return values, must be pure
        function(int32, bool) internal pure y;

        // Does not compile:
        // Wrong number of return types and mutability is not compatible
        // y = func1;
    }

    function func1(int32 arg, bool arg2) internal view returns (bool) {
        return false;
    }
}

External function types store both the contract address and function selector. Assigning to external function types requires specifying the contract and function.

contract ft {
 function test(address p) external {
 paffling.set_callback{ program_id: p } (this.callback);
 }

 function callback(int32 count, string foo) public {
 // ...
 }
}

contract paffling {
 function(int32, string) external internal callback;

 function set_callback(function(int32, string) external c) public {
 callback = c;
 }

 function piffle() public {
 callback{ accounts: [] } (1, "paffled");
 }
}

On Solana, for calls from variables of external function types, you must use the accounts call argument. The compiler can't automatically determine the required accounts, so you need to manually specify them.

function test(function(int32, string) external myFunc) public {
 myFunc{accounts: []}(24, "accounts");
}

In the next lesson, we will explore constructors in Solidity, furthering your understanding of how to structure and initialize Solidity contracts.