/
royalty_allocation.go
41 lines (37 loc) · 1.06 KB
/
royalty_allocation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package keeper
import (
"math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/likecoin/likecoin-chain/v3/x/likenft/types"
)
func (k Keeper) ComputeRoyaltyAllocation(ctx sdk.Context, txnAmount uint64, config types.RoyaltyConfig) (royaltyAmount uint64, allocations []types.RoyaltyAllocation, err error) {
err = k.validateRoyaltyConfig(ctx, config)
if err != nil {
return
}
// max allocable amount
allocatable := uint64(math.Floor(float64(txnAmount) / float64(10000) * float64(config.RateBasisPoints)))
if allocatable <= 0 {
return
}
// sum total weight
totalWeight := uint64(0)
for _, stakeholder := range config.Stakeholders {
totalWeight += stakeholder.Weight
}
if totalWeight <= 0 {
return
}
// split by weights
for _, stakeholder := range config.Stakeholders {
amount := uint64(math.Floor(float64(allocatable) / float64(totalWeight) * float64(stakeholder.Weight)))
if amount > 0 {
allocations = append(allocations, types.RoyaltyAllocation{
Account: stakeholder.Account,
Amount: amount,
})
royaltyAmount += amount
}
}
return
}