github.com/amazechain/amc@v0.1.3/modules/rpc/jsonrpc/client_example_test.go (about)

     1  // Copyright 2023 The AmazeChain Authors
     2  // This file is part of the AmazeChain library.
     3  //
     4  // The AmazeChain library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The AmazeChain library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package jsonrpc_test
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"github.com/amazechain/amc/common/hexutil"
    23  	"github.com/amazechain/amc/modules/rpc/jsonrpc"
    24  )
    25  
    26  // In this example, our client wishes to track the latest 'block number'
    27  // known to the server. The server supports two methods:
    28  //
    29  // eth_getBlockByNumber("latest", {})
    30  //    returns the latest block object.
    31  //
    32  // eth_subscribe("newHeads")
    33  //    creates a subscription which fires block objects when new blocks arrive.
    34  
    35  type Block struct {
    36  	Number *hexutil.Big
    37  }
    38  
    39  //func TestExampleClientSubscription(t *testing.T) {
    40  //	// Connect the client.
    41  //	client, err := jsonrpc.Dial("ws://127.0.0.1:20013")
    42  //	if nil != err {
    43  //		panic(err)
    44  //	}
    45  //	subch := make(chan Block)
    46  //
    47  //	// Ensure that subch receives the latest block.
    48  //	//go func() {
    49  //	//	for i := 0; ; i++ {
    50  //	//		if i > 0 {
    51  //	//			time.Sleep(2 * time.Second)
    52  //	//		}
    53  //	subscribeBlocks(client, subch)
    54  //	//	}
    55  //	//}()
    56  //
    57  //	// Print events from the subscription as they arrive.
    58  //	for block := range subch {
    59  //		fmt.Println("latest block:", block.Number)
    60  //	}
    61  //}
    62  
    63  // subscribeBlocks runs in its own goroutine and maintains
    64  // a subscription for new blocks.
    65  func subscribeBlocks(client *jsonrpc.Client, subch chan Block) {
    66  	//ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    67  	//defer cancel()
    68  
    69  	ctx := context.Background()
    70  
    71  	// Subscribe to new blocks.
    72  	sub, err := client.Subscribe(ctx, "eth", subch, "newHeads")
    73  	if err != nil {
    74  		fmt.Println("subscribe error:", err)
    75  		return
    76  	}
    77  
    78  	// The connection is established now.
    79  	// Update the channel with the current block.
    80  	//var lastBlock Block
    81  	//err = client.CallContext(ctx, &lastBlock, "eth_getBlockByNumber", "latest", false)
    82  	//if err != nil {
    83  	//	fmt.Println("can't get latest block:", err)
    84  	//	return
    85  	//}
    86  	//subch <- lastBlock
    87  
    88  	// The subscription will deliver events to the channel. Wait for the
    89  	// subscription to end for any reason, then loop around to re-establish
    90  	// the connection.
    91  	fmt.Println("connection lost: ", <-sub.Err())
    92  }