Skip to Content
Mastering Blockchain - Second Edition
book

Mastering Blockchain - Second Edition

by Ankur Daharwal, Imran Bashir, Pranav Burnwal
March 2018
Beginner to intermediate
656 pages
20h 9m
English
Packt Publishing
Content preview from Mastering Blockchain - Second Edition

Libraries

Libraries are deployed only once at a specific address and their code is called via CALLCODE or DELEGATECALL opcode of the EVM. The key idea behind libraries is code reusability. They are similar to contracts and act as base contracts to the calling contracts. A library can be declared as shown in the following example:

library Addition 
{ 
    function Add(uint x,uint y) returns (uint z) 
    { 
        return x + y; 
    } 
} 

This library can then be called in the contract, as shown here. First, it needs to be imported and then it can be used anywhere in the code. A simple example is shown as follows:

import "Addition.sol" 
function Addtwovalues() returns(uint) 
{ 
    return Addition.Add(100,100); 
} 

There are a few limitations with libraries; for example, ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering Blockchain - Third Edition

Mastering Blockchain - Third Edition

Imran Bashir
Mastering Blockchain

Mastering Blockchain

Lorne Lantz, Daniel Cawrey
Mastering Ethereum

Mastering Ethereum

Andreas M. Antonopoulos, Gavin Wood

Publisher Resources

ISBN: 9781788839044Supplemental Content