4

I am trying to compile the MetaCoin contract (part of the Truffle Init Webpack). I need to get the byte code as hex numbers so that I can use it with Nethereum. The MetaCoin contains an import to ConvertLib.sol:

pragma solidity ^0.4.4;

import "./ConvertLib.sol";

contract MetaCoin {
    ...

When I compile it (using either Truffle from the command line or the Solidity extension for VS Code), the byte code I get is:

0x6060 ... 0073__ConvertLib____________________________6396 ... 0029

This is obviously a reference the ConvertLib library. When I use Nethereum to try and deploy the contract:

...
var ByteCode = 
    "0x6060 ... 0073__ConvertLib____________________________6396 ... 0029";
var transactionHash = await web3.Eth.DeployContract.SendRequestAsync(
    Abi, ByteCode, Address, Gas, GasPrice);

I get the error message:

System.InvalidOperationException: Character '_' at index '604' is not valid alphanumeric character.

Clearly, it does not like the "ConvertLib" reference in the middle of the byte code. I could just include the library code in the MetaCoin contract and remove the import, but I would like to sort this problem out properly.

How can I compile the MetaCoin contract so that it includes the ConvertLib library in the byte code?

5

What you are looking for is

solc --optimize --bin MetaCoin.sol | solc --link --libraries ConvertLib:<address>

In order for this to work ConvertLib has to be deployed first, the linking will then replace the placeholders like 0073__ConvertLib____________________________6396 with the actual addresses.

To quote the relevant part of the documentation:

If your contracts use libraries, you will notice that the bytecode contains substrings of the form __LibraryName______. You can use solc as a linker meaning that it will insert the library addresses for you at those points:

Either add --libraries "Math:0x12345678901234567890 Heap:0xabcdef0123456" to your command to provide an address for each library or store the string in a file (one library per line) and run solc using --libraries fileName.

If solc is called with the option --link, all input files are interpreted to be unlinked binaries (hex-encoded) in the __LibraryName____-format given above and are linked in-place (if the input is read from stdin, it is written to stdout). All options except --libraries are ignored (including -o) in this case.

  • 2
    Ah - I see. I didn't realise the ConvertLib & MetaCoin contracts would be deployed to the blockchain separately. Interesting - thanks for that. – TallGuy Dec 13 '17 at 2:54

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.