Skip to content

Commit

Permalink
add fee abstraction app
Browse files Browse the repository at this point in the history
  • Loading branch information
Pantani committed Aug 28, 2024
1 parent e0194ef commit 09e9afb
Show file tree
Hide file tree
Showing 14 changed files with 2,297 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app.ignite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ apps:
explorer:
description: Easy to use terminal chain explorer for testing your Ignite blockchains
path: ./explorer
fee-abstraction:
description: Integrate the fee abstraction module from osmosis-labs to make it easy for new chains to accept the currencies of existing chains
path: ./fee-abstraction
hermes:
description: A wrapper around the hermes IBC relayer
path: ./hermes
Expand Down
136 changes: 136 additions & 0 deletions fee-abstraction/cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package cmd

import (
"os"
"path/filepath"
"sort"
"strings"

"github.com/ignite/cli/v28/ignite/pkg/cliui/colors"
"github.com/ignite/cli/v28/ignite/pkg/xgenny"
"github.com/ignite/cli/v28/ignite/services/chain"
"github.com/ignite/cli/v28/ignite/services/plugin"
)

const (
flagVersion = "fee-abstraction-version"
flagFeeAbsModule = "fee-abstraction"
flagPath = "path"
flagHome = "home"

statusScaffolding = "Scaffolding..."

defaultFeeAbsVersion = "v8.0.2"
)

var (
modifyPrefix = colors.Modified("modify ")
createPrefix = colors.Success("create ")
removePrefix = func(s string) string {
return strings.TrimPrefix(strings.TrimPrefix(s, modifyPrefix), createPrefix)
}
)

// GetCommands returns the list of fee-abstraction app commands.
func GetCommands() []*plugin.Command {
return []*plugin.Command{
{
Use: "fee-abstraction",
Short: "Integrate the fee abstraction module from osmosis-labs",
Long: "Integrate the fee abstraction module from osmosis-labs to make it easy for new chains to accept the currencies of existing chains",
},
}
}

// GetHooks returns the list of fee-abstraction app hooks.
func GetHooks() []*plugin.Hook {
return []*plugin.Hook{
{
Name: "scaffold-chain",
PlaceHookOn: "ignite scaffold chain",
Flags: []*plugin.Flag{
{
Name: flagFeeAbsModule,
Usage: "Create a project that includes the fee abstraction module",
DefaultValue: "false",
Type: plugin.FlagTypeBool,
},

{
Name: flagVersion,
Usage: "fee abstraction semantic version",
Shorthand: "v",
DefaultValue: defaultFeeAbsVersion,
Type: plugin.FlagTypeString,
},
},
},
}
}

func getPath(flags plugin.Flags) string {
path, _ := flags.GetString(flagPath)
return path
}

func getVersion(flags plugin.Flags) string {
version, _ := flags.GetString(flagVersion)
version = strings.Replace(version, "v", "", 1)
return version
}

// newChain create new *chain.Chain with home and path flags.
func newChain(chainFolder string, flags plugin.Flags, chainOption ...chain.Option) (*chain.Chain, error) {
appPath := getPath(flags)
absPath, err := filepath.Abs(appPath)
if err != nil {
return nil, err
}
absPath = filepath.Join(absPath, chainFolder)
return chain.New(absPath, chainOption...)
}

// sourceModificationToString output the modifications into a readable text.
func sourceModificationToString(sm xgenny.SourceModification) (string, error) {
// get file names and add prefix
var files []string
for _, modified := range sm.ModifiedFiles() {
// get the relative app path from the current directory
relativePath, err := relativePath(modified)
if err != nil {
return "", err
}
files = append(files, modifyPrefix+relativePath)
}
for _, created := range sm.CreatedFiles() {
// get the relative app path from the current directory
relativePath, err := relativePath(created)
if err != nil {
return "", err
}
files = append(files, createPrefix+relativePath)
}

// sort filenames without prefix
sort.Slice(files, func(i, j int) bool {
s1 := removePrefix(files[i])
s2 := removePrefix(files[j])

return strings.Compare(s1, s2) == -1
})

return "\n" + strings.Join(files, "\n"), nil
}

// relativePath return the relative app path from the current directory.
func relativePath(appPath string) (string, error) {
pwd, err := os.Getwd()
if err != nil {
return "", err
}
path, err := filepath.Rel(pwd, appPath)
if err != nil {
return "", err
}
return path, nil
}
65 changes: 65 additions & 0 deletions fee-abstraction/cmd/hook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package cmd

import (
"context"

"github.com/blang/semver/v4"
"github.com/ignite/cli/v28/ignite/pkg/cliui"
"github.com/ignite/cli/v28/ignite/pkg/gomodulepath"
"github.com/ignite/cli/v28/ignite/pkg/placeholder"
"github.com/ignite/cli/v28/ignite/services/chain"
"github.com/ignite/cli/v28/ignite/services/plugin"

"github.com/ignite/apps/fee-abstraction/services/scaffolder"
)

// ExecuteScaffoldChainHook executes the scaffold chain hook.
func ExecuteScaffoldChainHook(ctx context.Context, h *plugin.ExecutedHook, api plugin.ClientAPI) error {
var (
flags = plugin.Flags(h.Hook.Flags)
feeAbsModule, _ = flags.GetBool(flagFeeAbsModule)
name = h.ExecutedCommand.Args[0]
)
if !feeAbsModule {
return nil
}

session := cliui.New(cliui.StartSpinnerWithText(statusScaffolding))
defer session.End()

pathInfo, err := gomodulepath.Parse(name)
if err != nil {
return err
}

version := getVersion(flags)
semVersion, err := semver.Parse(version)
if err != nil {
return err
}

c, err := newChain(pathInfo.Root, flags, chain.WithOutputer(session), chain.CollectEvents(session.EventBus()))
if err != nil {
return err
}

sc, err := scaffolder.New(c, session)
if err != nil {
return err
}

sm, err := sc.AddFeeAbstraction(ctx, placeholder.New(), scaffolder.WithVersion(semVersion))
if err != nil {
return err
}

modificationsStr, err := sourceModificationToString(sm)
if err != nil {
return err
}

session.Println(modificationsStr)
session.Printf("\n🎉 Fee Abstraction added (`%[1]v`).\n\n", c.AppPath())

return nil
}
Loading

0 comments on commit 09e9afb

Please sign in to comment.