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: add faucet compatibility for latest sdk chains #3756

Merged
merged 4 commits into from
Nov 16, 2023
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
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- [#3670](https://github.com/ignite/cli/pull/3670) Remove nodetime binaries
- [#3724](https://github.com/ignite/cli/pull/3724) Add or vendor proto packages from Go dependencies
- [#3715](https://github.com/ignite/cli/pull/3715) Add test suite for the cli tests
- [#3756](https://github.com/ignite/cli/pull/3756) Add faucet compatibility for latest sdk chains

### Changes

Expand Down
16 changes: 16 additions & 0 deletions ignite/pkg/chaincmd/chaincmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,22 @@ func (c ChainCmd) QueryTxEventsCommand(query string) step.Option {
return c.cliCommand(command)
}

// QueryTxQueryCommand returns the command to query tx.
func (c ChainCmd) QueryTxQueryCommand(query string) step.Option {
command := []string{
commandQuery,
"txs",
"--query",
query,
"--page", "1",
"--limit", "1000",
"--output", "json",
}

command = c.attachNode(command)
return c.cliCommand(command)
}

// StatusCommand returns the command that fetches node's status.
func (c ChainCmd) StatusCommand() step.Option {
command := []string{
Expand Down
47 changes: 33 additions & 14 deletions ignite/pkg/chaincmd/runner/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,27 +268,46 @@ type EventAttribute struct {
Value string
}

// QueryTxEvents queries tx events by event selectors.
func (r Runner) QueryTxEvents(
// QueryTxByEvents queries tx events by event selectors.
func (r Runner) QueryTxByEvents(
ctx context.Context,
selector EventSelector,
moreSelectors ...EventSelector,
selectors ...EventSelector,
) ([]Event, error) {
// prepare the selector.
var list []string

eventsSelectors := append([]EventSelector{selector}, moreSelectors...)

for _, event := range eventsSelectors {
list = append(list, fmt.Sprintf("%s.%s=%s", event.typ, event.attr, event.value))
if len(selectors) == 0 {
return nil, errors.New("event selector list should be greater than zero")
}
list := make([]string, len(selectors))
for i, event := range selectors {
list[i] = fmt.Sprintf("%s.%s=%s", event.typ, event.attr, event.value)
}

query := strings.Join(list, "&")
return r.QueryTx(ctx, r.chainCmd.QueryTxEventsCommand(query))
}

// QueryTxByQuery queries tx events by event selectors.
func (r Runner) QueryTxByQuery(
ctx context.Context,
selectors ...EventSelector,
) ([]Event, error) {
if len(selectors) == 0 {
return nil, errors.New("event selector list should be greater than zero")
}
list := make([]string, len(selectors))
for i, query := range selectors {
list[i] = fmt.Sprintf("%s.%s='%s'", query.typ, query.attr, query.value)
}
query := strings.Join(list, " AND ")
return r.QueryTx(ctx, r.chainCmd.QueryTxQueryCommand(query))
}

// QueryTx queries tx events/query selectors.
func (r Runner) QueryTx(
ctx context.Context,
option ...step.Option,
) ([]Event, error) {
// execute the command and parse the output.
b := newBuffer()

if err := r.run(ctx, runOptions{stdout: b}, r.chainCmd.QueryTxEventsCommand(query)); err != nil {
if err := r.run(ctx, runOptions{stdout: b}, option...); err != nil {
return nil, err
}

Expand Down
11 changes: 11 additions & 0 deletions ignite/pkg/cosmosfaucet/cosmosfaucet.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

chaincmdrunner "github.com/ignite/cli/ignite/pkg/chaincmd/runner"
"github.com/ignite/cli/ignite/pkg/cosmosver"
)

const (
Expand Down Expand Up @@ -63,6 +64,9 @@ type Faucet struct {

// openAPIData holds template data customizations for serving OpenAPI page & spec.
openAPIData openAPIData

// version holds the cosmos-sdk version.
version cosmosver.Version
}

// Option configures the faucetOptions.
Expand Down Expand Up @@ -119,6 +123,13 @@ func OpenAPI(apiAddress string) Option {
}
}

// Version configures the cosmos-sdk version.
func Version(version cosmosver.Version) Option {
return func(f *Faucet) {
f.version = version
}
}

// New creates a new faucet with ccr (to access and use blockchain's CLI) and given options.
func New(ctx context.Context, ccr chaincmdrunner.Runner, options ...Option) (Faucet, error) {
f := Faucet{
Expand Down
20 changes: 16 additions & 4 deletions ignite/pkg/cosmosfaucet/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/ignite/cli/ignite/pkg/chaincmd"
chaincmdrunner "github.com/ignite/cli/ignite/pkg/chaincmd/runner"
"github.com/ignite/cli/ignite/pkg/cosmosver"
)

// transferMutex is a mutex used for keeping transfer requests in a queue so checking account balance and sending tokens is atomic.
Expand All @@ -24,11 +25,22 @@ func (f Faucet) TotalTransferredAmount(ctx context.Context, toAccountAddress, de
return sdkmath.NewInt(0), err
}

events, err := f.runner.QueryTxEvents(ctx,
opts := []chaincmdrunner.EventSelector{
chaincmdrunner.NewEventSelector("message", "sender", fromAccount.Address),
chaincmdrunner.NewEventSelector("transfer", "recipient", toAccountAddress))
if err != nil {
return sdkmath.NewInt(0), err
chaincmdrunner.NewEventSelector("transfer", "recipient", toAccountAddress),
}

var events []chaincmdrunner.Event
if f.version.GTE(cosmosver.StargateFiftyVersion) {
events, err = f.runner.QueryTxByQuery(ctx, opts...)
if err != nil {
return sdkmath.NewInt(0), err
}
} else {
events, err = f.runner.QueryTxByEvents(ctx, opts...)
if err != nil {
return sdkmath.NewInt(0), err
}
}

totalAmount = sdkmath.NewInt(0)
Expand Down
1 change: 1 addition & 0 deletions ignite/pkg/cosmosver/cosmosver.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var (
StargateFortyFourVersion = newVersion("0.44.0-alpha")
StargateFortyFiveThreeVersion = newVersion("0.45.3")
StargateFortySevenTwoVersion = newVersion("0.47.2")
StargateFiftyVersion = newVersion("0.50.0")
)

var (
Expand Down
4 changes: 2 additions & 2 deletions ignite/services/chain/faucet.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import (
"os"
"time"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/pkg/errors"

sdkmath "cosmossdk.io/math"

chainconfig "github.com/ignite/cli/ignite/config/chain"
chaincmdrunner "github.com/ignite/cli/ignite/pkg/chaincmd/runner"
"github.com/ignite/cli/ignite/pkg/cosmosfaucet"
Expand Down Expand Up @@ -82,6 +81,7 @@ func (c *Chain) Faucet(ctx context.Context) (cosmosfaucet.Faucet, error) {
cosmosfaucet.Account(*conf.Faucet.Name, "", ""),
cosmosfaucet.ChainID(id),
cosmosfaucet.OpenAPI(apiAddress),
cosmosfaucet.Version(c.Version),
}

// parse coins to pass to the faucet as coins.
Expand Down