-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
**Summary**: This revamps handling of Cosmos SDK-based chains and greatly simplifying the addition of new chains. The changes simplify the codebase, reduce redundancy, and make it easier to manage and extend. ### Key Updates: 1. **Centralized Cosmos SDK Logic**: - Modified the `FetchCosmosSDKNakaCoeff` function in `cosmos.go` to be exportable and handle all Cosmos SDK-based chains. 2. **New Chain Added**: - Added support for the **Sei** network. - Updated `chain.go` to include Sei in the list of supported chains. 3. **Streamlined Existing Chains**: - Refactored `agoric.go`, `osmosis.go`, `regen.go`, `sei.go`, and `stargaze.go` to use this shared function, cleaning up much redundant code. - Included pool data fetching to ensure more accurate Nakamoto coefficient calculations, since the previous method would not have included any for unbonded validators. [correct me if this seems off] ### Testing and Validation: - **Manual Testing**: Ran tests on each refactored chain to ensure the Nakamoto coefficient calculations are accurate and that everything works as expected. - **Validation**: Checked logs to confirm the refactored code runs smoothly and handles errors correctly. ### Looking Ahead: - **Automated Testing**: May be worth adding automated tests for the `FetchCosmosSDKNakaCoeff` function to ensure it continues to work well across different chains. It should work universally as-is, but this may change in the future. - **Further Simplification**: Could look into whether other non-Cosmos SDK chains could benefit from a similar centralized approach. Overall this should make the Nakamoto coefficient calculator easier to manage and extend, while keeping all the current features intact. Here's the image you requested also.  --------- Co-authored-by: Cordtus <[email protected]>
- Loading branch information
Showing
9 changed files
with
150 additions
and
315 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,8 @@ | ||
package chains | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"log" | ||
"net/http" | ||
"sort" | ||
"strconv" | ||
|
||
utils "github.com/xenowits/nakamoto-coefficient-calculator/core/utils" | ||
) | ||
|
||
type AgoricResponse struct { | ||
Result []struct { | ||
Tokens string `json:"tokens"` | ||
} `json:"result"` | ||
} | ||
|
||
type AgoricErrorResponse struct { | ||
Id int `json:"id"` | ||
Jsonrpc string `json:"jsonrpc"` | ||
Error string `json:"error"` | ||
} | ||
|
||
func Agoric() (int, error) { | ||
votingPowers := make([]int64, 0, 200) | ||
|
||
url := fmt.Sprintf("https://lcd-agoric.keplr.app/staking/validators") | ||
resp, err := http.Get(url) | ||
if err != nil { | ||
errBody, _ := io.ReadAll(resp.Body) | ||
var errResp AgoricErrorResponse | ||
json.Unmarshal(errBody, &errResp) | ||
log.Println(errResp.Error) | ||
return 0, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
var response AgoricResponse | ||
err = json.Unmarshal(body, &response) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
// loop through the validators voting powers | ||
for _, ele := range response.Result { | ||
val, _ := strconv.Atoi(ele.Tokens) | ||
votingPowers = append(votingPowers, int64(val)) | ||
} | ||
|
||
// need to sort the powers in descending order since they are in random order | ||
sort.Slice(votingPowers, func(i, j int) bool { return votingPowers[i] > votingPowers[j] }) | ||
|
||
totalVotingPower := utils.CalculateTotalVotingPower(votingPowers) | ||
fmt.Println("Total voting power:", totalVotingPower) | ||
|
||
// // now we're ready to calculate the nakomoto coefficient | ||
nakamotoCoefficient := utils.CalcNakamotoCoefficient(totalVotingPower, votingPowers) | ||
fmt.Println("The Nakamoto coefficient for agoric is", nakamotoCoefficient) | ||
validatorURL := "https://main.api.agoric.net/cosmos/staking/v1beta1/validators?page.offset=1&pagination.limit=100&status=BOND_STATUS_BONDED" | ||
stakingPoolURL := "https://main.api.agoric.net/cosmos/staking/v1beta1/pool" | ||
|
||
return nakamotoCoefficient, nil | ||
return FetchCosmosSDKNakaCoeff("agoric", validatorURL, stakingPoolURL) | ||
} |
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
Oops, something went wrong.