Skip to content

Commit

Permalink
Update Staking simple flow node sorting and selection logic (#2154)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruijialin-avalabs committed Dec 13, 2024
1 parent 66e88e5 commit e4f5996
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
2 changes: 2 additions & 0 deletions packages/core-mobile/app/hooks/earn/useSearchNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ export const useSearchNode = ({
stakingAmount,
stakingEndTime,
minUpTime: 98,
maxFee: 4,
isEndTimeOverOneYear
})

if (filteredValidators.length === 0) {
Logger.info(noMatchError.message)
return { validator: undefined, error: noMatchError }
Expand Down
42 changes: 42 additions & 0 deletions packages/core-mobile/app/services/earn/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,48 @@ describe('getRandomValidator function', () => {
const result = getRandomValidator([])
expect(result).toBe(undefined)
})

it('should return first item by end time if staking end time is over one year', () => {
const result = getRandomValidator(
mockValidators.validators as unknown as NodeValidators,
true
)
expect(result).toBe(mockValidators.validators[0])
})

it('should return first item if only one validator matches the lowest delegation fee', () => {
const validators = mockValidators.validators.map((validator, index) => {
if (index === 0) {
return { ...validator, delegationFee: '1.0000' }
}
return validator
})
const result = getRandomValidator(validators as unknown as NodeValidators)
expect(result).toBe(validators[0])
})
it('should return random validator from ones that matches the lowest delegation fee', () => {
const validators = mockValidators.validators.map((validator, index) => {
if (index <= 1) {
return { ...validator, delegationFee: '1.0000' }
}
return validator
})
const result = getRandomValidator(validators as unknown as NodeValidators)
const nodeIds = validators.map(validator => validator.nodeID)
expect(nodeIds.includes(result.nodeID)).toBeTruthy()
})

it('should return random validator from top five of ones that matches the lowest delegation fee', () => {
const validators = mockValidators.validators.map((validator, index) => {
if (index <= 10) {
return { ...validator, delegationFee: '1.0000' }
}
return validator
})
const result = getRandomValidator(validators as unknown as NodeValidators)
const topFive = validators.slice(0, 5).map(validator => validator.nodeID)
expect(topFive.includes(result.nodeID)).toBeTruthy()
})
})

describe('getAdvancedSortedValidators function', () => {
Expand Down
20 changes: 17 additions & 3 deletions packages/core-mobile/app/services/earn/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ export const getSimpleSortedValidators = (
}
return [...validators].sort(
(a, b): number =>
Number(b.uptime) - Number(a.uptime) ||
Number(a.delegationFee) - Number(b.delegationFee) ||
Number(b.uptime) - Number(a.uptime) ||
(peers === undefined
? 0
: comparePeerVersion(
Expand All @@ -250,7 +250,7 @@ export const getSimpleSortedValidators = (

/**
*
* @param validators input to take random item from,
* @param validators input sorted by delegationFee, uptime and node version to take random item from,
* @param isEndTimeOverOneYear boolean indicating if the stake end time is over one year
* @returns random item from either top 5 items in the array or all of the items in the array
*/
Expand All @@ -262,9 +262,23 @@ export const getRandomValidator = (
// get the first item in the array sorted by end time
return validators.at(0) as NodeValidator
}
const endIndex = validators.length >= 5 ? 4 : validators.length - 1

// get the validators with lowest delegation fee
const validatorsWithLowestFee = validators.filter(
validator => validator.delegationFee === validators[0]?.delegationFee
)

// if there is only one validator with the lowest fee, return it
if (validatorsWithLowestFee.length === 1) {
return validatorsWithLowestFee[0] as NodeValidator
}

// if there are more than 2 validators with the lowest fee, return a random one between the 2-5 validators
const endIndex =
validatorsWithLowestFee.length >= 5 ? 4 : validators.length - 1
const randomIndex = random(0, endIndex)
const matchedValidator = validators.at(randomIndex)

return matchedValidator as NodeValidator
}

Expand Down

0 comments on commit e4f5996

Please sign in to comment.