Skip to content

Commit

Permalink
refactor: add GetMinimumTokensThreshold func
Browse files Browse the repository at this point in the history
  • Loading branch information
trungnt1811 committed Jul 8, 2024
1 parent e072fbd commit 9968d3a
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 51 deletions.
6 changes: 2 additions & 4 deletions airdrop/chains/akash.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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
}

Expand Down
8 changes: 3 additions & 5 deletions airdrop/chains/bostrom.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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
}

Expand Down
9 changes: 4 additions & 5 deletions airdrop/chains/celestia.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Expand Down
8 changes: 3 additions & 5 deletions airdrop/chains/composable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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)
Expand Down
8 changes: 3 additions & 5 deletions airdrop/chains/cosmos.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
9 changes: 4 additions & 5 deletions airdrop/chains/neutron.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
}

Expand Down
7 changes: 3 additions & 4 deletions airdrop/chains/sentinel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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
}

Expand Down
8 changes: 3 additions & 5 deletions airdrop/chains/stargaze.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
}

Expand Down
10 changes: 4 additions & 6 deletions airdrop/chains/terra.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down
8 changes: 3 additions & 5 deletions airdrop/chains/terrac.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
}

Expand Down
2 changes: 2 additions & 0 deletions airdrop/config/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 1 addition & 1 deletion airdrop/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
)
10 changes: 10 additions & 0 deletions airdrop/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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

)
)

0 comments on commit 9968d3a

Please sign in to comment.