Created
March 3, 2023 05:13
-
-
Save AdvaithD/efddf4e589e5c92671641806f63461d6 to your computer and use it in GitHub Desktop.
skeleton repl
This file contains hidden or 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 ( | |
"bufio" | |
"fmt" | |
"log" | |
"os" | |
"strings" | |
"github.com/ethereum/go-ethereum/accounts/abi" | |
"github.com/ethereum/go-ethereum/common" | |
"github.com/ethereum/go-ethereum/ethclient" | |
"github.com/ethereum/go-ethereum/accounts/abi/bind" | |
) | |
func main() { | |
// Connect to the Ethereum network | |
client, err := ethclient.Dial("https://ropsten.infura.io/v3/YOUR-PROJECT-ID") | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Load the contract | |
contractAbi, err := abi.JSON(strings.NewReader(string(contractABI))) | |
if err != nil { | |
log.Fatal(err) | |
} | |
contractAddress := common.HexToAddress("...") | |
// Create an instance of the contract | |
contract, err := bind.NewBoundContract(contractAddress, contractAbi, client, client, client) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Start REPL | |
reader := bufio.NewReader(os.Stdin) | |
for { | |
fmt.Print("> ") | |
input, err := reader.ReadString('\n') | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Remove new line character from input | |
input = strings.TrimSpace(input) | |
// Call function on contract | |
switch input { | |
case "getNumber": | |
number, err := contract.GetNumber(nil) | |
if err != nil { | |
fmt.Println(err) | |
} else { | |
fmt.Println(number) | |
} | |
case "setNumber": | |
fmt.Print("Enter new number: ") | |
newNumber, err := reader.ReadString('\n') | |
if err != nil { | |
log.Fatal(err) | |
} | |
newNumber = strings.TrimSpace(newNumber) | |
tx, err := contract.SetNumber(nil, newNumber) | |
if err != nil { | |
fmt.Println(err) | |
} else { | |
fmt.Println("Transaction hash:", tx.Hash().Hex()) | |
} | |
case "exit": | |
os.Exit(0) | |
default: | |
fmt.Println("Invalid command") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment