0

Tried out libraries for the first time, had a really bad experience with them. I need some help in getting my contracts to work between libraries.

Library 1:

pragma solidity ^0.4.0;

library IntExtended{

    function increment(uint _self) returns (uint){
        return _self+1;
}

    function decrement(uint _self) returns(uint){
        return _self-1;
}

    function incrementByValue(uint _self,uint _value) returns (uint){
        return _self + _value;
}

    function decrementByValue(uint _self,uint _value) returns (uint){
        return _self -_value;
}

    }

File that works with it, I don't know what you call it, second library?

pragma solidity ^0.4.0;

import "browser/Library.sol";

contract TestLibrary{
    using IntExtended for uint;

    function testIncrement(uint _base) returns (uint){
        return IntExtended.increment(_base);
}

    function testDecrement(uint _base) returns (uint){
        return IntExtended.decrement(_base);
}

    function testIncrementByValue(uint _base,uint _value) returns(uint){
        return _base.incrementByValue(_value);
} 

    function testDecrementByValue(uint _base, uint _value) returns (uint){
        return _base.decrementByValue(_value);
}
}

The library IntExtended is not functioning, however, the contract referencing to IntExtended is functioning.

  • 1
    return IntExtended.increment(_base) should be return _base.increment(), right? And what do you mean by your last sentence? What exactly is working and what's not? (And for the stuff that's not working, what exactly does that mean? Do you get an error? An incorrect result?) – user19510 Jun 12 '18 at 13:32
  • Changed it to _base. , no difference. When deploying the IntExtended, and trying to execute would result in... transact to IntExtended.decrement errored pending ... transact to IntExtended.decrementByValue errored: VM error: revert. revert The transaction has been reverted to the initial state. Note: The constructor should be payable if you send value. Debug the transaction to get more information. – Durian Jaykin Jun 14 '18 at 5:54
  • Please update your question with your latest code. Your comment seems to have been mangled, and it's hard to reproduce your error without the corrected code. – user19510 Jun 14 '18 at 6:01
  • 1
    Oh, it looks like perhaps you're trying to deploy the library by itself and then call it? I don't think that's expected to work. – user19510 Jun 14 '18 at 6:03
  • Yeah sorry about that, can't edit comments for 5 minutes. You can't execute libraries? Oh good to know. – Durian Jaykin Jun 14 '18 at 6:05
1

You have no uint defined in your storage to use the library on, also your library functions can be pure.

below using IntExtended for uint

add

uint myUint

and then you can call the library functions like

myUint.increment()

or _base.increment()

Edit: Spun it up in remix

pragma solidity ^0.4.0;

library IntExtended{

    function increment(uint _self) returns (uint){
        return _self+1;
    }
}


contract TestLibrary{
    using IntExtended for uint;

    function testIncrement(uint _base) returns (uint){
        return _base.increment();
    }
}

0x1 Transaction mined and execution succeed
 decoded input  {
    "uint256 _base": "5"
}
 decoded output     {
    "0": "uint256: 6"
}
  • transact to IntExtended.decrementByValue pending ... transact to IntExtended.decrementByValue errored: VM error: revert. revert The transaction has been reverted to the initial state. Note: The constructor should be payable if you send value. Debug the transaction to get more information. {Received the same error} – Durian Jaykin Jun 12 '18 at 15:52
  • See edited answer for example :) – Nico Vergauwen Jun 13 '18 at 11:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.