From 0691e0df824baf965b4c885088cae7c2b40e1721 Mon Sep 17 00:00:00 2001 From: Salvatore Mazzarino Date: Mon, 13 Nov 2023 18:40:04 +0100 Subject: [PATCH] feat(faucet): set tx fee amount as option (#3745) * feat(faucet): set tx fee amount as option * add changelog * fix golangci-lint * fix nil exception * chore: rename applyOptions to apply * remove toolchain --- changelog.md | 1 + ignite/pkg/chaincmd/chaincmd.go | 30 +++++++++++++++---- ignite/pkg/chaincmd/runner/chain.go | 4 +-- ignite/pkg/cosmosfaucet/cosmosfaucet.go | 10 +++++++ ignite/pkg/cosmosfaucet/transfer.go | 4 +-- .../plugin/testdata/example-plugin/go.mod | 20 ++++++------- .../plugin/testdata/example-plugin/go.sum | 7 +++++ 7 files changed, 56 insertions(+), 20 deletions(-) diff --git a/changelog.md b/changelog.md index 1dc1d03e52..f603f82022 100644 --- a/changelog.md +++ b/changelog.md @@ -10,6 +10,7 @@ ### Changes - [#3529](https://github.com/ignite/cli/pull/3529) Refactor plugin system to use gRPC +- [#3745](https://github.com/ignite/cli/pull/3745) Set tx fee amount as option ## [`v0.27.1`](https://github.com/ignite/cli/releases/tag/v0.27.1) diff --git a/ignite/pkg/chaincmd/chaincmd.go b/ignite/pkg/chaincmd/chaincmd.go index f981804a1c..6a87f5079c 100644 --- a/ignite/pkg/chaincmd/chaincmd.go +++ b/ignite/pkg/chaincmd/chaincmd.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/cosmos/cosmos-sdk/client/flags" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/ignite/cli/ignite/pkg/cmdrunner/step" "github.com/ignite/cli/ignite/pkg/cosmosver" @@ -33,6 +34,7 @@ const ( optionRecover = "--recover" optionAddress = "--address" optionAmount = "--amount" + optionFees = "--fees" optionValidatorMoniker = "--moniker" optionValidatorCommissionRate = "--commission-rate" optionValidatorCommissionMaxRate = "--commission-max-rate" @@ -101,8 +103,8 @@ func (c ChainCmd) Copy(options ...Option) ChainCmd { type Option func(*ChainCmd) func applyOptions(c *ChainCmd, options []Option) { - for _, applyOption := range options { - applyOption(c) + for _, apply := range options { + apply(c) } } @@ -414,8 +416,8 @@ func (c ChainCmd) GentxCommand( } // Apply the options provided by the user - for _, applyOption := range options { - command = applyOption(command) + for _, apply := range options { + command = apply(command) } command = c.attachChainID(command) @@ -470,8 +472,21 @@ func (c ChainCmd) ExportCommand() step.Option { return c.daemonCommand(command) } +// BankSendOption for the BankSendCommand. +type BankSendOption func([]string) []string + +// BankSendWithFees sets fees to pay along with transaction for the bank send command. +func BankSendWithFees(fee sdk.Coin) BankSendOption { + return func(command []string) []string { + if !fee.IsNil() { + return append(command, optionFees, fee.String()) + } + return command + } +} + // BankSendCommand returns the command for transferring tokens. -func (c ChainCmd) BankSendCommand(fromAddress, toAddress, amount string) step.Option { +func (c ChainCmd) BankSendCommand(fromAddress, toAddress, amount string, options ...BankSendOption) step.Option { command := []string{ commandTx, } @@ -486,6 +501,11 @@ func (c ChainCmd) BankSendCommand(fromAddress, toAddress, amount string) step.Op optionYes, ) + // Apply the options provided by the user + for _, apply := range options { + command = apply(command) + } + command = c.attachChainID(command) command = c.attachKeyringBackend(command) command = c.attachNode(command) diff --git a/ignite/pkg/chaincmd/runner/chain.go b/ignite/pkg/chaincmd/runner/chain.go index 2b89e5258f..d235faa0b0 100644 --- a/ignite/pkg/chaincmd/runner/chain.go +++ b/ignite/pkg/chaincmd/runner/chain.go @@ -142,10 +142,10 @@ func (r Runner) Status(ctx context.Context) (NodeStatus, error) { } // BankSend sends amount from fromAccount to toAccount. -func (r Runner) BankSend(ctx context.Context, fromAccount, toAccount, amount string) (string, error) { +func (r Runner) BankSend(ctx context.Context, fromAccount, toAccount, amount string, options ...chaincmd.BankSendOption) (string, error) { b := newBuffer() opt := []step.Option{ - r.chainCmd.BankSendCommand(fromAccount, toAccount, amount), + r.chainCmd.BankSendCommand(fromAccount, toAccount, amount, options...), } if r.chainCmd.KeyringPassword() != "" { diff --git a/ignite/pkg/cosmosfaucet/cosmosfaucet.go b/ignite/pkg/cosmosfaucet/cosmosfaucet.go index e64ba81f10..cf022955bd 100644 --- a/ignite/pkg/cosmosfaucet/cosmosfaucet.go +++ b/ignite/pkg/cosmosfaucet/cosmosfaucet.go @@ -56,6 +56,9 @@ type Faucet struct { // it holds the maximum amounts of coins that can be sent to a single account. coinsMax map[string]sdkmath.Int + // fee to pay along with the transaction + feeAmount sdk.Coin + limitRefreshWindow time.Duration // openAPIData holds template data customizations for serving OpenAPI page & spec. @@ -102,6 +105,13 @@ func ChainID(id string) Option { } } +// FeeAmount sets a fee that it will be paid during the transaction. +func FeeAmount(amount sdkmath.Int, denom string) Option { + return func(f *Faucet) { + f.feeAmount = sdk.NewCoin(denom, amount) + } +} + // OpenAPI configures how to serve Open API page and spec. func OpenAPI(apiAddress string) Option { return func(f *Faucet) { diff --git a/ignite/pkg/cosmosfaucet/transfer.go b/ignite/pkg/cosmosfaucet/transfer.go index 4141977cc7..0eb62fbb32 100644 --- a/ignite/pkg/cosmosfaucet/transfer.go +++ b/ignite/pkg/cosmosfaucet/transfer.go @@ -9,7 +9,7 @@ import ( sdkmath "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" - + "github.com/ignite/cli/ignite/pkg/chaincmd" chaincmdrunner "github.com/ignite/cli/ignite/pkg/chaincmd/runner" ) @@ -92,7 +92,7 @@ func (f *Faucet) Transfer(ctx context.Context, toAccountAddress string, coins sd if err != nil { return err } - txHash, err := f.runner.BankSend(ctx, fromAccount.Address, toAccountAddress, strings.Join(coinsStr, ",")) + txHash, err := f.runner.BankSend(ctx, fromAccount.Address, toAccountAddress, strings.Join(coinsStr, ","), chaincmd.BankSendWithFees(f.feeAmount)) if err != nil { return err } diff --git a/integration/plugin/testdata/example-plugin/go.mod b/integration/plugin/testdata/example-plugin/go.mod index 70704d2477..92dd113591 100644 --- a/integration/plugin/testdata/example-plugin/go.mod +++ b/integration/plugin/testdata/example-plugin/go.mod @@ -1,6 +1,6 @@ module example-plugin -go 1.20 +go 1.21.1 require ( github.com/hashicorp/go-plugin v1.4.9 @@ -61,22 +61,20 @@ require ( github.com/spf13/pflag v1.0.5 // indirect github.com/xanzy/ssh-agent v0.3.3 // indirect go.etcd.io/bbolt v1.3.7 // indirect - golang.org/x/crypto v0.9.0 // indirect + golang.org/x/crypto v0.14.0 // indirect golang.org/x/exp v0.0.0-20230519143937-03e91628a987 // indirect - golang.org/x/mod v0.10.0 // indirect - golang.org/x/net v0.10.0 // indirect + golang.org/x/mod v0.12.0 // indirect + golang.org/x/net v0.17.0 // indirect golang.org/x/sync v0.2.0 // indirect - golang.org/x/sys v0.8.0 // indirect - golang.org/x/term v0.8.0 // indirect - golang.org/x/text v0.9.0 // indirect + golang.org/x/sys v0.13.0 // indirect + golang.org/x/term v0.13.0 // indirect + golang.org/x/text v0.13.0 // indirect golang.org/x/tools v0.9.1 // indirect google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect - google.golang.org/grpc v1.55.0 // indirect + google.golang.org/grpc v1.56.3 // indirect google.golang.org/protobuf v1.30.0 // indirect gopkg.in/warnings.v0 v0.1.2 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect ) -replace ( - github.com/ignite/cli => ../../../../ -) +replace github.com/ignite/cli => ../../../../ diff --git a/integration/plugin/testdata/example-plugin/go.sum b/integration/plugin/testdata/example-plugin/go.sum index 56b88f4cac..eeb703ebee 100644 --- a/integration/plugin/testdata/example-plugin/go.sum +++ b/integration/plugin/testdata/example-plugin/go.sum @@ -187,6 +187,7 @@ golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58 golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU= golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g= golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0= +golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= golang.org/x/exp v0.0.0-20230519143937-03e91628a987 h1:3xJIFvzUFbu4ls0BTBYcgbCGhA63eAOEMxIHugyXJqA= golang.org/x/exp v0.0.0-20230519143937-03e91628a987/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= @@ -194,6 +195,7 @@ golang.org/x/mod v0.6.0/go.mod h1:4mET923SAdbXp2ki8ey+zGs1SLqsuM2Y0uvdZR/fUNI= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk= golang.org/x/mod v0.10.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= @@ -206,6 +208,7 @@ golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M= golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= +golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220929204114-8fcdb60fdcc0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -236,6 +239,7 @@ golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.0.0-20220722155259-a9ba230a4035/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= @@ -244,6 +248,7 @@ golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U= golang.org/x/term v0.8.0 h1:n5xxQn2i3PC0yLAbjTpNT85q/Kgzcr2gIoX9OrJUols= golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= +golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= @@ -253,6 +258,7 @@ golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= @@ -266,6 +272,7 @@ google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 h1:KpwkzHKEF7B9Zxg google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1/go.mod h1:nKE/iIaLqn2bQwXBg8f1g2Ylh6r5MN5CmZvuzZCgsCU= google.golang.org/grpc v1.55.0 h1:3Oj82/tFSCeUrRTg/5E/7d/W5A1tj6Ky1ABAuZuv5ag= google.golang.org/grpc v1.55.0/go.mod h1:iYEXKGkEBhg1PjZQvoYEVPTDkHo1/bjTnfwTeGONTY8= +google.golang.org/grpc v1.56.3/go.mod h1:I9bI3vqKfayGqPUAwGdOSu7kt6oIJLixfffKrpXqQ9s= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=