14.3 C
Los Angeles
Sunday, April 2, 2023
HomeCryptoDeploy Solidity Contract using...

Deploy Solidity Contract using GoLang — Go-Simple-Storage-FCC | by Kunal Sagar | Coinmonks | Aug, 2022

I recently started learning Blockchain from an amazing tutorial By freeCodeCamp & Patrick Collins. I feel it’s the best & most up-to-date tutorial about blockchain & Solidity available for free. It starts with blockchain basics and then explains solidity contracts and deploying using a NodeJS Backend.

Being a Golang developer I thought of using the same solidity contract using golang instead of nodeJs. And guess what there is great support for Ethereum golang client you can find it here https://geth.ethereum.org/

I will use the contract from Chapter 5 EtherJs Simple Storage. Will reuse the same contract & deploy it on the golang backend.

Create a project directory and initialize it as a go module

mkdir go-simple-storage-fcc
cd go-simple-storage-fcc

Initialize the project using the go mod command

go mod github.com/kunalrsagar/go-simple-storage-fcc

Open the directory in your favorite IDE, create a new file name SimpleStorage.sol

It’s the same Solidity Contract explained by Patrick, that maintains a list of People and their favorite Number and also has a function to Store a single FavoriteNumber and retrieve it.

The major difference here is instead of Node & npm ecosystem, we will use golang, Solidity & Ethereum. This code is tested on golang 1.19, solidity 0.8.15 version.

Once installation is successful we will use the solc command to compile the Solidity file and generate bin and abi

solc --optimize --abi --bin ./SimpleStorage.sol -o build

The --optimize flag is optional but helps to compile into gas-efficient binary. On successful completion, the abi and bin files should be generated in the build directory.

the major difference while using geth client is to provide an interface to manage a contract using the abigen command.

mkdir api

Create a directory name api

abigen --abi=./build/SimpleStorage.abi --bin=./build/SimpleStorage.bin --pkg=api --out=./api/SimpleStorage.go

The above command takes in the bin and abi file and generates a Go file SimpleStorage.gowith an interface defined to create, access, and manage functions of the contract. It’s an auto-generated file and should not be edited.

Add below dependency

go get github.com/ethereum/go-ethereum

If you encounter some errors in related/indirect dependencies run the below command

go get all

Let’s make sure Ganache is running and copy the RPC Url & Private Key of any wallet from it. Its explain in great detail by Patrick.

Ganache Desktop app on macOS

Set RPC_URL & PRIVATE_KEY of wallet as environment variables in using the terminal.

export RPC_URL=<local_ganache_url:port>export PRIVATE_KEY=<Wallet Private key>

The wallet private key can be retrieved by clicking on the key icon on any Wallet Address in the Ganache app

Here is where we create a Go-based backend, we will create a deploy.go . The file will have the main function as an entry point.

ethclient connects to the blockchain network using RPC_URL. We will create an ECDSA private key by passing it the actual PRIVATE_KEY value from environment variables.

The public key will be extracted from the private key using Public() function and we need it in ECSDA format for our geth.

In the same way, the address of the wallet and chainID are extracted.

Next, we will need to manually create a Transaction Opts that will have a collection of authorized data (signed by the author of the block) to create a valid Ethereum transaction.

We will then call GenNextTransaction() function in our main function right after chainID and deploy our contract.

Let’s try and run deploy.go

If you see above 👏🏻 works! 🥳

Next, we will call our Retrieve and Store functions of the SimpleStorage Contract. you can access all contract functions as well as a few more using simpleStorageApi object.

Let’s run it once again, now you should see 0 as your favoriteNumber. Its a type of *big.Int in go.

Lastly, let’s set a new favorite number by calling the SimpleStorage contract’s Store function, Since it’s not a Gas-free function, we will make a call to GetNextTransaction function to get an authorized transaction Ops.

we then call simpleStorageApi.Store function with big.NewInt(20)

You should see an output something like this and yeah! congratulations you just deployed a Smart Contract using golang

Below is the full source code of deploy.go

Github Repo👇🏻

References

Join Coinmonks Telegram Channel and Youtube Channel learn about crypto trading and investing

Source link

Most Popular

LEAVE A REPLY

Please enter your comment!
Please enter your name here

More from Author

Demolition Man Writer Daniel Waters Thinks Taco Bell Was A Perfect Match

Vulture asked Daniel Waters if he was a "Taco Bell guy," which I assume meant he was a fan of the Mexican-inspired fast food chain, not an actual Chalupas guy. The author confirms that he does like "Live Más": "I'm from Taco Bell. We have great Mexican food...

Shaka Smart, Dwyane Wade Keep Marquette In The Spotlight During Final Four Weekend

The second loss ended Marquette's hopes of advancing to the Final Four but the Golden Eagles were well represented as the basketball finals weekend opened Saturday night in Houston as head coach Shaka Smart was named coach of the year by the Associated Press and former standout...

2 dead in hot air balloon accident outside of Mexico City

Two people died and a girl was injured after the hot air balloon they were riding in caught fire near Mexico City.MEXICO CITY -- Two people died and a girl was injured after the hot air balloon they were riding in caught fire nearby Mexico City, authorities...

Judge Joe Brown Addresses Sheryl Lee Ralph Rumors

Judge Joe Brown Isn't Amused By Speculation About His Involvement In Cheryl Lee Ralph Assault Case! James Blakeney Brown Jr. was a lawyer turned television personality, most notably starring in the arbitration-based reality show "Judge Joe Brown." The 75-year-old is unlike any other celebrity when it comes to...

Rey Mysterio Beats Dominik With Bad Bunny, Frog Splash

Rey Mysterio defeated Dominik Mysterio with the help of Bad Bunny at WrestleMania 39. Bad Bunny was featured as the guest of honor in the match, and played until the end when he thwarted Dominik's attempt to use the chain. This ending could be in the next...

‘Abbott Elementary’ Season 2: Everything to Know

School is back! After its premiere on ABC, Abbott Elementary School A delightful mock sitcom about the life of a Philadelphia schoolteacher, it quickly hits its stride. Although the first season premiered in December 2021, it is still on the air and has been renewed for a second...

Angel Of Empire Wins, King Russell Places (At 58-1), And Reincarnate Shows

Angel of Empire proved his love of distance and power when he took the Arkansas Derby by four and a half lengths Saturday at Oaklawn, solidifying his Derby bid and securing a second straight win for trainer Brad Cox at a cost of $1.25 million. He...

Kaley Cuoco Gives Birth, Welcomes First Child With Tom Pelphrey

Kaley Cuoco welcomes first child with Tom Pelphrey | Kaley Cuoco Entertainment Tonight ...

Today’s Wordle #652 Hints, Clues And Answer For Saturday, April 2nd

April is off to a rocky start for your humble Wordle narrator. Even though it's beautiful and Spring has finally, finally, sprung, I woke up on April 1st very sick. Not from alcohol, mind you, just from life. A migraine, perhaps, but certainly all...