Skip to content

Commit

Permalink
wip - golang refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
arianejasuwienas committed May 31, 2024
1 parent c0fc3f1 commit 190bfe4
Show file tree
Hide file tree
Showing 10 changed files with 412 additions and 210 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
3. go get github.com/ethereum/go-ethereum
go get github.com/ethereum/go-ethereum/accounts/abi/bind
go get github.com/ethereum/go-ethereum/crypto
4. go get -t hedera-golang-example-project
4. go get github.com/joho/godotenv
5. export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
Expand Down
208 changes: 0 additions & 208 deletions client.go

This file was deleted.

43 changes: 43 additions & 0 deletions contract_compiler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"os/exec"
)

type CompilationResult struct {
Contracts map[string]struct {
ABI json.RawMessage
Bin string
Metadata string
} `json:"contracts"`
}

func compileContract(filePath string) (string, []byte, error) {
cmd := exec.Command("solc", "--combined-json", "abi,bin", filePath)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return "", nil, fmt.Errorf("failed to compile contract: %v", err)
}

var result CompilationResult
err = json.Unmarshal(out.Bytes(), &result)
if err != nil {
return "", nil, fmt.Errorf("failed to parse solc output: %v", err)
}

for _, contract := range result.Contracts {
abiBytes, err := json.Marshal(contract.ABI)
if err != nil {
return "", nil, fmt.Errorf("failed to marshal ABI: %v", err)
}
bin := contract.Bin
return bin, abiBytes, nil
}

return "", nil, fmt.Errorf("no contract found in solc output")
}
42 changes: 42 additions & 0 deletions contract_interaction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"crypto/ecdsa"
"fmt"
"log"
"math/big"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
)

func interactWithContract(client *ethclient.Client, contractAddress common.Address, privateKey *ecdsa.PrivateKey, parsedABI abi.ABI) {
chainId := big.NewInt(296)
auth, err := bind.NewKeyedTransactorWithChainID(privateKey, chainId)
if err != nil {
log.Fatalf("Failed to create transactor: %v", err)
}
auth.GasLimit = uint64(3000000)

newGreeting := "Hello, Ethereum!"
fmt.Printf("Setting new greeting to: %s\n", newGreeting)

boundContract := bind.NewBoundContract(contractAddress, parsedABI, client, client, client)

setGreetingTx, err := boundContract.Transact(auth, "setGreeting", newGreeting)
if err != nil {
log.Fatalf("Failed to set greeting: %v", err)
}
fmt.Printf("Set greeting transaction hash: %s\n", setGreetingTx.Hash().Hex())

// Interact with the contract to get the greeting
var result []interface{}
err = boundContract.Call(nil, &result, "greet")
if err != nil {
log.Fatalf("Failed to get greeting: %v", err)
}

fmt.Printf("Greeting: %s\n", result[0].(string))
}
58 changes: 58 additions & 0 deletions eth_interaction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"crypto/ecdsa"
"log"
"math/big"
"os"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
)

func createTransactor(privateKey *ecdsa.PrivateKey, nonce uint64, gasPrice *big.Int) *bind.TransactOpts {
auth := bind.NewKeyedTransactor(privateKey)
auth.Nonce = big.NewInt(int64(nonce))
auth.Value = big.NewInt(0)
auth.GasLimit = uint64(3000000)
auth.GasPrice = gasPrice
return auth
}

func calculateTransactionCost(gasLimit uint64, gasPrice *big.Int) *big.Int {
return new(big.Int).Mul(big.NewInt(int64(gasLimit)), gasPrice)
}

func deployOrLoadContract(client *ethclient.Client, auth *bind.TransactOpts, parsedABI abi.ABI, bin string, initialGreeting string) common.Address {
addressFilePath := "cache/address"

var contractAddress common.Address
if _, err := os.Stat(addressFilePath); err == nil {
address, err := loadFromFile(addressFilePath)
if err != nil {
log.Fatalf("Failed to load contract address: %v", err)
}
contractAddress = common.HexToAddress(address)
log.Printf("Contract already deployed at address: %s", address)
} else {
bytecode := common.FromHex(bin)
address, tx, _, err := bind.DeployContract(auth, parsedABI, bytecode, client, initialGreeting)
if err != nil {
log.Fatalf("Failed to deploy contract: %v", err)
}
contractAddress = address

log.Printf("Contract deployed to address: %s", address.Hex())
log.Printf("Transaction hash: %s", tx.Hash().Hex())

err = saveToFile(addressFilePath, []byte(address.Hex()))
if err != nil {
log.Fatalf("Failed to save contract address: %v", err)
}
log.Println("Saved contract address to file.")
}

return contractAddress
}
Loading

0 comments on commit 190bfe4

Please sign in to comment.