Last active
May 2, 2023 08:32
-
-
Save joohhnnn/f137bfc2a8a9288543ebef04c17f025a to your computer and use it in GitHub Desktop.
A complete Go program that demonstrates how to interact with the Storage smart contract.
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
package main | |
import ( | |
"context" | |
"crypto/ecdsa" | |
"fmt" | |
"log" | |
"math/big" | |
"github.com/ethereum/go-ethereum/accounts/abi/bind" | |
"github.com/ethereum/go-ethereum/common" | |
"github.com/ethereum/go-ethereum/crypto" | |
"github.com/ethereum/go-ethereum/ethclient" | |
"path/to/your/package/main" | |
) | |
func main() { | |
// Create an Ethereum client instance | |
client, err := ethclient.Dial("http://localhost:8545") | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Instantiate the generated contract structure | |
contractAddress := common.HexToAddress("0xYourContractAddressHere") | |
storageInstance, err := main.NewStorage(contractAddress, client) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Read private key from environment variable | |
// 'export ETH_PRIVATE_KEY=your_private_key_here' set private key by linux/mac | |
// 'set ETH_PRIVATE_KEY=your_private_key_here' set private key by windows | |
// You can use other methods similar to a keyfile to store your private key | |
// but you must always pay attention to the security of your private key. | |
privateKeyEnv := os.Getenv("ETH_PRIVATE_KEY") | |
if privateKeyEnv == "" { | |
log.Fatal("ETH_PRIVATE_KEY environment variable not set") | |
} | |
privateKey, err := crypto.HexToECDSA(privateKeyEnv) | |
if err != nil { | |
log.Fatal(err) | |
} | |
publicKey := privateKey.Public() | |
publickeyECDSA, ok := publicKey.(*ecdsa.PublicKey) | |
if !ok { | |
log.Fatal("error casting public key to ECDSA") | |
} | |
fromAddress := crypto.PubkeyToAddress(*publickeyECDSA) | |
nonce, err := client.PendingNonceAt(context.Background(), fromAddress) | |
if err != nil { | |
log.Fatal(err) | |
} | |
gasPrice, err := client.SuggestGasPrice(context.Background()) | |
if err != nil { | |
log.Fatal(err) | |
} | |
auth := bind.NewKeyedTransactor(privateKey) | |
auth.Nonce = big.NewInt(int64(nonce)) | |
auth.Value = big.NewInt(0) // in wei | |
auth.GasLimit = uint64(300000) // in units | |
auth.GasPrice = gasPrice | |
// Use the Store method to store an integer in the contract | |
number := big.NewInt(42) | |
tx, err := storageInstance.Store(auth, number) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Printf("Transaction sent: %s\n", tx.Hash().Hex()) | |
// Wait for the transaction to complete | |
_, err = bind.WaitMined(context.Background(), client, tx) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Use the Retrieve method to retrieve the stored integer from the contract | |
storedNumber, err := storageInstance.Retrieve(nil) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Printf("Stored number: %d\n", storedNumber) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment