-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
start implementation of submit consolidations page
- Loading branch information
Showing
19 changed files
with
945 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
package handlers | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
"time" | ||
|
||
v1 "github.com/attestantio/go-eth2-client/api/v1" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/sirupsen/logrus" | ||
|
||
"github.com/ethpandaops/dora/indexer/execution" | ||
"github.com/ethpandaops/dora/services" | ||
"github.com/ethpandaops/dora/templates" | ||
"github.com/ethpandaops/dora/types/models" | ||
"github.com/ethpandaops/dora/utils" | ||
) | ||
|
||
// SubmitConsolidation will submit a consolidation request | ||
func SubmitConsolidation(w http.ResponseWriter, r *http.Request) { | ||
var submitConsolidationTemplateFiles = append(layoutTemplateFiles, | ||
"submit_consolidation/submit_consolidation.html", | ||
) | ||
var pageTemplate = templates.GetTemplate(submitConsolidationTemplateFiles...) | ||
|
||
if !utils.Config.Frontend.ShowSubmitConsolidation { | ||
handlePageError(w, r, errors.New("submit consolidation is not enabled")) | ||
return | ||
} | ||
|
||
query := r.URL.Query() | ||
if query.Has("ajax") { | ||
err := handleSubmitConsolidationPageDataAjax(w, r) | ||
if err != nil { | ||
handlePageError(w, r, err) | ||
} | ||
return | ||
} | ||
|
||
pageData, pageError := getSubmitConsolidationPageData() | ||
if pageError != nil { | ||
handlePageError(w, r, pageError) | ||
return | ||
} | ||
if pageData == nil { | ||
data := InitPageData(w, r, "blockchain", "/submit_consolidation", "Submit Consolidation", submitConsolidationTemplateFiles) | ||
w.Header().Set("Content-Type", "text/html") | ||
if handleTemplateError(w, r, "submit_consolidation.go", "Submit Consolidation", "", pageTemplate.ExecuteTemplate(w, "layout", data)) != nil { | ||
return // an error has occurred and was processed | ||
} | ||
return | ||
} | ||
|
||
data := InitPageData(w, r, "blockchain", "/submit_consolidation", "Submit Consolidation", submitConsolidationTemplateFiles) | ||
data.Data = pageData | ||
w.Header().Set("Content-Type", "text/html") | ||
if handleTemplateError(w, r, "submit_consolidation.go", "Submit Consolidation", "", pageTemplate.ExecuteTemplate(w, "layout", data)) != nil { | ||
return // an error has occurred and was processed | ||
} | ||
} | ||
|
||
func getSubmitConsolidationPageData() (*models.SubmitConsolidationPageData, error) { | ||
pageData := &models.SubmitConsolidationPageData{} | ||
pageCacheKey := "submit_consolidation" | ||
pageRes, pageErr := services.GlobalFrontendCache.ProcessCachedPage(pageCacheKey, true, pageData, func(pageCall *services.FrontendCacheProcessingPage) interface{} { | ||
pageData, cacheTimeout := buildSubmitConsolidationPageData() | ||
pageCall.CacheTimeout = cacheTimeout | ||
return pageData | ||
}) | ||
if pageErr == nil && pageRes != nil { | ||
resData, resOk := pageRes.(*models.SubmitConsolidationPageData) | ||
if !resOk { | ||
return nil, ErrInvalidPageModel | ||
} | ||
pageData = resData | ||
} | ||
return pageData, pageErr | ||
} | ||
|
||
func buildSubmitConsolidationPageData() (*models.SubmitConsolidationPageData, time.Duration) { | ||
logrus.Debugf("submit consolidation page called") | ||
|
||
chainState := services.GlobalBeaconService.GetChainState() | ||
specs := chainState.GetSpecs() | ||
|
||
pageData := &models.SubmitConsolidationPageData{ | ||
NetworkName: specs.ConfigName, | ||
PublicRPCUrl: utils.Config.Frontend.PublicRPCUrl, | ||
RainbowkitProjectId: utils.Config.Frontend.RainbowkitProjectId, | ||
ChainId: specs.DepositChainId, | ||
ConsolidationContract: execution.ConsolidationContractAddr, | ||
} | ||
|
||
return pageData, 1 * time.Hour | ||
} | ||
|
||
func handleSubmitConsolidationPageDataAjax(w http.ResponseWriter, r *http.Request) error { | ||
query := r.URL.Query() | ||
var pageData interface{} | ||
|
||
switch query.Get("ajax") { | ||
case "load_validators": | ||
address := query.Get("address") | ||
addressBytes := common.HexToAddress(address) | ||
|
||
validators := services.GlobalBeaconService.GetCachedValidatorSet() | ||
result := []models.SubmitConsolidationPageDataValidator{} | ||
for _, validator := range validators { | ||
if validator.Validator.WithdrawalCredentials[0] == 0x00 && false { | ||
continue | ||
} | ||
|
||
if !bytes.Equal(validator.Validator.WithdrawalCredentials[12:], addressBytes[:]) && false { | ||
continue | ||
} | ||
|
||
var status string | ||
if strings.HasPrefix(validator.Status.String(), "pending") { | ||
status = "Pending" | ||
} else if validator.Status == v1.ValidatorStateActiveOngoing { | ||
status = "Active" | ||
} else if validator.Status == v1.ValidatorStateActiveExiting { | ||
status = "Exiting" | ||
} else if validator.Status == v1.ValidatorStateActiveSlashed { | ||
status = "Slashed" | ||
} else if validator.Status == v1.ValidatorStateExitedUnslashed { | ||
status = "Exited" | ||
} else if validator.Status == v1.ValidatorStateExitedSlashed { | ||
status = "Slashed" | ||
} else { | ||
status = validator.Status.String() | ||
} | ||
|
||
result = append(result, models.SubmitConsolidationPageDataValidator{ | ||
Index: uint64(validator.Index), | ||
Pubkey: validator.Validator.PublicKey.String(), | ||
Balance: uint64(validator.Balance), | ||
CredType: fmt.Sprintf("%x", validator.Validator.WithdrawalCredentials[0]), | ||
Status: status, | ||
}) | ||
} | ||
|
||
pageData = result | ||
default: | ||
return errors.New("invalid ajax request") | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
err := json.NewEncoder(w).Encode(pageData) | ||
if err != nil { | ||
logrus.WithError(err).Error("error encoding index data") | ||
http.Error(w, "Internal server error", http.StatusServiceUnavailable) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
{{ define "page" }} | ||
<div class="container mt-2"> | ||
<div class="d-md-flex py-2 justify-content-md-between"> | ||
<h1 class="h4 mb-1 mb-md-0"> | ||
<i class="fas fa-square-plus mx-2"></i> Submit Consolidation | ||
</h1> | ||
<nav aria-label="breadcrumb"> | ||
<ol class="breadcrumb font-size-1 mb-0" style="padding:0; background-color:transparent;"> | ||
<li class="breadcrumb-item"><a href="/" title="Home">Home</a></li> | ||
<li class="breadcrumb-item"><a href="/validators" title="Validators">Validators</a></li> | ||
<li class="breadcrumb-item active" aria-current="page">Submit Consolidation</li> | ||
</ol> | ||
</nav> | ||
</div> | ||
|
||
<div id="header-placeholder" style="height:35px;"></div> | ||
|
||
<div class="card mt-2"> | ||
<div class="card-body"> | ||
<noscript> | ||
<div class="alert alert-warning"> | ||
<i class="fa fa-exclamation-triangle"></i> | ||
This page requires JavaScript to be enabled. | ||
</div> | ||
</noscript> | ||
<div id="submit-consolidation-container"></div> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
{{ end }} | ||
{{ define "js" }} | ||
<script src="/ui-package/react-ui.js"></script> | ||
<script type="text/javascript"> | ||
$(function() { | ||
window.doraUiComponents.SubmitConsolidationsForm( | ||
document.getElementById("submit-consolidation-container"), | ||
{ | ||
wagmiConfig: { | ||
projectId: "{{ .RainbowkitProjectId }}", | ||
chains: [ | ||
{ | ||
chainId: {{ .ChainId }}, | ||
name: "{{ .NetworkName }}", | ||
rpcUrl: "{{ .PublicRPCUrl }}", | ||
tokenName: "Ethereum", | ||
tokenSymbol: "ETH", | ||
} | ||
] | ||
}, | ||
submitConsolidationsConfig: { | ||
consolidationContract: "{{ .ConsolidationContract }}", | ||
loadValidatorsCallback: function(address) { | ||
return fetch(`?ajax=load_validators&address=${address}`) | ||
.then(response => response.json()) | ||
.then(data => { | ||
console.log(data); | ||
return data; | ||
}); | ||
} | ||
} | ||
} | ||
); | ||
}); | ||
</script> | ||
{{ end }} | ||
{{ define "css" }} | ||
{{ end }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package models | ||
|
||
type SubmitConsolidationPageData struct { | ||
NetworkName string `json:"netname"` | ||
PublicRPCUrl string `json:"pubrpc"` | ||
RainbowkitProjectId string `json:"rainbowkit"` | ||
ChainId uint64 `json:"chainid"` | ||
ConsolidationContract string `json:"consolidationcontract"` | ||
} | ||
|
||
type SubmitConsolidationPageDataValidator struct { | ||
Index uint64 `json:"index"` | ||
Pubkey string `json:"pubkey"` | ||
Balance uint64 `json:"balance"` | ||
CredType string `json:"credtype"` | ||
Status string `json:"status"` | ||
} |
Oops, something went wrong.