mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-27 08:45:19 +00:00
e1c11d411a
* rough auction type refactor * replace endTime type * split keeper file up * update store methods * move store methods to keeper.go * move nextAuctionID from params to genState * simplify auction type to not use pointers * add basic auction tests * update endblocker test * add payout to depositors feature * add more tests * move index updates to Get/Set for more safety * remove slightly unecessary ID type * remove unused message types * feat: add spec, update redundant type names * stop sending zero coins * use only one coins field in MsgPlaceBid * remove uncessary Auction interface methods * give auction types more accurate names * remove vuepress comments from spec * minor spec updates * update doc comments * add params validation * code cleanup, address review comments * resolve minor TODOs * sync spec with code Co-authored-by: Kevin Davis <karzak@users.noreply.github.com>
70 lines
1.9 KiB
Go
70 lines
1.9 KiB
Go
package keeper
|
|
|
|
import (
|
|
"sort"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
)
|
|
|
|
// splitIntIntoWeightedBuckets divides an initial +ve integer among several buckets in proportion to the buckets' weights
|
|
// It uses the largest remainder method:
|
|
// https://en.wikipedia.org/wiki/Largest_remainder_method
|
|
// see also: https://stackoverflow.com/questions/13483430/how-to-make-rounded-percentages-add-up-to-100
|
|
func splitIntIntoWeightedBuckets(amount sdk.Int, buckets []sdk.Int) []sdk.Int {
|
|
// TODO ideally change algorithm to work with -ve numbers. Limiting to +ve numbers until them
|
|
if amount.IsNegative() {
|
|
panic("negative amount")
|
|
}
|
|
for _, bucket := range buckets {
|
|
if bucket.IsNegative() {
|
|
panic("negative bucket")
|
|
}
|
|
}
|
|
|
|
totalWeights := totalInts(buckets...)
|
|
|
|
// split amount by weights, recording whole number part and remainder
|
|
quotients := make([]quoRem, len(buckets))
|
|
for i := range buckets {
|
|
q := amount.Mul(buckets[i]).Quo(totalWeights)
|
|
r := amount.Mul(buckets[i]).Mod(totalWeights)
|
|
quotients[i] = quoRem{index: i, quo: q, rem: r}
|
|
}
|
|
|
|
// apportion left over to buckets with the highest remainder (to minimize error)
|
|
sort.Slice(quotients, func(i, j int) bool {
|
|
return quotients[i].rem.GT(quotients[j].rem) // decreasing remainder order
|
|
})
|
|
|
|
allocated := sdk.ZeroInt()
|
|
for _, qr := range quotients {
|
|
allocated = allocated.Add(qr.quo)
|
|
}
|
|
leftToAllocate := amount.Sub(allocated)
|
|
|
|
results := make([]sdk.Int, len(quotients))
|
|
for _, qr := range quotients {
|
|
results[qr.index] = qr.quo
|
|
if !leftToAllocate.IsZero() {
|
|
results[qr.index] = results[qr.index].Add(sdk.OneInt())
|
|
leftToAllocate = leftToAllocate.Sub(sdk.OneInt())
|
|
}
|
|
}
|
|
return results
|
|
}
|
|
|
|
type quoRem struct {
|
|
index int
|
|
quo sdk.Int
|
|
rem sdk.Int
|
|
}
|
|
|
|
// totalInts adds together sdk.Ints
|
|
func totalInts(is ...sdk.Int) sdk.Int {
|
|
total := sdk.ZeroInt()
|
|
for _, i := range is {
|
|
total = total.Add(i)
|
|
}
|
|
return total
|
|
}
|