-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Cardano chain #43
Open
FilippoScaramuzza
wants to merge
8
commits into
xenowits:main
Choose a base branch
from
FilippoScaramuzza:add-cardano
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fc97144
feat: added cardano
FilippoScaramuzza f295ff6
fix: changed cardano data source
FilippoScaramuzza 4108de8
Merge remote-tracking branch 'origin/main' into add-cardano
FilippoScaramuzza 055bcba
fix: other merge fixes
FilippoScaramuzza 34e170b
fix: changed data source to new API by BALANCE and fixed threshold pe…
FilippoScaramuzza 6f0f2ea
Merge remote-tracking branch 'upstream/main' into add-cardano
FilippoScaramuzza cf427d0
fix: stakes were not sorted
FilippoScaramuzza 83af00e
doc: added cardano to README
FilippoScaramuzza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package chains | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"math/big" | ||
"net/http" | ||
|
||
utils "github.com/xenowits/nakamoto-coefficient-calculator/core/utils" | ||
) | ||
|
||
type ChartData struct { | ||
AvgStake float64 `json:"avgstake"` | ||
DelegateCount int `json:"delegatecount"` | ||
Epoch int `json:"epoch"` | ||
Label string `json:"label"` | ||
Leverage string `json:"leverage"` | ||
MavGroup string `json:"mavgroup"` | ||
Pledge float64 `json:"pledge"` | ||
PoolCount int `json:"poolcount"` | ||
PrctStake float64 `json:"prctstake"` | ||
Stake float64 `json:"stake"` | ||
} | ||
|
||
func Cardano() (int, error) { | ||
url := "https://api.balanceanalytics.io/rpc/pool_group_stake_donut" | ||
|
||
req, err := http.NewRequest("GET", url, nil) | ||
if err != nil { | ||
log.Println("Error creating request:", err) | ||
return 0, err | ||
} | ||
|
||
// This is the PUBLIC_BALANCE_API_TOKEN taken from https://www.balanceanalytics.io/chartboards/donut_shop | ||
req.Header.Set("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoid2ViYXBwX3VzZXIifQ.eg3Zb9ZduibYJr1pgUrfqy4PFhkVU1uO_F9gFPBZnBI") | ||
req.Header.Set("Content-Type", "application/json") | ||
|
||
client := &http.Client{} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
log.Println("Error making request:", err) | ||
return 0, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
var responseData []struct { | ||
Chartdata []ChartData `json:"chartdata"` | ||
} | ||
err = json.NewDecoder(resp.Body).Decode(&responseData) | ||
if err != nil { | ||
log.Println("Error decoding JSON:", err) | ||
return 0, err | ||
} | ||
|
||
var votingPowers []big.Int | ||
for _, data := range responseData { | ||
for _, chartData := range data.Chartdata { | ||
stakeInt := big.NewInt(int64(chartData.Stake)) | ||
votingPowers = append(votingPowers, *stakeInt) | ||
} | ||
} | ||
|
||
// Calculate total voting power | ||
totalVotingPower := utils.CalculateTotalVotingPowerBigNums(votingPowers) | ||
|
||
// Calculate Nakamoto coefficient | ||
nakamotoCoefficient := utils.CalcNakamotoCoefficientBigNums51(totalVotingPower, votingPowers) | ||
|
||
fmt.Println("Total voting power:", totalVotingPower) | ||
fmt.Println("The Nakamoto coefficient for Cardano is", nakamotoCoefficient) | ||
|
||
// Return Nakamoto coefficient | ||
return nakamotoCoefficient, 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to make this bearer value a param within a params file ? and import it from there ?
Is there and expiration date on the token ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can add it as a parameter:
Anyway, I'm not completely sure about the validity.
While searching for another data source to close this PR, I came across another PR. Here, @ccgarant, suggested to use https://www.balanceanalytics.io/chartboards/donut_shop. I searched the data source in the networking tab and noticed the API call to https://api.balanceanalytics.io/rpc/pool_group_stake_donut, where the bearer token to get authenticated can be found in the page's source code as
PUBLIC_BALANCE_API_TOKEN
:So they're apparently loading it from the environment and then using it. Anyway, I'm not really sure if this is intentional or not. I'll try to contact @ccgarant and see if he can help.