-
Notifications
You must be signed in to change notification settings - Fork 16
/
utils.go
146 lines (111 loc) · 3.61 KB
/
utils.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package keeper
import (
"encoding/json"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/bech32"
"github.com/jackalLabs/canine-chain/v3/x/storage/types"
)
// func MakeFid(data []byte) (string, error) {
// return bech32.ConvertAndEncode(types.FidPrefix, data)
// }
func MakeCid(data []byte) (string, error) {
return bech32.ConvertAndEncode(types.CidPrefix, data)
}
func (k Keeper) GetPaidAmount(ctx sdk.Context, address string) int64 {
payInfo, found := k.GetStoragePaymentInfo(
ctx,
address,
)
if !found {
return 0
}
return payInfo.SpaceAvailable
}
func (k Keeper) GetProviderDeals(ctx sdk.Context, provider string) int64 {
allDeals := k.GetAllActiveDeals(ctx)
var count int64
for i := 0; i < len(allDeals); i++ {
deal := allDeals[i]
if deal.Provider != provider {
continue
}
count++
}
return count
}
func (k Keeper) GetProviderUsing(ctx sdk.Context, provider string) int64 {
allDeals := k.GetAllActiveDeals(ctx)
var space int64
for i := 0; i < len(allDeals); i++ {
deal := allDeals[i]
if deal.Provider != provider {
continue
}
size, ok := sdk.NewIntFromString(deal.Filesize)
if !ok {
continue
}
space += size.Int64()
}
return space
}
// GetStorageCostKbs calculates storage cost in ujkl
// Uses kilobytes and months to calculate how much user has to pay
func (k Keeper) GetStorageCostKbs(ctx sdk.Context, kbs int64, hours int64) sdk.Int {
pricePerTBPerMonth := sdk.NewDec(k.GetParams(ctx).PricePerTbPerMonth)
quantifiedPricePerTBPerMonth := pricePerTBPerMonth.QuoInt64(3)
pricePerGbPerMonth := quantifiedPricePerTBPerMonth.QuoInt64(1000)
pricePerMbPerMonth := pricePerGbPerMonth.QuoInt64(1000)
pricePerKbPerMonth := pricePerMbPerMonth.QuoInt64(1000)
pricePerKbPerHour := pricePerKbPerMonth.QuoInt64(720)
pricePerHour := pricePerKbPerHour.MulInt64(kbs)
totalCost := pricePerHour.MulInt64(hours)
jklPrice := k.GetJklPrice(ctx)
// TODO: fetch denom unit from bank module
var ujklUnit int64 = 1000000
jklCost := totalCost.Quo(jklPrice)
ujklCost := jklCost.MulInt64(ujklUnit)
return ujklCost.TruncateInt()
}
// GetStorageCost calculates storage cost in ujkl
// Uses gigabytes and months to calculate how much user has to pay
func (k Keeper) GetStorageCost(ctx sdk.Context, gbs int64, hours int64) sdk.Int {
pricePerTBPerMonth := sdk.NewDec(k.GetParams(ctx).PricePerTbPerMonth)
quantifiedPricePerTBPerMonth := pricePerTBPerMonth.QuoInt64(3)
pricePerGbPerMonth := quantifiedPricePerTBPerMonth.QuoInt64(1000)
pricePerGbPerHour := pricePerGbPerMonth.QuoInt64(720)
pricePerHour := pricePerGbPerHour.MulInt64(gbs)
totalCost := pricePerHour.MulInt64(hours)
jklPrice := k.GetJklPrice(ctx)
// TODO: fetch denom unit from bank module
var ujklUnit int64 = 1000000
jklCost := totalCost.Quo(jklPrice)
ujklCost := jklCost.MulInt64(ujklUnit)
return ujklCost.TruncateInt()
}
// GetJklPrice uses oracle module to get jkl price
// Returns 0.20 if feed doesn't exist or failed to unmarshal data
// Unmarshal failure is logged
func (k Keeper) GetJklPrice(ctx sdk.Context) (price sdk.Dec) {
price = sdk.MustNewDecFromStr("0.20")
priceFeed := k.GetParams(ctx).PriceFeed
feed, found := k.oraclekeeper.GetFeed(ctx, priceFeed)
if found {
type data struct {
Price string `json:"price"`
Change string `json:"24h_change"`
}
var d data
err := json.Unmarshal([]byte(feed.Data), &d)
if err != nil {
ctx.Logger().Debug("Failed to unmarshal Feed.Data (%s)", feed.Data)
}
p, err := sdk.NewDecFromStr(d.Price)
if err != nil {
ctx.Logger().Debug("Failed to convert Feed.Data.Price to sdk.Dec (%s)", d.Price)
} else {
price = p
}
}
return price
}