What are Libraries in Solidity?

What are Libraries in Solidity?

Libraries are similar to Contracts, but are mainly intended for reuse. A Library contains functions which other contracts can call. It is very similar in a sense when we think about interfaces, except whereas interfaces are connecting contracts, but libraries are more like a storing of functional data that we can then apply to contracts. Solidity have certain restrictions on use of a Library. Following are the key characteristics of a Solidity Library.

  • Library functions can be called directly if they do not modify the state. That means pure or view functions only can be called from outside the Library.
  • A Library can not be destroyed as it is assumed to be stateless.
  • A Library cannot have state variable.
  • A Library cannot inherit any element.
  • A Library cannot be inherited.

Let's take this theorizing of what a Library is and put it into some code. To bring a library we type the keyword library and call it Search. Let's say we want search through a database. Inside the Library we can write a public view function (remember that it can be called from outside the Library).

No alt text provided for this image

How do we search through things? We can manually look through it, or alternatively, we can loop through something. So, in our example, we will use the For Loop. We want this loop to return the index value of some integer we are searching for. For doing this we will use an if statement:

No alt text provided for this image

Because our statement only has single lines, we don't need these curly brackets, we can just write it all in one line. It is nice and clean, it is fine to do like this.

No alt text provided for this image

Just like that, we have created our first Library. Now, let's test this Library by creating a contract with a variable and a constructor for the beginning.

No alt text provided for this image

Also we are going to make a function (isValuePresent) which will check for a value (uint val), make it external view and return a integer. What we are going to do here is we are going to create a value and we are going to set it to the input.

No alt text provided for this image

And then how do we actually connect the dots? How do we say: "Well, now search!". We want to search through the data and set up the data to the array uint[] data and the value to our val input. Do do that, we can directly grab our Search Library. From our Library, with dot notation, we have access to our function, so we can grab indexOf and then we go into our arguments and write data as first argument and value as second argument. Next we will return index.

No alt text provided for this image

Now we are ready to test if it is working. Let's deploy and have a look.

No alt text provided for this image

It is working fine :)

See you in the next articles about #solidity , #remix , #evm , #ethereum , #smartcontracts and other interesting topics from the #blockchain world.

Here is the link to my GitHub repository for the code from the article.

要查看或添加评论,请登录

Ion Platon的更多文章

  • Storage vs Memory in Solidity

    Storage vs Memory in Solidity

    Understanding the storage and memory is not always intuitive. Having a good understanding of storage versus memory is…

  • What is Mapping in Solidity?

    What is Mapping in Solidity?

    If you are coming from other programming languages, then is good to know that the solidity mapping is similar to a…

  • Struct and Enum in Solidity

    Struct and Enum in Solidity

    A struct is a collection of key->value pairs similar to a mapping, but the values can have different types. Structs…

  • What are Interfaces in Solidity?

    What are Interfaces in Solidity?

    Interfaces are similar to abstract contracts and are created using interface keyword. There are some characteristics…

  • What is Inheritance in Solidity?

    What is Inheritance in Solidity?

    Let's take a closer inspection at Inheritance in Solidity. Inheritance is an advanced concept in #solidity and also in…

  • The Restricted Access Pattern in Solidity

    The Restricted Access Pattern in Solidity

    The Restricted Access to a Contract is a common practice. By default, a contract state is read-only, unless it is…

  • Build First Cryptocurrency in Testnet using Solidity and Remix

    Build First Cryptocurrency in Testnet using Solidity and Remix

    Creating own currency is a great way not only to solidify our coding or solidify our knowledge of building of smart…

  • The Fallback Functions in Solidity. How gas costs work in Smart Contracts.

    The Fallback Functions in Solidity. How gas costs work in Smart Contracts.

    Let's take a look at Fallback Functions in solidity. We are going to open our eyes and become a little bit more…

  • Bytes vs Strings in Solidity

    Bytes vs Strings in Solidity

    Strings are values that are made up of ordered sequences of characters, such as "hello world!" A string can contain any…

  • Loops in Solidity

    Loops in Solidity

    Looping is a type of functionality in coding, which is very handy. Loops allow you to iterate through data and take…

社区洞察

其他会员也浏览了