From b5bce1b4a3967adaaa79a1ed3fdc5e645fef3900 Mon Sep 17 00:00:00 2001 From: Keefer Taylor | Tessellated Date: Mon, 30 Oct 2023 16:46:23 -0400 Subject: [PATCH] Add CLI Commands and Makefile (#7) * Install commands for building * remove main.go * update icon --- Makefile | 72 ++++++++++++--------------------- bursar/bursar.go | 4 +- cmd/cmd.go | 10 ++--- cmd/paymaster/cmd/root.go | 29 ++++++++++++++ cmd/paymaster/cmd/start.go | 22 ++++++++++ cmd/paymaster/cmd/version.go | 39 ++++++++++++++++++ cmd/paymaster/main.go | 10 +++++ go.mod | 2 +- main.go | 78 ------------------------------------ server/server.go | 6 +-- skip/api-client.go | 2 +- tracker/addresses_test.go | 2 +- 12 files changed, 138 insertions(+), 138 deletions(-) create mode 100644 cmd/paymaster/cmd/root.go create mode 100644 cmd/paymaster/cmd/start.go create mode 100644 cmd/paymaster/cmd/version.go create mode 100644 cmd/paymaster/main.go delete mode 100644 main.go diff --git a/Makefile b/Makefile index 2a25a13..d4dd40f 100644 --- a/Makefile +++ b/Makefile @@ -2,53 +2,33 @@ DOCKER := $(shell which docker) -CURRENT_DIR = $(shell pwd) BUILDDIR ?= $(CURDIR)/build -BUILD_FLAGS := -tags "$(build_tags)" -ldflags '$(ldflags)' -# check for nostrip option -ifeq (,$(findstring nostrip,$(COSMOS_BUILD_OPTIONS))) - BUILD_FLAGS += -trimpath -endif +BRANCH := $(shell git rev-parse --abbrev-ref HEAD) +COMMIT := $(shell git log -1 --format='%H') -# Check for debug option -ifeq (debug,$(findstring debug,$(COSMOS_BUILD_OPTIONS))) - BUILD_FLAGS += -gcflags "all=-N -l" +DIRTY := -dirty +ifeq (,$(shell git status --porcelain)) + DIRTY := endif -############################################################################### -### Protobuf ### -############################################################################### - -protoImageName=proto-genc -protoImage=$(DOCKER) run --rm -v $(CURDIR):/workspace --workdir /workspace/proto $(protoImageName) - -proto-all: proto-build-docker proto-format proto-lint make proto-format proto-update-deps proto-gen - -proto-build-docker: - @echo "Building Docker Container '$(protoImageName)' for Protobuf Compilation" - @docker build -t $(protoImageName) -f ./proto/Dockerfile . - -proto-gen: - @echo "Generating Protobuf Files" - @$(protoImage) sh -c "cd .. && sh ./scripts/protocgen.sh" +VERSION := $(shell git describe --tags --exact-match 2>/dev/null) +# if VERSION is empty, then populate it with branch's name and raw commit hash +ifeq (,$(VERSION)) + VERSION := $(BRANCH)-$(COMMIT) +endif -proto-format: - @echo "Formatting Protobuf Files with Clang" - @$(protoImage) find ./ -name "*.proto" -exec clang-format -i {} \; +VERSION := $(VERSION)$(DIRTY) -proto-lint: - @echo "Linting Protobuf Files With Buf" - @$(protoImage) buf lint +GIT_REVISION := $(shell git rev-parse HEAD)$(DIRTY) -proto-check-breaking: - @$(protoImage) buf breaking --against $(HTTPS_GIT)#branch=main +GO_SYSTEM_VERSION = $(shell go version | cut -c 14- | cut -d' ' -f1 | cut -d'.' -f1-2) -proto-update-deps: - @echo "Updating Protobuf dependencies" - @$(protoImage) buf mod update +ldflags= -X github.com/tessellated-io/paymaster/cmd/paymaster/cmd.PaymasterVersion=${VERSION} \ + -X github.com/tessellated-io/paymaster/cmd/paymaster/cmd.GitRevision=${GIT_REVISION} \ + -X github.com/tessellated-io/paymaster/cmd/paymaster/cmd.GoVersion=${GO_SYSTEM_VERSION} -.PHONY: proto-all proto-gen proto-format proto-lint proto-check-breaking proto-update-deps +BUILD_FLAGS := -tags "$(build_tags)" -ldflags '$(ldflags)' ############################################################################### ### Build ### @@ -56,19 +36,17 @@ proto-update-deps: BUILD_TARGETS := build -build: BUILD_ARGS= - -build-linux-amd64: - @GOOS=linux GOARCH=amd64 LEDGER_ENABLED=false $(MAKE) build +build: + mkdir -p $(BUILDDIR)/ + go build -mod=readonly -ldflags '$(ldflags)' -trimpath -o $(BUILDDIR) ./...; -build-linux-arm64: - @GOOS=linux GOARCH=arm64 LEDGER_ENABLED=false $(MAKE) build +install: go.sum + go install $(BUILD_FLAGS) ./cmd/paymaster -$(BUILD_TARGETS): go.sum $(BUILDDIR)/ - @cd ${CURRENT_DIR} && go $@ -mod=readonly $(BUILD_FLAGS) $(BUILD_ARGS) ./... +clean: + rm -rf $(BUILDDIR)/* -$(BUILDDIR)/: - @mkdir -p $(BUILDDIR)/ +.PHONY: build ############################################################################### ### Tools & Dependencies ### diff --git a/bursar/bursar.go b/bursar/bursar.go index eb9fc87..e17bc98 100644 --- a/bursar/bursar.go +++ b/bursar/bursar.go @@ -4,8 +4,8 @@ import ( "fmt" "time" - "github.com/tessellated-io/mail-in-rebates/paymaster/crypto" - "github.com/tessellated-io/mail-in-rebates/paymaster/skip" + "github.com/tessellated-io/paymaster/crypto" + "github.com/tessellated-io/paymaster/skip" "github.com/tessellated-io/pickaxe/arrays" "github.com/tessellated-io/pickaxe/chains" "github.com/tessellated-io/pickaxe/coding" diff --git a/cmd/cmd.go b/cmd/cmd.go index 0196dd4..5670ae6 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -2,11 +2,11 @@ package cmd import ( "github.com/spf13/cobra" - "github.com/tessellated-io/mail-in-rebates/paymaster/bursar" - "github.com/tessellated-io/mail-in-rebates/paymaster/codec" - "github.com/tessellated-io/mail-in-rebates/paymaster/server" - "github.com/tessellated-io/mail-in-rebates/paymaster/skip" - "github.com/tessellated-io/mail-in-rebates/paymaster/tracker" + "github.com/tessellated-io/paymaster/bursar" + "github.com/tessellated-io/paymaster/codec" + "github.com/tessellated-io/paymaster/server" + "github.com/tessellated-io/paymaster/skip" + "github.com/tessellated-io/paymaster/tracker" "github.com/tessellated-io/pickaxe/chains" pacrypto "github.com/tessellated-io/pickaxe/crypto" ) diff --git a/cmd/paymaster/cmd/root.go b/cmd/paymaster/cmd/root.go new file mode 100644 index 0000000..8d39042 --- /dev/null +++ b/cmd/paymaster/cmd/root.go @@ -0,0 +1,29 @@ +/* +Copyright © 2023 Tessellated +*/ +package cmd + +import ( + "os" + + "github.com/spf13/cobra" +) + +// rootCmd represents the base command when called without any subcommands +var rootCmd = &cobra.Command{ + Use: "paymaster", + Short: "Paymaster automatically tops of balances of addresses on the interchain.", + // TODO: Consider long form here +} + +// Execute adds all child commands to the root command and sets flags appropriately. +// This is called by main.main(). It only needs to happen once to the rootCmd. +func Execute() { + err := rootCmd.Execute() + if err != nil { + os.Exit(1) + } +} + +func init() { +} diff --git a/cmd/paymaster/cmd/start.go b/cmd/paymaster/cmd/start.go new file mode 100644 index 0000000..c84d8fd --- /dev/null +++ b/cmd/paymaster/cmd/start.go @@ -0,0 +1,22 @@ +/* +Copyright © 2023 Tessellated +*/ +package cmd + +import ( + "github.com/spf13/cobra" +) + +// startCmd represents the start command +var startCmd = &cobra.Command{ + Use: "start", + Short: "Start paymaster", + Long: `Starts paymaster with the given configuration.`, + Run: func(cmd *cobra.Command, args []string) { + panic("TODO: Implement me") + }, +} + +func init() { + rootCmd.AddCommand(startCmd) +} diff --git a/cmd/paymaster/cmd/version.go b/cmd/paymaster/cmd/version.go new file mode 100644 index 0000000..0aea097 --- /dev/null +++ b/cmd/paymaster/cmd/version.go @@ -0,0 +1,39 @@ +/* +Copyright © 2023 Tessellated +*/ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +// Binary name +const ( + binaryName = "paymaster" + binaryIcon = "💸" +) + +// Version +var ( + PaymasterVersion string + GoVersion string + GitRevision string +) + +// versionCmd represents the version command +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Display the current version of paymaster", + Run: func(cmd *cobra.Command, args []string) { + fmt.Printf("%s %s:\n", binaryIcon, binaryName) + fmt.Printf(" - Version: %s\n", PaymasterVersion) + fmt.Printf(" - Git Revision: %s\n", GitRevision) + fmt.Printf(" - Go Version: %s\n", GoVersion) + }, +} + +func init() { + rootCmd.AddCommand(versionCmd) +} diff --git a/cmd/paymaster/main.go b/cmd/paymaster/main.go new file mode 100644 index 0000000..fb79829 --- /dev/null +++ b/cmd/paymaster/main.go @@ -0,0 +1,10 @@ +/* +Copyright © 2023 Tessellated +*/ +package main + +import "github.com/tessellated-io/paymaster/cmd/paymaster/cmd" + +func main() { + cmd.Execute() +} diff --git a/go.mod b/go.mod index d1c1ed1..3de05da 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/tessellated-io/mail-in-rebates/paymaster +module github.com/tessellated-io/paymaster go 1.20 diff --git a/main.go b/main.go deleted file mode 100644 index 39fd5d5..0000000 --- a/main.go +++ /dev/null @@ -1,78 +0,0 @@ -package main - -import ( - "encoding/base64" - "fmt" - - "github.com/tessellated-io/mail-in-rebates/paymaster/codec" - "github.com/tessellated-io/mail-in-rebates/paymaster/crypto" - "github.com/tessellated-io/mail-in-rebates/paymaster/skip" - "github.com/tessellated-io/pickaxe/chains" - pacrypto "github.com/tessellated-io/pickaxe/crypto" - "github.com/tessellated-io/pickaxe/tx" - - sdk "github.com/cosmos/cosmos-sdk/types" - txtypes "github.com/cosmos/cosmos-sdk/types/tx" -) - -func main() { - cdc := codec.GetCodec() - - // Get data from Skip API - offlineRegistry := chains.NewOfflineChainRegistry() - skipClient := skip.NewSkipClient(offlineRegistry, cdc) - - pubKeyBytes, _ := base64.StdEncoding.DecodeString("AtUYI/oVCa6LV2k6uXPF1u483KrICYZlOQhfer+weTu1") - pubKey := crypto.Secp256k1PublicKeyFromBytes(pubKeyBytes) - - sourceChainID := "cosmoshub-4" - senderAdddress := "cosmos15qth07rmamcue638q4fvzfrg9ra6eykngvzzd2" - ibcXferMessage, err := skipClient.GetMessages( - senderAdddress, - pubKey, - "axelar1jw7a28g98q3e7ul9f78cuzxnaw67dax8znaz9s", - "100000", - "uatom", - sourceChainID, - "uaxl", - "axelar-dojo-1", - ) - if err != nil { - panic(err) - } - - // Get a signer - mnemonic := "TODO" - keyPair := pacrypto.NewCosmosKeyPairFromMnemonic(mnemonic) - - grpcRes, err := tx.SendMessages( - []sdk.Msg{ibcXferMessage}, - "cosmos", - sourceChainID, - cdc, - keyPair, - txtypes.BroadcastMode_BROADCAST_MODE_SYNC, - "cosmos-validator.tessageo.net:9090", - 1.1, - 0.0, // TODO - "TODO", - ) - if err != nil { - panic(err) - } - - fmt.Printf("Codespace: %s \n", grpcRes.TxResponse.Codespace) - fmt.Printf("Info: %s\n", grpcRes.TxResponse.Info) - fmt.Printf("Info: %s\n", grpcRes.TxResponse.TxHash) - fmt.Printf("Code: %d\n", grpcRes.TxResponse.Code) // Should be `0` if the tx is successful - fmt.Printf("Logs: %s\n", grpcRes.TxResponse.Logs) - - if grpcRes.TxResponse.Code == 0 { - - fmt.Println("🎉 🎉 🎉") - fmt.Printf("🎉 Transaction Sent\n") - fmt.Println("🎉 🎉 🎉") - return - } - fmt.Printf("Failed to send for height %d. Error: %s\n", 123, grpcRes.TxResponse.RawLog) -} diff --git a/server/server.go b/server/server.go index 95b0237..430d517 100644 --- a/server/server.go +++ b/server/server.go @@ -8,9 +8,9 @@ import ( "sync" "time" - "github.com/tessellated-io/mail-in-rebates/paymaster/bursar" - proto "github.com/tessellated-io/mail-in-rebates/paymaster/server/proto" - "github.com/tessellated-io/mail-in-rebates/paymaster/tracker" + "github.com/tessellated-io/paymaster/bursar" + proto "github.com/tessellated-io/paymaster/server/proto" + "github.com/tessellated-io/paymaster/tracker" "google.golang.org/grpc" "google.golang.org/grpc/peer" ) diff --git a/skip/api-client.go b/skip/api-client.go index a4039f7..d290e64 100644 --- a/skip/api-client.go +++ b/skip/api-client.go @@ -8,7 +8,7 @@ import ( "strings" ibctypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types" - "github.com/tessellated-io/mail-in-rebates/paymaster/crypto" + "github.com/tessellated-io/paymaster/crypto" "github.com/tessellated-io/pickaxe/chains" cdc "github.com/cosmos/cosmos-sdk/codec" diff --git a/tracker/addresses_test.go b/tracker/addresses_test.go index 9af6a94..d3f0dd5 100644 --- a/tracker/addresses_test.go +++ b/tracker/addresses_test.go @@ -3,7 +3,7 @@ package tracker_test import ( "testing" - "github.com/tessellated-io/mail-in-rebates/paymaster/tracker" + "github.com/tessellated-io/paymaster/tracker" ) func TestFileWrite(t *testing.T) {