From 9968d3a3ea60508cc60bca64fbede1fbfa0ceff0 Mon Sep 17 00:00:00 2001 From: trungnt1811 Date: Mon, 8 Jul 2024 13:48:28 +0700 Subject: [PATCH] refactor: add GetMinimumTokensThreshold func --- airdrop/chains/akash.go | 6 ++---- airdrop/chains/bostrom.go | 8 +++----- airdrop/chains/celestia.go | 9 ++++----- airdrop/chains/composable.go | 8 +++----- airdrop/chains/cosmos.go | 8 +++----- airdrop/chains/neutron.go | 9 ++++----- airdrop/chains/sentinel.go | 7 +++---- airdrop/chains/stargaze.go | 8 +++----- airdrop/chains/terra.go | 10 ++++------ airdrop/chains/terrac.go | 8 +++----- airdrop/config/constant.go | 2 ++ airdrop/go.mod | 2 +- airdrop/utils/utils.go | 10 ++++++++++ go.mod | 2 +- 14 files changed, 46 insertions(+), 51 deletions(-) diff --git a/airdrop/chains/akash.go b/airdrop/chains/akash.go index 28de0417..4588162c 100644 --- a/airdrop/chains/akash.go +++ b/airdrop/chains/akash.go @@ -62,13 +62,11 @@ func Akash() ([]banktypes.Balance, []config.Reward, int, error) { } // Calculate token price and threshold - usd := sdkmath.LegacyMustNewDecFromStr("20") - tokenInUsd, err := utils.FetchTokenPrice(config.GetAkashConfig().CoinID) + minimumTokensThreshold, err := utils.GetMinimumTokensThreshold(config.GetAkashConfig().CoinID) if err != nil { log.Printf("Failed to fetch Akash token price: %v", err) return nil, nil, 0, fmt.Errorf("failed to fetch Akash token price: %w", err) } - tokenIn20Usd := usd.Quo(tokenInUsd) // Prepare for airdrop calculation rewardInfo := []config.Reward{} @@ -95,7 +93,7 @@ func Akash() ([]banktypes.Balance, []config.Reward, int, error) { validatorInfo := validators[validatorIndex] token := (delegator.Delegation.Shares.MulInt(validatorInfo.Tokens)).QuoTruncate(validatorInfo.DelegatorShares) - if token.LT(tokenIn20Usd) { + if token.LT(minimumTokensThreshold) { continue } diff --git a/airdrop/chains/bostrom.go b/airdrop/chains/bostrom.go index 649bceab..1a1cd4cc 100644 --- a/airdrop/chains/bostrom.go +++ b/airdrop/chains/bostrom.go @@ -41,14 +41,12 @@ func Bostrom() ([]banktypes.Balance, []config.Reward, int, error) { delegators = append(delegators, delegations...) } - // Fetch token price in USD - usd := sdkmath.LegacyMustNewDecFromStr("20") - tokenInUsd, err := utils.FetchTokenPrice(config.GetBostromConfig().CoinID) + // Calculate token price and threshold + minimumTokensThreshold, err := utils.GetMinimumTokensThreshold(config.GetBostromConfig().CoinID) if err != nil { log.Printf("Failed to fetch Bostrom token price: %v", err) return nil, nil, 0, fmt.Errorf("failed to fetch Bostrom token price: %w", err) } - tokenIn20Usd := usd.Quo(tokenInUsd) rewardInfo := []config.Reward{} balanceInfo := []banktypes.Balance{} @@ -75,7 +73,7 @@ func Bostrom() ([]banktypes.Balance, []config.Reward, int, error) { validatorIndex := utils.FindValidatorInfoCustomType(validators, delegator.Delegation.ValidatorAddress) validatorInfo := validators[validatorIndex] token := (delegator.Delegation.Shares.MulInt(validatorInfo.Tokens)).QuoTruncate(validatorInfo.DelegatorShares) - if token.LT(tokenIn20Usd) { + if token.LT(minimumTokensThreshold) { continue } diff --git a/airdrop/chains/celestia.go b/airdrop/chains/celestia.go index 31e06135..e089bd89 100644 --- a/airdrop/chains/celestia.go +++ b/airdrop/chains/celestia.go @@ -62,13 +62,12 @@ func Celestia() ([]banktypes.Balance, []config.Reward, int, error) { delegators = append(delegators, delegations...) } - // Fetch token price in USD - tokenInUsd, err := utils.FetchTokenPrice(config.GetCelestiaConfig().CoinID) + // Calculate token price and threshold + minimumTokensThreshold, err := utils.GetMinimumTokensThreshold(config.GetCelestiaConfig().CoinID) if err != nil { log.Printf("Failed to fetch Celestial token price: %v", err) - return nil, nil, 0, fmt.Errorf("failed to fetch Celestia token price: %w", err) + return nil, nil, 0, fmt.Errorf("failed to fetch Celestial token price: %w", err) } - tokenIn20Usd := sdkmath.LegacyMustNewDecFromStr("20").Quo(tokenInUsd) // Process delegations and calculate rewards totalTokenDelegate := sdkmath.LegacyMustNewDecFromStr("0") @@ -93,7 +92,7 @@ func Celestia() ([]banktypes.Balance, []config.Reward, int, error) { validatorIndex := utils.FindValidatorInfo(validators, delegator.Delegation.ValidatorAddress) validatorInfo := validators[validatorIndex] token := delegator.Delegation.Shares.MulInt(validatorInfo.Tokens).QuoTruncate(validatorInfo.DelegatorShares) - if token.LT(tokenIn20Usd) { + if token.LT(minimumTokensThreshold) { continue } eveAirdropToken := eveAirdrop.MulInt64(int64(config.GetCelestiaConfig().Percent)).QuoInt64(100).Mul(token).QuoTruncate(totalTokenDelegate) diff --git a/airdrop/chains/composable.go b/airdrop/chains/composable.go index 72eff989..fa030b13 100644 --- a/airdrop/chains/composable.go +++ b/airdrop/chains/composable.go @@ -60,14 +60,12 @@ func Composable() ([]banktypes.Balance, []config.Reward, int, error) { delegators = append(delegators, delegationsResponse.DelegationResponses...) } - // Fetch token price in USD - usd := sdkmath.LegacyMustNewDecFromStr("20") - tokenInUsd, err := utils.FetchTokenPrice(config.GetComposableConfig().CoinID) + // Calculate token price and threshold + minimumTokensThreshold, err := utils.GetMinimumTokensThreshold(config.GetComposableConfig().CoinID) if err != nil { log.Printf("Failed to fetch Composable token price: %v", err) return nil, nil, 0, fmt.Errorf("failed to fetch Composable token price: %w", err) } - tokenIn20Usd := usd.Quo(tokenInUsd) // Process delegations and calculate rewards var ( @@ -94,7 +92,7 @@ func Composable() ([]banktypes.Balance, []config.Reward, int, error) { validatorIndex := utils.FindValidatorInfo(validators, delegator.Delegation.ValidatorAddress) validatorInfo := validators[validatorIndex] token := delegator.Delegation.Shares.MulInt(validatorInfo.Tokens).QuoTruncate(validatorInfo.DelegatorShares) - if token.LT(tokenIn20Usd) { + if token.LT(minimumTokensThreshold) { continue } eveAirdropToken := eveAirdrop.MulInt64(int64(config.GetComposableConfig().Percent)).QuoInt64(100).Mul(token).QuoTruncate(totalTokenDelegate) diff --git a/airdrop/chains/cosmos.go b/airdrop/chains/cosmos.go index 7d70cb17..b4108bb9 100644 --- a/airdrop/chains/cosmos.go +++ b/airdrop/chains/cosmos.go @@ -42,14 +42,12 @@ func Cosmos() ([]banktypes.Balance, []config.Reward, int, error) { delegators = append(delegators, delegations...) } - // Fetch token price in USD - usd := sdkmath.LegacyMustNewDecFromStr("20") - tokenInUsd, err := utils.FetchTokenPrice(config.GetCosmosHubConfig().CoinID) + // Calculate token price and threshold + minimumTokensThreshold, err := utils.GetMinimumTokensThreshold(config.GetCosmosHubConfig().CoinID) if err != nil { log.Printf("Failed to fetch Cosmos token price: %v", err) return nil, nil, 0, fmt.Errorf("failed to fetch Cosmos token price: %w", err) } - tokenIn20Usd := usd.Quo(tokenInUsd) var ( rewardInfo []config.Reward @@ -78,7 +76,7 @@ func Cosmos() ([]banktypes.Balance, []config.Reward, int, error) { validatorIndex := utils.FindValidatorInfoCustomType(validators, delegator.Delegation.ValidatorAddress) validatorInfo := validators[validatorIndex] token := delegator.Delegation.Shares.MulInt(validatorInfo.Tokens).QuoTruncate(validatorInfo.DelegatorShares) - if token.LT(tokenIn20Usd) { + if token.LT(minimumTokensThreshold) { continue } diff --git a/airdrop/chains/neutron.go b/airdrop/chains/neutron.go index d4aa684c..c40149ee 100644 --- a/airdrop/chains/neutron.go +++ b/airdrop/chains/neutron.go @@ -30,13 +30,12 @@ func Neutron() ([]banktypes.Balance, []config.Reward, int, error) { return nil, nil, 0, fmt.Errorf("failed to fetch balance for Neutron: %w", err) } - usd := sdkmath.LegacyMustNewDecFromStr("20") - tokenInUsd, err := utils.FetchTokenPrice(config.GetNeutronConfig().CoinID) + // Calculate token price and threshold + minimumTokensThreshold, err := utils.GetMinimumTokensThreshold(config.GetNeutronConfig().CoinID) if err != nil { - log.Printf("Failed to get Neutron token price: %v", err) + log.Printf("Failed to fetch Neutron token price: %v", err) return nil, nil, 0, fmt.Errorf("failed to fetch Neutron token price: %w", err) } - tokenIn20Usd := usd.Quo(tokenInUsd) rewardInfo := make([]config.Reward, 0, len(addresses)) balanceInfo := make([]banktypes.Balance, 0, len(addresses)) @@ -53,7 +52,7 @@ func Neutron() ([]banktypes.Balance, []config.Reward, int, error) { } for _, address := range addresses { - if sdkmath.LegacyNewDecFromInt(address.Balance.Amount).LT(tokenIn20Usd) { + if sdkmath.LegacyNewDecFromInt(address.Balance.Amount).LT(minimumTokensThreshold) { continue } diff --git a/airdrop/chains/sentinel.go b/airdrop/chains/sentinel.go index 57e2e96d..efe4ddc9 100644 --- a/airdrop/chains/sentinel.go +++ b/airdrop/chains/sentinel.go @@ -55,13 +55,12 @@ func Sentinel() ([]banktypes.Balance, []config.Reward, int, error) { delegators = append(delegators, delegationsResponse.DelegationResponses...) } - usd := sdkmath.LegacyMustNewDecFromStr("20") - tokenInUsd, err := utils.FetchTokenPrice(config.GetSentinelConfig().CoinID) + // Calculate token price and threshold + minimumTokensThreshold, err := utils.GetMinimumTokensThreshold(config.GetSentinelConfig().CoinID) if err != nil { log.Printf("Failed to fetch Sentinel token price: %v", err) return nil, nil, 0, fmt.Errorf("failed to fetch Sentinel token price: %w", err) } - tokenIn20Usd := usd.Quo(tokenInUsd) rewardInfo := []config.Reward{} balanceInfo := []banktypes.Balance{} @@ -85,7 +84,7 @@ func Sentinel() ([]banktypes.Balance, []config.Reward, int, error) { validatorIndex := utils.FindValidatorInfo(validators, delegator.Delegation.ValidatorAddress) validatorInfo := validators[validatorIndex] token := (delegator.Delegation.Shares.MulInt(validatorInfo.Tokens)).QuoTruncate(validatorInfo.DelegatorShares) - if token.LT(tokenIn20Usd) { + if token.LT(minimumTokensThreshold) { continue } diff --git a/airdrop/chains/stargaze.go b/airdrop/chains/stargaze.go index 0e5ebe7b..1a504519 100644 --- a/airdrop/chains/stargaze.go +++ b/airdrop/chains/stargaze.go @@ -52,15 +52,13 @@ func Stargaze() ([]banktypes.Balance, []config.Reward, int, error) { delegators = append(delegators, delegationsResponse.DelegationResponses...) } - tokenInUsd, err := utils.FetchTokenPrice(config.GetStargazeConfig().CoinID) + // Calculate token price and threshold + minimumTokensThreshold, err := utils.GetMinimumTokensThreshold(config.GetStargazeConfig().CoinID) if err != nil { log.Printf("Failed to fetch Stargaze token price: %v", err) return nil, nil, 0, fmt.Errorf("failed to fetch Stargaze token price: %w", err) } - usd := sdkmath.LegacyMustNewDecFromStr("20") - tokenIn20Usd := usd.Quo(tokenInUsd) - var rewardInfo []config.Reward var balanceInfo []banktypes.Balance totalTokenDelegate := sdkmath.LegacyMustNewDecFromStr("0") @@ -80,7 +78,7 @@ func Stargaze() ([]banktypes.Balance, []config.Reward, int, error) { for _, delegator := range delegators { validatorIndex := utils.FindValidatorInfo(validators, delegator.Delegation.ValidatorAddress) token := (delegator.Delegation.Shares.MulInt(validators[validatorIndex].Tokens)).QuoTruncate(validators[validatorIndex].DelegatorShares) - if token.LT(tokenIn20Usd) { + if token.LT(minimumTokensThreshold) { continue } diff --git a/airdrop/chains/terra.go b/airdrop/chains/terra.go index 7b6cdc27..b2627765 100644 --- a/airdrop/chains/terra.go +++ b/airdrop/chains/terra.go @@ -42,14 +42,12 @@ func Terra() ([]banktypes.Balance, []config.Reward, int, error) { delegators = append(delegators, delegations...) } - usd := sdkmath.LegacyMustNewDecFromStr("20") - - tokenInUsd, err := utils.FetchTokenPrice(config.GetTerraConfig().CoinID) + // Calculate token price and threshold + minimumTokensThreshold, err := utils.GetMinimumTokensThreshold(config.GetTerraConfig().CoinID) if err != nil { - log.Printf("Failed to fetch Terra token price: %v\n", err) + log.Printf("Failed to fetch Terra token price: %v", err) return nil, nil, 0, fmt.Errorf("failed to fetch Terra token price: %w", err) } - tokenIn20Usd := usd.Quo(tokenInUsd) var rewardInfo []config.Reward var balanceInfo []banktypes.Balance @@ -73,7 +71,7 @@ func Terra() ([]banktypes.Balance, []config.Reward, int, error) { validatorIndex := utils.FindValidatorInfoCustomType(validators, delegator.Delegation.ValidatorAddress) validatorInfo := validators[validatorIndex] token := (delegator.Delegation.Shares.MulInt(validatorInfo.Tokens)).QuoTruncate(validatorInfo.DelegatorShares) - if token.LT(tokenIn20Usd) { + if token.LT(minimumTokensThreshold) { continue } diff --git a/airdrop/chains/terrac.go b/airdrop/chains/terrac.go index a66ee4e3..208387ae 100644 --- a/airdrop/chains/terrac.go +++ b/airdrop/chains/terrac.go @@ -42,14 +42,12 @@ func Terrac() ([]banktypes.Balance, []config.Reward, int, error) { delegators = append(delegators, delegations...) } - usd := sdkmath.LegacyMustNewDecFromStr("20") - tokenInUsd, err := utils.FetchTokenPrice(config.GetTerracConfig().CoinID) + // Calculate token price and threshold + minimumTokensThreshold, err := utils.GetMinimumTokensThreshold(config.GetTerracConfig().CoinID) if err != nil { log.Printf("Failed to fetch TerraC token price: %v", err) return nil, nil, 0, fmt.Errorf("failed to fetch TerraC token price: %w", err) } - tokenIn20Usd := usd.Quo(tokenInUsd) - var rewardInfo []config.Reward var balanceInfo []banktypes.Balance @@ -72,7 +70,7 @@ func Terrac() ([]banktypes.Balance, []config.Reward, int, error) { validatorIndex := utils.FindValidatorInfoCustomType(validators, delegator.Delegation.ValidatorAddress) validatorInfo := validators[validatorIndex] token := (delegator.Delegation.Shares.MulInt(validatorInfo.Tokens)).QuoTruncate(validatorInfo.DelegatorShares) - if token.LT(tokenIn20Usd) { + if token.LT(minimumTokensThreshold) { continue } diff --git a/airdrop/config/constant.go b/airdrop/config/constant.go index d6cae3f0..6ba091d6 100644 --- a/airdrop/config/constant.go +++ b/airdrop/config/constant.go @@ -7,4 +7,6 @@ const ( CryptoniumContractAddress = "stars1g2ptrqnky5pu70r3g584zpk76cwqplyc63e8apwayau6l3jr8c0sp9q45u" APICoingecko = "https://api.coingecko.com/api/v3/simple/price?ids=" MaxRetries = 5 + MinimumStakingTokensWorth = "20" // Minimum threshold for staking tokens worth (in USD) to filter out accounts with low stake + // aiming to exclude governance manipulation accounts ) diff --git a/airdrop/go.mod b/airdrop/go.mod index 6acda808..5f879fd5 100644 --- a/airdrop/go.mod +++ b/airdrop/go.mod @@ -159,4 +159,4 @@ require ( nhooyr.io/websocket v1.8.10 // indirect pgregory.net/rapid v1.1.0 // indirect sigs.k8s.io/yaml v1.4.0 // indirect -) \ No newline at end of file +) diff --git a/airdrop/utils/utils.go b/airdrop/utils/utils.go index 4061d56f..8e25fcac 100644 --- a/airdrop/utils/utils.go +++ b/airdrop/utils/utils.go @@ -419,3 +419,13 @@ func FetchTokenIds(contractAddress, apiFromConfig string) ([]string, error) { func StringFromEthAddress(codec address.Codec, ethAddress common.Address) (string, error) { return codec.BytesToString(ethAddress.Bytes()) } + +func GetMinimumTokensThreshold(coinID string) (sdkmath.LegacyDec, error) { + minimumStakingTokensWorth := sdkmath.LegacyMustNewDecFromStr(config.MinimumStakingTokensWorth) + tokenInUsd, err := FetchTokenPrice(coinID) + if err != nil { + return sdkmath.LegacyDec{}, fmt.Errorf("failed to fetch token price for coin ID %s: %w", coinID, err) + } + minimumTokensThreshold := minimumStakingTokensWorth.Quo(tokenInUsd) + return minimumTokensThreshold, nil +} diff --git a/go.mod b/go.mod index 29a51245..e3389c4c 100644 --- a/go.mod +++ b/go.mod @@ -245,4 +245,4 @@ replace ( // pin version! 126854af5e6d has issues with the store so that queries fail github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 -) \ No newline at end of file +)