Spaces:
Build error
Build error
| // package main | |
| // import ( | |
| // "context" | |
| // "crypto/ecdsa" | |
| // "fmt" | |
| // "log" | |
| // "math/big" | |
| // "os" | |
| // "strings" | |
| // "github.com/ethereum/go-ethereum" | |
| // "github.com/ethereum/go-ethereum/accounts/abi" | |
| // "github.com/ethereum/go-ethereum/common" | |
| // "github.com/ethereum/go-ethereum/core/types" | |
| // "github.com/ethereum/go-ethereum/crypto" | |
| // "github.com/ethereum/go-ethereum/ethclient" | |
| // "github.com/joho/godotenv" | |
| // ) | |
| // // Global variables | |
| // var ( | |
| // client *ethclient.Client | |
| // contractAddress common.Address | |
| // parsedABI abi.ABI | |
| // privateKey *ecdsa.PrivateKey | |
| // fromAddress common.Address | |
| // chainID *big.Int | |
| // ) | |
| // func init() { | |
| // // Load environment variables | |
| // if err := godotenv.Load(); err != nil { | |
| // log.Fatal("Error loading .env file") | |
| // } | |
| // // Connect to the blockchain | |
| // var err error | |
| // client, err = ethclient.Dial("https://bsc-testnet.publicnode.com") // BSC Testnet | |
| // if err != nil { | |
| // log.Fatalf("Failed to connect to the blockchain: %v", err) | |
| // } | |
| // // Load contract information | |
| // contractAddress = common.HexToAddress(os.Getenv("CONTRACT_ADDRESS")) | |
| // parsedABI, err = abi.JSON(strings.NewReader(os.Getenv("ABI_JSON"))) | |
| // if err != nil { | |
| // log.Fatalf("Failed to parse ABI: %v", err) | |
| // } | |
| // // Load private key | |
| // privateKey, err = crypto.HexToECDSA(os.Getenv("PRIVATE_KEY")) | |
| // if err != nil { | |
| // log.Fatalf("Failed to parse private key: %v", err) | |
| // } | |
| // // Get public key and address | |
| // publicKey := privateKey.Public() | |
| // publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey) | |
| // if !ok { | |
| // log.Fatal("Error converting public key") | |
| // } | |
| // fromAddress = crypto.PubkeyToAddress(*publicKeyECDSA) | |
| // // Set chain ID for BSC Testnet | |
| // chainID = big.NewInt(97) | |
| // fmt.Printf("Connected to blockchain with address: %s\n", fromAddress.Hex()) | |
| // } | |
| // // Read function example - getAllRooms | |
| // func getAllRooms() { | |
| // callData, err := parsedABI.Pack("getAllRooms") | |
| // if err != nil { | |
| // log.Fatalf("Failed to pack call data: %v", err) | |
| // } | |
| // result, err := client.CallContract(context.Background(), ethereum.CallMsg{ | |
| // To: &contractAddress, | |
| // Data: callData, | |
| // }, nil) | |
| // if err != nil { | |
| // log.Fatalf("Failed to call getAllRooms: %v", err) | |
| // } | |
| // var roomIds []string | |
| // err = parsedABI.UnpackIntoInterface(&roomIds, "getAllRooms", result) | |
| // if err != nil { | |
| // log.Fatalf("Failed to unpack result: %v", err) | |
| // } | |
| // fmt.Println("All Room IDs:") | |
| // for i, roomId := range roomIds { | |
| // fmt.Printf("%d. %s\n", i+1, roomId) | |
| // } | |
| // } | |
| // // Read function - getRoomDetails | |
| // func getRoomDetails(roomId string) { | |
| // callData, err := parsedABI.Pack("getRoomDetails", roomId) | |
| // if err != nil { | |
| // log.Fatalf("Failed to pack call data: %v", err) | |
| // } | |
| // result, err := client.CallContract(context.Background(), ethereum.CallMsg{ | |
| // To: &contractAddress, | |
| // Data: callData, | |
| // }, nil) | |
| // if err != nil { | |
| // log.Fatalf("Failed to call getRoomDetails: %v", err) | |
| // } | |
| // // Unpack result | |
| // output, err := parsedABI.Unpack("getRoomDetails", result) | |
| // if err != nil { | |
| // log.Fatalf("Failed to unpack result: %v", err) | |
| // } | |
| // // Process output | |
| // if len(output) == 6 { | |
| // fmt.Println("Room Details:") | |
| // fmt.Printf("ID: %s\n", output[0].(string)) | |
| // fmt.Printf("Name: %s\n", output[1].(string)) | |
| // fmt.Printf("Metadata: %s\n", output[2].(string)) | |
| // fmt.Printf("Owner: %s\n", output[3].(common.Address).Hex()) | |
| // fmt.Printf("Created At: %s\n", output[4].(*big.Int).String()) | |
| // fmt.Printf("Is Active: %t\n", output[5].(bool)) | |
| // } else { | |
| // fmt.Println("Unexpected output format") | |
| // } | |
| // } | |
| // // Read function - getRoomParticipants | |
| // func getRoomParticipants(roomId string) { | |
| // callData, err := parsedABI.Pack("getRoomParticipants", roomId) | |
| // if err != nil { | |
| // log.Fatalf("Failed to pack call data: %v", err) | |
| // } | |
| // result, err := client.CallContract(context.Background(), ethereum.CallMsg{ | |
| // To: &contractAddress, | |
| // Data: callData, | |
| // }, nil) | |
| // if err != nil { | |
| // log.Fatalf("Failed to call getRoomParticipants: %v", err) | |
| // } | |
| // var participants []common.Address | |
| // err = parsedABI.UnpackIntoInterface(&participants, "getRoomParticipants", result) | |
| // if err != nil { | |
| // log.Fatalf("Failed to unpack result: %v", err) | |
| // } | |
| // fmt.Printf("Participants in room %s:\n", roomId) | |
| // for i, address := range participants { | |
| // fmt.Printf("%d. %s\n", i+1, address.Hex()) | |
| // } | |
| // } | |
| // // Read function - getIceServers | |
| // func getIceServers() { | |
| // callData, err := parsedABI.Pack("getIceServers") | |
| // if err != nil { | |
| // log.Fatalf("Failed to pack call data: %v", err) | |
| // } | |
| // result, err := client.CallContract(context.Background(), ethereum.CallMsg{ | |
| // To: &contractAddress, | |
| // Data: callData, | |
| // }, nil) | |
| // if err != nil { | |
| // log.Fatalf("Failed to call getIceServers: %v", err) | |
| // } | |
| // // This is a bit trickier due to struct array return type | |
| // // Just showing raw result for now | |
| // fmt.Printf("ICE Servers Raw Result: %x\n", result) | |
| // } | |
| // // Write function - createRoom | |
| // func createRoom(roomId, name, metadata string) { | |
| // nonce, err := client.PendingNonceAt(context.Background(), fromAddress) | |
| // if err != nil { | |
| // log.Fatalf("Failed to get nonce: %v", err) | |
| // } | |
| // gasPrice, err := client.SuggestGasPrice(context.Background()) | |
| // if err != nil { | |
| // log.Fatalf("Failed to get gas price: %v", err) | |
| // } | |
| // // Pack the data for the function call | |
| // data, err := parsedABI.Pack("createRoom", roomId, name, metadata) | |
| // if err != nil { | |
| // log.Fatalf("Failed to pack transaction data: %v", err) | |
| // } | |
| // // Estimate gas | |
| // gasLimit := uint64(500000) // Default high value | |
| // estimatedGas, err := client.EstimateGas(context.Background(), ethereum.CallMsg{ | |
| // From: fromAddress, | |
| // To: &contractAddress, | |
| // Value: big.NewInt(0), | |
| // Data: data, | |
| // }) | |
| // if err == nil { | |
| // // Add 20% buffer to estimated gas | |
| // gasLimit = uint64(float64(estimatedGas) * 1.2) | |
| // fmt.Printf("Gas estimate: %d, using: %d\n", estimatedGas, gasLimit) | |
| // } else { | |
| // fmt.Printf("Gas estimation failed: %v. Using default: %d\n", err, gasLimit) | |
| // } | |
| // // Create the transaction | |
| // tx := types.NewTransaction(nonce, contractAddress, big.NewInt(0), gasLimit, gasPrice, data) | |
| // // Sign the transaction | |
| // signedTx, err := types.SignTx(tx, types.NewLondonSigner(chainID), privateKey) | |
| // if err != nil { | |
| // log.Fatalf("Failed to sign transaction: %v", err) | |
| // } | |
| // // Send the transaction | |
| // err = client.SendTransaction(context.Background(), signedTx) | |
| // if err != nil { | |
| // log.Fatalf("Failed to send transaction: %v", err) | |
| // } | |
| // fmt.Printf("Transaction sent! Hash: %s\n", signedTx.Hash().Hex()) | |
| // fmt.Println("Room creation initiated. Wait for the transaction to be mined.") | |
| // } | |
| // // Write function - joinRoom | |
| // func joinRoom(roomId string) { | |
| // nonce, err := client.PendingNonceAt(context.Background(), fromAddress) | |
| // if err != nil { | |
| // log.Fatalf("Failed to get nonce: %v", err) | |
| // } | |
| // gasPrice, err := client.SuggestGasPrice(context.Background()) | |
| // if err != nil { | |
| // log.Fatalf("Failed to get gas price: %v", err) | |
| // } | |
| // // Pack the data for the function call | |
| // data, err := parsedABI.Pack("joinRoom", roomId) | |
| // if err != nil { | |
| // log.Fatalf("Failed to pack transaction data: %v", err) | |
| // } | |
| // gasLimit := uint64(300000) | |
| // // Create the transaction | |
| // tx := types.NewTransaction(nonce, contractAddress, big.NewInt(0), gasLimit, gasPrice, data) | |
| // // Sign the transaction | |
| // signedTx, err := types.SignTx(tx, types.NewLondonSigner(chainID), privateKey) | |
| // if err != nil { | |
| // log.Fatalf("Failed to sign transaction: %v", err) | |
| // } | |
| // // Send the transaction | |
| // err = client.SendTransaction(context.Background(), signedTx) | |
| // if err != nil { | |
| // log.Fatalf("Failed to send transaction: %v", err) | |
| // } | |
| // fmt.Printf("Transaction sent! Hash: %s\n", signedTx.Hash().Hex()) | |
| // fmt.Println("Room join initiated. Wait for the transaction to be mined.") | |
| // } | |
| // // Write function - leaveRoom | |
| // func leaveRoom(roomId string) { | |
| // nonce, err := client.PendingNonceAt(context.Background(), fromAddress) | |
| // if err != nil { | |
| // log.Fatalf("Failed to get nonce: %v", err) | |
| // } | |
| // gasPrice, err := client.SuggestGasPrice(context.Background()) | |
| // if err != nil { | |
| // log.Fatalf("Failed to get gas price: %v", err) | |
| // } | |
| // // Pack the data for the function call | |
| // data, err := parsedABI.Pack("leaveRoom", roomId) | |
| // if err != nil { | |
| // log.Fatalf("Failed to pack transaction data: %v", err) | |
| // } | |
| // gasLimit := uint64(300000) | |
| // // Create the transaction | |
| // tx := types.NewTransaction(nonce, contractAddress, big.NewInt(0), gasLimit, gasPrice, data) | |
| // // Sign the transaction | |
| // signedTx, err := types.SignTx(tx, types.NewLondonSigner(chainID), privateKey) | |
| // if err != nil { | |
| // log.Fatalf("Failed to sign transaction: %v", err) | |
| // } | |
| // // Send the transaction | |
| // err = client.SendTransaction(context.Background(), signedTx) | |
| // if err != nil { | |
| // log.Fatalf("Failed to send transaction: %v", err) | |
| // } | |
| // fmt.Printf("Transaction sent! Hash: %s\n", signedTx.Hash().Hex()) | |
| // fmt.Println("Room leave initiated. Wait for the transaction to be mined.") | |
| // } | |
| // // Write function - saveSessionID | |
| // func saveSessionID(roomId, sessionID string) { | |
| // nonce, err := client.PendingNonceAt(context.Background(), fromAddress) | |
| // if err != nil { | |
| // log.Fatalf("Failed to get nonce: %v", err) | |
| // } | |
| // gasPrice, err := client.SuggestGasPrice(context.Background()) | |
| // if err != nil { | |
| // log.Fatalf("Failed to get gas price: %v", err) | |
| // } | |
| // // Pack the data for the function call | |
| // data, err := parsedABI.Pack("saveSessionID", roomId, sessionID) | |
| // if err != nil { | |
| // log.Fatalf("Failed to pack transaction data: %v", err) | |
| // } | |
| // gasLimit := uint64(300000) | |
| // // Create the transaction | |
| // tx := types.NewTransaction(nonce, contractAddress, big.NewInt(0), gasLimit, gasPrice, data) | |
| // // Sign the transaction | |
| // signedTx, err := types.SignTx(tx, types.NewLondonSigner(chainID), privateKey) | |
| // if err != nil { | |
| // log.Fatalf("Failed to sign transaction: %v", err) | |
| // } | |
| // // Send the transaction | |
| // err = client.SendTransaction(context.Background(), signedTx) | |
| // if err != nil { | |
| // log.Fatalf("Failed to send transaction: %v", err) | |
| // } | |
| // fmt.Printf("Transaction sent! Hash: %s\n", signedTx.Hash().Hex()) | |
| // fmt.Println("Session ID save initiated. Wait for the transaction to be mined.") | |
| // } | |
| // // Write function - updateRoomMetadata | |
| // func updateRoomMetadata(roomId, key, value string) { | |
| // nonce, err := client.PendingNonceAt(context.Background(), fromAddress) | |
| // if err != nil { | |
| // log.Fatalf("Failed to get nonce: %v", err) | |
| // } | |
| // gasPrice, err := client.SuggestGasPrice(context.Background()) | |
| // if err != nil { | |
| // log.Fatalf("Failed to get gas price: %v", err) | |
| // } | |
| // // Pack the data for the function call | |
| // data, err := parsedABI.Pack("updateRoomMetadata", roomId, key, value) | |
| // if err != nil { | |
| // log.Fatalf("Failed to pack transaction data: %v", err) | |
| // } | |
| // gasLimit := uint64(300000) | |
| // // Create the transaction | |
| // tx := types.NewTransaction(nonce, contractAddress, big.NewInt(0), gasLimit, gasPrice, data) | |
| // // Sign the transaction | |
| // signedTx, err := types.SignTx(tx, types.NewLondonSigner(chainID), privateKey) | |
| // if err != nil { | |
| // log.Fatalf("Failed to sign transaction: %v", err) | |
| // } | |
| // // Send the transaction | |
| // err = client.SendTransaction(context.Background(), signedTx) | |
| // if err != nil { | |
| // log.Fatalf("Failed to send transaction: %v", err) | |
| // } | |
| // fmt.Printf("Transaction sent! Hash: %s\n", signedTx.Hash().Hex()) | |
| // fmt.Println("Metadata update initiated. Wait for the transaction to be mined.") | |
| // } | |
| // // Write function - setRoomStatus | |
| // func setRoomStatus(roomId string, isActive bool) { | |
| // nonce, err := client.PendingNonceAt(context.Background(), fromAddress) | |
| // if err != nil { | |
| // log.Fatalf("Failed to get nonce: %v", err) | |
| // } | |
| // gasPrice, err := client.SuggestGasPrice(context.Background()) | |
| // if err != nil { | |
| // log.Fatalf("Failed to get gas price: %v", err) | |
| // } | |
| // // Pack the data for the function call | |
| // data, err := parsedABI.Pack("setRoomStatus", roomId, isActive) | |
| // if err != nil { | |
| // log.Fatalf("Failed to pack transaction data: %v", err) | |
| // } | |
| // gasLimit := uint64(300000) | |
| // // Create the transaction | |
| // tx := types.NewTransaction(nonce, contractAddress, big.NewInt(0), gasLimit, gasPrice, data) | |
| // // Sign the transaction | |
| // signedTx, err := types.SignTx(tx, types.NewLondonSigner(chainID), privateKey) | |
| // if err != nil { | |
| // log.Fatalf("Failed to sign transaction: %v", err) | |
| // } | |
| // // Send the transaction | |
| // err = client.SendTransaction(context.Background(), signedTx) | |
| // if err != nil { | |
| // log.Fatalf("Failed to send transaction: %v", err) | |
| // } | |
| // fmt.Printf("Transaction sent! Hash: %s\n", signedTx.Hash().Hex()) | |
| // fmt.Println("Room status update initiated. Wait for the transaction to be mined.") | |
| // } | |
| // func main() { | |
| // fmt.Println("Smart Contract Testing Tool") | |
| // fmt.Println("===========================") | |
| // for { | |
| // fmt.Println("\nSelect an operation:") | |
| // fmt.Println("Read Operations:") | |
| // fmt.Println("1. Get All Rooms") | |
| // fmt.Println("2. Get Room Details") | |
| // fmt.Println("3. Get Room Participants") | |
| // fmt.Println("4. Get ICE Servers") | |
| // fmt.Println("\nWrite Operations:") | |
| // fmt.Println("5. Create Room") | |
| // fmt.Println("6. Join Room") | |
| // fmt.Println("7. Leave Room") | |
| // fmt.Println("8. Save Session ID") | |
| // fmt.Println("9. Update Room Metadata") | |
| // fmt.Println("10. Set Room Status") | |
| // fmt.Println("\n0. Exit") | |
| // var choice int | |
| // fmt.Print("\nEnter your choice: ") | |
| // fmt.Scan(&choice) | |
| // switch choice { | |
| // case 0: | |
| // fmt.Println("Exiting...") | |
| // return | |
| // case 1: // Get All Rooms | |
| // getAllRooms() | |
| // case 2: // Get Room Details | |
| // var roomId string | |
| // fmt.Print("Enter Room ID: ") | |
| // fmt.Scan(&roomId) | |
| // getRoomDetails(roomId) | |
| // case 3: // Get Room Participants | |
| // var roomId string | |
| // fmt.Print("Enter Room ID: ") | |
| // fmt.Scan(&roomId) | |
| // getRoomParticipants(roomId) | |
| // case 4: // Get ICE Servers | |
| // getIceServers() | |
| // case 5: // Create Room | |
| // var roomId, name, metadata string | |
| // fmt.Print("Enter Room ID: ") | |
| // fmt.Scan(&roomId) | |
| // fmt.Print("Enter Room Name: ") | |
| // fmt.Scan(&name) | |
| // fmt.Print("Enter Metadata (JSON string): ") | |
| // fmt.Scan(&metadata) | |
| // createRoom(roomId, name, metadata) | |
| // case 6: // Join Room | |
| // var roomId string | |
| // fmt.Print("Enter Room ID: ") | |
| // fmt.Scan(&roomId) | |
| // joinRoom(roomId) | |
| // case 7: // Leave Room | |
| // var roomId string | |
| // fmt.Print("Enter Room ID: ") | |
| // fmt.Scan(&roomId) | |
| // leaveRoom(roomId) | |
| // case 8: // Save Session ID | |
| // var roomId, sessionID string | |
| // fmt.Print("Enter Room ID: ") | |
| // fmt.Scan(&roomId) | |
| // fmt.Print("Enter Session ID: ") | |
| // fmt.Scan(&sessionID) | |
| // saveSessionID(roomId, sessionID) | |
| // case 9: // Update Room Metadata | |
| // var roomId, key, value string | |
| // fmt.Print("Enter Room ID: ") | |
| // fmt.Scan(&roomId) | |
| // fmt.Print("Enter Metadata Key: ") | |
| // fmt.Scan(&key) | |
| // fmt.Print("Enter Metadata Value: ") | |
| // fmt.Scan(&value) | |
| // updateRoomMetadata(roomId, key, value) | |
| // case 10: // Set Room Status | |
| // var roomId string | |
| // var status int | |
| // fmt.Print("Enter Room ID: ") | |
| // fmt.Scan(&roomId) | |
| // fmt.Print("Enter Status (1 for active, 0 for inactive): ") | |
| // fmt.Scan(&status) | |
| // isActive := status == 1 | |
| // setRoomStatus(roomId, isActive) | |
| // default: | |
| // fmt.Println("Invalid choice. Please try again.") | |
| // } | |
| // fmt.Println("\nPress Enter to continue...") | |
| // fmt.Scanln() // Wait for Enter | |
| // } | |
| // } | |