|
| 1 | +# Bridge Service Client |
| 2 | + |
| 3 | +A Go client library for interacting with the Bridge Service REST API. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +go get github.com/agglayer/aggkit/bridgeservice/client |
| 9 | +``` |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +### Creating a Client |
| 14 | + |
| 15 | +```go |
| 16 | +import "github.com/agglayer/aggkit/bridgeservice/client" |
| 17 | + |
| 18 | +// Create a client with default timeout (30 seconds) |
| 19 | +c := client.New(client.Config{ |
| 20 | + BaseURL: "http://localhost:8080", |
| 21 | +}) |
| 22 | + |
| 23 | +// Create a client with custom timeout |
| 24 | +c := client.New(client.Config{ |
| 25 | + BaseURL: "http://localhost:8080", |
| 26 | + Timeout: 10 * time.Second, |
| 27 | +}) |
| 28 | +``` |
| 29 | + |
| 30 | +### Health Check |
| 31 | + |
| 32 | +```go |
| 33 | +resp, err := c.HealthCheck(ctx) |
| 34 | +if err != nil { |
| 35 | + log.Fatal(err) |
| 36 | +} |
| 37 | +fmt.Printf("Status: %s, Version: %s\n", resp.Status, resp.Version) |
| 38 | +``` |
| 39 | + |
| 40 | +### Get Bridges |
| 41 | + |
| 42 | +```go |
| 43 | +// Minimal parameters |
| 44 | +resp, err := c.GetBridges(ctx, client.GetBridgesParams{ |
| 45 | + NetworkID: 1, |
| 46 | +}) |
| 47 | + |
| 48 | +// With optional parameters |
| 49 | +pageNum := uint32(1) |
| 50 | +pageSize := uint32(20) |
| 51 | +depositCount := uint64(10) |
| 52 | +fromAddr := "0x1234567890123456789012345678901234567890" |
| 53 | + |
| 54 | +resp, err := c.GetBridges(ctx, client.GetBridgesParams{ |
| 55 | + NetworkID: 1, |
| 56 | + PageNumber: &pageNum, |
| 57 | + PageSize: &pageSize, |
| 58 | + DepositCount: &depositCount, |
| 59 | + FromAddress: &fromAddr, |
| 60 | + NetworkIDs: []uint32{2, 3}, |
| 61 | +}) |
| 62 | +``` |
| 63 | + |
| 64 | +### Get Claims |
| 65 | + |
| 66 | +```go |
| 67 | +// Minimal parameters |
| 68 | +resp, err := c.GetClaims(ctx, client.GetClaimsParams{ |
| 69 | + NetworkID: 1, |
| 70 | +}) |
| 71 | + |
| 72 | +// With optional parameters |
| 73 | +includeAll := true |
| 74 | +globalIndex := big.NewInt(123) |
| 75 | + |
| 76 | +resp, err := c.GetClaims(ctx, client.GetClaimsParams{ |
| 77 | + NetworkID: 1, |
| 78 | + IncludeAllFields: &includeAll, |
| 79 | + GlobalIndex: globalIndex, |
| 80 | +}) |
| 81 | +``` |
| 82 | + |
| 83 | +### Get Unset Claims (L2 only) |
| 84 | + |
| 85 | +```go |
| 86 | +pageNum := 1 |
| 87 | +pageSize := 10 |
| 88 | +globalIndex := big.NewInt(456) |
| 89 | + |
| 90 | +resp, err := c.GetUnsetClaims(ctx, client.GetUnsetClaimsParams{ |
| 91 | + PageNumber: &pageNum, |
| 92 | + PageSize: &pageSize, |
| 93 | + GlobalIndex: globalIndex, |
| 94 | +}) |
| 95 | +``` |
| 96 | + |
| 97 | +### Get Set Claims (L2 only) |
| 98 | + |
| 99 | +```go |
| 100 | +resp, err := c.GetSetClaims(ctx, client.GetSetClaimsParams{ |
| 101 | + PageNumber: &pageNum, |
| 102 | + PageSize: &pageSize, |
| 103 | +}) |
| 104 | +``` |
| 105 | + |
| 106 | +### Get Token Mappings |
| 107 | + |
| 108 | +```go |
| 109 | +tokenAddr := "0xabcdef0123456789abcdef0123456789abcdef01" |
| 110 | + |
| 111 | +resp, err := c.GetTokenMappings(ctx, client.GetTokenMappingsParams{ |
| 112 | + NetworkID: 1, |
| 113 | + OriginTokenAddress: &tokenAddr, |
| 114 | +}) |
| 115 | +``` |
| 116 | + |
| 117 | +### Get Legacy Token Migrations |
| 118 | + |
| 119 | +```go |
| 120 | +resp, err := c.GetLegacyTokenMigrations(ctx, client.GetLegacyTokenMigrationsParams{ |
| 121 | + NetworkID: 2, |
| 122 | +}) |
| 123 | +``` |
| 124 | + |
| 125 | +### Get L1 Info Tree Index |
| 126 | + |
| 127 | +```go |
| 128 | +index, err := c.GetL1InfoTreeIndex(ctx, 1, 10) |
| 129 | +if err != nil { |
| 130 | + log.Fatal(err) |
| 131 | +} |
| 132 | +fmt.Printf("L1 Info Tree Index: %d\n", index) |
| 133 | +``` |
| 134 | + |
| 135 | +### Get Injected L1 Info Leaf |
| 136 | + |
| 137 | +```go |
| 138 | +resp, err := c.GetInjectedL1InfoLeaf(ctx, 2, 5) |
| 139 | +if err != nil { |
| 140 | + log.Fatal(err) |
| 141 | +} |
| 142 | +fmt.Printf("L1 Info Tree Index: %d\n", resp.L1InfoTreeIndex) |
| 143 | +``` |
| 144 | + |
| 145 | +### Get Claim Proof |
| 146 | + |
| 147 | +```go |
| 148 | +proof, err := c.GetClaimProof(ctx, 1, 10, 5) |
| 149 | +if err != nil { |
| 150 | + log.Fatal(err) |
| 151 | +} |
| 152 | +fmt.Printf("Proof Local Exit Root: %v\n", proof.ProofLocalExitRoot) |
| 153 | +``` |
| 154 | + |
| 155 | +### Get Last Reorg Event |
| 156 | + |
| 157 | +```go |
| 158 | +// For L1 (networkID = 0) |
| 159 | +resp, err := c.GetLastReorgEvent(ctx, 0) |
| 160 | + |
| 161 | +// For L2 |
| 162 | +resp, err := c.GetLastReorgEvent(ctx, 1) |
| 163 | +``` |
| 164 | + |
| 165 | +### Get Sync Status |
| 166 | + |
| 167 | +```go |
| 168 | +resp, err := c.GetSyncStatus(ctx) |
| 169 | +if err != nil { |
| 170 | + log.Fatal(err) |
| 171 | +} |
| 172 | +fmt.Printf("L1 Synced: %v, L2 Synced: %v\n", resp.L1Info.IsSynced, resp.L2Info.IsSynced) |
| 173 | +``` |
| 174 | + |
| 175 | +### Get Remove GER Events |
| 176 | + |
| 177 | +```go |
| 178 | +// Without filters |
| 179 | +resp, err := c.GetRemoveGEREvents(ctx, client.GetRemoveGEREventsParams{}) |
| 180 | + |
| 181 | +// With filters |
| 182 | +ger := "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef" |
| 183 | +limit := 25 |
| 184 | + |
| 185 | +resp, err := c.GetRemoveGEREvents(ctx, client.GetRemoveGEREventsParams{ |
| 186 | + GlobalExitRoot: &ger, |
| 187 | + Limit: &limit, |
| 188 | +}) |
| 189 | +``` |
| 190 | + |
| 191 | +## Error Handling |
| 192 | + |
| 193 | +All methods return errors that should be checked: |
| 194 | + |
| 195 | +```go |
| 196 | +resp, err := c.GetBridges(ctx, params) |
| 197 | +if err != nil { |
| 198 | + // Handle error |
| 199 | + // Error messages include HTTP status codes and response bodies |
| 200 | + log.Printf("Error: %v", err) |
| 201 | + return |
| 202 | +} |
| 203 | +// Use response |
| 204 | +``` |
| 205 | + |
| 206 | +## Context Support |
| 207 | + |
| 208 | +All API methods accept a `context.Context` parameter for cancellation and timeout control: |
| 209 | + |
| 210 | +```go |
| 211 | +// With timeout |
| 212 | +ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 213 | +defer cancel() |
| 214 | + |
| 215 | +resp, err := c.GetBridges(ctx, params) |
| 216 | + |
| 217 | +// With cancellation |
| 218 | +ctx, cancel := context.WithCancel(context.Background()) |
| 219 | +// Cancel when needed |
| 220 | +cancel() |
| 221 | +``` |
| 222 | + |
| 223 | +## Testing |
| 224 | + |
| 225 | +Run the tests: |
| 226 | + |
| 227 | +```bash |
| 228 | +go test ./bridgeservice/client/... |
| 229 | +``` |
| 230 | + |
| 231 | +Run with coverage: |
| 232 | + |
| 233 | +```bash |
| 234 | +go test -cover ./bridgeservice/client/... |
| 235 | +``` |
| 236 | + |
| 237 | +Current test coverage: **91.9%** |
0 commit comments