Golang Smart Contract Compile and Deploy with ‘os/exec’ Library

Ertugrul Bal
2 min readFeb 13, 2023

--

https://miro.medium.com/v2/resize:fit:1010/1*u6ciyG_aoLwPBjjYPTphcw.png

Hi everyone,
Welcome to the our new story that about smart contract compile and deployment processes. For these processing we suppose you have a smart contract that .sol file and you already downloaded required libraries and tools.
(If you don’t know these tools or you don’t have, you can read my old story. https://medium.com/@ertgrl.bal/deploying-smart-contract-with-golang-cb21dfb14c80)

The smart contract compiling and deployment process be like terminal procedures.

So we need a solc and abigen tools. First we check these tools there or not.

If the check status are true, we continue to compiling processings.

We assume that, we have a Store.sol file.

So, let’s use “solc — abi” command with our contract.

At the end of these commands, code will generate ‘store.abi’ file into ~/build directory. Also if contract have a some other libraries or contracts, the libraries or contracts will also generate into the ~/build directory.

And then we use “solc — bin” command for generate .bin files.

This “solc — bind” command will also generate .bin files as like .abi files.

When We obtain .abi and .bin files, we have to combine these files and generate “.go” file.

For this purpose, we use “abigen” tool.

The above code parts generate “.go” file. Therefore Smart Contract Compiling process finished.

You can also see Full Code For Compiling Process.

So, let’s go to the Deployment Part.

The smart contract deployment process on Golang have a some settings that can called a static settings.

For example:

After set the values as like above code, we just call “deploy” function from “Store.go” file. These function will be deploy and return tx hash.

Then we can check it with the tx hash while trading. If the status is true, the transaction was done correctly, otherwise, you have some code or contract errors. You need to fix the errors and then try again.

Full Code For Deploy Contract Process:

In conclusion, we talked about smart contract compilation and distribution with the ‘os/exec’ library above. Thanks to these tools, we can write and generate many different types of code and other processes. I wrote this article for the benefit of both me and other developers working in the field of Smart Contract and Golang. I hope it helps everyone. Best wishes.

--

--