From f7eab5db0050b8d00634e9d1e3c6a83b92abeab9 Mon Sep 17 00:00:00 2001 From: kompotkot Date: Fri, 16 Aug 2024 12:58:05 +0000 Subject: [PATCH] Usage of registered contract id as dropper address --- moonstream.go | 39 +++++++++++++++++++++++++++++++++++++++ server.go | 31 ++++++++++++++++++++++++++----- 2 files changed, 65 insertions(+), 5 deletions(-) diff --git a/moonstream.go b/moonstream.go index def982c..bf247c1 100644 --- a/moonstream.go +++ b/moonstream.go @@ -14,6 +14,7 @@ import ( type RegisteredContract struct { Id string `json:"id"` Blockchain string `json:"blockchain"` + ChainId int64 `json:"chain_id"` Address string `json:"address"` MetatxRequesterId string `json:"metatx_requester_id"` Title string `json:"title"` @@ -143,6 +144,44 @@ func (client *MoonstreamEngineAPIClient) ListRegisteredContracts(accessToken, bl return contracts, nil } +func (client *MoonstreamEngineAPIClient) GetRegisteredContract(accessToken, contractId string) (int, RegisteredContract, string) { + var contract RegisteredContract + + request, requestCreationErr := http.NewRequest("GET", fmt.Sprintf("%s/metatx/contracts/%s", client.BaseURL, contractId), nil) + if requestCreationErr != nil { + log.Printf("Unable to create request, error: %v", requestCreationErr) + return 500, contract, "" + } + + request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", accessToken)) + request.Header.Add("Accept", "application/json") + + response, responseErr := client.HTTPClient.Do(request) + if responseErr != nil { + log.Printf("Unable to do request, error: %v", responseErr) + return 500, contract, "" + } + defer response.Body.Close() + + responseBody, responseBodyErr := io.ReadAll(response.Body) + if responseBodyErr != nil { + log.Printf("Unable to parse response body, error: %v", responseBodyErr) + return response.StatusCode, contract, response.Status + } + + if response.StatusCode != 200 { + return response.StatusCode, contract, string(responseBody) + } + + unmarshalErr := json.Unmarshal(responseBody, &contract) + if unmarshalErr != nil { + log.Printf("Could not parse response body, error: %s", unmarshalErr) + return response.StatusCode, contract, response.Status + } + + return response.StatusCode, contract, response.Status +} + func (client *MoonstreamEngineAPIClient) ListCallRequests(accessToken, contractId, contractAddress, caller string, limit, offset int, showExpired bool) ([]CallRequest, error) { var callRequests []CallRequest diff --git a/server.go b/server.go index 464f829..e304697 100644 --- a/server.go +++ b/server.go @@ -52,11 +52,12 @@ type SignersResponse struct { } type SignDropperRequest struct { - ChainId int64 `json:"chain_id"` - Dropper string `json:"dropper"` - TtlDays int `json:"ttl_days"` - Sensible bool `json:"sensible"` - Requests []*DropperClaimMessage `json:"requests"` + ChainId int64 `json:"chain_id"` + Dropper string `json:"dropper"` + RegisteredContractId string `json:"registered_contract_id"` + TtlDays int `json:"ttl_days"` + Sensible bool `json:"sensible"` + Requests []*DropperClaimMessage `json:"requests"` NoMetatx bool `json:"no_metatx"` NoCheckMetatx bool `json:"no_check_metatx"` @@ -462,6 +463,26 @@ func (server *Server) signDropperRoute(w http.ResponseWriter, r *http.Request, s return } + if req.Dropper == "" && req.RegisteredContractId == "" { + http.Error(w, "Dropper address or registered contract ID should be specified", http.StatusBadRequest) + return + } + + if req.RegisteredContractId != "" { + contractStatusCode, registeredContract, contractStatus := server.MoonstreamEngineAPIClient.GetRegisteredContract(authorizationToken, req.RegisteredContractId) + if contractStatusCode == 500 { + http.Error(w, "Internal server error", http.StatusInternalServerError) + return + } + if contractStatusCode != 200 { + http.Error(w, contractStatus, contractStatusCode) + return + } + + req.ChainId = registeredContract.ChainId + req.Dropper = registeredContract.Address + } + batchSize := 100 callRequestsLen := len(req.Requests)