From d983dce78ca99e0680f09e471e632c858548f65a Mon Sep 17 00:00:00 2001 From: tungle Date: Tue, 19 Mar 2024 15:13:37 +0700 Subject: [PATCH 1/7] add desc --- app/app.go | 1 + 1 file changed, 1 insertion(+) diff --git a/app/app.go b/app/app.go index 3b785d67..baaeeabd 100644 --- a/app/app.go +++ b/app/app.go @@ -149,6 +149,7 @@ import ( "github.com/CosmWasm/wasmd/x/wasm" wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper" wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types" + // add airdrop tool ) const appName = "EveApp" From d05f433386ad519d5a1695496488c373f05f5c1e Mon Sep 17 00:00:00 2001 From: tungla Date: Wed, 20 Mar 2024 17:43:55 +0700 Subject: [PATCH 2/7] add feature for other chains --- .gitignore | 4 ++ airdrop/akash.go | 139 ++++++++++++++++++++++++++++++++++++++ airdrop/bostrom.go | 139 ++++++++++++++++++++++++++++++++++++++ airdrop/celestia.go | 141 +++++++++++++++++++++++++++++++++++++++ airdrop/composable.go | 139 ++++++++++++++++++++++++++++++++++++++ airdrop/config/config.go | 109 ++++++++++++++++++++++++++++++ airdrop/config/types.go | 92 +++++++++++++++++++++++++ airdrop/cosmos.go | 139 ++++++++++++++++++++++++++++++++++++++ airdrop/main.go | 95 ++++++++++++++++++++++++++ airdrop/neutron.go | 140 ++++++++++++++++++++++++++++++++++++++ airdrop/sentinel.go | 139 ++++++++++++++++++++++++++++++++++++++ airdrop/stargaze.go | 139 ++++++++++++++++++++++++++++++++++++++ airdrop/terra.go | 139 ++++++++++++++++++++++++++++++++++++++ go.mod | 3 +- go.sum | 2 + 15 files changed, 1558 insertions(+), 1 deletion(-) create mode 100644 airdrop/akash.go create mode 100644 airdrop/bostrom.go create mode 100644 airdrop/celestia.go create mode 100644 airdrop/composable.go create mode 100644 airdrop/config/config.go create mode 100644 airdrop/config/types.go create mode 100644 airdrop/cosmos.go create mode 100644 airdrop/main.go create mode 100644 airdrop/neutron.go create mode 100644 airdrop/sentinel.go create mode 100644 airdrop/stargaze.go create mode 100644 airdrop/terra.go diff --git a/.gitignore b/.gitignore index f5f5405d..522c10eb 100644 --- a/.gitignore +++ b/.gitignore @@ -46,3 +46,7 @@ dependency-graph.png *.out *.synctex.gz contract_tests/* + +#Airdrop tool +balance.json +rewards.json \ No newline at end of file diff --git a/airdrop/akash.go b/airdrop/akash.go new file mode 100644 index 00000000..03249048 --- /dev/null +++ b/airdrop/akash.go @@ -0,0 +1,139 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "strconv" + + "cosmossdk.io/math" + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + grpctypes "github.com/cosmos/cosmos-sdk/types/grpc" + "github.com/cosmos/cosmos-sdk/types/query" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + "github.com/eve-network/eve/airdrop/config" + "github.com/joho/godotenv" + "google.golang.org/grpc" + "google.golang.org/grpc/metadata" +) + +func akash() ([]banktypes.Balance, []config.Reward) { + block_height := getLatestHeight(config.GetAkashConfig().NodeStatusUrl) + godotenv.Load() + grpcAddr := config.GetAkashConfig().GRPCAddr + grpcConn, err := grpc.Dial(grpcAddr, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) + if err != nil { + panic(err) + } + defer grpcConn.Close() + stakingClient := stakingtypes.NewQueryClient(grpcConn) + + delegators := []stakingtypes.DelegationResponse{} + + validators := getValidators(stakingClient, block_height) + fmt.Println("Validators: ", len(validators)) + for validatorIndex, validator := range validators { + var header metadata.MD + delegationsResponse, _ := stakingClient.ValidatorDelegations( + metadata.AppendToOutgoingContext(context.Background(), grpctypes.GRPCBlockHeightHeader, block_height), // Add metadata to request + &stakingtypes.QueryValidatorDelegationsRequest{ + ValidatorAddr: validator.OperatorAddress, + Pagination: &query.PageRequest{ + CountTotal: true, + Limit: LIMIT_PER_PAGE, + }, + }, + grpc.Header(&header), // Retrieve header from response + ) + total := delegationsResponse.Pagination.Total + fmt.Println("Response ", len(delegationsResponse.DelegationResponses)) + fmt.Println("Validator "+strconv.Itoa(validatorIndex)+" ", total) + delegators = append(delegators, delegationsResponse.DelegationResponses...) + } + + usd := math.LegacyMustNewDecFromStr("20") + + apiUrl := "https://api.coingecko.com/api/v3/simple/price?ids=" + config.GetAkashConfig().CoinId + "&vs_currencies=usd" + tokenInUsd := fetchAkashTokenPrice(apiUrl) + tokenIn20Usd := usd.QuoTruncate(tokenInUsd) + + rewardInfo := []config.Reward{} + balanceInfo := []banktypes.Balance{} + + totalTokenDelegate := math.LegacyMustNewDecFromStr("0") + for _, delegator := range delegators { + validatorIndex := findValidatorInfo(validators, delegator.Delegation.ValidatorAddress) + validatorInfo := validators[validatorIndex] + token := (delegator.Delegation.Shares.MulInt(validatorInfo.Tokens)).QuoTruncate(validatorInfo.DelegatorShares) + if token.LT(tokenIn20Usd) { + continue + } + totalTokenDelegate = totalTokenDelegate.Add(token) + } + eveAirdrop := math.LegacyMustNewDecFromStr(EVE_AIRDROP) + testAmount, _ := math.LegacyNewDecFromStr("0") + for _, delegator := range delegators { + validatorIndex := findValidatorInfo(validators, delegator.Delegation.ValidatorAddress) + validatorInfo := validators[validatorIndex] + token := (delegator.Delegation.Shares.MulInt(validatorInfo.Tokens)).QuoTruncate(validatorInfo.DelegatorShares) + if token.LT(tokenIn20Usd) { + continue + } + eveAirdrop := (eveAirdrop.MulInt64(int64(config.GetAkashConfig().Percent))).QuoInt64(100).Mul(token).QuoTruncate(totalTokenDelegate) + eveBech32Address := convertBech32Address(delegator.Delegation.DelegatorAddress) + rewardInfo = append(rewardInfo, config.Reward{ + Address: delegator.Delegation.DelegatorAddress, + EveAddress: eveBech32Address, + Shares: delegator.Delegation.Shares, + Token: token, + EveAirdropToken: eveAirdrop, + ChainId: config.GetAkashConfig().ChainID, + }) + testAmount = eveAirdrop.Add(testAmount) + balanceInfo = append(balanceInfo, banktypes.Balance{ + Address: eveBech32Address, + Coins: sdk.NewCoins(sdk.NewCoin("eve", eveAirdrop.TruncateInt())), + }) + } + fmt.Println(testAmount) + // Write delegations to file + // fileForDebug, _ := json.MarshalIndent(rewardInfo, "", " ") + // _ = os.WriteFile("rewards.json", fileForDebug, 0644) + + // fileBalance, _ := json.MarshalIndent(balanceInfo, "", " ") + // _ = os.WriteFile("balance.json", fileBalance, 0644) + return balanceInfo, rewardInfo +} + +func fetchAkashTokenPrice(apiUrl string) math.LegacyDec { + // Make a GET request to the API + response, err := http.Get(apiUrl) + if err != nil { + fmt.Println("Error making GET request:", err) + panic("") + } + defer response.Body.Close() + + // Read the response body + responseBody, err := ioutil.ReadAll(response.Body) + if err != nil { + fmt.Println("Error reading response body:", err) + panic("") + } + + var data config.AkashPrice + + // Unmarshal the JSON byte slice into the defined struct + err = json.Unmarshal(responseBody, &data) + if err != nil { + fmt.Println("Error unmarshalling JSON:", err) + panic("") + } + + tokenInUsd := math.LegacyMustNewDecFromStr(data.Token.USD.String()) + return tokenInUsd +} diff --git a/airdrop/bostrom.go b/airdrop/bostrom.go new file mode 100644 index 00000000..a119ec0f --- /dev/null +++ b/airdrop/bostrom.go @@ -0,0 +1,139 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "strconv" + + "cosmossdk.io/math" + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + grpctypes "github.com/cosmos/cosmos-sdk/types/grpc" + "github.com/cosmos/cosmos-sdk/types/query" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + "github.com/eve-network/eve/airdrop/config" + "github.com/joho/godotenv" + "google.golang.org/grpc" + "google.golang.org/grpc/metadata" +) + +func bostrom() ([]banktypes.Balance, []config.Reward) { + block_height := getLatestHeight(config.GetBostromConfig().NodeStatusUrl) + godotenv.Load() + grpcAddr := config.GetBostromConfig().GRPCAddr + grpcConn, err := grpc.Dial(grpcAddr, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) + if err != nil { + panic(err) + } + defer grpcConn.Close() + stakingClient := stakingtypes.NewQueryClient(grpcConn) + + delegators := []stakingtypes.DelegationResponse{} + + validators := getValidators(stakingClient, block_height) + fmt.Println("Validators: ", len(validators)) + for validatorIndex, validator := range validators { + var header metadata.MD + delegationsResponse, _ := stakingClient.ValidatorDelegations( + metadata.AppendToOutgoingContext(context.Background(), grpctypes.GRPCBlockHeightHeader, block_height), // Add metadata to request + &stakingtypes.QueryValidatorDelegationsRequest{ + ValidatorAddr: validator.OperatorAddress, + Pagination: &query.PageRequest{ + CountTotal: true, + Limit: LIMIT_PER_PAGE, + }, + }, + grpc.Header(&header), // Retrieve header from response + ) + total := delegationsResponse.Pagination.Total + fmt.Println("Response ", len(delegationsResponse.DelegationResponses)) + fmt.Println("Validator "+strconv.Itoa(validatorIndex)+" ", total) + delegators = append(delegators, delegationsResponse.DelegationResponses...) + } + + usd := math.LegacyMustNewDecFromStr("20") + + apiUrl := "https://api.coingecko.com/api/v3/simple/price?ids=" + config.GetBostromConfig().CoinId + "&vs_currencies=usd" + tokenInUsd := fetchBostromTokenPrice(apiUrl) + tokenIn20Usd := usd.QuoTruncate(tokenInUsd) + + rewardInfo := []config.Reward{} + balanceInfo := []banktypes.Balance{} + + totalTokenDelegate := math.LegacyMustNewDecFromStr("0") + for _, delegator := range delegators { + validatorIndex := findValidatorInfo(validators, delegator.Delegation.ValidatorAddress) + validatorInfo := validators[validatorIndex] + token := (delegator.Delegation.Shares.MulInt(validatorInfo.Tokens)).QuoTruncate(validatorInfo.DelegatorShares) + if token.LT(tokenIn20Usd) { + continue + } + totalTokenDelegate = totalTokenDelegate.Add(token) + } + eveAirdrop := math.LegacyMustNewDecFromStr(EVE_AIRDROP) + testAmount, _ := math.LegacyNewDecFromStr("0") + for _, delegator := range delegators { + validatorIndex := findValidatorInfo(validators, delegator.Delegation.ValidatorAddress) + validatorInfo := validators[validatorIndex] + token := (delegator.Delegation.Shares.MulInt(validatorInfo.Tokens)).QuoTruncate(validatorInfo.DelegatorShares) + if token.LT(tokenIn20Usd) { + continue + } + eveAirdrop := (eveAirdrop.MulInt64(int64(config.GetBostromConfig().Percent))).QuoInt64(100).Mul(token).QuoTruncate(totalTokenDelegate) + eveBech32Address := convertBech32Address(delegator.Delegation.DelegatorAddress) + rewardInfo = append(rewardInfo, config.Reward{ + Address: delegator.Delegation.DelegatorAddress, + EveAddress: eveBech32Address, + Shares: delegator.Delegation.Shares, + Token: token, + EveAirdropToken: eveAirdrop, + ChainId: config.GetBostromConfig().ChainID, + }) + testAmount = eveAirdrop.Add(testAmount) + balanceInfo = append(balanceInfo, banktypes.Balance{ + Address: eveBech32Address, + Coins: sdk.NewCoins(sdk.NewCoin("eve", eveAirdrop.TruncateInt())), + }) + } + fmt.Println(testAmount) + // Write delegations to file + // fileForDebug, _ := json.MarshalIndent(rewardInfo, "", " ") + // _ = os.WriteFile("rewards.json", fileForDebug, 0644) + + // fileBalance, _ := json.MarshalIndent(balanceInfo, "", " ") + // _ = os.WriteFile("balance.json", fileBalance, 0644) + return balanceInfo, rewardInfo +} + +func fetchBostromTokenPrice(apiUrl string) math.LegacyDec { + // Make a GET request to the API + response, err := http.Get(apiUrl) + if err != nil { + fmt.Println("Error making GET request:", err) + panic("") + } + defer response.Body.Close() + + // Read the response body + responseBody, err := ioutil.ReadAll(response.Body) + if err != nil { + fmt.Println("Error reading response body:", err) + panic("") + } + + var data config.BostromPrice + + // Unmarshal the JSON byte slice into the defined struct + err = json.Unmarshal(responseBody, &data) + if err != nil { + fmt.Println("Error unmarshalling JSON:", err) + panic("") + } + + tokenInUsd := math.LegacyMustNewDecFromStr(data.Token.USD.String()) + return tokenInUsd +} diff --git a/airdrop/celestia.go b/airdrop/celestia.go new file mode 100644 index 00000000..455363be --- /dev/null +++ b/airdrop/celestia.go @@ -0,0 +1,141 @@ +package main + +// error max size response +import ( + "context" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "strconv" + + "cosmossdk.io/math" + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + grpctypes "github.com/cosmos/cosmos-sdk/types/grpc" + "github.com/cosmos/cosmos-sdk/types/query" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + "github.com/eve-network/eve/airdrop/config" + "github.com/joho/godotenv" + "google.golang.org/grpc" + "google.golang.org/grpc/metadata" +) + +func celestia() ([]banktypes.Balance, []config.Reward) { + block_height := getLatestHeight(config.GetCelestiaConfig().NodeStatusUrl) + godotenv.Load() + grpcAddr := config.GetCelestiaConfig().GRPCAddr + grpcConn, err := grpc.Dial(grpcAddr, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) + if err != nil { + panic(err) + } + defer grpcConn.Close() + stakingClient := stakingtypes.NewQueryClient(grpcConn) + + delegators := []stakingtypes.DelegationResponse{} + + validators := getValidators(stakingClient, block_height) + fmt.Println("Validators: ", len(validators)) + for validatorIndex, validator := range validators { + var header metadata.MD + delegationsResponse, err := stakingClient.ValidatorDelegations( + metadata.AppendToOutgoingContext(context.Background(), grpctypes.GRPCBlockHeightHeader, block_height), // Add metadata to request + &stakingtypes.QueryValidatorDelegationsRequest{ + ValidatorAddr: validator.OperatorAddress, + Pagination: &query.PageRequest{ + CountTotal: true, + Limit: LIMIT_PER_PAGE, + }, + }, + grpc.Header(&header), // Retrieve header from response + ) + fmt.Println("err: ", err) + total := delegationsResponse.Pagination.Total + fmt.Println("Response ", len(delegationsResponse.DelegationResponses)) + fmt.Println("Validator "+strconv.Itoa(validatorIndex)+" ", total) + delegators = append(delegators, delegationsResponse.DelegationResponses...) + } + + usd := math.LegacyMustNewDecFromStr("20") + + apiUrl := "https://api.coingecko.com/api/v3/simple/price?ids=" + config.GetCelestiaConfig().CoinId + "&vs_currencies=usd" + tokenInUsd := fetchCelestiaTokenPrice(apiUrl) + tokenIn20Usd := usd.QuoTruncate(tokenInUsd) + + rewardInfo := []config.Reward{} + balanceInfo := []banktypes.Balance{} + + totalTokenDelegate := math.LegacyMustNewDecFromStr("0") + for _, delegator := range delegators { + validatorIndex := findValidatorInfo(validators, delegator.Delegation.ValidatorAddress) + validatorInfo := validators[validatorIndex] + token := (delegator.Delegation.Shares.MulInt(validatorInfo.Tokens)).QuoTruncate(validatorInfo.DelegatorShares) + if token.LT(tokenIn20Usd) { + continue + } + totalTokenDelegate = totalTokenDelegate.Add(token) + } + eveAirdrop := math.LegacyMustNewDecFromStr(EVE_AIRDROP) + testAmount, _ := math.LegacyNewDecFromStr("0") + for _, delegator := range delegators { + validatorIndex := findValidatorInfo(validators, delegator.Delegation.ValidatorAddress) + validatorInfo := validators[validatorIndex] + token := (delegator.Delegation.Shares.MulInt(validatorInfo.Tokens)).QuoTruncate(validatorInfo.DelegatorShares) + if token.LT(tokenIn20Usd) { + continue + } + eveAirdrop := (eveAirdrop.MulInt64(int64(config.GetCelestiaConfig().Percent))).QuoInt64(100).Mul(token).QuoTruncate(totalTokenDelegate) + eveBech32Address := convertBech32Address(delegator.Delegation.DelegatorAddress) + rewardInfo = append(rewardInfo, config.Reward{ + Address: delegator.Delegation.DelegatorAddress, + EveAddress: eveBech32Address, + Shares: delegator.Delegation.Shares, + Token: token, + EveAirdropToken: eveAirdrop, + ChainId: config.GetCelestiaConfig().ChainID, + }) + testAmount = eveAirdrop.Add(testAmount) + balanceInfo = append(balanceInfo, banktypes.Balance{ + Address: eveBech32Address, + Coins: sdk.NewCoins(sdk.NewCoin("eve", eveAirdrop.TruncateInt())), + }) + } + fmt.Println(testAmount) + // Write delegations to file + // fileForDebug, _ := json.MarshalIndent(rewardInfo, "", " ") + // _ = os.WriteFile("rewards.json", fileForDebug, 0644) + + // fileBalance, _ := json.MarshalIndent(balanceInfo, "", " ") + // _ = os.WriteFile("balance.json", fileBalance, 0644) + return balanceInfo, rewardInfo +} + +func fetchCelestiaTokenPrice(apiUrl string) math.LegacyDec { + // Make a GET request to the API + response, err := http.Get(apiUrl) + if err != nil { + fmt.Println("Error making GET request:", err) + panic("") + } + defer response.Body.Close() + + // Read the response body + responseBody, err := ioutil.ReadAll(response.Body) + if err != nil { + fmt.Println("Error reading response body:", err) + panic("") + } + + var data config.CelestiaPrice + + // Unmarshal the JSON byte slice into the defined struct + err = json.Unmarshal(responseBody, &data) + if err != nil { + fmt.Println("Error unmarshalling JSON:", err) + panic("") + } + + tokenInUsd := math.LegacyMustNewDecFromStr(data.Token.USD.String()) + return tokenInUsd +} diff --git a/airdrop/composable.go b/airdrop/composable.go new file mode 100644 index 00000000..cc6e4b76 --- /dev/null +++ b/airdrop/composable.go @@ -0,0 +1,139 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "strconv" + + "cosmossdk.io/math" + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + grpctypes "github.com/cosmos/cosmos-sdk/types/grpc" + "github.com/cosmos/cosmos-sdk/types/query" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + "github.com/eve-network/eve/airdrop/config" + "github.com/joho/godotenv" + "google.golang.org/grpc" + "google.golang.org/grpc/metadata" +) + +func composable() ([]banktypes.Balance, []config.Reward) { + block_height := getLatestHeight(config.GetComposableConfig().NodeStatusUrl) + godotenv.Load() + grpcAddr := config.GetComposableConfig().GRPCAddr + grpcConn, err := grpc.Dial(grpcAddr, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) + if err != nil { + panic(err) + } + defer grpcConn.Close() + stakingClient := stakingtypes.NewQueryClient(grpcConn) + + delegators := []stakingtypes.DelegationResponse{} + + validators := getValidators(stakingClient, block_height) + fmt.Println("Validators: ", len(validators)) + for validatorIndex, validator := range validators { + var header metadata.MD + delegationsResponse, _ := stakingClient.ValidatorDelegations( + metadata.AppendToOutgoingContext(context.Background(), grpctypes.GRPCBlockHeightHeader, block_height), // Add metadata to request + &stakingtypes.QueryValidatorDelegationsRequest{ + ValidatorAddr: validator.OperatorAddress, + Pagination: &query.PageRequest{ + CountTotal: true, + Limit: LIMIT_PER_PAGE, + }, + }, + grpc.Header(&header), // Retrieve header from response + ) + total := delegationsResponse.Pagination.Total + fmt.Println("Response ", len(delegationsResponse.DelegationResponses)) + fmt.Println("Validator "+strconv.Itoa(validatorIndex)+" ", total) + delegators = append(delegators, delegationsResponse.DelegationResponses...) + } + + usd := math.LegacyMustNewDecFromStr("20") + + apiUrl := "https://api.coingecko.com/api/v3/simple/price?ids=" + config.GetComposableConfig().CoinId + "&vs_currencies=usd" + tokenInUsd := fetchComposableTokenPrice(apiUrl) + tokenIn20Usd := usd.QuoTruncate(tokenInUsd) + + rewardInfo := []config.Reward{} + balanceInfo := []banktypes.Balance{} + + totalTokenDelegate := math.LegacyMustNewDecFromStr("0") + for _, delegator := range delegators { + validatorIndex := findValidatorInfo(validators, delegator.Delegation.ValidatorAddress) + validatorInfo := validators[validatorIndex] + token := (delegator.Delegation.Shares.MulInt(validatorInfo.Tokens)).QuoTruncate(validatorInfo.DelegatorShares) + if token.LT(tokenIn20Usd) { + continue + } + totalTokenDelegate = totalTokenDelegate.Add(token) + } + eveAirdrop := math.LegacyMustNewDecFromStr(EVE_AIRDROP) + testAmount, _ := math.LegacyNewDecFromStr("0") + for _, delegator := range delegators { + validatorIndex := findValidatorInfo(validators, delegator.Delegation.ValidatorAddress) + validatorInfo := validators[validatorIndex] + token := (delegator.Delegation.Shares.MulInt(validatorInfo.Tokens)).QuoTruncate(validatorInfo.DelegatorShares) + if token.LT(tokenIn20Usd) { + continue + } + eveAirdrop := (eveAirdrop.MulInt64(int64(config.GetComposableConfig().Percent))).QuoInt64(100).Mul(token).QuoTruncate(totalTokenDelegate) + eveBech32Address := convertBech32Address(delegator.Delegation.DelegatorAddress) + rewardInfo = append(rewardInfo, config.Reward{ + Address: delegator.Delegation.DelegatorAddress, + EveAddress: eveBech32Address, + Shares: delegator.Delegation.Shares, + Token: token, + EveAirdropToken: eveAirdrop, + ChainId: config.GetComposableConfig().ChainID, + }) + testAmount = eveAirdrop.Add(testAmount) + balanceInfo = append(balanceInfo, banktypes.Balance{ + Address: eveBech32Address, + Coins: sdk.NewCoins(sdk.NewCoin("eve", eveAirdrop.TruncateInt())), + }) + } + fmt.Println(testAmount) + // Write delegations to file + // fileForDebug, _ := json.MarshalIndent(rewardInfo, "", " ") + // _ = os.WriteFile("rewards.json", fileForDebug, 0644) + + // fileBalance, _ := json.MarshalIndent(balanceInfo, "", " ") + // _ = os.WriteFile("balance.json", fileBalance, 0644) + return balanceInfo, rewardInfo +} + +func fetchComposableTokenPrice(apiUrl string) math.LegacyDec { + // Make a GET request to the API + response, err := http.Get(apiUrl) + if err != nil { + fmt.Println("Error making GET request:", err) + panic("") + } + defer response.Body.Close() + + // Read the response body + responseBody, err := ioutil.ReadAll(response.Body) + if err != nil { + fmt.Println("Error reading response body:", err) + panic("") + } + + var data config.ComposablePrice + + // Unmarshal the JSON byte slice into the defined struct + err = json.Unmarshal(responseBody, &data) + if err != nil { + fmt.Println("Error unmarshalling JSON:", err) + panic("") + } + + tokenInUsd := math.LegacyMustNewDecFromStr(data.Token.USD.String()) + return tokenInUsd +} diff --git a/airdrop/config/config.go b/airdrop/config/config.go new file mode 100644 index 00000000..011c8eff --- /dev/null +++ b/airdrop/config/config.go @@ -0,0 +1,109 @@ +package config + +func GetCosmosHubConfig() *ChainClientConfig { + return &ChainClientConfig{ + Key: "default", + ChainID: "cosmoshub-4", + GRPCAddr: "grpc-cosmoshub-ia.cosmosia.notional.ventures:443", + AccountPrefix: "cosmos", + CoinId: "cosmos", + Percent: 0, + NodeStatusUrl: "https://cosmos-rpc.polkachu.com/status", + } +} + +func GetComposableConfig() *ChainClientConfig { + return &ChainClientConfig{ + Key: "default", + ChainID: "centauri-1", + GRPCAddr: "composable-grpc.polkachu.com:22290", + AccountPrefix: "centauri", + Percent: 13, + CoinId: "picasso", + NodeStatusUrl: "https://composable-rpc.polkachu.com/status", + } +} + +func GetCelestiaConfig() *ChainClientConfig { + return &ChainClientConfig{ + Key: "default", + ChainID: "celestia", + GRPCAddr: "celestia-grpc.polkachu.com:11690", + AccountPrefix: "celestia", + Percent: 13, + CoinId: "celestia", + NodeStatusUrl: "https://celestia-rpc.polkachu.com/status", + } +} + +func GetSentinelConfig() *ChainClientConfig { + return &ChainClientConfig{ + Key: "default", + ChainID: "sentinelhub-2", + GRPCAddr: "sentinel-grpc.polkachu.com:23990", + AccountPrefix: "sent", + Percent: 13, + CoinId: "sentinel", + NodeStatusUrl: "https://sentinel-rpc.polkachu.com/status", + } +} + +func GetAkashConfig() *ChainClientConfig { + return &ChainClientConfig{ + Key: "default", + ChainID: "akashnet-2", + GRPCAddr: "akash-grpc.polkachu.com:12890", + AccountPrefix: "akash", + Percent: 13, + CoinId: "akash-network", + NodeStatusUrl: "https://akash-rpc.polkachu.com/status", + } +} + +func GetStargazeConfig() *ChainClientConfig { + return &ChainClientConfig{ + Key: "default", + ChainID: "stargaze-1", + GRPCAddr: "stargaze-grpc.polkachu.com:13790", + AccountPrefix: "stars", + Percent: 13, + CoinId: "stargaze", + NodeStatusUrl: "https://stargaze-rpc.polkachu.com/status", + } +} + +func GetNeutronConfig() *ChainClientConfig { + return &ChainClientConfig{ + Key: "default", + ChainID: "neutron-1", + GRPCAddr: "neutron-grpc.polkachu.com:19190", + AccountPrefix: "neutron", + Percent: 13, + CoinId: "neutron-3", + NodeStatusUrl: "https://neutron-rpc.polkachu.com/status", + } +} + +func GetTerraConfig() *ChainClientConfig { + return &ChainClientConfig{ + Key: "default", + ChainID: "phoenix-1", + GRPCAddr: "terra-grpc.polkachu.com:11790", + AccountPrefix: "terra", + Percent: 13, + CoinId: "terra-luna-2", + NodeStatusUrl: "https://terra-rpc.polkachu.com/status", + } +} + +func GetBostromConfig() *ChainClientConfig { + return &ChainClientConfig{ + Key: "default", + ChainID: "bostrom", + GRPCAddr: "grpc-cyber-ia.cosmosia.notional.ventures:443", + AccountPrefix: "bostrom", + Percent: 13, + CoinId: "bostrom", + NodeStatusUrl: "https://rpc-cyber-ia.cosmosia.notional.ventures/status", + } +} diff --git a/airdrop/config/types.go b/airdrop/config/types.go new file mode 100644 index 00000000..c5ecc53d --- /dev/null +++ b/airdrop/config/types.go @@ -0,0 +1,92 @@ +package config + +import ( + "encoding/json" + + "cosmossdk.io/math" +) + +type ChainClientConfig struct { + Key string `json:"key" yaml:"key"` + ChainID string `json:"chain-id" yaml:"chain-id"` + GRPCAddr string `json:"grpc-addr" yaml:"grpc-addr"` + AccountPrefix string `json:"account-prefix" yaml:"account-prefix"` + Percent int `json:"percent" yaml:"percent"` + CoinId string `json:"coin-id" yaml:"coin-id"` + NodeStatusUrl string `json:"node-status" yaml:"node-status"` +} + +type Reward struct { + Address string `json:"address"` + EveAddress string `json:"eve_address"` + Shares math.LegacyDec `json:"shares"` + Token math.LegacyDec `json:"tokens"` + EveAirdropToken math.LegacyDec `json:"eve"` + ChainId string `json:"chain"` +} + +type ComposablePrice struct { + Token Price `json:"picasso"` +} + +type AkashPrice struct { + Token Price `json:"akash-network"` +} + +type CelestiaPrice struct { + Token Price `json:"celestia"` +} + +type CosmosPrice struct { + Token Price `json:"cosmos"` +} + +type NeutronPrice struct { + Token Price `json:"neutron-3"` +} + +type SentinelPrice struct { + Token Price `json:"sentinel"` +} + +type StargazePrice struct { + Token Price `json:"stargaze"` +} + +type TerraPrice struct { + Token Price `json:"terra-luna-2"` +} + +type BostromPrice struct { + Token Price `json:"bostrom"` +} + +type Price struct { + USD json.Number `json:"usd"` +} + +type NodeResponse struct { + Id json.Number `json:"id"` + JsonRPC string `json:"jsonrpc"` + Result Result `json:"result"` +} + +type Result struct { + NodeInfo NodeInfo `json:"node_info"` + SyncInfo SyncInfo `json:"sync_info"` + ValidatorInfo ValidatorInfo `json:"validator_info"` +} + +type NodeInfo struct{} +type SyncInfo struct { + CatchingUp bool `json:"catching_up"` + EarlieastAppHash string `json:"earliest_app_hash"` + EarlieastBlockHash string `json:"earliest_block_hash"` + EarlieastBlockHeight string `json:"earliest_block_height"` + EarlieastBlockTime string `json:"earliest_block_time"` + LatestAppHash string `json:"latest_app_hash"` + LatestBlockHash string `json:"latest_block_hash"` + LatestBlockHeight string `json:"latest_block_height"` + LatestBlockTime string `json:"latest_block_time"` +} +type ValidatorInfo struct{} diff --git a/airdrop/cosmos.go b/airdrop/cosmos.go new file mode 100644 index 00000000..3f3ab279 --- /dev/null +++ b/airdrop/cosmos.go @@ -0,0 +1,139 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "strconv" + + "cosmossdk.io/math" + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + grpctypes "github.com/cosmos/cosmos-sdk/types/grpc" + "github.com/cosmos/cosmos-sdk/types/query" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + "github.com/eve-network/eve/airdrop/config" + "github.com/joho/godotenv" + "google.golang.org/grpc" + "google.golang.org/grpc/metadata" +) + +func cosmos() ([]banktypes.Balance, []config.Reward) { + block_height := getLatestHeight(config.GetCosmosHubConfig().NodeStatusUrl) + godotenv.Load() + grpcAddr := config.GetCosmosHubConfig().GRPCAddr + grpcConn, err := grpc.Dial(grpcAddr, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) + if err != nil { + panic(err) + } + defer grpcConn.Close() + stakingClient := stakingtypes.NewQueryClient(grpcConn) + + delegators := []stakingtypes.DelegationResponse{} + + validators := getValidators(stakingClient, block_height) + fmt.Println("Validators: ", len(validators)) + for validatorIndex, validator := range validators { + var header metadata.MD + delegationsResponse, _ := stakingClient.ValidatorDelegations( + metadata.AppendToOutgoingContext(context.Background(), grpctypes.GRPCBlockHeightHeader, block_height), // Add metadata to request + &stakingtypes.QueryValidatorDelegationsRequest{ + ValidatorAddr: validator.OperatorAddress, + Pagination: &query.PageRequest{ + CountTotal: true, + Limit: LIMIT_PER_PAGE, + }, + }, + grpc.Header(&header), // Retrieve header from response + ) + total := delegationsResponse.Pagination.Total + fmt.Println("Response ", len(delegationsResponse.DelegationResponses)) + fmt.Println("Validator "+strconv.Itoa(validatorIndex)+" ", total) + delegators = append(delegators, delegationsResponse.DelegationResponses...) + } + + usd := math.LegacyMustNewDecFromStr("20") + + apiUrl := "https://api.coingecko.com/api/v3/simple/price?ids=" + config.GetCosmosHubConfig().CoinId + "&vs_currencies=usd" + tokenInUsd := fetchCosmosTokenPrice(apiUrl) + tokenIn20Usd := usd.QuoTruncate(tokenInUsd) + + rewardInfo := []config.Reward{} + balanceInfo := []banktypes.Balance{} + + totalTokenDelegate := math.LegacyMustNewDecFromStr("0") + for _, delegator := range delegators { + validatorIndex := findValidatorInfo(validators, delegator.Delegation.ValidatorAddress) + validatorInfo := validators[validatorIndex] + token := (delegator.Delegation.Shares.MulInt(validatorInfo.Tokens)).QuoTruncate(validatorInfo.DelegatorShares) + if token.LT(tokenIn20Usd) { + continue + } + totalTokenDelegate = totalTokenDelegate.Add(token) + } + eveAirdrop := math.LegacyMustNewDecFromStr(EVE_AIRDROP) + testAmount, _ := math.LegacyNewDecFromStr("0") + for _, delegator := range delegators { + validatorIndex := findValidatorInfo(validators, delegator.Delegation.ValidatorAddress) + validatorInfo := validators[validatorIndex] + token := (delegator.Delegation.Shares.MulInt(validatorInfo.Tokens)).QuoTruncate(validatorInfo.DelegatorShares) + if token.LT(tokenIn20Usd) { + continue + } + eveAirdrop := (eveAirdrop.MulInt64(int64(config.GetCosmosHubConfig().Percent))).QuoInt64(100).Mul(token).QuoTruncate(totalTokenDelegate) + eveBech32Address := convertBech32Address(delegator.Delegation.DelegatorAddress) + rewardInfo = append(rewardInfo, config.Reward{ + Address: delegator.Delegation.DelegatorAddress, + EveAddress: eveBech32Address, + Shares: delegator.Delegation.Shares, + Token: token, + EveAirdropToken: eveAirdrop, + ChainId: config.GetCosmosHubConfig().ChainID, + }) + testAmount = eveAirdrop.Add(testAmount) + balanceInfo = append(balanceInfo, banktypes.Balance{ + Address: eveBech32Address, + Coins: sdk.NewCoins(sdk.NewCoin("eve", eveAirdrop.TruncateInt())), + }) + } + fmt.Println(testAmount) + // Write delegations to file + // fileForDebug, _ := json.MarshalIndent(rewardInfo, "", " ") + // _ = os.WriteFile("rewards.json", fileForDebug, 0644) + + // fileBalance, _ := json.MarshalIndent(balanceInfo, "", " ") + // _ = os.WriteFile("balance.json", fileBalance, 0644) + return balanceInfo, rewardInfo +} + +func fetchCosmosTokenPrice(apiUrl string) math.LegacyDec { + // Make a GET request to the API + response, err := http.Get(apiUrl) + if err != nil { + fmt.Println("Error making GET request:", err) + panic("") + } + defer response.Body.Close() + + // Read the response body + responseBody, err := ioutil.ReadAll(response.Body) + if err != nil { + fmt.Println("Error reading response body:", err) + panic("") + } + + var data config.CosmosPrice + + // Unmarshal the JSON byte slice into the defined struct + err = json.Unmarshal(responseBody, &data) + if err != nil { + fmt.Println("Error unmarshalling JSON:", err) + panic("") + } + + tokenInUsd := math.LegacyMustNewDecFromStr(data.Token.USD.String()) + return tokenInUsd +} diff --git a/airdrop/main.go b/airdrop/main.go new file mode 100644 index 00000000..c083fe4b --- /dev/null +++ b/airdrop/main.go @@ -0,0 +1,95 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "os" + + "github.com/cosmos/cosmos-sdk/types/bech32" + grpctypes "github.com/cosmos/cosmos-sdk/types/grpc" + "github.com/cosmos/cosmos-sdk/types/query" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + "github.com/eve-network/eve/airdrop/config" + "google.golang.org/grpc" + "google.golang.org/grpc/metadata" +) + +const ( + EVE_AIRDROP = "1000000000" // 1,000,000,000 + LIMIT_PER_PAGE = 100000000 +) + +func getValidators(stakingClient stakingtypes.QueryClient, block_height string) []stakingtypes.Validator { + // Get validator + var header metadata.MD + var totalValidatorsResponse *stakingtypes.QueryValidatorsResponse + totalValidatorsResponse, err := stakingClient.Validators( + metadata.AppendToOutgoingContext(context.Background(), grpctypes.GRPCBlockHeightHeader, block_height), // Add metadata to request + &stakingtypes.QueryValidatorsRequest{ + Pagination: &query.PageRequest{ + Limit: LIMIT_PER_PAGE, + }, + }, + grpc.Header(&header), + ) + fmt.Println(err) + validatorsInfo := totalValidatorsResponse.Validators + return validatorsInfo +} + +func main() { + balanceComposableInfo, rewardComposableInfo := terra() + // Write delegations to file + fileForDebug, _ := json.MarshalIndent(rewardComposableInfo, "", " ") + _ = os.WriteFile("rewards.json", fileForDebug, 0644) + + fileBalance, _ := json.MarshalIndent(balanceComposableInfo, "", " ") + _ = os.WriteFile("balance.json", fileBalance, 0644) +} + +func findValidatorInfo(validators []stakingtypes.Validator, address string) int { + for key, v := range validators { + if v.OperatorAddress == address { + return key + } + } + return -1 +} + +func getLatestHeight(apiUrl string) string { + // Make a GET request to the API + response, err := http.Get(apiUrl) + if err != nil { + fmt.Println("Error making GET request:", err) + panic("") + } + defer response.Body.Close() + + // Read the response body + responseBody, err := ioutil.ReadAll(response.Body) + if err != nil { + fmt.Println("Error reading response body:", err) + panic("") + } + + // Print the response body + var data config.NodeResponse + + // Unmarshal the JSON byte slice into the defined struct + err = json.Unmarshal(responseBody, &data) + if err != nil { + fmt.Println("Error unmarshalling JSON:", err) + panic("") + } + fmt.Println("Block height: ", data.Result.SyncInfo.LatestBlockHeight) + return data.Result.SyncInfo.LatestBlockHeight +} + +func convertBech32Address(otherChainAddress string) string { + _, bz, _ := bech32.DecodeAndConvert(otherChainAddress) + newBech32DelAddr, _ := bech32.ConvertAndEncode("eve", bz) + return newBech32DelAddr +} diff --git a/airdrop/neutron.go b/airdrop/neutron.go new file mode 100644 index 00000000..ed93987e --- /dev/null +++ b/airdrop/neutron.go @@ -0,0 +1,140 @@ +package main + +// code = Unimplemented desc = unknown service cosmos.staking.v1beta1.Query +import ( + "context" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "strconv" + + "cosmossdk.io/math" + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + grpctypes "github.com/cosmos/cosmos-sdk/types/grpc" + "github.com/cosmos/cosmos-sdk/types/query" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + "github.com/eve-network/eve/airdrop/config" + "github.com/joho/godotenv" + "google.golang.org/grpc" + "google.golang.org/grpc/metadata" +) + +func neutron() ([]banktypes.Balance, []config.Reward) { + block_height := getLatestHeight(config.GetNeutronConfig().NodeStatusUrl) + godotenv.Load() + grpcAddr := config.GetNeutronConfig().GRPCAddr + grpcConn, err := grpc.Dial(grpcAddr, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) + if err != nil { + panic(err) + } + defer grpcConn.Close() + stakingClient := stakingtypes.NewQueryClient(grpcConn) + + delegators := []stakingtypes.DelegationResponse{} + + validators := getValidators(stakingClient, block_height) + fmt.Println("Validators: ", len(validators)) + for validatorIndex, validator := range validators { + var header metadata.MD + delegationsResponse, _ := stakingClient.ValidatorDelegations( + metadata.AppendToOutgoingContext(context.Background(), grpctypes.GRPCBlockHeightHeader, block_height), // Add metadata to request + &stakingtypes.QueryValidatorDelegationsRequest{ + ValidatorAddr: validator.OperatorAddress, + Pagination: &query.PageRequest{ + CountTotal: true, + Limit: LIMIT_PER_PAGE, + }, + }, + grpc.Header(&header), // Retrieve header from response + ) + total := delegationsResponse.Pagination.Total + fmt.Println("Response ", len(delegationsResponse.DelegationResponses)) + fmt.Println("Validator "+strconv.Itoa(validatorIndex)+" ", total) + delegators = append(delegators, delegationsResponse.DelegationResponses...) + } + + usd := math.LegacyMustNewDecFromStr("20") + + apiUrl := "https://api.coingecko.com/api/v3/simple/price?ids=" + config.GetNeutronConfig().CoinId + "&vs_currencies=usd" + tokenInUsd := fetchNeutronTokenPrice(apiUrl) + tokenIn20Usd := usd.QuoTruncate(tokenInUsd) + + rewardInfo := []config.Reward{} + balanceInfo := []banktypes.Balance{} + + totalTokenDelegate := math.LegacyMustNewDecFromStr("0") + for _, delegator := range delegators { + validatorIndex := findValidatorInfo(validators, delegator.Delegation.ValidatorAddress) + validatorInfo := validators[validatorIndex] + token := (delegator.Delegation.Shares.MulInt(validatorInfo.Tokens)).QuoTruncate(validatorInfo.DelegatorShares) + if token.LT(tokenIn20Usd) { + continue + } + totalTokenDelegate = totalTokenDelegate.Add(token) + } + eveAirdrop := math.LegacyMustNewDecFromStr(EVE_AIRDROP) + testAmount, _ := math.LegacyNewDecFromStr("0") + for _, delegator := range delegators { + validatorIndex := findValidatorInfo(validators, delegator.Delegation.ValidatorAddress) + validatorInfo := validators[validatorIndex] + token := (delegator.Delegation.Shares.MulInt(validatorInfo.Tokens)).QuoTruncate(validatorInfo.DelegatorShares) + if token.LT(tokenIn20Usd) { + continue + } + eveAirdrop := (eveAirdrop.MulInt64(int64(config.GetNeutronConfig().Percent))).QuoInt64(100).Mul(token).QuoTruncate(totalTokenDelegate) + eveBech32Address := convertBech32Address(delegator.Delegation.DelegatorAddress) + rewardInfo = append(rewardInfo, config.Reward{ + Address: delegator.Delegation.DelegatorAddress, + EveAddress: eveBech32Address, + Shares: delegator.Delegation.Shares, + Token: token, + EveAirdropToken: eveAirdrop, + ChainId: config.GetNeutronConfig().ChainID, + }) + testAmount = eveAirdrop.Add(testAmount) + balanceInfo = append(balanceInfo, banktypes.Balance{ + Address: eveBech32Address, + Coins: sdk.NewCoins(sdk.NewCoin("eve", eveAirdrop.TruncateInt())), + }) + } + fmt.Println(testAmount) + // Write delegations to file + // fileForDebug, _ := json.MarshalIndent(rewardInfo, "", " ") + // _ = os.WriteFile("rewards.json", fileForDebug, 0644) + + // fileBalance, _ := json.MarshalIndent(balanceInfo, "", " ") + // _ = os.WriteFile("balance.json", fileBalance, 0644) + return balanceInfo, rewardInfo +} + +func fetchNeutronTokenPrice(apiUrl string) math.LegacyDec { + // Make a GET request to the API + response, err := http.Get(apiUrl) + if err != nil { + fmt.Println("Error making GET request:", err) + panic("") + } + defer response.Body.Close() + + // Read the response body + responseBody, err := ioutil.ReadAll(response.Body) + if err != nil { + fmt.Println("Error reading response body:", err) + panic("") + } + + var data config.CosmosPrice + + // Unmarshal the JSON byte slice into the defined struct + err = json.Unmarshal(responseBody, &data) + if err != nil { + fmt.Println("Error unmarshalling JSON:", err) + panic("") + } + + tokenInUsd := math.LegacyMustNewDecFromStr(data.Token.USD.String()) + return tokenInUsd +} diff --git a/airdrop/sentinel.go b/airdrop/sentinel.go new file mode 100644 index 00000000..95e88d6e --- /dev/null +++ b/airdrop/sentinel.go @@ -0,0 +1,139 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "strconv" + + "cosmossdk.io/math" + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + grpctypes "github.com/cosmos/cosmos-sdk/types/grpc" + "github.com/cosmos/cosmos-sdk/types/query" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + "github.com/eve-network/eve/airdrop/config" + "github.com/joho/godotenv" + "google.golang.org/grpc" + "google.golang.org/grpc/metadata" +) + +func sentinel() ([]banktypes.Balance, []config.Reward) { + block_height := getLatestHeight(config.GetSentinelConfig().NodeStatusUrl) + godotenv.Load() + grpcAddr := config.GetSentinelConfig().GRPCAddr + grpcConn, err := grpc.Dial(grpcAddr, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) + if err != nil { + panic(err) + } + defer grpcConn.Close() + stakingClient := stakingtypes.NewQueryClient(grpcConn) + + delegators := []stakingtypes.DelegationResponse{} + + validators := getValidators(stakingClient, block_height) + fmt.Println("Validators: ", len(validators)) + for validatorIndex, validator := range validators { + var header metadata.MD + delegationsResponse, _ := stakingClient.ValidatorDelegations( + metadata.AppendToOutgoingContext(context.Background(), grpctypes.GRPCBlockHeightHeader, block_height), // Add metadata to request + &stakingtypes.QueryValidatorDelegationsRequest{ + ValidatorAddr: validator.OperatorAddress, + Pagination: &query.PageRequest{ + CountTotal: true, + Limit: LIMIT_PER_PAGE, + }, + }, + grpc.Header(&header), // Retrieve header from response + ) + total := delegationsResponse.Pagination.Total + fmt.Println("Response ", len(delegationsResponse.DelegationResponses)) + fmt.Println("Validator "+strconv.Itoa(validatorIndex)+" ", total) + delegators = append(delegators, delegationsResponse.DelegationResponses...) + } + + usd := math.LegacyMustNewDecFromStr("20") + + apiUrl := "https://api.coingecko.com/api/v3/simple/price?ids=" + config.GetSentinelConfig().CoinId + "&vs_currencies=usd" + tokenInUsd := fetchSentinelTokenPrice(apiUrl) + tokenIn20Usd := usd.QuoTruncate(tokenInUsd) + + rewardInfo := []config.Reward{} + balanceInfo := []banktypes.Balance{} + + totalTokenDelegate := math.LegacyMustNewDecFromStr("0") + for _, delegator := range delegators { + validatorIndex := findValidatorInfo(validators, delegator.Delegation.ValidatorAddress) + validatorInfo := validators[validatorIndex] + token := (delegator.Delegation.Shares.MulInt(validatorInfo.Tokens)).QuoTruncate(validatorInfo.DelegatorShares) + if token.LT(tokenIn20Usd) { + continue + } + totalTokenDelegate = totalTokenDelegate.Add(token) + } + eveAirdrop := math.LegacyMustNewDecFromStr(EVE_AIRDROP) + testAmount, _ := math.LegacyNewDecFromStr("0") + for _, delegator := range delegators { + validatorIndex := findValidatorInfo(validators, delegator.Delegation.ValidatorAddress) + validatorInfo := validators[validatorIndex] + token := (delegator.Delegation.Shares.MulInt(validatorInfo.Tokens)).QuoTruncate(validatorInfo.DelegatorShares) + if token.LT(tokenIn20Usd) { + continue + } + eveAirdrop := (eveAirdrop.MulInt64(int64(config.GetSentinelConfig().Percent))).QuoInt64(100).Mul(token).QuoTruncate(totalTokenDelegate) + eveBech32Address := convertBech32Address(delegator.Delegation.DelegatorAddress) + rewardInfo = append(rewardInfo, config.Reward{ + Address: delegator.Delegation.DelegatorAddress, + EveAddress: eveBech32Address, + Shares: delegator.Delegation.Shares, + Token: token, + EveAirdropToken: eveAirdrop, + ChainId: config.GetSentinelConfig().ChainID, + }) + testAmount = eveAirdrop.Add(testAmount) + balanceInfo = append(balanceInfo, banktypes.Balance{ + Address: eveBech32Address, + Coins: sdk.NewCoins(sdk.NewCoin("eve", eveAirdrop.TruncateInt())), + }) + } + fmt.Println(testAmount) + // Write delegations to file + // fileForDebug, _ := json.MarshalIndent(rewardInfo, "", " ") + // _ = os.WriteFile("rewards.json", fileForDebug, 0644) + + // fileBalance, _ := json.MarshalIndent(balanceInfo, "", " ") + // _ = os.WriteFile("balance.json", fileBalance, 0644) + return balanceInfo, rewardInfo +} + +func fetchSentinelTokenPrice(apiUrl string) math.LegacyDec { + // Make a GET request to the API + response, err := http.Get(apiUrl) + if err != nil { + fmt.Println("Error making GET request:", err) + panic("") + } + defer response.Body.Close() + + // Read the response body + responseBody, err := ioutil.ReadAll(response.Body) + if err != nil { + fmt.Println("Error reading response body:", err) + panic("") + } + + var data config.SentinelPrice + + // Unmarshal the JSON byte slice into the defined struct + err = json.Unmarshal(responseBody, &data) + if err != nil { + fmt.Println("Error unmarshalling JSON:", err) + panic("") + } + + tokenInUsd := math.LegacyMustNewDecFromStr(data.Token.USD.String()) + return tokenInUsd +} diff --git a/airdrop/stargaze.go b/airdrop/stargaze.go new file mode 100644 index 00000000..4d0997c5 --- /dev/null +++ b/airdrop/stargaze.go @@ -0,0 +1,139 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "strconv" + + "cosmossdk.io/math" + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + grpctypes "github.com/cosmos/cosmos-sdk/types/grpc" + "github.com/cosmos/cosmos-sdk/types/query" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + "github.com/eve-network/eve/airdrop/config" + "github.com/joho/godotenv" + "google.golang.org/grpc" + "google.golang.org/grpc/metadata" +) + +func stargaze() ([]banktypes.Balance, []config.Reward) { + block_height := getLatestHeight(config.GetStargazeConfig().NodeStatusUrl) + godotenv.Load() + grpcAddr := config.GetStargazeConfig().GRPCAddr + grpcConn, err := grpc.Dial(grpcAddr, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) + if err != nil { + panic(err) + } + defer grpcConn.Close() + stakingClient := stakingtypes.NewQueryClient(grpcConn) + + delegators := []stakingtypes.DelegationResponse{} + + validators := getValidators(stakingClient, block_height) + fmt.Println("Validators: ", len(validators)) + for validatorIndex, validator := range validators { + var header metadata.MD + delegationsResponse, _ := stakingClient.ValidatorDelegations( + metadata.AppendToOutgoingContext(context.Background(), grpctypes.GRPCBlockHeightHeader, block_height), // Add metadata to request + &stakingtypes.QueryValidatorDelegationsRequest{ + ValidatorAddr: validator.OperatorAddress, + Pagination: &query.PageRequest{ + CountTotal: true, + Limit: LIMIT_PER_PAGE, + }, + }, + grpc.Header(&header), // Retrieve header from response + ) + total := delegationsResponse.Pagination.Total + fmt.Println("Response ", len(delegationsResponse.DelegationResponses)) + fmt.Println("Validator "+strconv.Itoa(validatorIndex)+" ", total) + delegators = append(delegators, delegationsResponse.DelegationResponses...) + } + + usd := math.LegacyMustNewDecFromStr("20") + + apiUrl := "https://api.coingecko.com/api/v3/simple/price?ids=" + config.GetStargazeConfig().CoinId + "&vs_currencies=usd" + tokenInUsd := fetchStargazeTokenPrice(apiUrl) + tokenIn20Usd := usd.QuoTruncate(tokenInUsd) + + rewardInfo := []config.Reward{} + balanceInfo := []banktypes.Balance{} + + totalTokenDelegate := math.LegacyMustNewDecFromStr("0") + for _, delegator := range delegators { + validatorIndex := findValidatorInfo(validators, delegator.Delegation.ValidatorAddress) + validatorInfo := validators[validatorIndex] + token := (delegator.Delegation.Shares.MulInt(validatorInfo.Tokens)).QuoTruncate(validatorInfo.DelegatorShares) + if token.LT(tokenIn20Usd) { + continue + } + totalTokenDelegate = totalTokenDelegate.Add(token) + } + eveAirdrop := math.LegacyMustNewDecFromStr(EVE_AIRDROP) + testAmount, _ := math.LegacyNewDecFromStr("0") + for _, delegator := range delegators { + validatorIndex := findValidatorInfo(validators, delegator.Delegation.ValidatorAddress) + validatorInfo := validators[validatorIndex] + token := (delegator.Delegation.Shares.MulInt(validatorInfo.Tokens)).QuoTruncate(validatorInfo.DelegatorShares) + if token.LT(tokenIn20Usd) { + continue + } + eveAirdrop := (eveAirdrop.MulInt64(int64(config.GetStargazeConfig().Percent))).QuoInt64(100).Mul(token).QuoTruncate(totalTokenDelegate) + eveBech32Address := convertBech32Address(delegator.Delegation.DelegatorAddress) + rewardInfo = append(rewardInfo, config.Reward{ + Address: delegator.Delegation.DelegatorAddress, + EveAddress: eveBech32Address, + Shares: delegator.Delegation.Shares, + Token: token, + EveAirdropToken: eveAirdrop, + ChainId: config.GetStargazeConfig().ChainID, + }) + testAmount = eveAirdrop.Add(testAmount) + balanceInfo = append(balanceInfo, banktypes.Balance{ + Address: eveBech32Address, + Coins: sdk.NewCoins(sdk.NewCoin("eve", eveAirdrop.TruncateInt())), + }) + } + fmt.Println(testAmount) + // Write delegations to file + // fileForDebug, _ := json.MarshalIndent(rewardInfo, "", " ") + // _ = os.WriteFile("rewards.json", fileForDebug, 0644) + + // fileBalance, _ := json.MarshalIndent(balanceInfo, "", " ") + // _ = os.WriteFile("balance.json", fileBalance, 0644) + return balanceInfo, rewardInfo +} + +func fetchStargazeTokenPrice(apiUrl string) math.LegacyDec { + // Make a GET request to the API + response, err := http.Get(apiUrl) + if err != nil { + fmt.Println("Error making GET request:", err) + panic("") + } + defer response.Body.Close() + + // Read the response body + responseBody, err := ioutil.ReadAll(response.Body) + if err != nil { + fmt.Println("Error reading response body:", err) + panic("") + } + + var data config.StargazePrice + + // Unmarshal the JSON byte slice into the defined struct + err = json.Unmarshal(responseBody, &data) + if err != nil { + fmt.Println("Error unmarshalling JSON:", err) + panic("") + } + + tokenInUsd := math.LegacyMustNewDecFromStr(data.Token.USD.String()) + return tokenInUsd +} diff --git a/airdrop/terra.go b/airdrop/terra.go new file mode 100644 index 00000000..37ee6a5a --- /dev/null +++ b/airdrop/terra.go @@ -0,0 +1,139 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "strconv" + + "cosmossdk.io/math" + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + grpctypes "github.com/cosmos/cosmos-sdk/types/grpc" + "github.com/cosmos/cosmos-sdk/types/query" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + "github.com/eve-network/eve/airdrop/config" + "github.com/joho/godotenv" + "google.golang.org/grpc" + "google.golang.org/grpc/metadata" +) + +func terra() ([]banktypes.Balance, []config.Reward) { + block_height := getLatestHeight(config.GetTerraConfig().NodeStatusUrl) + godotenv.Load() + grpcAddr := config.GetTerraConfig().GRPCAddr + grpcConn, err := grpc.Dial(grpcAddr, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) + if err != nil { + panic(err) + } + defer grpcConn.Close() + stakingClient := stakingtypes.NewQueryClient(grpcConn) + + delegators := []stakingtypes.DelegationResponse{} + + validators := getValidators(stakingClient, block_height) + fmt.Println("Validators: ", len(validators)) + for validatorIndex, validator := range validators { + var header metadata.MD + delegationsResponse, _ := stakingClient.ValidatorDelegations( + metadata.AppendToOutgoingContext(context.Background(), grpctypes.GRPCBlockHeightHeader, block_height), // Add metadata to request + &stakingtypes.QueryValidatorDelegationsRequest{ + ValidatorAddr: validator.OperatorAddress, + Pagination: &query.PageRequest{ + CountTotal: true, + Limit: LIMIT_PER_PAGE, + }, + }, + grpc.Header(&header), // Retrieve header from response + ) + total := delegationsResponse.Pagination.Total + fmt.Println("Response ", len(delegationsResponse.DelegationResponses)) + fmt.Println("Validator "+strconv.Itoa(validatorIndex)+" ", total) + delegators = append(delegators, delegationsResponse.DelegationResponses...) + } + + usd := math.LegacyMustNewDecFromStr("20") + + apiUrl := "https://api.coingecko.com/api/v3/simple/price?ids=" + config.GetTerraConfig().CoinId + "&vs_currencies=usd" + tokenInUsd := fetchTerraTokenPrice(apiUrl) + tokenIn20Usd := usd.QuoTruncate(tokenInUsd) + + rewardInfo := []config.Reward{} + balanceInfo := []banktypes.Balance{} + + totalTokenDelegate := math.LegacyMustNewDecFromStr("0") + for _, delegator := range delegators { + validatorIndex := findValidatorInfo(validators, delegator.Delegation.ValidatorAddress) + validatorInfo := validators[validatorIndex] + token := (delegator.Delegation.Shares.MulInt(validatorInfo.Tokens)).QuoTruncate(validatorInfo.DelegatorShares) + if token.LT(tokenIn20Usd) { + continue + } + totalTokenDelegate = totalTokenDelegate.Add(token) + } + eveAirdrop := math.LegacyMustNewDecFromStr(EVE_AIRDROP) + testAmount, _ := math.LegacyNewDecFromStr("0") + for _, delegator := range delegators { + validatorIndex := findValidatorInfo(validators, delegator.Delegation.ValidatorAddress) + validatorInfo := validators[validatorIndex] + token := (delegator.Delegation.Shares.MulInt(validatorInfo.Tokens)).QuoTruncate(validatorInfo.DelegatorShares) + if token.LT(tokenIn20Usd) { + continue + } + eveAirdrop := (eveAirdrop.MulInt64(int64(config.GetTerraConfig().Percent))).QuoInt64(100).Mul(token).QuoTruncate(totalTokenDelegate) + eveBech32Address := convertBech32Address(delegator.Delegation.DelegatorAddress) + rewardInfo = append(rewardInfo, config.Reward{ + Address: delegator.Delegation.DelegatorAddress, + EveAddress: eveBech32Address, + Shares: delegator.Delegation.Shares, + Token: token, + EveAirdropToken: eveAirdrop, + ChainId: config.GetTerraConfig().ChainID, + }) + testAmount = eveAirdrop.Add(testAmount) + balanceInfo = append(balanceInfo, banktypes.Balance{ + Address: eveBech32Address, + Coins: sdk.NewCoins(sdk.NewCoin("eve", eveAirdrop.TruncateInt())), + }) + } + fmt.Println(testAmount) + // Write delegations to file + // fileForDebug, _ := json.MarshalIndent(rewardInfo, "", " ") + // _ = os.WriteFile("rewards.json", fileForDebug, 0644) + + // fileBalance, _ := json.MarshalIndent(balanceInfo, "", " ") + // _ = os.WriteFile("balance.json", fileBalance, 0644) + return balanceInfo, rewardInfo +} + +func fetchTerraTokenPrice(apiUrl string) math.LegacyDec { + // Make a GET request to the API + response, err := http.Get(apiUrl) + if err != nil { + fmt.Println("Error making GET request:", err) + panic("") + } + defer response.Body.Close() + + // Read the response body + responseBody, err := ioutil.ReadAll(response.Body) + if err != nil { + fmt.Println("Error reading response body:", err) + panic("") + } + + var data config.TerraPrice + + // Unmarshal the JSON byte slice into the defined struct + err = json.Unmarshal(responseBody, &data) + if err != nil { + fmt.Println("Error unmarshalling JSON:", err) + panic("") + } + + tokenInUsd := math.LegacyMustNewDecFromStr(data.Token.USD.String()) + return tokenInUsd +} diff --git a/go.mod b/go.mod index a7d60b4b..a4e331ea 100644 --- a/go.mod +++ b/go.mod @@ -26,7 +26,7 @@ require ( github.com/stretchr/testify v1.9.0 github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect google.golang.org/genproto v0.0.0-20240213162025-012b6fc9bca9 // indirect - google.golang.org/grpc v1.62.1 // indirect + google.golang.org/grpc v1.62.1 gopkg.in/yaml.v2 v2.4.0 // indirect ) @@ -51,6 +51,7 @@ require ( github.com/cosmos/ibc-go/modules/capability v1.0.0 github.com/cosmos/ibc-go/modules/light-clients/08-wasm v0.0.0-20240314094315-e89424c5bf2e github.com/cosmos/ibc-go/v8 v8.1.1 + github.com/joho/godotenv v1.5.1 github.com/osmosis-labs/tokenfactory v0.0.0-20240310155926-981fbeb0fe42 github.com/spf13/viper v1.18.2 ) diff --git a/go.sum b/go.sum index bfd894c8..13426f92 100644 --- a/go.sum +++ b/go.sum @@ -716,6 +716,8 @@ github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGw github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= github.com/jmhodges/levigo v1.0.0 h1:q5EC36kV79HWeTBWsod3mG11EgStG3qArTKcvlksN1U= github.com/jmhodges/levigo v1.0.0/go.mod h1:Q6Qx+uH3RAqyK4rFQroq9RL7mdkABMcfhEI+nNuzMJQ= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= From 58c876494c7021b6c9d8f8cfacdbd069898b81b5 Mon Sep 17 00:00:00 2001 From: tungla Date: Thu, 21 Mar 2024 18:16:03 +0700 Subject: [PATCH 3/7] update fetch logic for other chains --- airdrop/akash.go | 2 +- airdrop/bostrom.go | 68 +++++++++++++++++++++++++++++----------- airdrop/celestia.go | 63 +++++++++++++++++++++++++++---------- airdrop/composable.go | 2 +- airdrop/config/config.go | 21 +++++++------ airdrop/config/types.go | 3 +- airdrop/cosmos.go | 2 +- airdrop/main.go | 4 ++- airdrop/neutron.go | 2 +- airdrop/sentinel.go | 2 +- airdrop/stargaze.go | 2 +- airdrop/terra.go | 5 +-- 12 files changed, 120 insertions(+), 56 deletions(-) diff --git a/airdrop/akash.go b/airdrop/akash.go index 03249048..b255b622 100644 --- a/airdrop/akash.go +++ b/airdrop/akash.go @@ -22,7 +22,7 @@ import ( ) func akash() ([]banktypes.Balance, []config.Reward) { - block_height := getLatestHeight(config.GetAkashConfig().NodeStatusUrl) + block_height := getLatestHeight(config.GetAkashConfig().RPC + "/status") godotenv.Load() grpcAddr := config.GetAkashConfig().GRPCAddr grpcConn, err := grpc.Dial(grpcAddr, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) diff --git a/airdrop/bostrom.go b/airdrop/bostrom.go index a119ec0f..dc585272 100644 --- a/airdrop/bostrom.go +++ b/airdrop/bostrom.go @@ -1,7 +1,6 @@ package main import ( - "context" "encoding/json" "fmt" "io/ioutil" @@ -11,18 +10,15 @@ import ( "cosmossdk.io/math" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - grpctypes "github.com/cosmos/cosmos-sdk/types/grpc" - "github.com/cosmos/cosmos-sdk/types/query" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/eve-network/eve/airdrop/config" "github.com/joho/godotenv" "google.golang.org/grpc" - "google.golang.org/grpc/metadata" ) func bostrom() ([]banktypes.Balance, []config.Reward) { - block_height := getLatestHeight(config.GetBostromConfig().NodeStatusUrl) + // block_height := getLatestHeight(config.GetBostromConfig().RPC + "/status") godotenv.Load() grpcAddr := config.GetBostromConfig().GRPCAddr grpcConn, err := grpc.Dial(grpcAddr, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) @@ -30,25 +26,30 @@ func bostrom() ([]banktypes.Balance, []config.Reward) { panic(err) } defer grpcConn.Close() - stakingClient := stakingtypes.NewQueryClient(grpcConn) + // stakingClient := stakingtypes.NewQueryClient(grpcConn) delegators := []stakingtypes.DelegationResponse{} - validators := getValidators(stakingClient, block_height) + // validators := getValidators(stakingClient, block_height) + rpc := config.GetBostromConfig().RPC + "/cosmos/staking/v1beta1/validators?pagination.count_total=true" + validatorsResponse := fetchValidators(rpc) + validators := validatorsResponse.Validators fmt.Println("Validators: ", len(validators)) for validatorIndex, validator := range validators { - var header metadata.MD - delegationsResponse, _ := stakingClient.ValidatorDelegations( - metadata.AppendToOutgoingContext(context.Background(), grpctypes.GRPCBlockHeightHeader, block_height), // Add metadata to request - &stakingtypes.QueryValidatorDelegationsRequest{ - ValidatorAddr: validator.OperatorAddress, - Pagination: &query.PageRequest{ - CountTotal: true, - Limit: LIMIT_PER_PAGE, - }, - }, - grpc.Header(&header), // Retrieve header from response - ) + // var header metadata.MD + // delegationsResponse, _ := stakingClient.ValidatorDelegations( + // metadata.AppendToOutgoingContext(context.Background(), grpctypes.GRPCBlockHeightHeader, block_height), // Add metadata to request + // &stakingtypes.QueryValidatorDelegationsRequest{ + // ValidatorAddr: validator.OperatorAddress, + // Pagination: &query.PageRequest{ + // CountTotal: true, + // Limit: LIMIT_PER_PAGE, + // }, + // }, + // grpc.Header(&header), // Retrieve header from response + // ) + rpcUrl := config.GetBostromConfig().RPC + "/cosmos/staking/v1beta1/validators/" + validator.String() + "/delegations?pagination.limit=" + string(LIMIT_PER_PAGE) + "&pagination.count_total=true" + delegationsResponse := fetchDelegations(rpcUrl) total := delegationsResponse.Pagination.Total fmt.Println("Response ", len(delegationsResponse.DelegationResponses)) fmt.Println("Validator "+strconv.Itoa(validatorIndex)+" ", total) @@ -137,3 +138,32 @@ func fetchBostromTokenPrice(apiUrl string) math.LegacyDec { tokenInUsd := math.LegacyMustNewDecFromStr(data.Token.USD.String()) return tokenInUsd } + +func fetchValidators(rpcUrl string) stakingtypes.QueryValidatorsResponse { + // Make a GET request to the API + response, err := http.Get(rpcUrl) + if err != nil { + fmt.Println("Error making GET request:", err) + panic("") + } + defer response.Body.Close() + + // Read the response body + responseBody, err := ioutil.ReadAll(response.Body) + if err != nil { + fmt.Println("Error reading response body:", err) + panic("") + } + + var data stakingtypes.QueryValidatorsResponse + + // Unmarshal the JSON byte slice into the defined struct + err = json.Unmarshal(responseBody, &data) + if err != nil { + fmt.Println("Error unmarshalling JSON:", err) + panic("") + } + + fmt.Println(data) + return data +} diff --git a/airdrop/celestia.go b/airdrop/celestia.go index 455363be..66eece12 100644 --- a/airdrop/celestia.go +++ b/airdrop/celestia.go @@ -2,7 +2,6 @@ package main // error max size response import ( - "context" "encoding/json" "fmt" "io/ioutil" @@ -12,18 +11,15 @@ import ( "cosmossdk.io/math" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - grpctypes "github.com/cosmos/cosmos-sdk/types/grpc" - "github.com/cosmos/cosmos-sdk/types/query" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/eve-network/eve/airdrop/config" "github.com/joho/godotenv" "google.golang.org/grpc" - "google.golang.org/grpc/metadata" ) func celestia() ([]banktypes.Balance, []config.Reward) { - block_height := getLatestHeight(config.GetCelestiaConfig().NodeStatusUrl) + block_height := getLatestHeight(config.GetCelestiaConfig().RPC + "/status") godotenv.Load() grpcAddr := config.GetCelestiaConfig().GRPCAddr grpcConn, err := grpc.Dial(grpcAddr, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) @@ -38,18 +34,22 @@ func celestia() ([]banktypes.Balance, []config.Reward) { validators := getValidators(stakingClient, block_height) fmt.Println("Validators: ", len(validators)) for validatorIndex, validator := range validators { - var header metadata.MD - delegationsResponse, err := stakingClient.ValidatorDelegations( - metadata.AppendToOutgoingContext(context.Background(), grpctypes.GRPCBlockHeightHeader, block_height), // Add metadata to request - &stakingtypes.QueryValidatorDelegationsRequest{ - ValidatorAddr: validator.OperatorAddress, - Pagination: &query.PageRequest{ - CountTotal: true, - Limit: LIMIT_PER_PAGE, - }, - }, - grpc.Header(&header), // Retrieve header from response - ) + // var header metadata.MD + // delegationsResponse, err := stakingClient.ValidatorDelegations( + // metadata.AppendToOutgoingContext(context.Background(), grpctypes.GRPCBlockHeightHeader, block_height), // Add metadata to request + // &stakingtypes.QueryValidatorDelegationsRequest{ + // ValidatorAddr: validator.OperatorAddress, + // Pagination: &query.PageRequest{ + // CountTotal: true, + // Limit: LIMIT_PER_PAGE, + // }, + // }, + // grpc.Header(&header), // Retrieve header from response + // ) + // stakingtypes.QueryValidatorDelegationsResponse + rpcUrl := config.GetCelestiaConfig().API + "/validators/" + validator.String() + "/delegations?pagination.limit=" + string(LIMIT_PER_PAGE) + "&pagination.count_total=true" + fmt.Println(rpcUrl) + delegationsResponse := fetchDelegations(rpcUrl) fmt.Println("err: ", err) total := delegationsResponse.Pagination.Total fmt.Println("Response ", len(delegationsResponse.DelegationResponses)) @@ -111,6 +111,35 @@ func celestia() ([]banktypes.Balance, []config.Reward) { return balanceInfo, rewardInfo } +func fetchDelegations(rpcUrl string) stakingtypes.QueryValidatorDelegationsResponse { + // Make a GET request to the API + response, err := http.Get(rpcUrl) + if err != nil { + fmt.Println("Error making GET request:", err) + panic("") + } + defer response.Body.Close() + + // Read the response body + responseBody, err := ioutil.ReadAll(response.Body) + if err != nil { + fmt.Println("Error reading response body:", err) + panic("") + } + + var data stakingtypes.QueryValidatorDelegationsResponse + fmt.Println(responseBody) + // Unmarshal the JSON byte slice into the defined struct + err = data.Unmarshal(responseBody) + if err != nil { + fmt.Println("Error unmarshalling JSON:", err) + panic("") + } + + fmt.Println(data) + return data +} + func fetchCelestiaTokenPrice(apiUrl string) math.LegacyDec { // Make a GET request to the API response, err := http.Get(apiUrl) diff --git a/airdrop/composable.go b/airdrop/composable.go index cc6e4b76..2ccc3c48 100644 --- a/airdrop/composable.go +++ b/airdrop/composable.go @@ -22,7 +22,7 @@ import ( ) func composable() ([]banktypes.Balance, []config.Reward) { - block_height := getLatestHeight(config.GetComposableConfig().NodeStatusUrl) + block_height := getLatestHeight(config.GetComposableConfig().RPC + "/status") godotenv.Load() grpcAddr := config.GetComposableConfig().GRPCAddr grpcConn, err := grpc.Dial(grpcAddr, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) diff --git a/airdrop/config/config.go b/airdrop/config/config.go index 011c8eff..20a25890 100644 --- a/airdrop/config/config.go +++ b/airdrop/config/config.go @@ -8,7 +8,7 @@ func GetCosmosHubConfig() *ChainClientConfig { AccountPrefix: "cosmos", CoinId: "cosmos", Percent: 0, - NodeStatusUrl: "https://cosmos-rpc.polkachu.com/status", + RPC: "https://cosmos-rpc.polkachu.com", } } @@ -20,7 +20,7 @@ func GetComposableConfig() *ChainClientConfig { AccountPrefix: "centauri", Percent: 13, CoinId: "picasso", - NodeStatusUrl: "https://composable-rpc.polkachu.com/status", + RPC: "https://composable-rpc.polkachu.com", } } @@ -32,7 +32,8 @@ func GetCelestiaConfig() *ChainClientConfig { AccountPrefix: "celestia", Percent: 13, CoinId: "celestia", - NodeStatusUrl: "https://celestia-rpc.polkachu.com/status", + RPC: "https://celestia-rpc.polkachu.com", + API: "https://public-celestia-lcd.numia.xyz", } } @@ -44,7 +45,7 @@ func GetSentinelConfig() *ChainClientConfig { AccountPrefix: "sent", Percent: 13, CoinId: "sentinel", - NodeStatusUrl: "https://sentinel-rpc.polkachu.com/status", + RPC: "https://sentinel-rpc.polkachu.com", } } @@ -56,7 +57,7 @@ func GetAkashConfig() *ChainClientConfig { AccountPrefix: "akash", Percent: 13, CoinId: "akash-network", - NodeStatusUrl: "https://akash-rpc.polkachu.com/status", + RPC: "https://akash-rpc.polkachu.com", } } @@ -68,7 +69,7 @@ func GetStargazeConfig() *ChainClientConfig { AccountPrefix: "stars", Percent: 13, CoinId: "stargaze", - NodeStatusUrl: "https://stargaze-rpc.polkachu.com/status", + RPC: "https://stargaze-rpc.polkachu.com", } } @@ -80,7 +81,7 @@ func GetNeutronConfig() *ChainClientConfig { AccountPrefix: "neutron", Percent: 13, CoinId: "neutron-3", - NodeStatusUrl: "https://neutron-rpc.polkachu.com/status", + RPC: "https://neutron-rpc.polkachu.com", } } @@ -92,7 +93,7 @@ func GetTerraConfig() *ChainClientConfig { AccountPrefix: "terra", Percent: 13, CoinId: "terra-luna-2", - NodeStatusUrl: "https://terra-rpc.polkachu.com/status", + RPC: "https://terra-rpc.polkachu.com", } } @@ -100,10 +101,10 @@ func GetBostromConfig() *ChainClientConfig { return &ChainClientConfig{ Key: "default", ChainID: "bostrom", - GRPCAddr: "grpc-cyber-ia.cosmosia.notional.ventures:443", + GRPCAddr: "grpc.cyber.bronbro.io:443", AccountPrefix: "bostrom", Percent: 13, CoinId: "bostrom", - NodeStatusUrl: "https://rpc-cyber-ia.cosmosia.notional.ventures/status", + RPC: "https://rpc.bostrom.cybernode.ai", } } diff --git a/airdrop/config/types.go b/airdrop/config/types.go index c5ecc53d..7c97d67f 100644 --- a/airdrop/config/types.go +++ b/airdrop/config/types.go @@ -13,7 +13,8 @@ type ChainClientConfig struct { AccountPrefix string `json:"account-prefix" yaml:"account-prefix"` Percent int `json:"percent" yaml:"percent"` CoinId string `json:"coin-id" yaml:"coin-id"` - NodeStatusUrl string `json:"node-status" yaml:"node-status"` + RPC string `json:"rpc" yaml:"rpc"` + API string `json:"api" yaml:"api"` } type Reward struct { diff --git a/airdrop/cosmos.go b/airdrop/cosmos.go index 3f3ab279..ef10944d 100644 --- a/airdrop/cosmos.go +++ b/airdrop/cosmos.go @@ -22,7 +22,7 @@ import ( ) func cosmos() ([]banktypes.Balance, []config.Reward) { - block_height := getLatestHeight(config.GetCosmosHubConfig().NodeStatusUrl) + block_height := getLatestHeight(config.GetCosmosHubConfig().RPC + "/status") godotenv.Load() grpcAddr := config.GetCosmosHubConfig().GRPCAddr grpcConn, err := grpc.Dial(grpcAddr, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) diff --git a/airdrop/main.go b/airdrop/main.go index c083fe4b..8ecbbbfb 100644 --- a/airdrop/main.go +++ b/airdrop/main.go @@ -17,6 +17,8 @@ import ( "google.golang.org/grpc/metadata" ) +// got to export genesis state from neutron and bostrom chain + const ( EVE_AIRDROP = "1000000000" // 1,000,000,000 LIMIT_PER_PAGE = 100000000 @@ -41,7 +43,7 @@ func getValidators(stakingClient stakingtypes.QueryClient, block_height string) } func main() { - balanceComposableInfo, rewardComposableInfo := terra() + balanceComposableInfo, rewardComposableInfo := celestia() // Write delegations to file fileForDebug, _ := json.MarshalIndent(rewardComposableInfo, "", " ") _ = os.WriteFile("rewards.json", fileForDebug, 0644) diff --git a/airdrop/neutron.go b/airdrop/neutron.go index ed93987e..4d663670 100644 --- a/airdrop/neutron.go +++ b/airdrop/neutron.go @@ -23,7 +23,7 @@ import ( ) func neutron() ([]banktypes.Balance, []config.Reward) { - block_height := getLatestHeight(config.GetNeutronConfig().NodeStatusUrl) + block_height := getLatestHeight(config.GetNeutronConfig().RPC + "/status") godotenv.Load() grpcAddr := config.GetNeutronConfig().GRPCAddr grpcConn, err := grpc.Dial(grpcAddr, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) diff --git a/airdrop/sentinel.go b/airdrop/sentinel.go index 95e88d6e..ff144015 100644 --- a/airdrop/sentinel.go +++ b/airdrop/sentinel.go @@ -22,7 +22,7 @@ import ( ) func sentinel() ([]banktypes.Balance, []config.Reward) { - block_height := getLatestHeight(config.GetSentinelConfig().NodeStatusUrl) + block_height := getLatestHeight(config.GetSentinelConfig().RPC + "/status") godotenv.Load() grpcAddr := config.GetSentinelConfig().GRPCAddr grpcConn, err := grpc.Dial(grpcAddr, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) diff --git a/airdrop/stargaze.go b/airdrop/stargaze.go index 4d0997c5..c5eac5cc 100644 --- a/airdrop/stargaze.go +++ b/airdrop/stargaze.go @@ -22,7 +22,7 @@ import ( ) func stargaze() ([]banktypes.Balance, []config.Reward) { - block_height := getLatestHeight(config.GetStargazeConfig().NodeStatusUrl) + block_height := getLatestHeight(config.GetStargazeConfig().RPC + "/status") godotenv.Load() grpcAddr := config.GetStargazeConfig().GRPCAddr grpcConn, err := grpc.Dial(grpcAddr, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) diff --git a/airdrop/terra.go b/airdrop/terra.go index 37ee6a5a..c371a4bf 100644 --- a/airdrop/terra.go +++ b/airdrop/terra.go @@ -22,7 +22,7 @@ import ( ) func terra() ([]banktypes.Balance, []config.Reward) { - block_height := getLatestHeight(config.GetTerraConfig().NodeStatusUrl) + block_height := getLatestHeight(config.GetTerraConfig().RPC + "/status") godotenv.Load() grpcAddr := config.GetTerraConfig().GRPCAddr grpcConn, err := grpc.Dial(grpcAddr, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) @@ -38,7 +38,7 @@ func terra() ([]banktypes.Balance, []config.Reward) { fmt.Println("Validators: ", len(validators)) for validatorIndex, validator := range validators { var header metadata.MD - delegationsResponse, _ := stakingClient.ValidatorDelegations( + delegationsResponse, err := stakingClient.ValidatorDelegations( metadata.AppendToOutgoingContext(context.Background(), grpctypes.GRPCBlockHeightHeader, block_height), // Add metadata to request &stakingtypes.QueryValidatorDelegationsRequest{ ValidatorAddr: validator.OperatorAddress, @@ -49,6 +49,7 @@ func terra() ([]banktypes.Balance, []config.Reward) { }, grpc.Header(&header), // Retrieve header from response ) + fmt.Println("err: ", err) total := delegationsResponse.Pagination.Total fmt.Println("Response ", len(delegationsResponse.DelegationResponses)) fmt.Println("Validator "+strconv.Itoa(validatorIndex)+" ", total) From 8211fc6f2953f9a33d8a24c02424b284a64423ed Mon Sep 17 00:00:00 2001 From: tungla Date: Fri, 22 Mar 2024 18:35:42 +0700 Subject: [PATCH 4/7] feat: 90% --- airdrop/bostrom.go | 81 ++++++++++++++++++++---------- airdrop/celestia.go | 89 ++++++++++++++++---------------- airdrop/config/config.go | 4 +- airdrop/config/types.go | 30 +++++++++++ airdrop/main.go | 29 ++++++++++- airdrop/neutron.go | 106 ++++++++++++++++++++++----------------- 6 files changed, 218 insertions(+), 121 deletions(-) diff --git a/airdrop/bostrom.go b/airdrop/bostrom.go index dc585272..e1da2450 100644 --- a/airdrop/bostrom.go +++ b/airdrop/bostrom.go @@ -3,6 +3,7 @@ package main import ( "encoding/json" "fmt" + "io" "io/ioutil" "net/http" "strconv" @@ -26,34 +27,20 @@ func bostrom() ([]banktypes.Balance, []config.Reward) { panic(err) } defer grpcConn.Close() - // stakingClient := stakingtypes.NewQueryClient(grpcConn) delegators := []stakingtypes.DelegationResponse{} - // validators := getValidators(stakingClient, block_height) - rpc := config.GetBostromConfig().RPC + "/cosmos/staking/v1beta1/validators?pagination.count_total=true" + rpc := config.GetBostromConfig().API + "/cosmos/staking/v1beta1/validators?pagination.limit" + strconv.Itoa(LIMIT_PER_PAGE) + "pagination.count_total=true" validatorsResponse := fetchValidators(rpc) validators := validatorsResponse.Validators fmt.Println("Validators: ", len(validators)) for validatorIndex, validator := range validators { - // var header metadata.MD - // delegationsResponse, _ := stakingClient.ValidatorDelegations( - // metadata.AppendToOutgoingContext(context.Background(), grpctypes.GRPCBlockHeightHeader, block_height), // Add metadata to request - // &stakingtypes.QueryValidatorDelegationsRequest{ - // ValidatorAddr: validator.OperatorAddress, - // Pagination: &query.PageRequest{ - // CountTotal: true, - // Limit: LIMIT_PER_PAGE, - // }, - // }, - // grpc.Header(&header), // Retrieve header from response - // ) - rpcUrl := config.GetBostromConfig().RPC + "/cosmos/staking/v1beta1/validators/" + validator.String() + "/delegations?pagination.limit=" + string(LIMIT_PER_PAGE) + "&pagination.count_total=true" - delegationsResponse := fetchDelegations(rpcUrl) - total := delegationsResponse.Pagination.Total - fmt.Println("Response ", len(delegationsResponse.DelegationResponses)) + url := config.GetBostromConfig().API + "/cosmos/staking/v1beta1/validators/" + validator.OperatorAddress + "/delegations?pagination.limit=" + strconv.Itoa(LIMIT_PER_PAGE) + "&pagination.count_total=true" + delegations, total := fetchDelegations(url) + fmt.Println(validator.OperatorAddress) + fmt.Println("Response ", len(delegations)) fmt.Println("Validator "+strconv.Itoa(validatorIndex)+" ", total) - delegators = append(delegators, delegationsResponse.DelegationResponses...) + delegators = append(delegators, delegations...) } usd := math.LegacyMustNewDecFromStr("20") @@ -67,7 +54,7 @@ func bostrom() ([]banktypes.Balance, []config.Reward) { totalTokenDelegate := math.LegacyMustNewDecFromStr("0") for _, delegator := range delegators { - validatorIndex := findValidatorInfo(validators, delegator.Delegation.ValidatorAddress) + validatorIndex := findValidatorBostromInfo(validators, delegator.Delegation.ValidatorAddress) validatorInfo := validators[validatorIndex] token := (delegator.Delegation.Shares.MulInt(validatorInfo.Tokens)).QuoTruncate(validatorInfo.DelegatorShares) if token.LT(tokenIn20Usd) { @@ -78,7 +65,7 @@ func bostrom() ([]banktypes.Balance, []config.Reward) { eveAirdrop := math.LegacyMustNewDecFromStr(EVE_AIRDROP) testAmount, _ := math.LegacyNewDecFromStr("0") for _, delegator := range delegators { - validatorIndex := findValidatorInfo(validators, delegator.Delegation.ValidatorAddress) + validatorIndex := findValidatorBostromInfo(validators, delegator.Delegation.ValidatorAddress) validatorInfo := validators[validatorIndex] token := (delegator.Delegation.Shares.MulInt(validatorInfo.Tokens)).QuoTruncate(validatorInfo.DelegatorShares) if token.LT(tokenIn20Usd) { @@ -134,12 +121,13 @@ func fetchBostromTokenPrice(apiUrl string) math.LegacyDec { fmt.Println("Error unmarshalling JSON:", err) panic("") } - + fmt.Println(data.Token.USD) tokenInUsd := math.LegacyMustNewDecFromStr(data.Token.USD.String()) + fmt.Println(tokenInUsd) return tokenInUsd } -func fetchValidators(rpcUrl string) stakingtypes.QueryValidatorsResponse { +func fetchValidators(rpcUrl string) config.ValidatorResponse { // Make a GET request to the API response, err := http.Get(rpcUrl) if err != nil { @@ -149,13 +137,13 @@ func fetchValidators(rpcUrl string) stakingtypes.QueryValidatorsResponse { defer response.Body.Close() // Read the response body - responseBody, err := ioutil.ReadAll(response.Body) + responseBody, err := io.ReadAll(response.Body) if err != nil { fmt.Println("Error reading response body:", err) panic("") } - var data stakingtypes.QueryValidatorsResponse + var data config.ValidatorResponse // Unmarshal the JSON byte slice into the defined struct err = json.Unmarshal(responseBody, &data) @@ -164,6 +152,45 @@ func fetchValidators(rpcUrl string) stakingtypes.QueryValidatorsResponse { panic("") } - fmt.Println(data) + fmt.Println(data.Pagination.Total) return data } + +func findValidatorBostromInfo(validators []config.Validator, address string) int { + for key, v := range validators { + if v.OperatorAddress == address { + return key + } + } + return -1 +} + +func fetchDelegations(rpcUrl string) (stakingtypes.DelegationResponses, uint64) { + // Make a GET request to the API + response, err := http.Get(rpcUrl) + if err != nil { + fmt.Println("Error making GET request:", err) + panic("") + } + defer response.Body.Close() + + // Read the response body + responseBody, err := ioutil.ReadAll(response.Body) + if err != nil { + fmt.Println("Error reading response body:", err) + panic("") + } + + var data config.QueryValidatorDelegationsResponse + + // Unmarshal the JSON byte slice into the defined struct + err = json.Unmarshal(responseBody, &data) + if err != nil { + fmt.Println("Error unmarshalling JSON:", err) + panic("") + } + + fmt.Println(data.Pagination.Total) + total, _ := strconv.ParseUint(data.Pagination.Total, 10, 64) + return data.DelegationResponses, total +} diff --git a/airdrop/celestia.go b/airdrop/celestia.go index 66eece12..1b3c9840 100644 --- a/airdrop/celestia.go +++ b/airdrop/celestia.go @@ -2,6 +2,7 @@ package main // error max size response import ( + "context" "encoding/json" "fmt" "io/ioutil" @@ -11,11 +12,14 @@ import ( "cosmossdk.io/math" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" + grpctypes "github.com/cosmos/cosmos-sdk/types/grpc" + "github.com/cosmos/cosmos-sdk/types/query" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/eve-network/eve/airdrop/config" "github.com/joho/godotenv" "google.golang.org/grpc" + "google.golang.org/grpc/metadata" ) func celestia() ([]banktypes.Balance, []config.Reward) { @@ -34,27 +38,11 @@ func celestia() ([]banktypes.Balance, []config.Reward) { validators := getValidators(stakingClient, block_height) fmt.Println("Validators: ", len(validators)) for validatorIndex, validator := range validators { - // var header metadata.MD - // delegationsResponse, err := stakingClient.ValidatorDelegations( - // metadata.AppendToOutgoingContext(context.Background(), grpctypes.GRPCBlockHeightHeader, block_height), // Add metadata to request - // &stakingtypes.QueryValidatorDelegationsRequest{ - // ValidatorAddr: validator.OperatorAddress, - // Pagination: &query.PageRequest{ - // CountTotal: true, - // Limit: LIMIT_PER_PAGE, - // }, - // }, - // grpc.Header(&header), // Retrieve header from response - // ) - // stakingtypes.QueryValidatorDelegationsResponse - rpcUrl := config.GetCelestiaConfig().API + "/validators/" + validator.String() + "/delegations?pagination.limit=" + string(LIMIT_PER_PAGE) + "&pagination.count_total=true" - fmt.Println(rpcUrl) - delegationsResponse := fetchDelegations(rpcUrl) - fmt.Println("err: ", err) - total := delegationsResponse.Pagination.Total - fmt.Println("Response ", len(delegationsResponse.DelegationResponses)) + delegations, total := fetchDelegationsv2(validator.OperatorAddress, block_height, validatorIndex) + fmt.Println(validator.OperatorAddress) + fmt.Println("Response ", len(delegations)) fmt.Println("Validator "+strconv.Itoa(validatorIndex)+" ", total) - delegators = append(delegators, delegationsResponse.DelegationResponses...) + delegators = append(delegators, delegations...) } usd := math.LegacyMustNewDecFromStr("20") @@ -111,33 +99,46 @@ func celestia() ([]banktypes.Balance, []config.Reward) { return balanceInfo, rewardInfo } -func fetchDelegations(rpcUrl string) stakingtypes.QueryValidatorDelegationsResponse { - // Make a GET request to the API - response, err := http.Get(rpcUrl) - if err != nil { - fmt.Println("Error making GET request:", err) - panic("") - } - defer response.Body.Close() - - // Read the response body - responseBody, err := ioutil.ReadAll(response.Body) +func fetchDelegationsv2(valAddr string, block_height string, validatorIndex int) ([]stakingtypes.DelegationResponse, uint64) { + grpcAddr := config.GetCelestiaConfig().GRPCAddr + grpcConn, err := grpc.Dial(grpcAddr, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) if err != nil { - fmt.Println("Error reading response body:", err) - panic("") + panic(err) } - - var data stakingtypes.QueryValidatorDelegationsResponse - fmt.Println(responseBody) - // Unmarshal the JSON byte slice into the defined struct - err = data.Unmarshal(responseBody) - if err != nil { - fmt.Println("Error unmarshalling JSON:", err) - panic("") + defer grpcConn.Close() + stakingClient := stakingtypes.NewQueryClient(grpcConn) + var header metadata.MD + var delegations *stakingtypes.QueryValidatorDelegationsResponse + var paginationKey []byte + delegationInfo := stakingtypes.DelegationResponses{} + step := 5000 + total := uint64(0) + // Fetch delegations, 5000 at a time + i := 0 + for { + i += 1 + fmt.Println("Fetching delegations", step*i, "to", step*(i+1)) + delegations, err = stakingClient.ValidatorDelegations( + metadata.AppendToOutgoingContext(context.Background(), grpctypes.GRPCBlockHeightHeader, block_height), // Add metadata to request + &stakingtypes.QueryValidatorDelegationsRequest{ + ValidatorAddr: valAddr, + Pagination: &query.PageRequest{ + Limit: uint64(step), + Key: paginationKey, + CountTotal: true, + }, + }, + grpc.Header(&header), // Retrieve header from response + ) + fmt.Println(err) + total = delegations.Pagination.Total + delegationInfo = append(delegationInfo, delegations.DelegationResponses...) + paginationKey = delegations.Pagination.NextKey + if len(paginationKey) == 0 { + break + } } - - fmt.Println(data) - return data + return delegationInfo, total } func fetchCelestiaTokenPrice(apiUrl string) math.LegacyDec { diff --git a/airdrop/config/config.go b/airdrop/config/config.go index 20a25890..492e1e5c 100644 --- a/airdrop/config/config.go +++ b/airdrop/config/config.go @@ -33,7 +33,7 @@ func GetCelestiaConfig() *ChainClientConfig { Percent: 13, CoinId: "celestia", RPC: "https://celestia-rpc.polkachu.com", - API: "https://public-celestia-lcd.numia.xyz", + API: "https://celestia-api.polkachu.com", } } @@ -105,6 +105,6 @@ func GetBostromConfig() *ChainClientConfig { AccountPrefix: "bostrom", Percent: 13, CoinId: "bostrom", - RPC: "https://rpc.bostrom.cybernode.ai", + API: "https://lcd.bostrom.cybernode.ai", } } diff --git a/airdrop/config/types.go b/airdrop/config/types.go index 7c97d67f..b274e7d0 100644 --- a/airdrop/config/types.go +++ b/airdrop/config/types.go @@ -4,6 +4,7 @@ import ( "encoding/json" "cosmossdk.io/math" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) type ChainClientConfig struct { @@ -91,3 +92,32 @@ type SyncInfo struct { LatestBlockTime string `json:"latest_block_time"` } type ValidatorInfo struct{} + +type ValidatorResponse struct { + Validators []Validator `json:"validators"` + Pagination Pagination `json:"pagination"` +} + +type Pagination struct { + // next_key is the key to be passed to PageRequest.key to + // query the next page most efficiently + NextKey []byte `protobuf:"bytes,1,opt,name=next_key,json=nextKey,proto3" json:"next_key,omitempty"` + // total is total number of results available if PageRequest.count_total + // was set, its value is undefined otherwise + Total string `protobuf:"varint,2,opt,name=total,proto3" json:"total,omitempty"` +} + +type Validator struct { + // operator_address defines the address of the validator's operator; bech encoded in JSON. + OperatorAddress string `protobuf:"bytes,1,opt,name=operator_address,json=operatorAddress,proto3" json:"operator_address,omitempty" yaml:"operator_address"` + // tokens define the delegated tokens (incl. self-delegation). + Tokens math.Int `protobuf:"bytes,5,opt,name=tokens,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Int" json:"tokens"` + // delegator_shares defines total shares issued to a validator's delegators. + DelegatorShares math.LegacyDec `protobuf:"bytes,6,opt,name=delegator_shares,json=delegatorShares,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"delegator_shares" yaml:"delegator_shares"` +} + +type QueryValidatorDelegationsResponse struct { + DelegationResponses stakingtypes.DelegationResponses `protobuf:"bytes,1,rep,name=delegation_responses,json=delegationResponses,proto3,castrepeated=DelegationResponses" json:"delegation_responses"` + // pagination defines the pagination in the response. + Pagination Pagination `protobuf:"bytes,2,opt,name=pagination,proto3" json:"pagination,omitempty"` +} diff --git a/airdrop/main.go b/airdrop/main.go index 8ecbbbfb..40b61ad1 100644 --- a/airdrop/main.go +++ b/airdrop/main.go @@ -8,9 +8,12 @@ import ( "net/http" "os" + "cosmossdk.io/math" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/bech32" grpctypes "github.com/cosmos/cosmos-sdk/types/grpc" "github.com/cosmos/cosmos-sdk/types/query" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/eve-network/eve/airdrop/config" "google.golang.org/grpc" @@ -43,12 +46,34 @@ func getValidators(stakingClient stakingtypes.QueryClient, block_height string) } func main() { - balanceComposableInfo, rewardComposableInfo := celestia() + apiUrl := "https://api.coingecko.com/api/v3/simple/price?ids=" + config.GetBostromConfig().CoinId + "&vs_currencies=usd" + fetchBostromTokenPrice(apiUrl) + return + balanceComposableInfo, rewardComposableInfo := bostrom() + + airdropMap := make(map[string]int) + for _, info := range balanceComposableInfo { + amount := airdropMap[info.Address] + airdropMap[info.Address] = amount + int(info.Coins.AmountOf("eve").Int64()) + } + + balanceInfo := []banktypes.Balance{} + checkBalance := 0 + for address, amount := range airdropMap { + checkBalance += amount + balanceInfo = append(balanceInfo, banktypes.Balance{ + Address: address, + Coins: sdk.NewCoins(sdk.NewCoin("eve", math.NewInt(int64(amount)))), + }) + } + + fmt.Println("Check balance: ", checkBalance) + // Write delegations to file fileForDebug, _ := json.MarshalIndent(rewardComposableInfo, "", " ") _ = os.WriteFile("rewards.json", fileForDebug, 0644) - fileBalance, _ := json.MarshalIndent(balanceComposableInfo, "", " ") + fileBalance, _ := json.MarshalIndent(balanceInfo, "", " ") _ = os.WriteFile("balance.json", fileBalance, 0644) } diff --git a/airdrop/neutron.go b/airdrop/neutron.go index 4d663670..03cfe056 100644 --- a/airdrop/neutron.go +++ b/airdrop/neutron.go @@ -7,7 +7,6 @@ import ( "fmt" "io/ioutil" "net/http" - "strconv" "cosmossdk.io/math" "github.com/cosmos/cosmos-sdk/codec" @@ -15,7 +14,6 @@ import ( grpctypes "github.com/cosmos/cosmos-sdk/types/grpc" "github.com/cosmos/cosmos-sdk/types/query" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" - stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/eve-network/eve/airdrop/config" "github.com/joho/godotenv" "google.golang.org/grpc" @@ -31,66 +29,38 @@ func neutron() ([]banktypes.Balance, []config.Reward) { panic(err) } defer grpcConn.Close() - stakingClient := stakingtypes.NewQueryClient(grpcConn) - delegators := []stakingtypes.DelegationResponse{} + addresses, total := fetchBalance(block_height) + fmt.Println("Response ", len(addresses)) + fmt.Println("Total ", total) - validators := getValidators(stakingClient, block_height) - fmt.Println("Validators: ", len(validators)) - for validatorIndex, validator := range validators { - var header metadata.MD - delegationsResponse, _ := stakingClient.ValidatorDelegations( - metadata.AppendToOutgoingContext(context.Background(), grpctypes.GRPCBlockHeightHeader, block_height), // Add metadata to request - &stakingtypes.QueryValidatorDelegationsRequest{ - ValidatorAddr: validator.OperatorAddress, - Pagination: &query.PageRequest{ - CountTotal: true, - Limit: LIMIT_PER_PAGE, - }, - }, - grpc.Header(&header), // Retrieve header from response - ) - total := delegationsResponse.Pagination.Total - fmt.Println("Response ", len(delegationsResponse.DelegationResponses)) - fmt.Println("Validator "+strconv.Itoa(validatorIndex)+" ", total) - delegators = append(delegators, delegationsResponse.DelegationResponses...) - } - - usd := math.LegacyMustNewDecFromStr("20") + usd, _ := math.LegacyNewDecFromStr("20") apiUrl := "https://api.coingecko.com/api/v3/simple/price?ids=" + config.GetNeutronConfig().CoinId + "&vs_currencies=usd" tokenInUsd := fetchNeutronTokenPrice(apiUrl) - tokenIn20Usd := usd.QuoTruncate(tokenInUsd) - + tokenIn20Usd := usd.Quo(tokenInUsd) rewardInfo := []config.Reward{} balanceInfo := []banktypes.Balance{} - totalTokenDelegate := math.LegacyMustNewDecFromStr("0") - for _, delegator := range delegators { - validatorIndex := findValidatorInfo(validators, delegator.Delegation.ValidatorAddress) - validatorInfo := validators[validatorIndex] - token := (delegator.Delegation.Shares.MulInt(validatorInfo.Tokens)).QuoTruncate(validatorInfo.DelegatorShares) - if token.LT(tokenIn20Usd) { + totalTokenBalance, _ := math.NewIntFromString("0") + for _, address := range addresses { + if math.LegacyNewDecFromInt(address.Balance.Amount).LT(tokenIn20Usd) { continue } - totalTokenDelegate = totalTokenDelegate.Add(token) + totalTokenBalance = totalTokenBalance.Add(address.Balance.Amount) } eveAirdrop := math.LegacyMustNewDecFromStr(EVE_AIRDROP) testAmount, _ := math.LegacyNewDecFromStr("0") - for _, delegator := range delegators { - validatorIndex := findValidatorInfo(validators, delegator.Delegation.ValidatorAddress) - validatorInfo := validators[validatorIndex] - token := (delegator.Delegation.Shares.MulInt(validatorInfo.Tokens)).QuoTruncate(validatorInfo.DelegatorShares) - if token.LT(tokenIn20Usd) { + for _, address := range addresses { + if math.LegacyNewDecFromInt(address.Balance.Amount).LT(tokenIn20Usd) { continue } - eveAirdrop := (eveAirdrop.MulInt64(int64(config.GetNeutronConfig().Percent))).QuoInt64(100).Mul(token).QuoTruncate(totalTokenDelegate) - eveBech32Address := convertBech32Address(delegator.Delegation.DelegatorAddress) + eveAirdrop := (eveAirdrop.MulInt64(int64(config.GetNeutronConfig().Percent))).QuoInt64(100).MulInt(address.Balance.Amount).QuoInt(totalTokenBalance) + eveBech32Address := convertBech32Address(address.Address) rewardInfo = append(rewardInfo, config.Reward{ - Address: delegator.Delegation.DelegatorAddress, + Address: address.Address, EveAddress: eveBech32Address, - Shares: delegator.Delegation.Shares, - Token: token, + Token: address.Balance.Amount.ToLegacyDec(), EveAirdropToken: eveAirdrop, ChainId: config.GetNeutronConfig().ChainID, }) @@ -110,6 +80,50 @@ func neutron() ([]banktypes.Balance, []config.Reward) { return balanceInfo, rewardInfo } +func fetchBalance(block_height string) ([]*banktypes.DenomOwner, uint64) { + grpcAddr := config.GetNeutronConfig().GRPCAddr + grpcConn, err := grpc.Dial(grpcAddr, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) + if err != nil { + panic(err) + } + defer grpcConn.Close() + bankClient := banktypes.NewQueryClient(grpcConn) + var header metadata.MD + var addresses *banktypes.QueryDenomOwnersResponse //QueryValidatorDelegationsResponse + var paginationKey []byte + addressInfo := []*banktypes.DenomOwner{} + step := 5000 + total := uint64(0) + // Fetch addresses, 5000 at a time + i := 0 + for { + i += 1 + fmt.Println("Fetching addresses", step*i, "to", step*(i+1)) + addresses, err = bankClient.DenomOwners( + metadata.AppendToOutgoingContext(context.Background(), grpctypes.GRPCBlockHeightHeader, block_height), // Add metadata to request + &banktypes.QueryDenomOwnersRequest{ + Denom: "untrn", + Pagination: &query.PageRequest{ + Limit: uint64(step), + Key: paginationKey, + CountTotal: true, + }, + }, + grpc.Header(&header), // Retrieve header from response + ) + fmt.Println("err: ", err) + if total != 0 { + total = addresses.Pagination.Total + } + addressInfo = append(addressInfo, addresses.DenomOwners...) + paginationKey = addresses.Pagination.NextKey + if len(paginationKey) == 0 { + break + } + } + return addressInfo, total +} + func fetchNeutronTokenPrice(apiUrl string) math.LegacyDec { // Make a GET request to the API response, err := http.Get(apiUrl) @@ -126,7 +140,7 @@ func fetchNeutronTokenPrice(apiUrl string) math.LegacyDec { panic("") } - var data config.CosmosPrice + var data config.NeutronPrice // Unmarshal the JSON byte slice into the defined struct err = json.Unmarshal(responseBody, &data) From d9e1f98aadf2c6d59603e5994dc4a8d98d6f0341 Mon Sep 17 00:00:00 2001 From: tungla Date: Mon, 25 Mar 2024 21:36:13 +0700 Subject: [PATCH 5/7] feat: add snapshot for nft on cosmos chain --- airdrop/akash.go | 2 +- airdrop/bostrom.go | 22 ++++--- airdrop/celestia.go | 51 +--------------- airdrop/composable.go | 2 +- airdrop/config/config.go | 35 +++++++---- airdrop/config/types.go | 31 ++++++++++ airdrop/cosmos.go | 36 ++++------- airdrop/cosmosnft.go | 116 ++++++++++++++++++++++++++++++++++++ airdrop/main.go | 64 +++++++++++++++++--- airdrop/neutron.go | 2 +- airdrop/sentinel.go | 2 +- airdrop/stargaze.go | 2 +- airdrop/terra.go | 37 ++++-------- airdrop/terrac.go | 125 +++++++++++++++++++++++++++++++++++++++ 14 files changed, 396 insertions(+), 131 deletions(-) create mode 100644 airdrop/cosmosnft.go create mode 100644 airdrop/terrac.go diff --git a/airdrop/akash.go b/airdrop/akash.go index b255b622..51979523 100644 --- a/airdrop/akash.go +++ b/airdrop/akash.go @@ -99,7 +99,7 @@ func akash() ([]banktypes.Balance, []config.Reward) { Coins: sdk.NewCoins(sdk.NewCoin("eve", eveAirdrop.TruncateInt())), }) } - fmt.Println(testAmount) + fmt.Println("Akash ", testAmount) // Write delegations to file // fileForDebug, _ := json.MarshalIndent(rewardInfo, "", " ") // _ = os.WriteFile("rewards.json", fileForDebug, 0644) diff --git a/airdrop/bostrom.go b/airdrop/bostrom.go index e1da2450..2f6961e0 100644 --- a/airdrop/bostrom.go +++ b/airdrop/bostrom.go @@ -7,6 +7,7 @@ import ( "io/ioutil" "net/http" "strconv" + "strings" "cosmossdk.io/math" "github.com/cosmos/cosmos-sdk/codec" @@ -30,7 +31,7 @@ func bostrom() ([]banktypes.Balance, []config.Reward) { delegators := []stakingtypes.DelegationResponse{} - rpc := config.GetBostromConfig().API + "/cosmos/staking/v1beta1/validators?pagination.limit" + strconv.Itoa(LIMIT_PER_PAGE) + "pagination.count_total=true" + rpc := config.GetBostromConfig().API + "/cosmos/staking/v1beta1/validators?pagination.limit=" + strconv.Itoa(LIMIT_PER_PAGE) + "&pagination.count_total=true" validatorsResponse := fetchValidators(rpc) validators := validatorsResponse.Validators fmt.Println("Validators: ", len(validators)) @@ -54,7 +55,7 @@ func bostrom() ([]banktypes.Balance, []config.Reward) { totalTokenDelegate := math.LegacyMustNewDecFromStr("0") for _, delegator := range delegators { - validatorIndex := findValidatorBostromInfo(validators, delegator.Delegation.ValidatorAddress) + validatorIndex := findValidatorInfoCustomType(validators, delegator.Delegation.ValidatorAddress) validatorInfo := validators[validatorIndex] token := (delegator.Delegation.Shares.MulInt(validatorInfo.Tokens)).QuoTruncate(validatorInfo.DelegatorShares) if token.LT(tokenIn20Usd) { @@ -65,7 +66,7 @@ func bostrom() ([]banktypes.Balance, []config.Reward) { eveAirdrop := math.LegacyMustNewDecFromStr(EVE_AIRDROP) testAmount, _ := math.LegacyNewDecFromStr("0") for _, delegator := range delegators { - validatorIndex := findValidatorBostromInfo(validators, delegator.Delegation.ValidatorAddress) + validatorIndex := findValidatorInfoCustomType(validators, delegator.Delegation.ValidatorAddress) validatorInfo := validators[validatorIndex] token := (delegator.Delegation.Shares.MulInt(validatorInfo.Tokens)).QuoTruncate(validatorInfo.DelegatorShares) if token.LT(tokenIn20Usd) { @@ -87,7 +88,7 @@ func bostrom() ([]banktypes.Balance, []config.Reward) { Coins: sdk.NewCoins(sdk.NewCoin("eve", eveAirdrop.TruncateInt())), }) } - fmt.Println(testAmount) + fmt.Println("bostrom ", testAmount) // Write delegations to file // fileForDebug, _ := json.MarshalIndent(rewardInfo, "", " ") // _ = os.WriteFile("rewards.json", fileForDebug, 0644) @@ -121,9 +122,13 @@ func fetchBostromTokenPrice(apiUrl string) math.LegacyDec { fmt.Println("Error unmarshalling JSON:", err) panic("") } - fmt.Println(data.Token.USD) - tokenInUsd := math.LegacyMustNewDecFromStr(data.Token.USD.String()) - fmt.Println(tokenInUsd) + rawPrice := strings.Split(data.Token.USD.String(), "e-") + base := rawPrice[0] + power := rawPrice[1] + powerInt, _ := strconv.ParseUint(power, 10, 64) + baseDec, _ := math.LegacyNewDecFromStr(base) + tenDec, _ := math.LegacyNewDecFromStr("10") + tokenInUsd := baseDec.Quo(tenDec.Power(powerInt)) return tokenInUsd } @@ -151,12 +156,11 @@ func fetchValidators(rpcUrl string) config.ValidatorResponse { fmt.Println("Error unmarshalling JSON:", err) panic("") } - fmt.Println(data.Pagination.Total) return data } -func findValidatorBostromInfo(validators []config.Validator, address string) int { +func findValidatorInfoCustomType(validators []config.Validator, address string) int { for key, v := range validators { if v.OperatorAddress == address { return key diff --git a/airdrop/celestia.go b/airdrop/celestia.go index 1b3c9840..9711f9bf 100644 --- a/airdrop/celestia.go +++ b/airdrop/celestia.go @@ -2,7 +2,6 @@ package main // error max size response import ( - "context" "encoding/json" "fmt" "io/ioutil" @@ -12,14 +11,11 @@ import ( "cosmossdk.io/math" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - grpctypes "github.com/cosmos/cosmos-sdk/types/grpc" - "github.com/cosmos/cosmos-sdk/types/query" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/eve-network/eve/airdrop/config" "github.com/joho/godotenv" "google.golang.org/grpc" - "google.golang.org/grpc/metadata" ) func celestia() ([]banktypes.Balance, []config.Reward) { @@ -38,7 +34,8 @@ func celestia() ([]banktypes.Balance, []config.Reward) { validators := getValidators(stakingClient, block_height) fmt.Println("Validators: ", len(validators)) for validatorIndex, validator := range validators { - delegations, total := fetchDelegationsv2(validator.OperatorAddress, block_height, validatorIndex) + url := config.GetCelestiaConfig().API + "/cosmos/staking/v1beta1/validators/" + validator.OperatorAddress + "/delegations?pagination.limit=" + strconv.Itoa(LIMIT_PER_PAGE) + "&pagination.count_total=true" + delegations, total := fetchDelegations(url) fmt.Println(validator.OperatorAddress) fmt.Println("Response ", len(delegations)) fmt.Println("Validator "+strconv.Itoa(validatorIndex)+" ", total) @@ -89,7 +86,7 @@ func celestia() ([]banktypes.Balance, []config.Reward) { Coins: sdk.NewCoins(sdk.NewCoin("eve", eveAirdrop.TruncateInt())), }) } - fmt.Println(testAmount) + fmt.Println("Celestia ", testAmount) // Write delegations to file // fileForDebug, _ := json.MarshalIndent(rewardInfo, "", " ") // _ = os.WriteFile("rewards.json", fileForDebug, 0644) @@ -99,48 +96,6 @@ func celestia() ([]banktypes.Balance, []config.Reward) { return balanceInfo, rewardInfo } -func fetchDelegationsv2(valAddr string, block_height string, validatorIndex int) ([]stakingtypes.DelegationResponse, uint64) { - grpcAddr := config.GetCelestiaConfig().GRPCAddr - grpcConn, err := grpc.Dial(grpcAddr, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) - if err != nil { - panic(err) - } - defer grpcConn.Close() - stakingClient := stakingtypes.NewQueryClient(grpcConn) - var header metadata.MD - var delegations *stakingtypes.QueryValidatorDelegationsResponse - var paginationKey []byte - delegationInfo := stakingtypes.DelegationResponses{} - step := 5000 - total := uint64(0) - // Fetch delegations, 5000 at a time - i := 0 - for { - i += 1 - fmt.Println("Fetching delegations", step*i, "to", step*(i+1)) - delegations, err = stakingClient.ValidatorDelegations( - metadata.AppendToOutgoingContext(context.Background(), grpctypes.GRPCBlockHeightHeader, block_height), // Add metadata to request - &stakingtypes.QueryValidatorDelegationsRequest{ - ValidatorAddr: valAddr, - Pagination: &query.PageRequest{ - Limit: uint64(step), - Key: paginationKey, - CountTotal: true, - }, - }, - grpc.Header(&header), // Retrieve header from response - ) - fmt.Println(err) - total = delegations.Pagination.Total - delegationInfo = append(delegationInfo, delegations.DelegationResponses...) - paginationKey = delegations.Pagination.NextKey - if len(paginationKey) == 0 { - break - } - } - return delegationInfo, total -} - func fetchCelestiaTokenPrice(apiUrl string) math.LegacyDec { // Make a GET request to the API response, err := http.Get(apiUrl) diff --git a/airdrop/composable.go b/airdrop/composable.go index 2ccc3c48..043d8827 100644 --- a/airdrop/composable.go +++ b/airdrop/composable.go @@ -99,7 +99,7 @@ func composable() ([]banktypes.Balance, []config.Reward) { Coins: sdk.NewCoins(sdk.NewCoin("eve", eveAirdrop.TruncateInt())), }) } - fmt.Println(testAmount) + fmt.Println("Composable ", testAmount) // Write delegations to file // fileForDebug, _ := json.MarshalIndent(rewardInfo, "", " ") // _ = os.WriteFile("rewards.json", fileForDebug, 0644) diff --git a/airdrop/config/config.go b/airdrop/config/config.go index 492e1e5c..d6c75d57 100644 --- a/airdrop/config/config.go +++ b/airdrop/config/config.go @@ -4,11 +4,12 @@ func GetCosmosHubConfig() *ChainClientConfig { return &ChainClientConfig{ Key: "default", ChainID: "cosmoshub-4", - GRPCAddr: "grpc-cosmoshub-ia.cosmosia.notional.ventures:443", + GRPCAddr: "cosmos-grpc.publicnode.com:443", AccountPrefix: "cosmos", CoinId: "cosmos", - Percent: 0, + Percent: 10, RPC: "https://cosmos-rpc.polkachu.com", + API: "https://cosmos-rest.publicnode.com", } } @@ -18,7 +19,7 @@ func GetComposableConfig() *ChainClientConfig { ChainID: "centauri-1", GRPCAddr: "composable-grpc.polkachu.com:22290", AccountPrefix: "centauri", - Percent: 13, + Percent: 10, CoinId: "picasso", RPC: "https://composable-rpc.polkachu.com", } @@ -30,7 +31,7 @@ func GetCelestiaConfig() *ChainClientConfig { ChainID: "celestia", GRPCAddr: "celestia-grpc.polkachu.com:11690", AccountPrefix: "celestia", - Percent: 13, + Percent: 10, CoinId: "celestia", RPC: "https://celestia-rpc.polkachu.com", API: "https://celestia-api.polkachu.com", @@ -43,7 +44,7 @@ func GetSentinelConfig() *ChainClientConfig { ChainID: "sentinelhub-2", GRPCAddr: "sentinel-grpc.polkachu.com:23990", AccountPrefix: "sent", - Percent: 13, + Percent: 10, CoinId: "sentinel", RPC: "https://sentinel-rpc.polkachu.com", } @@ -55,7 +56,7 @@ func GetAkashConfig() *ChainClientConfig { ChainID: "akashnet-2", GRPCAddr: "akash-grpc.polkachu.com:12890", AccountPrefix: "akash", - Percent: 13, + Percent: 10, CoinId: "akash-network", RPC: "https://akash-rpc.polkachu.com", } @@ -67,9 +68,10 @@ func GetStargazeConfig() *ChainClientConfig { ChainID: "stargaze-1", GRPCAddr: "stargaze-grpc.polkachu.com:13790", AccountPrefix: "stars", - Percent: 13, + Percent: 10, CoinId: "stargaze", RPC: "https://stargaze-rpc.polkachu.com", + API: "https://rest.stargaze-apis.com", } } @@ -79,7 +81,7 @@ func GetNeutronConfig() *ChainClientConfig { ChainID: "neutron-1", GRPCAddr: "neutron-grpc.polkachu.com:19190", AccountPrefix: "neutron", - Percent: 13, + Percent: 10, CoinId: "neutron-3", RPC: "https://neutron-rpc.polkachu.com", } @@ -91,9 +93,10 @@ func GetTerraConfig() *ChainClientConfig { ChainID: "phoenix-1", GRPCAddr: "terra-grpc.polkachu.com:11790", AccountPrefix: "terra", - Percent: 13, + Percent: 10, CoinId: "terra-luna-2", RPC: "https://terra-rpc.polkachu.com", + API: "https://terra-rest.publicnode.com", } } @@ -103,8 +106,20 @@ func GetBostromConfig() *ChainClientConfig { ChainID: "bostrom", GRPCAddr: "grpc.cyber.bronbro.io:443", AccountPrefix: "bostrom", - Percent: 13, + Percent: 10, CoinId: "bostrom", API: "https://lcd.bostrom.cybernode.ai", } } + +func GetTerracConfig() *ChainClientConfig { + return &ChainClientConfig{ + Key: "default", + ChainID: "columbus-5", + GRPCAddr: "terra-classic-grpc.publicnode.com:443", + AccountPrefix: "terra", + Percent: 10, + CoinId: "terra-luna", + API: "https://terra-classic-lcd.publicnode.com", + } +} diff --git a/airdrop/config/types.go b/airdrop/config/types.go index b274e7d0..6e2a8332 100644 --- a/airdrop/config/types.go +++ b/airdrop/config/types.go @@ -59,6 +59,10 @@ type TerraPrice struct { Token Price `json:"terra-luna-2"` } +type TerracPrice struct { + Token Price `json:"terra-luna"` +} + type BostromPrice struct { Token Price `json:"bostrom"` } @@ -121,3 +125,30 @@ type QueryValidatorDelegationsResponse struct { // pagination defines the pagination in the response. Pagination Pagination `protobuf:"bytes,2,opt,name=pagination,proto3" json:"pagination,omitempty"` } + +type Access struct { + Owner string `json:"owner"` + Approvals []string `json:"approvals"` +} +type Info struct { + TokenUri string `json:"token_uri"` +} +type Data struct { + Access Access `json:"access"` + Info Info `json:"info"` +} +type TokenInfoResponse struct { + Data Data `json:"data"` +} + +type TokenIds struct { + Token []string `json:"tokens"` +} +type TokenIdsResponse struct { + Data TokenIds `json:"data"` +} + +type NftHolder struct { + Address string + TokenId string +} diff --git a/airdrop/cosmos.go b/airdrop/cosmos.go index ef10944d..8a4b6705 100644 --- a/airdrop/cosmos.go +++ b/airdrop/cosmos.go @@ -1,7 +1,6 @@ package main import ( - "context" "encoding/json" "fmt" "io/ioutil" @@ -11,18 +10,14 @@ import ( "cosmossdk.io/math" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - grpctypes "github.com/cosmos/cosmos-sdk/types/grpc" - "github.com/cosmos/cosmos-sdk/types/query" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/eve-network/eve/airdrop/config" "github.com/joho/godotenv" "google.golang.org/grpc" - "google.golang.org/grpc/metadata" ) func cosmos() ([]banktypes.Balance, []config.Reward) { - block_height := getLatestHeight(config.GetCosmosHubConfig().RPC + "/status") godotenv.Load() grpcAddr := config.GetCosmosHubConfig().GRPCAddr grpcConn, err := grpc.Dial(grpcAddr, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) @@ -30,29 +25,20 @@ func cosmos() ([]banktypes.Balance, []config.Reward) { panic(err) } defer grpcConn.Close() - stakingClient := stakingtypes.NewQueryClient(grpcConn) delegators := []stakingtypes.DelegationResponse{} - validators := getValidators(stakingClient, block_height) + rpc := config.GetCosmosHubConfig().API + "/cosmos/staking/v1beta1/validators?pagination.limit=" + strconv.Itoa(LIMIT_PER_PAGE) + "&pagination.count_total=true" + validatorsResponse := fetchValidators(rpc) + validators := validatorsResponse.Validators fmt.Println("Validators: ", len(validators)) for validatorIndex, validator := range validators { - var header metadata.MD - delegationsResponse, _ := stakingClient.ValidatorDelegations( - metadata.AppendToOutgoingContext(context.Background(), grpctypes.GRPCBlockHeightHeader, block_height), // Add metadata to request - &stakingtypes.QueryValidatorDelegationsRequest{ - ValidatorAddr: validator.OperatorAddress, - Pagination: &query.PageRequest{ - CountTotal: true, - Limit: LIMIT_PER_PAGE, - }, - }, - grpc.Header(&header), // Retrieve header from response - ) - total := delegationsResponse.Pagination.Total - fmt.Println("Response ", len(delegationsResponse.DelegationResponses)) + url := config.GetCosmosHubConfig().API + "/cosmos/staking/v1beta1/validators/" + validator.OperatorAddress + "/delegations?pagination.limit=" + strconv.Itoa(LIMIT_PER_PAGE) + "&pagination.count_total=true" + delegations, total := fetchDelegations(url) + fmt.Println(validator.OperatorAddress) + fmt.Println("Response ", len(delegations)) fmt.Println("Validator "+strconv.Itoa(validatorIndex)+" ", total) - delegators = append(delegators, delegationsResponse.DelegationResponses...) + delegators = append(delegators, delegations...) } usd := math.LegacyMustNewDecFromStr("20") @@ -66,7 +52,7 @@ func cosmos() ([]banktypes.Balance, []config.Reward) { totalTokenDelegate := math.LegacyMustNewDecFromStr("0") for _, delegator := range delegators { - validatorIndex := findValidatorInfo(validators, delegator.Delegation.ValidatorAddress) + validatorIndex := findValidatorInfoCustomType(validators, delegator.Delegation.ValidatorAddress) validatorInfo := validators[validatorIndex] token := (delegator.Delegation.Shares.MulInt(validatorInfo.Tokens)).QuoTruncate(validatorInfo.DelegatorShares) if token.LT(tokenIn20Usd) { @@ -77,7 +63,7 @@ func cosmos() ([]banktypes.Balance, []config.Reward) { eveAirdrop := math.LegacyMustNewDecFromStr(EVE_AIRDROP) testAmount, _ := math.LegacyNewDecFromStr("0") for _, delegator := range delegators { - validatorIndex := findValidatorInfo(validators, delegator.Delegation.ValidatorAddress) + validatorIndex := findValidatorInfoCustomType(validators, delegator.Delegation.ValidatorAddress) validatorInfo := validators[validatorIndex] token := (delegator.Delegation.Shares.MulInt(validatorInfo.Tokens)).QuoTruncate(validatorInfo.DelegatorShares) if token.LT(tokenIn20Usd) { @@ -99,7 +85,7 @@ func cosmos() ([]banktypes.Balance, []config.Reward) { Coins: sdk.NewCoins(sdk.NewCoin("eve", eveAirdrop.TruncateInt())), }) } - fmt.Println(testAmount) + fmt.Println("Cosmos ", testAmount) // Write delegations to file // fileForDebug, _ := json.MarshalIndent(rewardInfo, "", " ") // _ = os.WriteFile("rewards.json", fileForDebug, 0644) diff --git a/airdrop/cosmosnft.go b/airdrop/cosmosnft.go new file mode 100644 index 00000000..f4c74325 --- /dev/null +++ b/airdrop/cosmosnft.go @@ -0,0 +1,116 @@ +package main + +import ( + "encoding/base64" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + + "cosmossdk.io/math" + sdk "github.com/cosmos/cosmos-sdk/types" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + "github.com/eve-network/eve/airdrop/config" +) + +func cosmosnft(contract string) ([]banktypes.Balance, []config.Reward) { + tokenIds := fetchTokenIds(contract) + allEveAirdrop := math.LegacyMustNewDecFromStr(EVE_AIRDROP) + rewardInfo := []config.Reward{} + balanceInfo := []banktypes.Balance{} + testAmount, _ := math.LegacyNewDecFromStr("0") + eveAirdrop := (allEveAirdrop.MulInt64(int64(config.GetAkashConfig().Percent))).QuoInt64(100).QuoInt(math.NewInt(int64(len(tokenIds)))) + fmt.Println("balance ", eveAirdrop) + for index, token := range tokenIds { + nftHolders := fetchTokenInfo(token, contract) + fmt.Println(index) + eveBech32Address := convertBech32Address(nftHolders.Address) + rewardInfo = append(rewardInfo, config.Reward{ + Address: nftHolders.Address, + EveAddress: eveBech32Address, + EveAirdropToken: eveAirdrop, + ChainId: config.GetAkashConfig().ChainID, + }) + testAmount = eveAirdrop.Add(testAmount) + balanceInfo = append(balanceInfo, banktypes.Balance{ + Address: eveBech32Address, + Coins: sdk.NewCoins(sdk.NewCoin("eve", eveAirdrop.TruncateInt())), + }) + } + fmt.Println(testAmount) + return balanceInfo, rewardInfo +} + +func fetchTokenInfo(token string, contract string) config.NftHolder { + queryString := fmt.Sprintf(`{"all_nft_info":{"token_id":%s}}`, token) + encodedQuery := base64.StdEncoding.EncodeToString([]byte(queryString)) + apiUrl := config.GetStargazeConfig().API + "/cosmwasm/wasm/v1/contract/" + contract + "/smart/" + encodedQuery + response, err := http.Get(apiUrl) + if err != nil { + fmt.Println("Error making GET request:", err) + panic("") + } + defer response.Body.Close() + + var data config.TokenInfoResponse + responseBody, err := ioutil.ReadAll(response.Body) + if err != nil { + fmt.Println("Error reading response body:", err) + panic("") + } + // Unmarshal the JSON byte slice into the defined struct + err = json.Unmarshal(responseBody, &data) + if err != nil { + fmt.Println("Error unmarshalling JSON:", err) + panic("") + } + return config.NftHolder{ + Address: data.Data.Access.Owner, + TokenId: token, + } +} + +func fetchTokenIds(contract string) []string { + // Make a GET request to the API + paginationKey := "0" + index := 0 + tokenIds := []string{} + for { + index += 1 + queryString := fmt.Sprintf(`{"all_tokens":{"limit":1000,"start_after":"%s"}}`, paginationKey) + encodedQuery := base64.StdEncoding.EncodeToString([]byte(queryString)) + apiUrl := config.GetStargazeConfig().API + "/cosmwasm/wasm/v1/contract/" + contract + "/smart/" + encodedQuery + response, err := http.Get(apiUrl) + if err != nil { + fmt.Println("Error making GET request:", err) + panic("") + } + defer response.Body.Close() + + var data config.TokenIdsResponse + responseBody, err := ioutil.ReadAll(response.Body) + if err != nil { + fmt.Println("Error reading response body:", err) + panic("") + } + // Unmarshal the JSON byte slice into the defined struct + err = json.Unmarshal(responseBody, &data) + if err != nil { + fmt.Println("Error unmarshalling JSON:", err) + panic("") + } + tokenIds = append(tokenIds, data.Data.Token...) + if len(data.Data.Token) == 0 { + break + } else { + paginationKey = data.Data.Token[len(data.Data.Token)-1] + fmt.Println("pagination key:", paginationKey) + if len(paginationKey) == 0 { + break + } + } + } + + fmt.Println(len(tokenIds)) + return tokenIds +} diff --git a/airdrop/main.go b/airdrop/main.go index 40b61ad1..1c94ee24 100644 --- a/airdrop/main.go +++ b/airdrop/main.go @@ -25,6 +25,8 @@ import ( const ( EVE_AIRDROP = "1000000000" // 1,000,000,000 LIMIT_PER_PAGE = 100000000 + BADKIDS = "stars19jq6mj84cnt9p7sagjxqf8hxtczwc8wlpuwe4sh62w45aheseues57n420" + CRYPTONIUM = "stars1g2ptrqnky5pu70r3g584zpk76cwqplyc63e8apwayau6l3jr8c0sp9q45u" ) func getValidators(stakingClient stakingtypes.QueryClient, block_height string) []stakingtypes.Validator { @@ -46,13 +48,59 @@ func getValidators(stakingClient stakingtypes.QueryClient, block_height string) } func main() { - apiUrl := "https://api.coingecko.com/api/v3/simple/price?ids=" + config.GetBostromConfig().CoinId + "&vs_currencies=usd" - fetchBostromTokenPrice(apiUrl) - return - balanceComposableInfo, rewardComposableInfo := bostrom() + balanceAkashInfo, _ := akash() + akashLength := len(balanceAkashInfo) + + balanceBostromInfo, _ := bostrom() + bostromLength := len(balanceBostromInfo) + balanceAkashInfo = append(balanceAkashInfo, balanceBostromInfo...) + + balanceCelestiaInfo, _ := celestia() + celestiaLength := len(balanceCelestiaInfo) + balanceAkashInfo = append(balanceAkashInfo, balanceCelestiaInfo...) + + balanceComposableInfo, _ := composable() + composableLength := len(balanceComposableInfo) + balanceAkashInfo = append(balanceAkashInfo, balanceComposableInfo...) + + balanceCosmosInfo, _ := cosmos() + cosmosLength := len(balanceCosmosInfo) + balanceAkashInfo = append(balanceAkashInfo, balanceCosmosInfo...) + + balanceNeutronInfo, _ := neutron() + neutronLength := len(balanceNeutronInfo) + balanceAkashInfo = append(balanceAkashInfo, balanceNeutronInfo...) + + balanceSentinelInfo, _ := sentinel() + sentinelLength := len(balanceSentinelInfo) + balanceAkashInfo = append(balanceAkashInfo, balanceSentinelInfo...) + + balanceStargazeInfo, _ := stargaze() + stargazeLength := len(balanceStargazeInfo) + balanceAkashInfo = append(balanceAkashInfo, balanceStargazeInfo...) + + balanceTerraInfo, _ := terra() + terraLength := len(balanceTerraInfo) + balanceAkashInfo = append(balanceAkashInfo, balanceTerraInfo...) + + balanceTerracInfo, _ := terrac() + terracLength := len(balanceTerracInfo) + balanceAkashInfo = append(balanceAkashInfo, balanceTerracInfo...) + + balanceBadKidsInfo, _ := cosmosnft(BADKIDS) + badkidsLength := len(balanceBadKidsInfo) + balanceAkashInfo = append(balanceAkashInfo, balanceBadKidsInfo...) + + balanceCryptoniumInfo, _ := cosmosnft(CRYPTONIUM) + cryptoniumLength := len(balanceCryptoniumInfo) + balanceAkashInfo = append(balanceAkashInfo, balanceCryptoniumInfo...) + + total := akashLength + bostromLength + celestiaLength + composableLength + cosmosLength + neutronLength + sentinelLength + stargazeLength + terraLength + terracLength + badkidsLength + cryptoniumLength + fmt.Println("total: ", total) + fmt.Println(len(balanceAkashInfo)) airdropMap := make(map[string]int) - for _, info := range balanceComposableInfo { + for _, info := range balanceAkashInfo { amount := airdropMap[info.Address] airdropMap[info.Address] = amount + int(info.Coins.AmountOf("eve").Int64()) } @@ -69,9 +117,9 @@ func main() { fmt.Println("Check balance: ", checkBalance) - // Write delegations to file - fileForDebug, _ := json.MarshalIndent(rewardComposableInfo, "", " ") - _ = os.WriteFile("rewards.json", fileForDebug, 0644) + // // Write delegations to file + // fileForDebug, _ := json.MarshalIndent(rewardComposableInfo, "", " ") + // _ = os.WriteFile("rewards.json", fileForDebug, 0644) fileBalance, _ := json.MarshalIndent(balanceInfo, "", " ") _ = os.WriteFile("balance.json", fileBalance, 0644) diff --git a/airdrop/neutron.go b/airdrop/neutron.go index 03cfe056..059b703b 100644 --- a/airdrop/neutron.go +++ b/airdrop/neutron.go @@ -70,7 +70,7 @@ func neutron() ([]banktypes.Balance, []config.Reward) { Coins: sdk.NewCoins(sdk.NewCoin("eve", eveAirdrop.TruncateInt())), }) } - fmt.Println(testAmount) + fmt.Println("Neutron ", testAmount) // Write delegations to file // fileForDebug, _ := json.MarshalIndent(rewardInfo, "", " ") // _ = os.WriteFile("rewards.json", fileForDebug, 0644) diff --git a/airdrop/sentinel.go b/airdrop/sentinel.go index ff144015..1250076a 100644 --- a/airdrop/sentinel.go +++ b/airdrop/sentinel.go @@ -99,7 +99,7 @@ func sentinel() ([]banktypes.Balance, []config.Reward) { Coins: sdk.NewCoins(sdk.NewCoin("eve", eveAirdrop.TruncateInt())), }) } - fmt.Println(testAmount) + fmt.Println("Sentinel ", testAmount) // Write delegations to file // fileForDebug, _ := json.MarshalIndent(rewardInfo, "", " ") // _ = os.WriteFile("rewards.json", fileForDebug, 0644) diff --git a/airdrop/stargaze.go b/airdrop/stargaze.go index c5eac5cc..1f463f69 100644 --- a/airdrop/stargaze.go +++ b/airdrop/stargaze.go @@ -99,7 +99,7 @@ func stargaze() ([]banktypes.Balance, []config.Reward) { Coins: sdk.NewCoins(sdk.NewCoin("eve", eveAirdrop.TruncateInt())), }) } - fmt.Println(testAmount) + fmt.Println("stargaze ", testAmount) // Write delegations to file // fileForDebug, _ := json.MarshalIndent(rewardInfo, "", " ") // _ = os.WriteFile("rewards.json", fileForDebug, 0644) diff --git a/airdrop/terra.go b/airdrop/terra.go index c371a4bf..f437efcf 100644 --- a/airdrop/terra.go +++ b/airdrop/terra.go @@ -1,7 +1,6 @@ package main import ( - "context" "encoding/json" "fmt" "io/ioutil" @@ -11,18 +10,14 @@ import ( "cosmossdk.io/math" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - grpctypes "github.com/cosmos/cosmos-sdk/types/grpc" - "github.com/cosmos/cosmos-sdk/types/query" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/eve-network/eve/airdrop/config" "github.com/joho/godotenv" "google.golang.org/grpc" - "google.golang.org/grpc/metadata" ) func terra() ([]banktypes.Balance, []config.Reward) { - block_height := getLatestHeight(config.GetTerraConfig().RPC + "/status") godotenv.Load() grpcAddr := config.GetTerraConfig().GRPCAddr grpcConn, err := grpc.Dial(grpcAddr, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) @@ -30,30 +25,20 @@ func terra() ([]banktypes.Balance, []config.Reward) { panic(err) } defer grpcConn.Close() - stakingClient := stakingtypes.NewQueryClient(grpcConn) delegators := []stakingtypes.DelegationResponse{} - validators := getValidators(stakingClient, block_height) + rpc := config.GetTerraConfig().API + "/cosmos/staking/v1beta1/validators?pagination.limit=" + strconv.Itoa(LIMIT_PER_PAGE) + "&pagination.count_total=true" + validatorsResponse := fetchValidators(rpc) + validators := validatorsResponse.Validators fmt.Println("Validators: ", len(validators)) for validatorIndex, validator := range validators { - var header metadata.MD - delegationsResponse, err := stakingClient.ValidatorDelegations( - metadata.AppendToOutgoingContext(context.Background(), grpctypes.GRPCBlockHeightHeader, block_height), // Add metadata to request - &stakingtypes.QueryValidatorDelegationsRequest{ - ValidatorAddr: validator.OperatorAddress, - Pagination: &query.PageRequest{ - CountTotal: true, - Limit: LIMIT_PER_PAGE, - }, - }, - grpc.Header(&header), // Retrieve header from response - ) - fmt.Println("err: ", err) - total := delegationsResponse.Pagination.Total - fmt.Println("Response ", len(delegationsResponse.DelegationResponses)) + url := config.GetTerraConfig().API + "/cosmos/staking/v1beta1/validators/" + validator.OperatorAddress + "/delegations?pagination.limit=" + strconv.Itoa(LIMIT_PER_PAGE) + "&pagination.count_total=true" + delegations, total := fetchDelegations(url) + fmt.Println(validator.OperatorAddress) + fmt.Println("Response ", len(delegations)) fmt.Println("Validator "+strconv.Itoa(validatorIndex)+" ", total) - delegators = append(delegators, delegationsResponse.DelegationResponses...) + delegators = append(delegators, delegations...) } usd := math.LegacyMustNewDecFromStr("20") @@ -67,7 +52,7 @@ func terra() ([]banktypes.Balance, []config.Reward) { totalTokenDelegate := math.LegacyMustNewDecFromStr("0") for _, delegator := range delegators { - validatorIndex := findValidatorInfo(validators, delegator.Delegation.ValidatorAddress) + validatorIndex := findValidatorInfoCustomType(validators, delegator.Delegation.ValidatorAddress) validatorInfo := validators[validatorIndex] token := (delegator.Delegation.Shares.MulInt(validatorInfo.Tokens)).QuoTruncate(validatorInfo.DelegatorShares) if token.LT(tokenIn20Usd) { @@ -78,7 +63,7 @@ func terra() ([]banktypes.Balance, []config.Reward) { eveAirdrop := math.LegacyMustNewDecFromStr(EVE_AIRDROP) testAmount, _ := math.LegacyNewDecFromStr("0") for _, delegator := range delegators { - validatorIndex := findValidatorInfo(validators, delegator.Delegation.ValidatorAddress) + validatorIndex := findValidatorInfoCustomType(validators, delegator.Delegation.ValidatorAddress) validatorInfo := validators[validatorIndex] token := (delegator.Delegation.Shares.MulInt(validatorInfo.Tokens)).QuoTruncate(validatorInfo.DelegatorShares) if token.LT(tokenIn20Usd) { @@ -100,7 +85,7 @@ func terra() ([]banktypes.Balance, []config.Reward) { Coins: sdk.NewCoins(sdk.NewCoin("eve", eveAirdrop.TruncateInt())), }) } - fmt.Println(testAmount) + fmt.Println("terra ", testAmount) // Write delegations to file // fileForDebug, _ := json.MarshalIndent(rewardInfo, "", " ") // _ = os.WriteFile("rewards.json", fileForDebug, 0644) diff --git a/airdrop/terrac.go b/airdrop/terrac.go new file mode 100644 index 00000000..05763a9d --- /dev/null +++ b/airdrop/terrac.go @@ -0,0 +1,125 @@ +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "strconv" + + "cosmossdk.io/math" + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + "github.com/eve-network/eve/airdrop/config" + "github.com/joho/godotenv" + "google.golang.org/grpc" +) + +func terrac() ([]banktypes.Balance, []config.Reward) { + godotenv.Load() + grpcAddr := config.GetTerracConfig().GRPCAddr + grpcConn, err := grpc.Dial(grpcAddr, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) + if err != nil { + panic(err) + } + defer grpcConn.Close() + + delegators := []stakingtypes.DelegationResponse{} + + rpc := config.GetTerracConfig().API + "/cosmos/staking/v1beta1/validators?pagination.limit=" + strconv.Itoa(LIMIT_PER_PAGE) + "&pagination.count_total=true" + validatorsResponse := fetchValidators(rpc) + validators := validatorsResponse.Validators + fmt.Println("Validators: ", len(validators)) + for validatorIndex, validator := range validators { + url := config.GetTerracConfig().API + "/cosmos/staking/v1beta1/validators/" + validator.OperatorAddress + "/delegations?pagination.limit=" + strconv.Itoa(LIMIT_PER_PAGE) + "&pagination.count_total=true" + delegations, total := fetchDelegations(url) + fmt.Println(validator.OperatorAddress) + fmt.Println("Response ", len(delegations)) + fmt.Println("Validator "+strconv.Itoa(validatorIndex)+" ", total) + delegators = append(delegators, delegations...) + } + + usd := math.LegacyMustNewDecFromStr("20") + + apiUrl := "https://api.coingecko.com/api/v3/simple/price?ids=" + config.GetTerracConfig().CoinId + "&vs_currencies=usd" + tokenInUsd := fetchTerracTokenPrice(apiUrl) + tokenIn20Usd := usd.QuoTruncate(tokenInUsd) + + rewardInfo := []config.Reward{} + balanceInfo := []banktypes.Balance{} + + totalTokenDelegate := math.LegacyMustNewDecFromStr("0") + for _, delegator := range delegators { + validatorIndex := findValidatorInfoCustomType(validators, delegator.Delegation.ValidatorAddress) + validatorInfo := validators[validatorIndex] + token := (delegator.Delegation.Shares.MulInt(validatorInfo.Tokens)).QuoTruncate(validatorInfo.DelegatorShares) + if token.LT(tokenIn20Usd) { + continue + } + totalTokenDelegate = totalTokenDelegate.Add(token) + } + eveAirdrop := math.LegacyMustNewDecFromStr(EVE_AIRDROP) + testAmount, _ := math.LegacyNewDecFromStr("0") + for _, delegator := range delegators { + validatorIndex := findValidatorInfoCustomType(validators, delegator.Delegation.ValidatorAddress) + validatorInfo := validators[validatorIndex] + token := (delegator.Delegation.Shares.MulInt(validatorInfo.Tokens)).QuoTruncate(validatorInfo.DelegatorShares) + if token.LT(tokenIn20Usd) { + continue + } + eveAirdrop := (eveAirdrop.MulInt64(int64(config.GetTerracConfig().Percent))).QuoInt64(100).Mul(token).QuoTruncate(totalTokenDelegate) + eveBech32Address := convertBech32Address(delegator.Delegation.DelegatorAddress) + rewardInfo = append(rewardInfo, config.Reward{ + Address: delegator.Delegation.DelegatorAddress, + EveAddress: eveBech32Address, + Shares: delegator.Delegation.Shares, + Token: token, + EveAirdropToken: eveAirdrop, + ChainId: config.GetTerracConfig().ChainID, + }) + testAmount = eveAirdrop.Add(testAmount) + balanceInfo = append(balanceInfo, banktypes.Balance{ + Address: eveBech32Address, + Coins: sdk.NewCoins(sdk.NewCoin("eve", eveAirdrop.TruncateInt())), + }) + } + fmt.Println("terrac", testAmount) + // Write delegations to file + // fileForDebug, _ := json.MarshalIndent(rewardInfo, "", " ") + // _ = os.WriteFile("rewards.json", fileForDebug, 0644) + + // fileBalance, _ := json.MarshalIndent(balanceInfo, "", " ") + // _ = os.WriteFile("balance.json", fileBalance, 0644) + return balanceInfo, rewardInfo +} + +func fetchTerracTokenPrice(apiUrl string) math.LegacyDec { + // Make a GET request to the API + response, err := http.Get(apiUrl) + if err != nil { + fmt.Println("Error making GET request:", err) + panic("") + } + defer response.Body.Close() + + // Read the response body + responseBody, err := ioutil.ReadAll(response.Body) + if err != nil { + fmt.Println("Error reading response body:", err) + panic("") + } + + var data config.TerracPrice + + // Unmarshal the JSON byte slice into the defined struct + err = json.Unmarshal(responseBody, &data) + if err != nil { + fmt.Println("Error unmarshalling JSON:", err) + panic("") + } + + tokenInUsd := math.LegacyMustNewDecFromStr(data.Token.USD.String()) + return tokenInUsd +} From f912f567954ebaf3c19198d6010a7b36a7868f7b Mon Sep 17 00:00:00 2001 From: tungla Date: Tue, 26 Mar 2024 17:30:04 +0700 Subject: [PATCH 6/7] feat: add snapshot for nft on ethereum --- .gitignore | 4 +- airdrop/bostrom.go | 80 ----------------------- airdrop/config/config.go | 23 ++++++- airdrop/config/types.go | 14 ++++ airdrop/cosmos.go | 11 ---- airdrop/cosmosnft.go | 6 +- airdrop/ethereumnft.go | 134 +++++++++++++++++++++++++++++++++++++++ airdrop/main.go | 80 ++++++++++++++++++++++- airdrop/neutron.go | 9 --- airdrop/terra.go | 11 ---- airdrop/terrac.go | 11 ---- go.mod | 9 ++- go.sum | 20 +++--- 13 files changed, 271 insertions(+), 141 deletions(-) create mode 100644 airdrop/ethereumnft.go diff --git a/.gitignore b/.gitignore index 522c10eb..68406f98 100644 --- a/.gitignore +++ b/.gitignore @@ -49,4 +49,6 @@ contract_tests/* #Airdrop tool balance.json -rewards.json \ No newline at end of file +rewards.json +balancenft.json +.env \ No newline at end of file diff --git a/airdrop/bostrom.go b/airdrop/bostrom.go index 2f6961e0..0b0df829 100644 --- a/airdrop/bostrom.go +++ b/airdrop/bostrom.go @@ -3,32 +3,19 @@ package main import ( "encoding/json" "fmt" - "io" "io/ioutil" "net/http" "strconv" "strings" "cosmossdk.io/math" - "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/eve-network/eve/airdrop/config" - "github.com/joho/godotenv" - "google.golang.org/grpc" ) func bostrom() ([]banktypes.Balance, []config.Reward) { - // block_height := getLatestHeight(config.GetBostromConfig().RPC + "/status") - godotenv.Load() - grpcAddr := config.GetBostromConfig().GRPCAddr - grpcConn, err := grpc.Dial(grpcAddr, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) - if err != nil { - panic(err) - } - defer grpcConn.Close() - delegators := []stakingtypes.DelegationResponse{} rpc := config.GetBostromConfig().API + "/cosmos/staking/v1beta1/validators?pagination.limit=" + strconv.Itoa(LIMIT_PER_PAGE) + "&pagination.count_total=true" @@ -131,70 +118,3 @@ func fetchBostromTokenPrice(apiUrl string) math.LegacyDec { tokenInUsd := baseDec.Quo(tenDec.Power(powerInt)) return tokenInUsd } - -func fetchValidators(rpcUrl string) config.ValidatorResponse { - // Make a GET request to the API - response, err := http.Get(rpcUrl) - if err != nil { - fmt.Println("Error making GET request:", err) - panic("") - } - defer response.Body.Close() - - // Read the response body - responseBody, err := io.ReadAll(response.Body) - if err != nil { - fmt.Println("Error reading response body:", err) - panic("") - } - - var data config.ValidatorResponse - - // Unmarshal the JSON byte slice into the defined struct - err = json.Unmarshal(responseBody, &data) - if err != nil { - fmt.Println("Error unmarshalling JSON:", err) - panic("") - } - fmt.Println(data.Pagination.Total) - return data -} - -func findValidatorInfoCustomType(validators []config.Validator, address string) int { - for key, v := range validators { - if v.OperatorAddress == address { - return key - } - } - return -1 -} - -func fetchDelegations(rpcUrl string) (stakingtypes.DelegationResponses, uint64) { - // Make a GET request to the API - response, err := http.Get(rpcUrl) - if err != nil { - fmt.Println("Error making GET request:", err) - panic("") - } - defer response.Body.Close() - - // Read the response body - responseBody, err := ioutil.ReadAll(response.Body) - if err != nil { - fmt.Println("Error reading response body:", err) - panic("") - } - - var data config.QueryValidatorDelegationsResponse - - // Unmarshal the JSON byte slice into the defined struct - err = json.Unmarshal(responseBody, &data) - if err != nil { - fmt.Println("Error unmarshalling JSON:", err) - panic("") - } - - fmt.Println(data.Pagination.Total) - total, _ := strconv.ParseUint(data.Pagination.Total, 10, 64) - return data.DelegationResponses, total -} diff --git a/airdrop/config/config.go b/airdrop/config/config.go index d6c75d57..217efec4 100644 --- a/airdrop/config/config.go +++ b/airdrop/config/config.go @@ -34,7 +34,7 @@ func GetCelestiaConfig() *ChainClientConfig { Percent: 10, CoinId: "celestia", RPC: "https://celestia-rpc.polkachu.com", - API: "https://celestia-api.polkachu.com", + API: "https://celestia-rest.publicnode.com", } } @@ -123,3 +123,24 @@ func GetTerracConfig() *ChainClientConfig { API: "https://terra-classic-lcd.publicnode.com", } } + +func GetBadKidsConfig() *ChainClientConfig { + return &ChainClientConfig{ + ChainID: "stargaze-1", + Percent: 10, + } +} + +func GetCryptoniumConfig() *ChainClientConfig { + return &ChainClientConfig{ + ChainID: "stargaze-1", + Percent: 10, + } +} + +func GetMiladyConfig() *ChainClientConfig { + return &ChainClientConfig{ + ChainID: "0x1", + Percent: 10, + } +} diff --git a/airdrop/config/types.go b/airdrop/config/types.go index 6e2a8332..20ae3956 100644 --- a/airdrop/config/types.go +++ b/airdrop/config/types.go @@ -152,3 +152,17 @@ type NftHolder struct { Address string TokenId string } + +type EthResult struct { + TokenId string `json:"token_id"` + TokenAddress string `json:"token_address"` + OwnerOf string `json:"owner_of"` +} + +type NftEthResponse struct { + Status string `json:"status"` + Page int `json:"page"` + PageSize int `json:"page_size"` + Cursor string `json:"cursor"` + Result []EthResult `json:"result"` +} diff --git a/airdrop/cosmos.go b/airdrop/cosmos.go index 8a4b6705..4b0af252 100644 --- a/airdrop/cosmos.go +++ b/airdrop/cosmos.go @@ -8,24 +8,13 @@ import ( "strconv" "cosmossdk.io/math" - "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/eve-network/eve/airdrop/config" - "github.com/joho/godotenv" - "google.golang.org/grpc" ) func cosmos() ([]banktypes.Balance, []config.Reward) { - godotenv.Load() - grpcAddr := config.GetCosmosHubConfig().GRPCAddr - grpcConn, err := grpc.Dial(grpcAddr, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) - if err != nil { - panic(err) - } - defer grpcConn.Close() - delegators := []stakingtypes.DelegationResponse{} rpc := config.GetCosmosHubConfig().API + "/cosmos/staking/v1beta1/validators?pagination.limit=" + strconv.Itoa(LIMIT_PER_PAGE) + "&pagination.count_total=true" diff --git a/airdrop/cosmosnft.go b/airdrop/cosmosnft.go index f4c74325..65f1e4c0 100644 --- a/airdrop/cosmosnft.go +++ b/airdrop/cosmosnft.go @@ -13,13 +13,13 @@ import ( "github.com/eve-network/eve/airdrop/config" ) -func cosmosnft(contract string) ([]banktypes.Balance, []config.Reward) { +func cosmosnft(contract string, percent int64) ([]banktypes.Balance, []config.Reward) { tokenIds := fetchTokenIds(contract) allEveAirdrop := math.LegacyMustNewDecFromStr(EVE_AIRDROP) rewardInfo := []config.Reward{} balanceInfo := []banktypes.Balance{} testAmount, _ := math.LegacyNewDecFromStr("0") - eveAirdrop := (allEveAirdrop.MulInt64(int64(config.GetAkashConfig().Percent))).QuoInt64(100).QuoInt(math.NewInt(int64(len(tokenIds)))) + eveAirdrop := (allEveAirdrop.MulInt64(percent)).QuoInt64(100).QuoInt(math.NewInt(int64(len(tokenIds)))) fmt.Println("balance ", eveAirdrop) for index, token := range tokenIds { nftHolders := fetchTokenInfo(token, contract) @@ -29,7 +29,7 @@ func cosmosnft(contract string) ([]banktypes.Balance, []config.Reward) { Address: nftHolders.Address, EveAddress: eveBech32Address, EveAirdropToken: eveAirdrop, - ChainId: config.GetAkashConfig().ChainID, + ChainId: config.GetBadKidsConfig().ChainID, }) testAmount = eveAirdrop.Add(testAmount) balanceInfo = append(balanceInfo, banktypes.Balance{ diff --git a/airdrop/ethereumnft.go b/airdrop/ethereumnft.go new file mode 100644 index 00000000..a50a0818 --- /dev/null +++ b/airdrop/ethereumnft.go @@ -0,0 +1,134 @@ +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "os" + + "cosmossdk.io/core/address" + "cosmossdk.io/math" + + addresscodec "github.com/cosmos/cosmos-sdk/codec/address" + sdk "github.com/cosmos/cosmos-sdk/types" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + "github.com/ethereum/go-ethereum/common" + "github.com/eve-network/eve/airdrop/config" + "github.com/joho/godotenv" +) + +const MILADY = "0x5af0d9827e0c53e4799bb226655a1de152a425a5" + +func ethereumnft() ([]banktypes.Balance, []config.Reward) { + nftOwners := fetchNftOwners() + allEveAirdrop := math.LegacyMustNewDecFromStr(EVE_AIRDROP) + rewardInfo := []config.Reward{} + balanceInfo := []banktypes.Balance{} + testAmount, _ := math.LegacyNewDecFromStr("0") + eveAirdrop := (allEveAirdrop.MulInt64(int64(config.GetMiladyConfig().Percent))).QuoInt64(100).QuoInt(math.NewInt(int64(len(nftOwners)))) + fmt.Println("balance ", eveAirdrop) + for index, owner := range nftOwners { + fmt.Println(index) + eveBech32Address := convertEvmAddress(owner.OwnerOf) + rewardInfo = append(rewardInfo, config.Reward{ + Address: owner.OwnerOf, + EveAddress: eveBech32Address, + EveAirdropToken: eveAirdrop, + ChainId: config.GetMiladyConfig().ChainID, + }) + testAmount = eveAirdrop.Add(testAmount) + balanceInfo = append(balanceInfo, banktypes.Balance{ + Address: eveBech32Address, + Coins: sdk.NewCoins(sdk.NewCoin("eve", eveAirdrop.TruncateInt())), + }) + } + fmt.Println(testAmount) + return balanceInfo, rewardInfo +} + +func constructMoralisUrl(cursor string) string { + return "https://deep-index.moralis.io/api/v2.2/nft/" + MILADY + "/owners?chain=eth&format=decimal&limit=100&cursor=" + cursor +} + +func fetchNftOwners() []config.EthResult { + godotenv.Load() + API_KEY := os.Getenv("API_KEY") + pageCount := 0 + cursor := "" + nftOwners := []config.EthResult{} + for { + pageCount += 1 + fmt.Println("Page ", pageCount) + url := constructMoralisUrl(cursor) + req, _ := http.NewRequest("GET", url, nil) + + req.Header.Add("Accept", "application/json") + req.Header.Add("X-API-Key", API_KEY) + + res, _ := http.DefaultClient.Do(req) + + body, _ := ioutil.ReadAll(res.Body) + var data config.NftEthResponse + + // Unmarshal the JSON byte slice into the defined struct + err := json.Unmarshal(body, &data) + if err != nil { + fmt.Println("Error unmarshalling JSON:", err) + panic("") + } + defer res.Body.Close() + + nftOwners = append(nftOwners, data.Result...) + if data.Cursor == "" { + break + } else { + cursor = data.Cursor + } + } + return nftOwners +} + +func convertEvmAddress(evmAddress string) string { + addr := common.HexToAddress(evmAddress) + accCodec := addresscodec.NewBech32Codec("eve") + eveAddress, err := StringFromEthAddress(accCodec, addr) + if err != nil { + fmt.Println("err ", err) + } + return eveAddress +} + +// EthAddressFromString converts a Cosmos SDK address string to an Ethereum `Address`. +func EthAddressFromString(codec address.Codec, addr string) (common.Address, error) { + bz, err := codec.StringToBytes(addr) + if err != nil { + return common.Address{}, err + } + return common.BytesToAddress(bz), nil +} + +// MustEthAddressFromString converts a Cosmos SDK address string to an Ethereum `Address`. It +// panics if the conversion fails. +func MustEthAddressFromString(codec address.Codec, addr string) common.Address { + address, err := EthAddressFromString(codec, addr) + if err != nil { + panic(err) + } + return address +} + +// StringFromEthAddress converts an Ethereum `Address` to a Cosmos SDK address string. +func StringFromEthAddress(codec address.Codec, ethAddress common.Address) (string, error) { + return codec.BytesToString(ethAddress.Bytes()) +} + +// MustStringFromEthAddress converts an Ethereum `Address` to a Cosmos SDK address string. It +// panics if the conversion fails. +func MustStringFromEthAddress(codec address.Codec, ethAddress common.Address) string { + addr, err := StringFromEthAddress(codec, ethAddress) + if err != nil { + panic(err) + } + return addr +} diff --git a/airdrop/main.go b/airdrop/main.go index 1c94ee24..5f577b21 100644 --- a/airdrop/main.go +++ b/airdrop/main.go @@ -4,9 +4,11 @@ import ( "context" "encoding/json" "fmt" + "io" "io/ioutil" "net/http" "os" + "strconv" "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" @@ -87,15 +89,20 @@ func main() { terracLength := len(balanceTerracInfo) balanceAkashInfo = append(balanceAkashInfo, balanceTerracInfo...) - balanceBadKidsInfo, _ := cosmosnft(BADKIDS) + balanceBadKidsInfo, _ := cosmosnft(BADKIDS, int64(config.GetBadKidsConfig().Percent)) badkidsLength := len(balanceBadKidsInfo) balanceAkashInfo = append(balanceAkashInfo, balanceBadKidsInfo...) - balanceCryptoniumInfo, _ := cosmosnft(CRYPTONIUM) + balanceCryptoniumInfo, _ := cosmosnft(CRYPTONIUM, int64(config.GetCryptoniumConfig().Percent)) cryptoniumLength := len(balanceCryptoniumInfo) balanceAkashInfo = append(balanceAkashInfo, balanceCryptoniumInfo...) - total := akashLength + bostromLength + celestiaLength + composableLength + cosmosLength + neutronLength + sentinelLength + stargazeLength + terraLength + terracLength + badkidsLength + cryptoniumLength + // need set coin type on Eve + balanceMiladyInfo, _ := ethereumnft() + miladyLength := len(balanceMiladyInfo) + balanceAkashInfo = append(balanceAkashInfo, balanceMiladyInfo...) + + total := akashLength + bostromLength + celestiaLength + composableLength + cosmosLength + neutronLength + sentinelLength + stargazeLength + terraLength + terracLength + badkidsLength + cryptoniumLength + miladyLength fmt.Println("total: ", total) fmt.Println(len(balanceAkashInfo)) @@ -168,3 +175,70 @@ func convertBech32Address(otherChainAddress string) string { newBech32DelAddr, _ := bech32.ConvertAndEncode("eve", bz) return newBech32DelAddr } + +func fetchValidators(rpcUrl string) config.ValidatorResponse { + // Make a GET request to the API + response, err := http.Get(rpcUrl) + if err != nil { + fmt.Println("Error making GET request:", err) + panic("") + } + defer response.Body.Close() + + // Read the response body + responseBody, err := io.ReadAll(response.Body) + if err != nil { + fmt.Println("Error reading response body:", err) + panic("") + } + + var data config.ValidatorResponse + + // Unmarshal the JSON byte slice into the defined struct + err = json.Unmarshal(responseBody, &data) + if err != nil { + fmt.Println("Error unmarshalling JSON:", err) + panic("") + } + fmt.Println(data.Pagination.Total) + return data +} + +func findValidatorInfoCustomType(validators []config.Validator, address string) int { + for key, v := range validators { + if v.OperatorAddress == address { + return key + } + } + return -1 +} + +func fetchDelegations(rpcUrl string) (stakingtypes.DelegationResponses, uint64) { + // Make a GET request to the API + response, err := http.Get(rpcUrl) + if err != nil { + fmt.Println("Error making GET request:", err) + panic("") + } + defer response.Body.Close() + + // Read the response body + responseBody, err := ioutil.ReadAll(response.Body) + if err != nil { + fmt.Println("Error reading response body:", err) + panic("") + } + + var data config.QueryValidatorDelegationsResponse + + // Unmarshal the JSON byte slice into the defined struct + err = json.Unmarshal(responseBody, &data) + if err != nil { + fmt.Println("Error unmarshalling JSON:", err) + panic("") + } + + fmt.Println(data.Pagination.Total) + total, _ := strconv.ParseUint(data.Pagination.Total, 10, 64) + return data.DelegationResponses, total +} diff --git a/airdrop/neutron.go b/airdrop/neutron.go index 059b703b..2907bb5c 100644 --- a/airdrop/neutron.go +++ b/airdrop/neutron.go @@ -15,21 +15,12 @@ import ( "github.com/cosmos/cosmos-sdk/types/query" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" "github.com/eve-network/eve/airdrop/config" - "github.com/joho/godotenv" "google.golang.org/grpc" "google.golang.org/grpc/metadata" ) func neutron() ([]banktypes.Balance, []config.Reward) { block_height := getLatestHeight(config.GetNeutronConfig().RPC + "/status") - godotenv.Load() - grpcAddr := config.GetNeutronConfig().GRPCAddr - grpcConn, err := grpc.Dial(grpcAddr, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) - if err != nil { - panic(err) - } - defer grpcConn.Close() - addresses, total := fetchBalance(block_height) fmt.Println("Response ", len(addresses)) fmt.Println("Total ", total) diff --git a/airdrop/terra.go b/airdrop/terra.go index f437efcf..aae2488d 100644 --- a/airdrop/terra.go +++ b/airdrop/terra.go @@ -8,24 +8,13 @@ import ( "strconv" "cosmossdk.io/math" - "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/eve-network/eve/airdrop/config" - "github.com/joho/godotenv" - "google.golang.org/grpc" ) func terra() ([]banktypes.Balance, []config.Reward) { - godotenv.Load() - grpcAddr := config.GetTerraConfig().GRPCAddr - grpcConn, err := grpc.Dial(grpcAddr, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) - if err != nil { - panic(err) - } - defer grpcConn.Close() - delegators := []stakingtypes.DelegationResponse{} rpc := config.GetTerraConfig().API + "/cosmos/staking/v1beta1/validators?pagination.limit=" + strconv.Itoa(LIMIT_PER_PAGE) + "&pagination.count_total=true" diff --git a/airdrop/terrac.go b/airdrop/terrac.go index 05763a9d..414d0faf 100644 --- a/airdrop/terrac.go +++ b/airdrop/terrac.go @@ -8,24 +8,13 @@ import ( "strconv" "cosmossdk.io/math" - "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/eve-network/eve/airdrop/config" - "github.com/joho/godotenv" - "google.golang.org/grpc" ) func terrac() ([]banktypes.Balance, []config.Reward) { - godotenv.Load() - grpcAddr := config.GetTerracConfig().GRPCAddr - grpcConn, err := grpc.Dial(grpcAddr, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) - if err != nil { - panic(err) - } - defer grpcConn.Close() - delegators := []stakingtypes.DelegationResponse{} rpc := config.GetTerracConfig().API + "/cosmos/staking/v1beta1/validators?pagination.limit=" + strconv.Itoa(LIMIT_PER_PAGE) + "&pagination.count_total=true" diff --git a/go.mod b/go.mod index a4e331ea..6939d982 100644 --- a/go.mod +++ b/go.mod @@ -51,6 +51,7 @@ require ( github.com/cosmos/ibc-go/modules/capability v1.0.0 github.com/cosmos/ibc-go/modules/light-clients/08-wasm v0.0.0-20240314094315-e89424c5bf2e github.com/cosmos/ibc-go/v8 v8.1.1 + github.com/ethereum/go-ethereum v1.13.14 github.com/joho/godotenv v1.5.1 github.com/osmosis-labs/tokenfactory v0.0.0-20240310155926-981fbeb0fe42 github.com/spf13/viper v1.18.2 @@ -74,7 +75,7 @@ require ( github.com/beorn7/perks v1.0.1 // indirect github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816 // indirect - github.com/bits-and-blooms/bitset v1.8.0 // indirect + github.com/bits-and-blooms/bitset v1.11.0 // indirect github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect github.com/cenkalti/backoff/v4 v4.2.1 // indirect github.com/cespare/xxhash v1.1.0 // indirect @@ -126,7 +127,7 @@ require ( github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect github.com/googleapis/gax-go/v2 v2.12.0 // indirect github.com/gorilla/handlers v1.5.2 // indirect - github.com/gorilla/websocket v1.5.0 // indirect + github.com/gorilla/websocket v1.5.1 // indirect github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect github.com/hashicorp/go-cleanhttp v0.5.2 // indirect @@ -141,6 +142,7 @@ require ( github.com/hashicorp/hcl v1.0.0 // indirect github.com/hashicorp/yamux v0.1.1 // indirect github.com/hdevalence/ed25519consensus v0.1.0 // indirect + github.com/holiman/uint256 v1.2.4 // indirect github.com/huandu/skiplist v1.2.0 // indirect github.com/iancoleman/strcase v0.3.0 // indirect github.com/improbable-eng/grpc-web v0.15.0 // indirect @@ -164,6 +166,7 @@ require ( github.com/mtibben/percent v0.2.1 // indirect github.com/oasisprotocol/curve25519-voi v0.0.0-20230904125328-1f23a7beb09a // indirect github.com/oklog/run v1.1.0 // indirect + github.com/onsi/gomega v1.30.0 // indirect github.com/opencontainers/go-digest v1.0.0 // indirect github.com/pelletier/go-toml/v2 v2.1.0 // indirect github.com/petermattis/goid v0.0.0-20230904192822-1876fd5063bc // indirect @@ -173,7 +176,7 @@ require ( github.com/prometheus/procfs v0.12.0 // indirect github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect github.com/rogpeppe/go-internal v1.12.0 // indirect - github.com/rs/cors v1.8.3 // indirect + github.com/rs/cors v1.10.1 // indirect github.com/rs/zerolog v1.32.0 // indirect github.com/sagikazarmark/locafero v0.4.0 // indirect github.com/sagikazarmark/slog-shim v0.1.0 // indirect diff --git a/go.sum b/go.sum index 13426f92..ebc7b6c1 100644 --- a/go.sum +++ b/go.sum @@ -278,8 +278,8 @@ github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d/go.mod h1:6QX/PXZ github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816 h1:41iFGWnSlI2gVpmOtVTJZNodLdLQLn/KsJqFvXwnd/s= github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= -github.com/bits-and-blooms/bitset v1.8.0 h1:FD+XqgOZDUxxZ8hzoBFuV9+cGWY9CslN6d5MS5JVb4c= -github.com/bits-and-blooms/bitset v1.8.0/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8= +github.com/bits-and-blooms/bitset v1.11.0 h1:RMyy2mBBShArUAhfVRZJ2xyBO58KCBCtZFShw3umo6k= +github.com/bits-and-blooms/bitset v1.11.0/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8= github.com/btcsuite/btcd/btcec/v2 v2.3.2 h1:5n0X6hX0Zk+6omWcihdYvdAlGf2DfasC0GMf7DClJ3U= github.com/btcsuite/btcd/btcec/v2 v2.3.2/go.mod h1:zYzJ8etWJQIv1Ogk7OzpWjowwOdXY1W/17j2MW85J04= github.com/btcsuite/btcd/btcutil v1.1.3 h1:xfbtw8lwpp0G6NwSHb+UE67ryTFHJAiNuipusjXSohQ= @@ -442,6 +442,8 @@ github.com/envoyproxy/go-control-plane v0.10.2-0.20220325020618-49ff273808a1/go. github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/envoyproxy/protoc-gen-validate v1.0.4 h1:gVPz/FMfvh57HdSJQyvBtF00j8JU4zdyUgIUNhlgg0A= github.com/envoyproxy/protoc-gen-validate v1.0.4/go.mod h1:qys6tmnRsYrQqIhm2bvKZH4Blx/1gTIZ2UKVY1M+Yew= +github.com/ethereum/go-ethereum v1.13.14 h1:EwiY3FZP94derMCIam1iW4HFVrSgIcpsu0HwTQtm6CQ= +github.com/ethereum/go-ethereum v1.13.14/go.mod h1:TN8ZiHrdJwSe8Cb6x+p0hs5CxhJZPbqB7hHkaUXcmIU= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs= @@ -631,8 +633,8 @@ github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= -github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= -github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY= +github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY= github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= github.com/grpc-ecosystem/go-grpc-middleware v1.2.2/go.mod h1:EaizFBKfUKtMIF5iaDEhniwNedqGo9FuLFzppDr3uwI= github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 h1:UH//fgunKIs4JdUbpDl1VZCDaL56wXCB/5+wF6uHfaI= @@ -691,6 +693,8 @@ github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE github.com/hashicorp/yamux v0.1.1/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ= github.com/hdevalence/ed25519consensus v0.1.0 h1:jtBwzzcHuTmFrQN6xQZn6CQEO/V9f7HsjsjeEZ6auqU= github.com/hdevalence/ed25519consensus v0.1.0/go.mod h1:w3BHWjwJbFU29IRHL1Iqkw3sus+7FctEyM4RqDxYNzo= +github.com/holiman/uint256 v1.2.4 h1:jUc4Nk8fm9jZabQuqr2JzednajVmBpC+oiTiXZJEApU= +github.com/holiman/uint256 v1.2.4/go.mod h1:EOMSn4q6Nyt9P6efbI3bueV4e1b3dGlUCXeiRV4ng7E= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/huandu/go-assert v1.1.5 h1:fjemmA7sSfYHJD7CUqs9qTwwfdNAx7/j2/ZlHXzNB3c= github.com/huandu/go-assert v1.1.5/go.mod h1:yOLvuqZwmcHIC5rIzrBhT7D3Q9c3GFnd0JrPVhn/06U= @@ -842,8 +846,8 @@ github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vv github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= -github.com/onsi/gomega v1.26.0 h1:03cDLK28U6hWvCAns6NeydX3zIm4SF3ci69ulidS32Q= -github.com/onsi/gomega v1.26.0/go.mod h1:r+zV744Re+DiYCIPRlYOTxn0YkOLcAnW8k1xXdMPGhM= +github.com/onsi/gomega v1.30.0 h1:hvMK7xYz4D3HapigLTeGdId/NcfQx1VHMJc60ew99+8= +github.com/onsi/gomega v1.30.0/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ= github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk= github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U= github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= @@ -936,8 +940,8 @@ github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/f github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= -github.com/rs/cors v1.8.3 h1:O+qNyWn7Z+F9M0ILBHgMVPuB1xTOucVd5gtaYyXBpRo= -github.com/rs/cors v1.8.3/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU= +github.com/rs/cors v1.10.1 h1:L0uuZVXIKlI1SShY2nhFfo44TYvDPQ1w4oFkUJNfhyo= +github.com/rs/cors v1.10.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU= github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0= github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= From 86ee66a931bb66acd88d76f4077de282a09875ed Mon Sep 17 00:00:00 2001 From: tungla Date: Wed, 27 Mar 2024 09:01:42 +0700 Subject: [PATCH 7/7] fix: lint --- airdrop/akash.go | 28 +++++++++++++++++----------- airdrop/bostrom.go | 14 ++++++++------ airdrop/celestia.go | 26 ++++++++++++++++---------- airdrop/composable.go | 28 +++++++++++++++++----------- airdrop/config/types.go | 27 +++++++++++++++------------ airdrop/cosmos.go | 14 ++++++++------ airdrop/cosmosnft.go | 16 +++++++++------- airdrop/ethereumnft.go | 17 +++++++++++------ airdrop/main.go | 22 ++++++++++++---------- airdrop/neutron.go | 20 +++++++++++--------- airdrop/sentinel.go | 28 +++++++++++++++++----------- airdrop/stargaze.go | 28 +++++++++++++++++----------- airdrop/terra.go | 14 ++++++++------ airdrop/terrac.go | 14 ++++++++------ app/app.go | 1 - 15 files changed, 174 insertions(+), 123 deletions(-) diff --git a/airdrop/akash.go b/airdrop/akash.go index 51979523..05b5ed77 100644 --- a/airdrop/akash.go +++ b/airdrop/akash.go @@ -4,28 +4,34 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "strconv" + "github.com/eve-network/eve/airdrop/config" + "github.com/joho/godotenv" + "google.golang.org/grpc" + "google.golang.org/grpc/metadata" + "cosmossdk.io/math" + "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" grpctypes "github.com/cosmos/cosmos-sdk/types/grpc" "github.com/cosmos/cosmos-sdk/types/query" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - "github.com/eve-network/eve/airdrop/config" - "github.com/joho/godotenv" - "google.golang.org/grpc" - "google.golang.org/grpc/metadata" ) func akash() ([]banktypes.Balance, []config.Reward) { block_height := getLatestHeight(config.GetAkashConfig().RPC + "/status") - godotenv.Load() + err := godotenv.Load() + if err != nil { + fmt.Println("Error loading env:", err) + panic("") + } grpcAddr := config.GetAkashConfig().GRPCAddr - grpcConn, err := grpc.Dial(grpcAddr, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) + grpcConn, err := grpc.Dial(grpcAddr, grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) if err != nil { panic(err) } @@ -51,13 +57,13 @@ func akash() ([]banktypes.Balance, []config.Reward) { ) total := delegationsResponse.Pagination.Total fmt.Println("Response ", len(delegationsResponse.DelegationResponses)) - fmt.Println("Validator "+strconv.Itoa(validatorIndex)+" ", total) + fmt.Println("Akash validator "+strconv.Itoa(validatorIndex)+" ", total) delegators = append(delegators, delegationsResponse.DelegationResponses...) } usd := math.LegacyMustNewDecFromStr("20") - apiUrl := "https://api.coingecko.com/api/v3/simple/price?ids=" + config.GetAkashConfig().CoinId + "&vs_currencies=usd" + apiUrl := API_COINGECKO + config.GetAkashConfig().CoinId + "&vs_currencies=usd" tokenInUsd := fetchAkashTokenPrice(apiUrl) tokenIn20Usd := usd.QuoTruncate(tokenInUsd) @@ -111,7 +117,7 @@ func akash() ([]banktypes.Balance, []config.Reward) { func fetchAkashTokenPrice(apiUrl string) math.LegacyDec { // Make a GET request to the API - response, err := http.Get(apiUrl) + response, err := http.Get(apiUrl) //nolint if err != nil { fmt.Println("Error making GET request:", err) panic("") @@ -119,7 +125,7 @@ func fetchAkashTokenPrice(apiUrl string) math.LegacyDec { defer response.Body.Close() // Read the response body - responseBody, err := ioutil.ReadAll(response.Body) + responseBody, err := io.ReadAll(response.Body) if err != nil { fmt.Println("Error reading response body:", err) panic("") diff --git a/airdrop/bostrom.go b/airdrop/bostrom.go index 0b0df829..985758ee 100644 --- a/airdrop/bostrom.go +++ b/airdrop/bostrom.go @@ -3,16 +3,18 @@ package main import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "strconv" "strings" + "github.com/eve-network/eve/airdrop/config" + "cosmossdk.io/math" + sdk "github.com/cosmos/cosmos-sdk/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - "github.com/eve-network/eve/airdrop/config" ) func bostrom() ([]banktypes.Balance, []config.Reward) { @@ -27,13 +29,13 @@ func bostrom() ([]banktypes.Balance, []config.Reward) { delegations, total := fetchDelegations(url) fmt.Println(validator.OperatorAddress) fmt.Println("Response ", len(delegations)) - fmt.Println("Validator "+strconv.Itoa(validatorIndex)+" ", total) + fmt.Println("Bostrom validator "+strconv.Itoa(validatorIndex)+" ", total) delegators = append(delegators, delegations...) } usd := math.LegacyMustNewDecFromStr("20") - apiUrl := "https://api.coingecko.com/api/v3/simple/price?ids=" + config.GetBostromConfig().CoinId + "&vs_currencies=usd" + apiUrl := API_COINGECKO + config.GetBostromConfig().CoinId + "&vs_currencies=usd" tokenInUsd := fetchBostromTokenPrice(apiUrl) tokenIn20Usd := usd.QuoTruncate(tokenInUsd) @@ -87,7 +89,7 @@ func bostrom() ([]banktypes.Balance, []config.Reward) { func fetchBostromTokenPrice(apiUrl string) math.LegacyDec { // Make a GET request to the API - response, err := http.Get(apiUrl) + response, err := http.Get(apiUrl) //nolint if err != nil { fmt.Println("Error making GET request:", err) panic("") @@ -95,7 +97,7 @@ func fetchBostromTokenPrice(apiUrl string) math.LegacyDec { defer response.Body.Close() // Read the response body - responseBody, err := ioutil.ReadAll(response.Body) + responseBody, err := io.ReadAll(response.Body) if err != nil { fmt.Println("Error reading response body:", err) panic("") diff --git a/airdrop/celestia.go b/airdrop/celestia.go index 9711f9bf..71f7faa6 100644 --- a/airdrop/celestia.go +++ b/airdrop/celestia.go @@ -4,25 +4,31 @@ package main import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "strconv" + "github.com/eve-network/eve/airdrop/config" + "github.com/joho/godotenv" + "google.golang.org/grpc" + "cosmossdk.io/math" + "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - "github.com/eve-network/eve/airdrop/config" - "github.com/joho/godotenv" - "google.golang.org/grpc" ) func celestia() ([]banktypes.Balance, []config.Reward) { block_height := getLatestHeight(config.GetCelestiaConfig().RPC + "/status") - godotenv.Load() + err := godotenv.Load() + if err != nil { + fmt.Println("Error loading env:", err) + panic("") + } grpcAddr := config.GetCelestiaConfig().GRPCAddr - grpcConn, err := grpc.Dial(grpcAddr, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) + grpcConn, err := grpc.Dial(grpcAddr, grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) if err != nil { panic(err) } @@ -38,13 +44,13 @@ func celestia() ([]banktypes.Balance, []config.Reward) { delegations, total := fetchDelegations(url) fmt.Println(validator.OperatorAddress) fmt.Println("Response ", len(delegations)) - fmt.Println("Validator "+strconv.Itoa(validatorIndex)+" ", total) + fmt.Println("Celestia validator "+strconv.Itoa(validatorIndex)+" ", total) delegators = append(delegators, delegations...) } usd := math.LegacyMustNewDecFromStr("20") - apiUrl := "https://api.coingecko.com/api/v3/simple/price?ids=" + config.GetCelestiaConfig().CoinId + "&vs_currencies=usd" + apiUrl := API_COINGECKO + config.GetCelestiaConfig().CoinId + "&vs_currencies=usd" tokenInUsd := fetchCelestiaTokenPrice(apiUrl) tokenIn20Usd := usd.QuoTruncate(tokenInUsd) @@ -98,7 +104,7 @@ func celestia() ([]banktypes.Balance, []config.Reward) { func fetchCelestiaTokenPrice(apiUrl string) math.LegacyDec { // Make a GET request to the API - response, err := http.Get(apiUrl) + response, err := http.Get(apiUrl) //nolint if err != nil { fmt.Println("Error making GET request:", err) panic("") @@ -106,7 +112,7 @@ func fetchCelestiaTokenPrice(apiUrl string) math.LegacyDec { defer response.Body.Close() // Read the response body - responseBody, err := ioutil.ReadAll(response.Body) + responseBody, err := io.ReadAll(response.Body) if err != nil { fmt.Println("Error reading response body:", err) panic("") diff --git a/airdrop/composable.go b/airdrop/composable.go index 043d8827..cb35e500 100644 --- a/airdrop/composable.go +++ b/airdrop/composable.go @@ -4,28 +4,34 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "strconv" + "github.com/eve-network/eve/airdrop/config" + "github.com/joho/godotenv" + "google.golang.org/grpc" + "google.golang.org/grpc/metadata" + "cosmossdk.io/math" + "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" grpctypes "github.com/cosmos/cosmos-sdk/types/grpc" "github.com/cosmos/cosmos-sdk/types/query" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - "github.com/eve-network/eve/airdrop/config" - "github.com/joho/godotenv" - "google.golang.org/grpc" - "google.golang.org/grpc/metadata" ) func composable() ([]banktypes.Balance, []config.Reward) { block_height := getLatestHeight(config.GetComposableConfig().RPC + "/status") - godotenv.Load() + err := godotenv.Load() + if err != nil { + fmt.Println("Error loading env:", err) + panic("") + } grpcAddr := config.GetComposableConfig().GRPCAddr - grpcConn, err := grpc.Dial(grpcAddr, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) + grpcConn, err := grpc.Dial(grpcAddr, grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) if err != nil { panic(err) } @@ -51,13 +57,13 @@ func composable() ([]banktypes.Balance, []config.Reward) { ) total := delegationsResponse.Pagination.Total fmt.Println("Response ", len(delegationsResponse.DelegationResponses)) - fmt.Println("Validator "+strconv.Itoa(validatorIndex)+" ", total) + fmt.Println("Composable validator "+strconv.Itoa(validatorIndex)+" ", total) delegators = append(delegators, delegationsResponse.DelegationResponses...) } usd := math.LegacyMustNewDecFromStr("20") - apiUrl := "https://api.coingecko.com/api/v3/simple/price?ids=" + config.GetComposableConfig().CoinId + "&vs_currencies=usd" + apiUrl := API_COINGECKO + config.GetComposableConfig().CoinId + "&vs_currencies=usd" tokenInUsd := fetchComposableTokenPrice(apiUrl) tokenIn20Usd := usd.QuoTruncate(tokenInUsd) @@ -111,7 +117,7 @@ func composable() ([]banktypes.Balance, []config.Reward) { func fetchComposableTokenPrice(apiUrl string) math.LegacyDec { // Make a GET request to the API - response, err := http.Get(apiUrl) + response, err := http.Get(apiUrl) //nolint if err != nil { fmt.Println("Error making GET request:", err) panic("") @@ -119,7 +125,7 @@ func fetchComposableTokenPrice(apiUrl string) math.LegacyDec { defer response.Body.Close() // Read the response body - responseBody, err := ioutil.ReadAll(response.Body) + responseBody, err := io.ReadAll(response.Body) if err != nil { fmt.Println("Error reading response body:", err) panic("") diff --git a/airdrop/config/types.go b/airdrop/config/types.go index 20ae3956..63dddbd6 100644 --- a/airdrop/config/types.go +++ b/airdrop/config/types.go @@ -4,6 +4,7 @@ import ( "encoding/json" "cosmossdk.io/math" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) @@ -83,18 +84,20 @@ type Result struct { ValidatorInfo ValidatorInfo `json:"validator_info"` } -type NodeInfo struct{} -type SyncInfo struct { - CatchingUp bool `json:"catching_up"` - EarlieastAppHash string `json:"earliest_app_hash"` - EarlieastBlockHash string `json:"earliest_block_hash"` - EarlieastBlockHeight string `json:"earliest_block_height"` - EarlieastBlockTime string `json:"earliest_block_time"` - LatestAppHash string `json:"latest_app_hash"` - LatestBlockHash string `json:"latest_block_hash"` - LatestBlockHeight string `json:"latest_block_height"` - LatestBlockTime string `json:"latest_block_time"` -} +type ( + NodeInfo struct{} + SyncInfo struct { + CatchingUp bool `json:"catching_up"` + EarlieastAppHash string `json:"earliest_app_hash"` + EarlieastBlockHash string `json:"earliest_block_hash"` + EarlieastBlockHeight string `json:"earliest_block_height"` + EarlieastBlockTime string `json:"earliest_block_time"` + LatestAppHash string `json:"latest_app_hash"` + LatestBlockHash string `json:"latest_block_hash"` + LatestBlockHeight string `json:"latest_block_height"` + LatestBlockTime string `json:"latest_block_time"` + } +) type ValidatorInfo struct{} type ValidatorResponse struct { diff --git a/airdrop/cosmos.go b/airdrop/cosmos.go index 4b0af252..28ff7ea2 100644 --- a/airdrop/cosmos.go +++ b/airdrop/cosmos.go @@ -3,15 +3,17 @@ package main import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "strconv" + "github.com/eve-network/eve/airdrop/config" + "cosmossdk.io/math" + sdk "github.com/cosmos/cosmos-sdk/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - "github.com/eve-network/eve/airdrop/config" ) func cosmos() ([]banktypes.Balance, []config.Reward) { @@ -26,13 +28,13 @@ func cosmos() ([]banktypes.Balance, []config.Reward) { delegations, total := fetchDelegations(url) fmt.Println(validator.OperatorAddress) fmt.Println("Response ", len(delegations)) - fmt.Println("Validator "+strconv.Itoa(validatorIndex)+" ", total) + fmt.Println("Cosmos validator "+strconv.Itoa(validatorIndex)+" ", total) delegators = append(delegators, delegations...) } usd := math.LegacyMustNewDecFromStr("20") - apiUrl := "https://api.coingecko.com/api/v3/simple/price?ids=" + config.GetCosmosHubConfig().CoinId + "&vs_currencies=usd" + apiUrl := API_COINGECKO + config.GetCosmosHubConfig().CoinId + "&vs_currencies=usd" tokenInUsd := fetchCosmosTokenPrice(apiUrl) tokenIn20Usd := usd.QuoTruncate(tokenInUsd) @@ -86,7 +88,7 @@ func cosmos() ([]banktypes.Balance, []config.Reward) { func fetchCosmosTokenPrice(apiUrl string) math.LegacyDec { // Make a GET request to the API - response, err := http.Get(apiUrl) + response, err := http.Get(apiUrl) //nolint if err != nil { fmt.Println("Error making GET request:", err) panic("") @@ -94,7 +96,7 @@ func fetchCosmosTokenPrice(apiUrl string) math.LegacyDec { defer response.Body.Close() // Read the response body - responseBody, err := ioutil.ReadAll(response.Body) + responseBody, err := io.ReadAll(response.Body) if err != nil { fmt.Println("Error reading response body:", err) panic("") diff --git a/airdrop/cosmosnft.go b/airdrop/cosmosnft.go index 65f1e4c0..9cb6a904 100644 --- a/airdrop/cosmosnft.go +++ b/airdrop/cosmosnft.go @@ -4,13 +4,15 @@ import ( "encoding/base64" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" + "github.com/eve-network/eve/airdrop/config" + "cosmossdk.io/math" + sdk "github.com/cosmos/cosmos-sdk/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" - "github.com/eve-network/eve/airdrop/config" ) func cosmosnft(contract string, percent int64) ([]banktypes.Balance, []config.Reward) { @@ -41,11 +43,11 @@ func cosmosnft(contract string, percent int64) ([]banktypes.Balance, []config.Re return balanceInfo, rewardInfo } -func fetchTokenInfo(token string, contract string) config.NftHolder { +func fetchTokenInfo(token, contract string) config.NftHolder { queryString := fmt.Sprintf(`{"all_nft_info":{"token_id":%s}}`, token) encodedQuery := base64.StdEncoding.EncodeToString([]byte(queryString)) apiUrl := config.GetStargazeConfig().API + "/cosmwasm/wasm/v1/contract/" + contract + "/smart/" + encodedQuery - response, err := http.Get(apiUrl) + response, err := http.Get(apiUrl) //nolint if err != nil { fmt.Println("Error making GET request:", err) panic("") @@ -53,7 +55,7 @@ func fetchTokenInfo(token string, contract string) config.NftHolder { defer response.Body.Close() var data config.TokenInfoResponse - responseBody, err := ioutil.ReadAll(response.Body) + responseBody, err := io.ReadAll(response.Body) if err != nil { fmt.Println("Error reading response body:", err) panic("") @@ -80,7 +82,7 @@ func fetchTokenIds(contract string) []string { queryString := fmt.Sprintf(`{"all_tokens":{"limit":1000,"start_after":"%s"}}`, paginationKey) encodedQuery := base64.StdEncoding.EncodeToString([]byte(queryString)) apiUrl := config.GetStargazeConfig().API + "/cosmwasm/wasm/v1/contract/" + contract + "/smart/" + encodedQuery - response, err := http.Get(apiUrl) + response, err := http.Get(apiUrl) //nolint if err != nil { fmt.Println("Error making GET request:", err) panic("") @@ -88,7 +90,7 @@ func fetchTokenIds(contract string) []string { defer response.Body.Close() var data config.TokenIdsResponse - responseBody, err := ioutil.ReadAll(response.Body) + responseBody, err := io.ReadAll(response.Body) if err != nil { fmt.Println("Error reading response body:", err) panic("") diff --git a/airdrop/ethereumnft.go b/airdrop/ethereumnft.go index a50a0818..2586973f 100644 --- a/airdrop/ethereumnft.go +++ b/airdrop/ethereumnft.go @@ -3,19 +3,20 @@ package main import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "os" + "github.com/ethereum/go-ethereum/common" + "github.com/eve-network/eve/airdrop/config" + "github.com/joho/godotenv" + "cosmossdk.io/core/address" "cosmossdk.io/math" addresscodec "github.com/cosmos/cosmos-sdk/codec/address" sdk "github.com/cosmos/cosmos-sdk/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" - "github.com/ethereum/go-ethereum/common" - "github.com/eve-network/eve/airdrop/config" - "github.com/joho/godotenv" ) const MILADY = "0x5af0d9827e0c53e4799bb226655a1de152a425a5" @@ -52,7 +53,11 @@ func constructMoralisUrl(cursor string) string { } func fetchNftOwners() []config.EthResult { - godotenv.Load() + err := godotenv.Load() + if err != nil { + fmt.Println("Error loading env:", err) + panic("") + } API_KEY := os.Getenv("API_KEY") pageCount := 0 cursor := "" @@ -68,7 +73,7 @@ func fetchNftOwners() []config.EthResult { res, _ := http.DefaultClient.Do(req) - body, _ := ioutil.ReadAll(res.Body) + body, _ := io.ReadAll(res.Body) var data config.NftEthResponse // Unmarshal the JSON byte slice into the defined struct diff --git a/airdrop/main.go b/airdrop/main.go index 5f577b21..305c899f 100644 --- a/airdrop/main.go +++ b/airdrop/main.go @@ -5,21 +5,22 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "net/http" "os" "strconv" + "github.com/eve-network/eve/airdrop/config" + "google.golang.org/grpc" + "google.golang.org/grpc/metadata" + "cosmossdk.io/math" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/bech32" grpctypes "github.com/cosmos/cosmos-sdk/types/grpc" "github.com/cosmos/cosmos-sdk/types/query" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - "github.com/eve-network/eve/airdrop/config" - "google.golang.org/grpc" - "google.golang.org/grpc/metadata" ) // got to export genesis state from neutron and bostrom chain @@ -29,6 +30,7 @@ const ( LIMIT_PER_PAGE = 100000000 BADKIDS = "stars19jq6mj84cnt9p7sagjxqf8hxtczwc8wlpuwe4sh62w45aheseues57n420" CRYPTONIUM = "stars1g2ptrqnky5pu70r3g584zpk76cwqplyc63e8apwayau6l3jr8c0sp9q45u" + API_COINGECKO = "https://api.coingecko.com/api/v3/simple/price?ids=" ) func getValidators(stakingClient stakingtypes.QueryClient, block_height string) []stakingtypes.Validator { @@ -129,7 +131,7 @@ func main() { // _ = os.WriteFile("rewards.json", fileForDebug, 0644) fileBalance, _ := json.MarshalIndent(balanceInfo, "", " ") - _ = os.WriteFile("balance.json", fileBalance, 0644) + _ = os.WriteFile("balance.json", fileBalance, 0o600) } func findValidatorInfo(validators []stakingtypes.Validator, address string) int { @@ -143,7 +145,7 @@ func findValidatorInfo(validators []stakingtypes.Validator, address string) int func getLatestHeight(apiUrl string) string { // Make a GET request to the API - response, err := http.Get(apiUrl) + response, err := http.Get(apiUrl) //nolint if err != nil { fmt.Println("Error making GET request:", err) panic("") @@ -151,7 +153,7 @@ func getLatestHeight(apiUrl string) string { defer response.Body.Close() // Read the response body - responseBody, err := ioutil.ReadAll(response.Body) + responseBody, err := io.ReadAll(response.Body) if err != nil { fmt.Println("Error reading response body:", err) panic("") @@ -178,7 +180,7 @@ func convertBech32Address(otherChainAddress string) string { func fetchValidators(rpcUrl string) config.ValidatorResponse { // Make a GET request to the API - response, err := http.Get(rpcUrl) + response, err := http.Get(rpcUrl) //nolint if err != nil { fmt.Println("Error making GET request:", err) panic("") @@ -215,7 +217,7 @@ func findValidatorInfoCustomType(validators []config.Validator, address string) func fetchDelegations(rpcUrl string) (stakingtypes.DelegationResponses, uint64) { // Make a GET request to the API - response, err := http.Get(rpcUrl) + response, err := http.Get(rpcUrl) //nolint if err != nil { fmt.Println("Error making GET request:", err) panic("") @@ -223,7 +225,7 @@ func fetchDelegations(rpcUrl string) (stakingtypes.DelegationResponses, uint64) defer response.Body.Close() // Read the response body - responseBody, err := ioutil.ReadAll(response.Body) + responseBody, err := io.ReadAll(response.Body) if err != nil { fmt.Println("Error reading response body:", err) panic("") diff --git a/airdrop/neutron.go b/airdrop/neutron.go index 2907bb5c..ed77c43a 100644 --- a/airdrop/neutron.go +++ b/airdrop/neutron.go @@ -5,18 +5,20 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" + "github.com/eve-network/eve/airdrop/config" + "google.golang.org/grpc" + "google.golang.org/grpc/metadata" + "cosmossdk.io/math" + "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" grpctypes "github.com/cosmos/cosmos-sdk/types/grpc" "github.com/cosmos/cosmos-sdk/types/query" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" - "github.com/eve-network/eve/airdrop/config" - "google.golang.org/grpc" - "google.golang.org/grpc/metadata" ) func neutron() ([]banktypes.Balance, []config.Reward) { @@ -27,7 +29,7 @@ func neutron() ([]banktypes.Balance, []config.Reward) { usd, _ := math.LegacyNewDecFromStr("20") - apiUrl := "https://api.coingecko.com/api/v3/simple/price?ids=" + config.GetNeutronConfig().CoinId + "&vs_currencies=usd" + apiUrl := API_COINGECKO + config.GetNeutronConfig().CoinId + "&vs_currencies=usd" tokenInUsd := fetchNeutronTokenPrice(apiUrl) tokenIn20Usd := usd.Quo(tokenInUsd) rewardInfo := []config.Reward{} @@ -73,14 +75,14 @@ func neutron() ([]banktypes.Balance, []config.Reward) { func fetchBalance(block_height string) ([]*banktypes.DenomOwner, uint64) { grpcAddr := config.GetNeutronConfig().GRPCAddr - grpcConn, err := grpc.Dial(grpcAddr, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) + grpcConn, err := grpc.Dial(grpcAddr, grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) if err != nil { panic(err) } defer grpcConn.Close() bankClient := banktypes.NewQueryClient(grpcConn) var header metadata.MD - var addresses *banktypes.QueryDenomOwnersResponse //QueryValidatorDelegationsResponse + var addresses *banktypes.QueryDenomOwnersResponse // QueryValidatorDelegationsResponse var paginationKey []byte addressInfo := []*banktypes.DenomOwner{} step := 5000 @@ -117,7 +119,7 @@ func fetchBalance(block_height string) ([]*banktypes.DenomOwner, uint64) { func fetchNeutronTokenPrice(apiUrl string) math.LegacyDec { // Make a GET request to the API - response, err := http.Get(apiUrl) + response, err := http.Get(apiUrl) //nolint if err != nil { fmt.Println("Error making GET request:", err) panic("") @@ -125,7 +127,7 @@ func fetchNeutronTokenPrice(apiUrl string) math.LegacyDec { defer response.Body.Close() // Read the response body - responseBody, err := ioutil.ReadAll(response.Body) + responseBody, err := io.ReadAll(response.Body) if err != nil { fmt.Println("Error reading response body:", err) panic("") diff --git a/airdrop/sentinel.go b/airdrop/sentinel.go index 1250076a..01ca0a63 100644 --- a/airdrop/sentinel.go +++ b/airdrop/sentinel.go @@ -4,28 +4,34 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "strconv" + "github.com/eve-network/eve/airdrop/config" + "github.com/joho/godotenv" + "google.golang.org/grpc" + "google.golang.org/grpc/metadata" + "cosmossdk.io/math" + "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" grpctypes "github.com/cosmos/cosmos-sdk/types/grpc" "github.com/cosmos/cosmos-sdk/types/query" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - "github.com/eve-network/eve/airdrop/config" - "github.com/joho/godotenv" - "google.golang.org/grpc" - "google.golang.org/grpc/metadata" ) func sentinel() ([]banktypes.Balance, []config.Reward) { block_height := getLatestHeight(config.GetSentinelConfig().RPC + "/status") - godotenv.Load() + err := godotenv.Load() + if err != nil { + fmt.Println("Error loading env:", err) + panic("") + } grpcAddr := config.GetSentinelConfig().GRPCAddr - grpcConn, err := grpc.Dial(grpcAddr, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) + grpcConn, err := grpc.Dial(grpcAddr, grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) if err != nil { panic(err) } @@ -51,13 +57,13 @@ func sentinel() ([]banktypes.Balance, []config.Reward) { ) total := delegationsResponse.Pagination.Total fmt.Println("Response ", len(delegationsResponse.DelegationResponses)) - fmt.Println("Validator "+strconv.Itoa(validatorIndex)+" ", total) + fmt.Println("Sentinel validator "+strconv.Itoa(validatorIndex)+" ", total) delegators = append(delegators, delegationsResponse.DelegationResponses...) } usd := math.LegacyMustNewDecFromStr("20") - apiUrl := "https://api.coingecko.com/api/v3/simple/price?ids=" + config.GetSentinelConfig().CoinId + "&vs_currencies=usd" + apiUrl := API_COINGECKO + config.GetSentinelConfig().CoinId + "&vs_currencies=usd" tokenInUsd := fetchSentinelTokenPrice(apiUrl) tokenIn20Usd := usd.QuoTruncate(tokenInUsd) @@ -111,7 +117,7 @@ func sentinel() ([]banktypes.Balance, []config.Reward) { func fetchSentinelTokenPrice(apiUrl string) math.LegacyDec { // Make a GET request to the API - response, err := http.Get(apiUrl) + response, err := http.Get(apiUrl) //nolint if err != nil { fmt.Println("Error making GET request:", err) panic("") @@ -119,7 +125,7 @@ func fetchSentinelTokenPrice(apiUrl string) math.LegacyDec { defer response.Body.Close() // Read the response body - responseBody, err := ioutil.ReadAll(response.Body) + responseBody, err := io.ReadAll(response.Body) if err != nil { fmt.Println("Error reading response body:", err) panic("") diff --git a/airdrop/stargaze.go b/airdrop/stargaze.go index 1f463f69..8cda6fd3 100644 --- a/airdrop/stargaze.go +++ b/airdrop/stargaze.go @@ -4,28 +4,34 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "strconv" + "github.com/eve-network/eve/airdrop/config" + "github.com/joho/godotenv" + "google.golang.org/grpc" + "google.golang.org/grpc/metadata" + "cosmossdk.io/math" + "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" grpctypes "github.com/cosmos/cosmos-sdk/types/grpc" "github.com/cosmos/cosmos-sdk/types/query" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - "github.com/eve-network/eve/airdrop/config" - "github.com/joho/godotenv" - "google.golang.org/grpc" - "google.golang.org/grpc/metadata" ) func stargaze() ([]banktypes.Balance, []config.Reward) { block_height := getLatestHeight(config.GetStargazeConfig().RPC + "/status") - godotenv.Load() + err := godotenv.Load() + if err != nil { + fmt.Println("Error loading env:", err) + panic("") + } grpcAddr := config.GetStargazeConfig().GRPCAddr - grpcConn, err := grpc.Dial(grpcAddr, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) + grpcConn, err := grpc.Dial(grpcAddr, grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec()))) if err != nil { panic(err) } @@ -51,13 +57,13 @@ func stargaze() ([]banktypes.Balance, []config.Reward) { ) total := delegationsResponse.Pagination.Total fmt.Println("Response ", len(delegationsResponse.DelegationResponses)) - fmt.Println("Validator "+strconv.Itoa(validatorIndex)+" ", total) + fmt.Println("Stargaze validator "+strconv.Itoa(validatorIndex)+" ", total) delegators = append(delegators, delegationsResponse.DelegationResponses...) } usd := math.LegacyMustNewDecFromStr("20") - apiUrl := "https://api.coingecko.com/api/v3/simple/price?ids=" + config.GetStargazeConfig().CoinId + "&vs_currencies=usd" + apiUrl := API_COINGECKO + config.GetStargazeConfig().CoinId + "&vs_currencies=usd" tokenInUsd := fetchStargazeTokenPrice(apiUrl) tokenIn20Usd := usd.QuoTruncate(tokenInUsd) @@ -111,7 +117,7 @@ func stargaze() ([]banktypes.Balance, []config.Reward) { func fetchStargazeTokenPrice(apiUrl string) math.LegacyDec { // Make a GET request to the API - response, err := http.Get(apiUrl) + response, err := http.Get(apiUrl) //nolint if err != nil { fmt.Println("Error making GET request:", err) panic("") @@ -119,7 +125,7 @@ func fetchStargazeTokenPrice(apiUrl string) math.LegacyDec { defer response.Body.Close() // Read the response body - responseBody, err := ioutil.ReadAll(response.Body) + responseBody, err := io.ReadAll(response.Body) if err != nil { fmt.Println("Error reading response body:", err) panic("") diff --git a/airdrop/terra.go b/airdrop/terra.go index aae2488d..964e3632 100644 --- a/airdrop/terra.go +++ b/airdrop/terra.go @@ -3,15 +3,17 @@ package main import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "strconv" + "github.com/eve-network/eve/airdrop/config" + "cosmossdk.io/math" + sdk "github.com/cosmos/cosmos-sdk/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - "github.com/eve-network/eve/airdrop/config" ) func terra() ([]banktypes.Balance, []config.Reward) { @@ -26,13 +28,13 @@ func terra() ([]banktypes.Balance, []config.Reward) { delegations, total := fetchDelegations(url) fmt.Println(validator.OperatorAddress) fmt.Println("Response ", len(delegations)) - fmt.Println("Validator "+strconv.Itoa(validatorIndex)+" ", total) + fmt.Println("Terra alidator "+strconv.Itoa(validatorIndex)+" ", total) delegators = append(delegators, delegations...) } usd := math.LegacyMustNewDecFromStr("20") - apiUrl := "https://api.coingecko.com/api/v3/simple/price?ids=" + config.GetTerraConfig().CoinId + "&vs_currencies=usd" + apiUrl := API_COINGECKO + config.GetTerraConfig().CoinId + "&vs_currencies=usd" tokenInUsd := fetchTerraTokenPrice(apiUrl) tokenIn20Usd := usd.QuoTruncate(tokenInUsd) @@ -86,7 +88,7 @@ func terra() ([]banktypes.Balance, []config.Reward) { func fetchTerraTokenPrice(apiUrl string) math.LegacyDec { // Make a GET request to the API - response, err := http.Get(apiUrl) + response, err := http.Get(apiUrl) //nolint if err != nil { fmt.Println("Error making GET request:", err) panic("") @@ -94,7 +96,7 @@ func fetchTerraTokenPrice(apiUrl string) math.LegacyDec { defer response.Body.Close() // Read the response body - responseBody, err := ioutil.ReadAll(response.Body) + responseBody, err := io.ReadAll(response.Body) if err != nil { fmt.Println("Error reading response body:", err) panic("") diff --git a/airdrop/terrac.go b/airdrop/terrac.go index 414d0faf..4b6d75c7 100644 --- a/airdrop/terrac.go +++ b/airdrop/terrac.go @@ -3,15 +3,17 @@ package main import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "strconv" + "github.com/eve-network/eve/airdrop/config" + "cosmossdk.io/math" + sdk "github.com/cosmos/cosmos-sdk/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - "github.com/eve-network/eve/airdrop/config" ) func terrac() ([]banktypes.Balance, []config.Reward) { @@ -26,13 +28,13 @@ func terrac() ([]banktypes.Balance, []config.Reward) { delegations, total := fetchDelegations(url) fmt.Println(validator.OperatorAddress) fmt.Println("Response ", len(delegations)) - fmt.Println("Validator "+strconv.Itoa(validatorIndex)+" ", total) + fmt.Println("Terrac validator "+strconv.Itoa(validatorIndex)+" ", total) delegators = append(delegators, delegations...) } usd := math.LegacyMustNewDecFromStr("20") - apiUrl := "https://api.coingecko.com/api/v3/simple/price?ids=" + config.GetTerracConfig().CoinId + "&vs_currencies=usd" + apiUrl := API_COINGECKO + config.GetTerracConfig().CoinId + "&vs_currencies=usd" tokenInUsd := fetchTerracTokenPrice(apiUrl) tokenIn20Usd := usd.QuoTruncate(tokenInUsd) @@ -86,7 +88,7 @@ func terrac() ([]banktypes.Balance, []config.Reward) { func fetchTerracTokenPrice(apiUrl string) math.LegacyDec { // Make a GET request to the API - response, err := http.Get(apiUrl) + response, err := http.Get(apiUrl) //nolint if err != nil { fmt.Println("Error making GET request:", err) panic("") @@ -94,7 +96,7 @@ func fetchTerracTokenPrice(apiUrl string) math.LegacyDec { defer response.Body.Close() // Read the response body - responseBody, err := ioutil.ReadAll(response.Body) + responseBody, err := io.ReadAll(response.Body) if err != nil { fmt.Println("Error reading response body:", err) panic("") diff --git a/app/app.go b/app/app.go index baaeeabd..3b785d67 100644 --- a/app/app.go +++ b/app/app.go @@ -149,7 +149,6 @@ import ( "github.com/CosmWasm/wasmd/x/wasm" wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper" wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types" - // add airdrop tool ) const appName = "EveApp"