2

my library

pragma solidity ^0.5.8;

library SafeMath {

    function add(uint256 a,uint256 b) public pure returns (uint256) {
        return a+b;
    }
}

My contract

pragma solidity ^0.5.8;

import "./SafeMath.sol";

contract Test {

using SafeMath for uint256;

uint256 c = 10;

    function addition() public {
        uint256 a = 10;
        uint256 b = 10;
        c = a.add(b);
    }

    function getValue() view public returns (uint256) {
        return c;
    }

}

THE STEPS I AM FOLLOWING ARE:-

step 1 :- I am compiling SafeMath.sol using commnadline [solc --abi --bin safeMath.sol ]

step 2 :- I am deploying the same with web3 and getting the address as 0x785763........

step3 :- I am linking and compiling the Test.sol contract using the command solc --abi --bin Test.sol --libraries SafeMath:0x785763........ -o bin

step4 :- I am deploying this contract and trying to just get the value of c without performing the addition operation ,i am getting default value 0

also no events gets fired in addition method inside contract(not shown in this code)

//--------------------------------------------------- //MY WEB3 CODE

var UserRepositoryInstance1 = web3.eth.contract(UserRepositoryABI1).at(UserRepositoryAddress1);

app.get('/checkAdd', function (req, res) { web3.eth.defaultAccount = web3.eth.coinbase; var i=UserRepositoryInstance1.getValue(); console.log(i) })

ANY HELP WOULD BE APPRECIATED THANK YOU.

kantus jee
  • 21
  • 3

1 Answers1

0

Try to add this --link before --libraries, and see this answer it could help you

What are the steps to compile and deploy a library in Solidity?

Majd TL
  • 3,217
  • 3
  • 17
  • 36
  • yes i tried that got "linking complete" but the problem is the same – kantus jee May 31 '19 at 12:09
  • my contract bytecode has the address of library contained in it ...the deployment of the contract is also done without any error – kantus jee May 31 '19 at 12:12
  • but when i call getValue() from node(web3) i get the default value 0.. – kantus jee May 31 '19 at 12:13
  • if i compile Test.sol first using command solc --optmize --bin Test.sol – kantus jee May 31 '19 at 12:20
  • can you upload your code somewhere? – Majd TL May 31 '19 at 13:47
  • the code is the same one as i have posted above only two of it... – kantus jee Jun 03 '19 at 04:25
  • i just dont know why am i getting this wrong .....inspite of following the documentation strictly.....i have been stuck on this for five days know .........please any help would be appreciated – kantus jee Jun 03 '19 at 04:27
  • try 0.5.3 instead of 0.5.8, – Majd TL Jun 03 '19 at 07:34
  • i mean your wb3js code and how you are implementing getValue() – Majd TL Jun 03 '19 at 08:39
  • app.get('/checkAdd', function (req, res) { web3.eth.defaultAccount = web3.eth.coinbase; var i=UserRepositoryInstance1.getValue(); console.log(i); – kantus jee Jun 04 '19 at 04:34
  • if i remove the import library and compile the same with solc 0.4.24 ......i can call the get value and get the expected result.......the problem is either with the library or with the version......thank you for your answer ...... please look into i just need to get this done – kantus jee Jun 04 '19 at 04:37
  • I think UserRepositoryInstance1.getValue() is async and need await before it Try please app.get('/checkAdd', async function (req, res) { web3.eth.defaultAccount = web3.eth.coinbase; var i = await UserRepositoryInstance1.getValue(); console.log(i); im not sure which web3js version you are using – Majd TL Jun 04 '19 at 07:40
  • the web3 version is 0.20.6.......and event making it synchronous is not working having the same problem......although i dont think the problem is with the asynchronous transaction as function is synchronous( am not sure) – kantus jee Jun 05 '19 at 07:20
  • same thing when i do using truffle it works(using deploy.link(a,b)) – kantus jee Jun 05 '19 at 07:21
  • but i dont have option of doing with truffle so please help – kantus jee Jun 05 '19 at 07:21
  • ask on https://gitter.im/ethereum/solidity, cause i have no more ideas :( – Majd TL Jun 05 '19 at 07:48