|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "log" |
| 7 | + "net/http" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/coinbase/rosetta-sdk-go/asserter" |
| 11 | + "github.com/coinbase/rosetta-sdk-go/client" |
| 12 | + "github.com/coinbase/rosetta-sdk-go/types" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + // serverURL is the URL of a Rosetta Server. |
| 17 | + serverURL = "http://localhost:8080" |
| 18 | + |
| 19 | + // agent is the user-agent on requests to the |
| 20 | + // Rosetta Server. |
| 21 | + agent = "ckb-rosetta-sdk-go" |
| 22 | + |
| 23 | + // defaultTimeout is the default timeout for |
| 24 | + // HTTP requests. |
| 25 | + defaultTimeout = 10 * time.Second |
| 26 | +) |
| 27 | + |
| 28 | +func main() { |
| 29 | + ctx := context.Background() |
| 30 | + |
| 31 | + // Step 1: Create a client |
| 32 | + clientCfg := client.NewConfiguration( |
| 33 | + serverURL, |
| 34 | + agent, |
| 35 | + &http.Client{ |
| 36 | + Timeout: defaultTimeout, |
| 37 | + }, |
| 38 | + ) |
| 39 | + |
| 40 | + client := client.NewAPIClient(clientCfg) |
| 41 | + |
| 42 | + // Step 2: Get all available networks |
| 43 | + networkList, rosettaErr, err := client.NetworkAPI.NetworkList( |
| 44 | + ctx, |
| 45 | + &types.MetadataRequest{}, |
| 46 | + ) |
| 47 | + if rosettaErr != nil { |
| 48 | + log.Printf("Rosetta Error: %+v\n", rosettaErr) |
| 49 | + } |
| 50 | + if err != nil { |
| 51 | + log.Fatal(err) |
| 52 | + } |
| 53 | + |
| 54 | + if len(networkList.NetworkIdentifiers) == 0 { |
| 55 | + log.Fatal("no available networks") |
| 56 | + } |
| 57 | + |
| 58 | + primaryNetwork := networkList.NetworkIdentifiers[0] |
| 59 | + |
| 60 | + // Step 3: Print the primary network |
| 61 | + prettyPrimaryNetwork, err := json.MarshalIndent(primaryNetwork, "", " ") |
| 62 | + if err != nil { |
| 63 | + log.Fatal(err) |
| 64 | + } |
| 65 | + log.Printf("Primary Network: %s\n", string(prettyPrimaryNetwork)) |
| 66 | + |
| 67 | + // Step 4: Fetch the network status |
| 68 | + networkStatus, rosettaErr, err := client.NetworkAPI.NetworkStatus( |
| 69 | + ctx, |
| 70 | + &types.NetworkRequest{ |
| 71 | + NetworkIdentifier: primaryNetwork, |
| 72 | + }, |
| 73 | + ) |
| 74 | + if rosettaErr != nil { |
| 75 | + log.Printf("Rosetta Error: %+v\n", rosettaErr) |
| 76 | + } |
| 77 | + if err != nil { |
| 78 | + log.Fatal(err) |
| 79 | + } |
| 80 | + |
| 81 | + // Step 5: Print the response |
| 82 | + prettyNetworkStatus, err := json.MarshalIndent(networkStatus, "", " ") |
| 83 | + if err != nil { |
| 84 | + log.Fatal(err) |
| 85 | + } |
| 86 | + log.Printf("Network Status: %s\n", string(prettyNetworkStatus)) |
| 87 | + |
| 88 | + // Step 6: Assert the response is valid |
| 89 | + err = asserter.NetworkStatusResponse(networkStatus) |
| 90 | + if err != nil { |
| 91 | + log.Fatalf("Assertion Error: %s\n", err.Error()) |
| 92 | + } |
| 93 | + |
| 94 | + // Step 7: Fetch the network options |
| 95 | + networkOptions, rosettaErr, err := client.NetworkAPI.NetworkOptions( |
| 96 | + ctx, |
| 97 | + &types.NetworkRequest{ |
| 98 | + NetworkIdentifier: primaryNetwork, |
| 99 | + }, |
| 100 | + ) |
| 101 | + if rosettaErr != nil { |
| 102 | + log.Printf("Rosetta Error: %+v\n", rosettaErr) |
| 103 | + } |
| 104 | + if err != nil { |
| 105 | + log.Fatal(err) |
| 106 | + } |
| 107 | + |
| 108 | + // Step 8: Print the response |
| 109 | + prettyNetworkOptions, err := json.MarshalIndent(networkOptions, "", " ") |
| 110 | + if err != nil { |
| 111 | + log.Fatal(err) |
| 112 | + } |
| 113 | + log.Printf("Network Options: %s\n", string(prettyNetworkOptions)) |
| 114 | + |
| 115 | + // Step 9: Assert the response is valid |
| 116 | + err = asserter.NetworkOptionsResponse(networkOptions) |
| 117 | + if err != nil { |
| 118 | + log.Fatalf("Assertion Error: %s\n", err.Error()) |
| 119 | + } |
| 120 | + |
| 121 | + // Step 10: Create an asserter using the retrieved NetworkStatus and |
| 122 | + // NetworkOptions. |
| 123 | + // |
| 124 | + // This will be used later to assert that a fetched block is |
| 125 | + // valid. |
| 126 | + asserter, err := asserter.NewClientWithResponses( |
| 127 | + primaryNetwork, |
| 128 | + networkStatus, |
| 129 | + networkOptions, |
| 130 | + ) |
| 131 | + if err != nil { |
| 132 | + log.Fatal(err) |
| 133 | + } |
| 134 | + |
| 135 | + // Step 11: Fetch the current block |
| 136 | + block, rosettaErr, err := client.BlockAPI.Block( |
| 137 | + ctx, |
| 138 | + &types.BlockRequest{ |
| 139 | + NetworkIdentifier: primaryNetwork, |
| 140 | + BlockIdentifier: types.ConstructPartialBlockIdentifier( |
| 141 | + networkStatus.CurrentBlockIdentifier, |
| 142 | + ), |
| 143 | + }, |
| 144 | + ) |
| 145 | + if rosettaErr != nil { |
| 146 | + log.Printf("Rosetta Error: %+v\n", rosettaErr) |
| 147 | + } |
| 148 | + if err != nil { |
| 149 | + log.Fatal(err) |
| 150 | + } |
| 151 | + |
| 152 | + // Step 12: Print the block |
| 153 | + prettyBlock, err := json.MarshalIndent(block.Block, "", " ") |
| 154 | + if err != nil { |
| 155 | + log.Fatal(err) |
| 156 | + } |
| 157 | + log.Printf("Current Block: %s\n", string(prettyBlock)) |
| 158 | + |
| 159 | + // Step 13: Assert the block response is valid |
| 160 | + // |
| 161 | + // It is important to note that this only ensures |
| 162 | + // required fields are populated and that operations |
| 163 | + // in the block only use types and statuses that were |
| 164 | + // provided in the networkStatusResponse. To run more |
| 165 | + // intensive validation, use the Rosetta Validator. It |
| 166 | + // can be found at: https://github.com/coinbase/rosetta-validator |
| 167 | + err = asserter.Block(block.Block) |
| 168 | + if err != nil { |
| 169 | + log.Fatalf("Assertion Error: %s\n", err.Error()) |
| 170 | + } |
| 171 | + |
| 172 | + // Step 14: Print remaining transactions to fetch |
| 173 | + // |
| 174 | + // If you want the client to automatically fetch these, consider |
| 175 | + // using the fetcher package. |
| 176 | + for _, txn := range block.OtherTransactions { |
| 177 | + log.Printf("Other Transaction: %+v\n", txn) |
| 178 | + } |
| 179 | + |
| 180 | + // Step 15: GetAccount |
| 181 | + account, rosettaErr, err := client.AccountAPI.AccountBalance(ctx, &types.AccountBalanceRequest{ |
| 182 | + NetworkIdentifier: primaryNetwork, |
| 183 | + AccountIdentifier: &types.AccountIdentifier{ |
| 184 | + Address: "ckb1qyqxsztqvpfdyu00kt99hxgxcwr2l4z67ars5nv5pp", |
| 185 | + }, |
| 186 | + }) |
| 187 | + if rosettaErr != nil { |
| 188 | + log.Printf("Rosetta Error: %+v\n", rosettaErr) |
| 189 | + } |
| 190 | + if err != nil { |
| 191 | + log.Fatal(err) |
| 192 | + } |
| 193 | + |
| 194 | + // Step 16: Print the account |
| 195 | + prettyAccount, err := json.MarshalIndent(account, "", " ") |
| 196 | + if err != nil { |
| 197 | + log.Fatal(err) |
| 198 | + } |
| 199 | + log.Printf("Account: %s\n", string(prettyAccount)) |
| 200 | +} |
0 commit comments