Skip to content

Commit

Permalink
backup
Browse files Browse the repository at this point in the history
  • Loading branch information
Reecepbcups committed Dec 9, 2023
1 parent d5bee5e commit f1c330e
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 13 deletions.
33 changes: 29 additions & 4 deletions chain/cosmos/module_feegrant.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,47 @@ package cosmos

import (
"context"
"fmt"
"strings"
"time"

"cosmossdk.io/x/feegrant"
)

// FeeGrant grants a fee grant.
func (tn *ChainNode) FeeGrant(ctx context.Context, granterKey, grantee string, extraFlags ...string) error {
cmd := []string{"feegrant", "grant", granterKey, grantee}
func (tn *ChainNode) FeeGrant(ctx context.Context, granterKey, grantee, spendLimit string, allowedMsgs []string, expiration time.Time, extraFlags ...string) error {
cmd := []string{"feegrant", "grant", granterKey, grantee, "--spend-limit", spendLimit}

if len(allowedMsgs) > 0 {
msgs := make([]string, len(allowedMsgs))
for i, msg := range allowedMsgs {
if !strings.HasPrefix(msg, "/") {
msg = "/" + msg
}
msgs[i] = msg
}

cmd = append(cmd, "--allowed-messages", strings.Join(msgs, ","))
}

if expiration.After(time.Now()) {
cmd = append(cmd, "--expiration", expiration.Format(time.RFC3339))
}

cmd = append(cmd, extraFlags...)

_, err := tn.ExecTx(ctx, granterKey, cmd...)
return err
}

// FeeGrantRevoke revokes a fee grant.
func (tn *ChainNode) FeeGrantRevoke(ctx context.Context, keyName, granterAddr, granteeAddr string) error {
_, err := tn.ExecTx(ctx, keyName, "feegrant", "revoke", granterAddr, granteeAddr)
func (tn *ChainNode) FeeGrantRevoke(ctx context.Context, keyName, granteeAddr string) error {
granterAddr, err := tn.KeyBech32(ctx, keyName, "acc")
if err != nil {
return fmt.Errorf("FeeGrantRevoke failed to get granter address: %w", err)
}

_, err = tn.ExecTx(ctx, keyName, "feegrant", "revoke", granterAddr, granteeAddr)
return err
}

Expand Down
107 changes: 98 additions & 9 deletions examples/cosmos/chain_core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"math"
"testing"
"time"

sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
Expand Down Expand Up @@ -72,6 +73,7 @@ func TestCoreSDKCommands(t *testing.T) {
CoinType: "118",
ModifyGenesis: cosmos.ModifyGenesis(sdk47Genesis),
},

NumValidators: &numVals,
NumFullNodes: &numFullNodes,
},
Expand Down Expand Up @@ -114,6 +116,11 @@ func TestCoreSDKCommands(t *testing.T) {
users := interchaintest.GetAndFundTestUsers(t, ctx, "default", genesisAmt, chain, chain, chain)
testDistribution(ctx, t, chain, users)
})

t.Run("feegrant", func(t *testing.T) {
users := interchaintest.GetAndFundTestUsers(t, ctx, "default", genesisAmt, chain, chain, chain, chain)
testFeeGrant(ctx, t, chain, users)
})
}

func testAuthz(ctx context.Context, t *testing.T, chain *cosmos.CosmosChain, users []ibc.Wallet) {
Expand Down Expand Up @@ -258,13 +265,6 @@ func testBank(ctx context.Context, t *testing.T, chain *cosmos.CosmosChain, user
}

func testDistribution(ctx context.Context, t *testing.T, chain *cosmos.CosmosChain, users []ibc.Wallet) {
// fund-community-pool Funds the community pool with the specified amount
// fund-validator-rewards-pool Fund the validator rewards pool with the specified amount
// set-withdraw-addr change the default withdraw address for rewards associated with an address
// withdraw-all-rewards withdraw all delegations rewards for a delegator
// withdraw-rewards

// setup
var err error
node := chain.GetNode()
acc := authtypes.NewModuleAddress("distribution")
Expand All @@ -282,8 +282,6 @@ func testDistribution(ctx context.Context, t *testing.T, chain *cosmos.CosmosCha

newWithdrawAddr := "cosmos1hj83l3auyqgy5qcp52l6sp2e67xwq9xx80alju"

// ----

t.Run("misc queries", func(t *testing.T) {
slashes, err := chain.DistributionValidatorSlashes(ctx, valAddr)
require.NoError(err)
Expand Down Expand Up @@ -376,5 +374,96 @@ func testDistribution(ctx context.Context, t *testing.T, chain *cosmos.CosmosCha
require.NoError(err)
require.EqualValues(1, rewards.Len())
})
}

func testFeeGrant(ctx context.Context, t *testing.T, chain *cosmos.CosmosChain, users []ibc.Wallet) {
var err error
node := chain.GetNode()

denom := chain.Config().Denom

// FeeGrant, FeeGrantRevoke

t.Run("successful grant and queries", func(t *testing.T) {
granter := users[0]
grantee := users[1]

err = node.FeeGrant(ctx, granter.KeyName(), grantee.FormattedAddress(), fmt.Sprintf("%d%s", 1000, chain.Config().Denom), []string{"/cosmos.bank.v1beta1.MsgSend"}, time.Now().Add(time.Hour*24*365))
require.NoError(t, err)

g, err := chain.FeeGrantGetAllowance(ctx, granter.FormattedAddress(), grantee.FormattedAddress())
require.NoError(t, err)
fmt.Printf("g: %+v\n", g)
require.EqualValues(t, granter.FormattedAddress(), g.Granter)
require.EqualValues(t, grantee.FormattedAddress(), g.Grantee)
require.EqualValues(t, "/cosmos.feegrant.v1beta1.BasicAllowance", g.Allowance.TypeUrl)
require.Contains(t, string(g.Allowance.Value), "/cosmos.bank.v1beta1.MsgSend")

all, err := chain.FeeGrantGetAllowances(ctx, grantee.FormattedAddress())
require.NoError(t, err)
require.Len(t, all, 1)
require.EqualValues(t, granter.FormattedAddress(), all[0].Granter)

all2, err := chain.FeeGrantGetAllowancesByGranter(ctx, granter.FormattedAddress())
require.NoError(t, err)
require.Len(t, all2, 1)
require.EqualValues(t, grantee.FormattedAddress(), all2[0].Grantee)
})

t.Run("successful execution and a revoke", func(t *testing.T) {
granter2 := users[2]
grantee2 := users[3]

err = node.FeeGrant(ctx, granter2.KeyName(), grantee2.FormattedAddress(), fmt.Sprintf("%d%s", 100_000, denom), nil, time.Unix(0, 0))
require.NoError(t, err)

bal, err := chain.BankGetBalance(ctx, granter2.FormattedAddress(), denom)
require.NoError(t, err)

fee := 500
sendAmt := 501
sendCoin := fmt.Sprintf("%d%s", sendAmt, denom)
feeCoin := fmt.Sprintf("%d%s", fee, denom)

// use the feegrant and validate the granter balance decreases by fee then add the sendAmt back
_, err = node.ExecTx(ctx,
grantee2.KeyName(), "bank", "send", grantee2.KeyName(), granter2.FormattedAddress(), sendCoin,
"--fees", feeCoin, "--fee-granter", granter2.FormattedAddress(),
)
require.NoError(t, err)

newBal, err := chain.BankGetBalance(ctx, granter2.FormattedAddress(), denom)
require.NoError(t, err)
require.EqualValues(t, bal.AddRaw(int64(sendAmt-fee)), newBal)

// revoke the grant
err = node.FeeGrantRevoke(ctx, granter2.KeyName(), grantee2.FormattedAddress())
require.NoError(t, err)

// fail; try to execute the above logic again
_, err = node.ExecTx(ctx,
grantee2.KeyName(), "bank", "send", grantee2.KeyName(), granter2.FormattedAddress(), sendCoin,
"--fees", feeCoin, "--fee-granter", granter2.FormattedAddress(),
)
// TODO: actually want an error here
require.NoError(t, err)

postRevokeBal, err := chain.BankGetBalance(ctx, granter2.FormattedAddress(), denom)
require.NoError(t, err)
require.EqualValues(t, newBal, postRevokeBal)
})

// perform feegrant msg

// chain.FeeGrantGetAllowance()
// chain.FeeGrantGetAllowances()
// chain.FeeGrantGetAllowancesByGranter()
}

// func testGov(ctx context.Context, t *testing.T, chain *cosmos.CosmosChain, users []ibc.Wallet) {}

// func testSlashing(ctx context.Context, t *testing.T, chain *cosmos.CosmosChain, users []ibc.Wallet) {}

// func testStaking(ctx context.Context, t *testing.T, chain *cosmos.CosmosChain, users []ibc.Wallet) {}

// func testUpgrade(ctx context.Context, t *testing.T, chain *cosmos.CosmosChain, users []ibc.Wallet) {}

0 comments on commit f1c330e

Please sign in to comment.