0

Suppose I have this code:

import "github.com/Arachnid/solidity-stringutils/strings.sol";

contract Contract {
    using strings for *;

    // ...
}

For this solidity online compiler generates this byte code:

60606040523415600b57fe5b5b60338060196000396000f30060606040525bfe00a165627a7a723058207a17f097139e731ad961366b8214b226041a3cd4427248c94ea39ee29bafface0029

Here I don't see any placeholder to add the library address. But when I deploy the byte code it creates a contract successfully.

So how does solidity online compiler find the address of the library?

And how does it know where to deploy the library i.e., mainnet or testnet. I am able to use the same byte code in both mainnet and testnet but the library address may be different in both networks. There are private networks also too.

2 Answers 2

2

In this case, the "strings" library only has functions with internal visibility therefore all the functions from the library are copied to the Contract contract therefore "strings" library was never deployed and so there isn't any placeholder to add the "strings" library address in the bytecode of Contract.

To know why it was copied read this http://solidity.readthedocs.io/en/develop/contracts.html#libraries

1

I assume you're using https://ethereum.github.io/browser-solidity/ when you're referring to the "solidity online compiler".

If that's the case, here's what happens when you "create" your Contract:

  1. The import generates a library from the github source, compiles it, deploys it, and keeps the address in memory.
  2. The compiler compiles your Contract, and then links the resulting bytecode to the address of the library's address.
  3. The linked bytecode is then deployed to the blockchain.

Hope that clarifies it. I'm a bit out of date on the internals of the browser-solidity project, so hopefully I'm not leading you astray.

6
  • But how does it know where to deploy the contract i.e., mainnet or testnet. I am able to use the same byte code in both mainnet and testnet but the library address may be different in both networks. There are private networks also too. Commented Feb 20, 2017 at 14:41
  • By default, browser-solidity deploys to an in-memory JavaScript VM. If you want to deploy to a live blockchain, you need to toggle the "cube" tab, which allows you to specify a Web3 provider endpoint to an Ethereum node. Whatever network that node is connected to is where the contract will be deployed to. Commented Feb 21, 2017 at 0:01
  • Exactly. I selected VM for compilation. But I was able to deploy the contract code on mainnet and tested and it worked fine. How did it work when it never got deployed to mainner and testnet? Commented Feb 21, 2017 at 5:45
  • I'm sorry, I don't understand your question. Commented Feb 21, 2017 at 13:07
  • The generated bytecode already contains the address of the "strings" library. To deploy the contract on mainnet I need should replace the address right? So that it points to the string library that's on mainnet. But I am able to deploy and run the contract's methods successfully without changing the "strings" library address. How is it possible? Commented Feb 21, 2017 at 19:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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