Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: grant allowance #115

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions cmd/cmd_allowance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package main

import (
"context"
"errors"
"fmt"
"time"

"cosmossdk.io/math"

"github.com/bnb-chain/greenfield/sdk/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/urfave/cli/v2"
)

// cmdGrantAllowance grant allowance to a account
func cmdGrantAllowance() *cli.Command {
return &cli.Command{
Name: "grant",
Action: grantAllowance,
Usage: "grant allowance",
ArgsUsage: "",
Description: `
The command is used to grant allowance to the grantee. --grantee defines the grantee address, --expire defines the expiration time in second

Examples:
$ gnfd-cmd fee grant --grantee 0x... --allowance 10000000000000000 --expire 3600`,
Flags: []cli.Flag{
&cli.StringFlag{
Name: granteeFlag,
Value: "",
Usage: "the address hex string of the grantee",
},
&cli.StringFlag{
Name: allowanceFlag,
Value: "",
Usage: "the bnb in wei",
},
&cli.Uint64Flag{
Name: expireTimeFlag,
Value: 0,
Usage: "set the expire unix time stamp of the allowance",
},
},
}
}

func grantAllowance(ctx *cli.Context) error {
grantee := ctx.String(granteeFlag)
granteeAddr, err := sdk.AccAddressFromHexUnsafe(grantee)
if err != nil {
return err
}
allowancesStr := ctx.String(allowanceFlag)
allowance, ok := math.NewIntFromString(allowancesStr)
if !ok {
return toCmdErr(errors.New("convert string to int failed"))
}
var expireTime *time.Time
if ctx.Uint64(expireTimeFlag) > 0 {
temp := time.Unix(int64(ctx.Uint64(expireTimeFlag)), 0)
expireTime = &temp
}

client, err := NewClient(ctx, ClientOptions{IsQueryCmd: false})
if err != nil {
return toCmdErr(err)
}
c, cancelSetTag := context.WithCancel(globalContext)
defer cancelSetTag()

txHash, err := client.GrantBasicAllowance(c, granteeAddr.String(), allowance, expireTime, types.TxOption{})
if err != nil {
return toCmdErr(err)
}
err = waitTxnStatus(client, c, txHash, "GrantBasicAllowance")
if err != nil {
return toCmdErr(err)
}
fmt.Printf("Grant %s to %s succ, txHash=%s\n", allowance.String(), grantee, txHash)

return nil
}
7 changes: 7 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ func main() {
cmdSetDefaultAccount(),
},
},
{
Name: "fee",
Usage: "support fee grant operation functions",
Subcommands: []*cli.Command{
cmdGrantAllowance(),
},
},
{
Name: "task",
Usage: "support the batch upload file",
Expand Down
1 change: 1 addition & 0 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const (
groupMemberExpireFlag = "expireTime"
groupIDFlag = "groupId"
granteeFlag = "grantee"
allowanceFlag = "allowance"
actionsFlag = "actions"
effectFlag = "effect"
expireTimeFlag = "expire"
Expand Down
Loading