-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c0fc3f1
commit 190bfe4
Showing
10 changed files
with
412 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.