Ethereum: Do not enter the address by default for the expected structure

Ethereum: Type address is not implicitly convertible to expected type struct HelperConfig.NetworkConfig memory

As an experienced Ethereum developer, you probably know the nuances of working with the Ethereum ecosystem. In this article we deal with a widespread problem that appears when trying to manipulate data structures in reliability, the programming language for writing intelligent contracts on blockchain ether.

Problem: Enter the address for structure

In reliability, “structure” is defined with the keyword “{}”, followed by the name of the structure, its fields and its types. “Type address” is an “address” instance that represents a clear digital contract or user recognition in the Ethereum network.

After you have defined “structures”, you will be created a new structure of data from certain fields and suitable types. However, if you try to assign the value of a structure type, e.g. B. to return “structure” from the function and initiate another variable of the same structural type, it seems an unknown conversion.

** Error: “The type address is not a replacement by default …”

The error message suggests that solidity does not know how the value of “address type” is returned from its function to the expected type of the second variable. This is because the definition of the “structure” in the code has a certain order of fields and the types used are not compatible.

Why does that happen?

In order to understand why this problem occurs, we take a closer look at how the Solidge compiler supports the definitions of the structural and frame allocations. According to the definition of “structures” with many fields, the reliability must ensure that all fields can be assigned to the corresponding variables in the correct order.

In your case, the problem arises, since after the return of “structures” from the function, the compiler tries to convert them into the structure type instance (e.g. {}) that awaits specific field types. However, the “address” mentioned by their function may not fit into one of these expected field types.

Example code

Let us consider a simple example that illustrates this problem:

`Solidity

Pragma solidity ^0.8.0;

Struct config {

Uint256 Public Network;

}

TotetworkConfig () Public Pure Returns (Config) function {

Return config ({{{{{

Network: 1, // The type address is not replaced by default for the structure

Version: 1,

Version name: "1.0"

});

}

In this example, the “TotetworkConfig” function returns the structure “Configuration” with three fields (“Network”, “Version” and “” “” “” “” “” “” “” “” “” ” “” “” “” “” “” “” “” “” “” “But initiated another variable with the returned value, however, he expects the” configuration “of the structure, but the compiler tries to” type address ” – Assign value.

Solutions

To solve this problem, you can use several approaches:

  • Use explicit conversions : fields in your “structure” with certain types (e.g. network Uint256;). This ensures that the right field types are used when assigning values.

`Solidity

Pragma solidity ^0.8.0;

Struct config {

Uint256 Public Network;

}

TotetworkConfig () Public Pure Returns (Config) function {

Return config ({{{{{

Network: 1,

Version: 2, // Use an open conversion to Uint256

Version name: "2.0"

});

}

2.

`Solidity

Pragma solidity ^0.8.0;

Struct config {

Uint256 Public Network;

}

Networkcconfig type = address;

Function TotetworkConfig () Public Pure Returns (NetworkConfig) {

Return NetworkConfig (1);

}

  • Throw on the corresponding type

    : In some cases it may be necessary to end the value of the “address” returned by their function in order to coordinate one of the expected field types:

“ Solidity

Solidity Pragma ^0.8.

bitcoin point want

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *