Skip to content

Commit

Permalink
Usage of registered contract id as dropper address
Browse files Browse the repository at this point in the history
  • Loading branch information
kompotkot committed Aug 16, 2024
1 parent c3b3f6a commit f7eab5d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
39 changes: 39 additions & 0 deletions moonstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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

Expand Down
31 changes: 26 additions & 5 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit f7eab5d

Please sign in to comment.