|
| 1 | +// Copyright © 2022-2026 Obol Labs Inc. Licensed under the terms of a Business Source License 1.1 |
| 2 | + |
| 3 | +package obolapi |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "encoding/hex" |
| 8 | + "encoding/json" |
| 9 | + "net/url" |
| 10 | + "strconv" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/obolnetwork/charon/app/errors" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + submitPartialFeeRecipientTmpl = "/fee_recipient/partial/" + lockHashPath + "/" + shareIndexPath |
| 18 | + fetchPartialFeeRecipientTmpl = "/fee_recipient/" + lockHashPath + "/" + valPubkeyPath |
| 19 | +) |
| 20 | + |
| 21 | +// submitPartialFeeRecipientURL returns the partial fee recipient Obol API URL for a given lock hash. |
| 22 | +func submitPartialFeeRecipientURL(lockHash string, shareIndex uint64) string { |
| 23 | + return strings.NewReplacer( |
| 24 | + lockHashPath, |
| 25 | + lockHash, |
| 26 | + shareIndexPath, |
| 27 | + strconv.FormatUint(shareIndex, 10), |
| 28 | + ).Replace(submitPartialFeeRecipientTmpl) |
| 29 | +} |
| 30 | + |
| 31 | +// fetchPartialFeeRecipientURL returns the partial fee recipient Obol API URL for a given validator public key. |
| 32 | +func fetchPartialFeeRecipientURL(valPubkey, lockHash string) string { |
| 33 | + return strings.NewReplacer( |
| 34 | + valPubkeyPath, |
| 35 | + valPubkey, |
| 36 | + lockHashPath, |
| 37 | + lockHash, |
| 38 | + ).Replace(fetchPartialFeeRecipientTmpl) |
| 39 | +} |
| 40 | + |
| 41 | +// PostPartialFeeRecipients POSTs partial fee recipient registrations to the Obol API. |
| 42 | +// It respects the timeout specified in the Client instance. |
| 43 | +func (c Client) PostPartialFeeRecipients(ctx context.Context, lockHash []byte, shareIndex uint64, partialRegs []PartialRegistration) error { |
| 44 | + lockHashStr := "0x" + hex.EncodeToString(lockHash) |
| 45 | + |
| 46 | + path := submitPartialFeeRecipientURL(lockHashStr, shareIndex) |
| 47 | + |
| 48 | + u, err := url.ParseRequestURI(c.baseURL) |
| 49 | + if err != nil { |
| 50 | + return errors.Wrap(err, "bad Obol API url") |
| 51 | + } |
| 52 | + |
| 53 | + u.Path = path |
| 54 | + |
| 55 | + req := PartialFeeRecipientRequest{PartialRegistrations: partialRegs} |
| 56 | + |
| 57 | + data, err := json.Marshal(req) |
| 58 | + if err != nil { |
| 59 | + return errors.Wrap(err, "json marshal error") |
| 60 | + } |
| 61 | + |
| 62 | + ctx, cancel := context.WithTimeout(ctx, c.reqTimeout) |
| 63 | + defer cancel() |
| 64 | + |
| 65 | + err = httpPost(ctx, u, data, nil) |
| 66 | + if err != nil { |
| 67 | + return errors.Wrap(err, "http Obol API POST request") |
| 68 | + } |
| 69 | + |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +// GetPartialFeeRecipients fetches partial fee recipient registrations from the Obol API. |
| 74 | +// It respects the timeout specified in the Client instance. |
| 75 | +func (c Client) GetPartialFeeRecipients(ctx context.Context, valPubkey string, lockHash []byte, _ int) (PartialFeeRecipientResponse, error) { |
| 76 | + path := fetchPartialFeeRecipientURL(valPubkey, "0x"+hex.EncodeToString(lockHash)) |
| 77 | + |
| 78 | + u, err := url.ParseRequestURI(c.baseURL) |
| 79 | + if err != nil { |
| 80 | + return PartialFeeRecipientResponse{}, errors.Wrap(err, "bad Obol API url") |
| 81 | + } |
| 82 | + |
| 83 | + u.Path = path |
| 84 | + |
| 85 | + ctx, cancel := context.WithTimeout(ctx, c.reqTimeout) |
| 86 | + defer cancel() |
| 87 | + |
| 88 | + respBody, err := httpGet(ctx, u, map[string]string{}) |
| 89 | + if err != nil { |
| 90 | + return PartialFeeRecipientResponse{}, errors.Wrap(err, "http Obol API GET request") |
| 91 | + } |
| 92 | + |
| 93 | + defer respBody.Close() |
| 94 | + |
| 95 | + var resp PartialFeeRecipientResponse |
| 96 | + if err := json.NewDecoder(respBody).Decode(&resp); err != nil { |
| 97 | + return PartialFeeRecipientResponse{}, errors.Wrap(err, "unmarshal response") |
| 98 | + } |
| 99 | + |
| 100 | + if len(resp.Partials) == 0 { |
| 101 | + return PartialFeeRecipientResponse{}, ErrNoValue |
| 102 | + } |
| 103 | + |
| 104 | + return resp, nil |
| 105 | +} |
0 commit comments