/
cmd_payment.go
123 lines (102 loc) · 3.26 KB
/
cmd_payment.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
package main
import (
"context"
"errors"
"fmt"
sdktypes "github.com/bnb-chain/greenfield-go-sdk/types"
"github.com/urfave/cli/v2"
)
// cmdBuyQuota buy the read quota of the bucket
func cmdBuyQuota() *cli.Command {
return &cli.Command{
Name: "buy-quota",
Action: buyQuotaForBucket,
Usage: "update bucket quota info",
ArgsUsage: "BUCKET-URL",
Description: `
Update the read quota metadata of the bucket, indicating the target quota of the bucket.
The command need to set the target quota with --chargedQuota
Examples:
$ gnfd-cmd payment buy-quota --chargedQuota 1000000 gnfd://bucket-name`,
Flags: []cli.Flag{
&cli.Uint64Flag{
Name: chargeQuotaFlag,
Usage: "indicate the target quota to be set for the bucket",
Required: true,
},
},
}
}
func cmdGetQuotaInfo() *cli.Command {
return &cli.Command{
Name: "get-quota",
Action: getQuotaInfo,
Usage: "get quota info of the bucket",
ArgsUsage: "BUCKET-URL",
Description: `
Get charged quota, free quota and consumed quota info from storage provider
Examples:
$ gnfd -c config.toml payment quota-info gnfd://bucket-name`,
}
}
// buyQuotaForBucket set the charged quota meta of bucket on chain
func buyQuotaForBucket(ctx *cli.Context) error {
bucketName, err := getBucketNameByUrl(ctx)
if err != nil {
return toCmdErr(err)
}
client, err := NewClient(ctx, ClientOptions{IsQueryCmd: false})
if err != nil {
return toCmdErr(err)
}
c, cancelBuyQuota := context.WithCancel(globalContext)
defer cancelBuyQuota()
// if bucket not exist, no need to buy quota
_, err = client.HeadBucket(c, bucketName)
if err != nil {
return toCmdErr(ErrBucketNotExist)
}
targetQuota := ctx.Uint64(chargeQuotaFlag)
if targetQuota == 0 {
return toCmdErr(errors.New("target quota not set"))
}
txnHash, err := client.BuyQuotaForBucket(c, bucketName, targetQuota, sdktypes.BuyQuotaOption{TxOpts: &TxnOptionWithSyncMode})
if err != nil {
fmt.Println("buy quota error:", err.Error())
return nil
}
fmt.Printf("buy quota for bucket: %s \n", bucketName)
fmt.Println("transaction hash: ", txnHash)
return nil
}
// getQuotaInfo query the quota price info of sp from greenfield chain
func getQuotaInfo(ctx *cli.Context) error {
bucketName, err := getBucketNameByUrl(ctx)
if err != nil {
return toCmdErr(err)
}
client, err := NewClient(ctx, ClientOptions{IsQueryCmd: false})
if err != nil {
return toCmdErr(err)
}
c, cancelGetQuota := context.WithCancel(globalContext)
defer cancelGetQuota()
// if bucket not exist, no need to get info of quota
_, err = client.HeadBucket(c, bucketName)
if err != nil {
return toCmdErr(ErrBucketNotExist)
}
quotaInfo, err := client.GetBucketReadQuota(c, bucketName)
if err != nil {
return toCmdErr(err)
}
nameMaxLen := len("consumed charged quota:")
format := fmt.Sprintf("%%-%ds %%-%dd \n", nameMaxLen, 50)
firstLineFormat := fmt.Sprintf("%%-%ds %%-%ds \n", nameMaxLen, 50)
fmt.Printf(firstLineFormat, "quota name", "quota value")
fmt.Printf(format, "charged quota:", quotaInfo.ReadQuotaSize)
fmt.Printf(format, "remained free quota:", quotaInfo.SPFreeReadQuotaSize)
fmt.Printf(format, "consumed charged quota:", quotaInfo.ReadConsumedSize)
fmt.Printf(format, "consumed free quota:", quotaInfo.FreeConsumedSize)
return nil
}