Skip to content

Commit 052abbd

Browse files
author
gitopia1c2zfrmhra3spfrc2m5ft64hef30guf60lvtcm3
committed
Merge pull request #14 from git-remote-gitopia/use-balance-first
2 parents 86bd1d9 + d8a7927 commit 052abbd

File tree

9 files changed

+71
-32
lines changed

9 files changed

+71
-32
lines changed

cmd/git-credential-gitopia/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func handleGetAction() {
5151
return
5252
}
5353

54-
wallet, err := wallet.InitWallet(nil)
54+
wallet, err := wallet.InitWallet(nil, nil)
5555
if err != nil {
5656
log.Debugf(err.Error())
5757
return

cmd/git-remote-gitopia/gitopia.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
sdk "github.com/cosmos/cosmos-sdk/types"
1616
"github.com/cosmos/cosmos-sdk/types/query"
1717
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
18+
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
1819
"github.com/cosmos/cosmos-sdk/x/feegrant"
1920
"github.com/gitopia/git-remote-gitopia/config"
2021
core "github.com/gitopia/git-remote-gitopia/core"
@@ -39,6 +40,7 @@ type GitopiaHandler struct {
3940
grpcConn *grpc.ClientConn
4041
queryClient gitopiatypes.QueryClient
4142
feegrantClient feegrant.QueryClient
43+
bankClient banktypes.QueryClient
4244

4345
chainId string
4446
remoteUserId string
@@ -69,6 +71,7 @@ func (h *GitopiaHandler) Initialize(remote *core.Remote) error {
6971
h.queryClient = gitopiatypes.NewQueryClient(h.grpcConn)
7072
serviceClient := tmservice.NewServiceClient(h.grpcConn)
7173
h.feegrantClient = feegrant.NewQueryClient(h.grpcConn)
74+
h.bankClient = banktypes.NewQueryClient(h.grpcConn)
7275

7376
// Get chain id for signing transaction
7477
nodeInfoRes, err := serviceClient.GetNodeInfo(context.Background(), &tmservice.GetNodeInfoRequest{})
@@ -176,7 +179,7 @@ func (h *GitopiaHandler) Push(remote *core.Remote, refsToPush []core.RefToPush)
176179
var err error
177180

178181
if h.wallet == nil {
179-
h.wallet, err = wallet.InitWallet(h.feegrantClient)
182+
h.wallet, err = wallet.InitWallet(h.bankClient, h.feegrantClient)
180183
if err != nil {
181184
return nil, err
182185
}

config/config_dev.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ const (
99
TmAddr = "https://rpc.devnet.gitopia.com:443"
1010
GasPrices = "0.001ulore"
1111
FeeGranterAddr = ""
12+
Denom = "ulore"
1213
)

config/config_prod.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ const (
99
TmAddr = "https://rpc.gitopia.com:443"
1010
GasPrices = "0.001ulore"
1111
FeeGranterAddr = "gitopia13ashgc6j5xle4m47kqyn5psavq0u3klmscfxql"
12+
Denom = "ulore"
1213
)

config/config_testing.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ const (
99
TmAddr = "http://localhost:26657"
1010
GasPrices = "0.001ulore"
1111
FeeGranterAddr = ""
12+
Denom = "ulore"
1213
)

core/wallet/gitopia_wallet.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
xauthsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
2222
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
2323
authtype "github.com/cosmos/cosmos-sdk/x/auth/types"
24+
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
2425
"github.com/cosmos/cosmos-sdk/x/feegrant"
2526
"github.com/gitopia/git-remote-gitopia/config"
2627
gitopia "github.com/gitopia/gitopia/v2/app"
@@ -52,7 +53,7 @@ type GitopiaWallet struct {
5253
feeGranterAddr string
5354
}
5455

55-
func InitGitopiaWallet(feegrantClient feegrant.QueryClient) (Wallet, error) {
56+
func InitGitopiaWallet(bankClient banktypes.QueryClient, feegrantClient feegrant.QueryClient) (Wallet, error) {
5657
var buffer []byte
5758
var err error
5859
gw := GitopiaWallet{}
@@ -85,13 +86,23 @@ func InitGitopiaWallet(feegrantClient feegrant.QueryClient) (Wallet, error) {
8586
}
8687
gw.address = sdk.AccAddress(gw.privateKey.PubKey().Address()).String()
8788

88-
fr, _ := feegrantClient.Allowance(context.Background(), &feegrant.QueryAllowanceRequest{
89-
Granter: config.FeeGranterAddr,
90-
Grantee: gw.address,
91-
})
92-
93-
if fr != nil {
94-
gw.feeGranterAddr = fr.Allowance.Granter
89+
if bankClient != nil && feegrantClient != nil {
90+
b, _ := bankClient.Balance(context.Background(), &banktypes.QueryBalanceRequest{
91+
Address: gw.address,
92+
Denom: config.Denom,
93+
})
94+
95+
// Use fee grant only when balance is zero
96+
if b.Balance.Amount.IsZero() {
97+
fr, _ := feegrantClient.Allowance(context.Background(), &feegrant.QueryAllowanceRequest{
98+
Granter: config.FeeGranterAddr,
99+
Grantee: gw.address,
100+
})
101+
102+
if fr != nil {
103+
gw.feeGranterAddr = fr.Allowance.Granter
104+
}
105+
}
95106
}
96107

97108
return gw, nil

core/wallet/ledger.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
xauthsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
2222
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
2323
authtype "github.com/cosmos/cosmos-sdk/x/auth/types"
24+
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
2425
"github.com/cosmos/cosmos-sdk/x/feegrant"
2526
"github.com/gitopia/git-remote-gitopia/config"
2627
gitopia "github.com/gitopia/gitopia/v2/app"
@@ -36,7 +37,7 @@ type Ledger struct {
3637
feeGranterAddr string
3738
}
3839

39-
func InitLedgerWallet(feegrantClient feegrant.QueryClient) (Wallet, error) {
40+
func InitLedgerWallet(bankClient banktypes.QueryClient, feegrantClient feegrant.QueryClient) (Wallet, error) {
4041
ledgerPrivKey, err := ledger.NewPrivKeySecp256k1Unsafe(hd.BIP44Params{
4142
Purpose: 44,
4243
CoinType: 118,
@@ -50,14 +51,24 @@ func InitLedgerWallet(feegrantClient feegrant.QueryClient) (Wallet, error) {
5051

5152
addr := sdk.AccAddress(ledgerPrivKey.PubKey().Address()).String()
5253

53-
fr, _ := feegrantClient.Allowance(context.Background(), &feegrant.QueryAllowanceRequest{
54-
Granter: config.FeeGranterAddr,
55-
Grantee: addr,
56-
})
57-
5854
feeGranter := ""
59-
if fr != nil {
60-
feeGranter = fr.Allowance.Granter
55+
if bankClient != nil && feegrantClient != nil {
56+
b, _ := bankClient.Balance(context.Background(), &banktypes.QueryBalanceRequest{
57+
Address: addr,
58+
Denom: config.Denom,
59+
})
60+
61+
// Use fee grant only when balance is zero
62+
if b.Balance.Amount.IsZero() {
63+
fr, _ := feegrantClient.Allowance(context.Background(), &feegrant.QueryAllowanceRequest{
64+
Granter: config.FeeGranterAddr,
65+
Grantee: addr,
66+
})
67+
68+
if fr != nil {
69+
feeGranter = fr.Allowance.Granter
70+
}
71+
}
6172
}
6273

6374
return Ledger{

core/wallet/os_keyring.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ import (
1313
sdkkeyring "github.com/cosmos/cosmos-sdk/crypto/keyring"
1414
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
1515
sdk "github.com/cosmos/cosmos-sdk/types"
16+
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
1617
"github.com/cosmos/cosmos-sdk/x/feegrant"
1718
"github.com/gitopia/git-remote-gitopia/config"
1819
glib "github.com/gitopia/gitopia-go"
1920
"github.com/gitopia/gitopia-go/logger"
2021
gitopia "github.com/gitopia/gitopia/v2/app"
2122
offchaintypes "github.com/gitopia/gitopia/v2/x/offchain/types"
2223
goGitConfig "github.com/go-git/go-git/v5/config"
24+
"github.com/pkg/errors"
2325
"github.com/sirupsen/logrus"
2426
"github.com/spf13/pflag"
25-
26-
"github.com/pkg/errors"
2727
"google.golang.org/grpc"
2828
)
2929

@@ -60,7 +60,7 @@ type OSKeyring struct {
6060
secType secretType
6161
}
6262

63-
func InitOSKeyringWallet(feegrantClient feegrant.QueryClient) (Wallet, error) {
63+
func InitOSKeyringWallet(bankClient banktypes.QueryClient, feegrantClient feegrant.QueryClient) (Wallet, error) {
6464
var key string
6565
var backend string
6666

@@ -96,13 +96,23 @@ func InitOSKeyringWallet(feegrantClient feegrant.QueryClient) (Wallet, error) {
9696
return nil, errors.Wrap(err, "error creating cosmos client context")
9797
}
9898

99-
fr, _ := feegrantClient.Allowance(context.Background(), &feegrant.QueryAllowanceRequest{
100-
Granter: config.FeeGranterAddr,
101-
Grantee: cc.FromAddress.String(),
102-
})
103-
104-
if fr != nil {
105-
cc.WithFeeGranterAddress(sdk.MustAccAddressFromBech32(fr.Allowance.Granter))
99+
if bankClient != nil && feegrantClient != nil {
100+
b, _ := bankClient.Balance(context.Background(), &banktypes.QueryBalanceRequest{
101+
Address: cc.FromAddress.String(),
102+
Denom: config.Denom,
103+
})
104+
105+
// Use fee grant only when balance is zero
106+
if b.Balance.Amount.IsZero() {
107+
fr, _ := feegrantClient.Allowance(context.Background(), &feegrant.QueryAllowanceRequest{
108+
Granter: config.FeeGranterAddr,
109+
Grantee: cc.FromAddress.String(),
110+
})
111+
112+
if fr != nil {
113+
cc.WithFeeGranterAddress(sdk.MustAccAddressFromBech32(fr.Allowance.Granter))
114+
}
115+
}
106116
}
107117

108118
txf := tx.NewFactoryCLI(cc, &pflag.FlagSet{}).WithGasAdjustment(GAS_ADJUSTMENT)

core/wallet/wallet.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66

77
sdk "github.com/cosmos/cosmos-sdk/types"
8+
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
89
"github.com/cosmos/cosmos-sdk/x/feegrant"
910
"google.golang.org/grpc"
1011
)
@@ -27,12 +28,12 @@ type Wallet interface {
2728
}
2829

2930
// TODO: make options optional
30-
func InitWallet(feegrantClient feegrant.QueryClient) (Wallet, error) {
31-
wallet, err := InitOSKeyringWallet(feegrantClient)
31+
func InitWallet(bankClient banktypes.QueryClient, feegrantClient feegrant.QueryClient) (Wallet, error) {
32+
wallet, err := InitOSKeyringWallet(bankClient, feegrantClient)
3233
if errors.Is(err, ErrGitopiaKeyNotConfigured) {
33-
wallet, err = InitGitopiaWallet(feegrantClient)
34+
wallet, err = InitGitopiaWallet(bankClient, feegrantClient)
3435
if err != nil {
35-
wallet, err = InitLedgerWallet(feegrantClient)
36+
wallet, err = InitLedgerWallet(bankClient, feegrantClient)
3637
if err != nil {
3738
return nil, fmt.Errorf("fatal: Gitopia wallet is not configured! Set gitopia key or use Ledger")
3839
}

0 commit comments

Comments
 (0)