diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index aef1abe..d129e85 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -13,6 +13,6 @@ jobs: - uses: actions/checkout@v4.1.7 - uses: actions/setup-go@v5 with: - go-version: 1.21.0 # The Go version to download (if necessary) and use. + go-version: 1.22.2 # The Go version to download (if necessary) and use. - name: Build CLI run: go install diff --git a/.github/workflows/cov.yml b/.github/workflows/cov.yml index 7cc3d83..d9f4a21 100644 --- a/.github/workflows/cov.yml +++ b/.github/workflows/cov.yml @@ -21,7 +21,7 @@ jobs: - uses: actions/checkout@v4.1.7 - uses: actions/setup-go@v5 with: - go-version: 1.21.0 # The Go version to download (if necessary) and use. + go-version: 1.22.0 # The Go version to download (if necessary) and use. - name: Get Coverage run: | go test ./... -timeout=30m -cover -coverprofile coverage.out diff --git a/.github/workflows/golangci.yml b/.github/workflows/golangci.yml index 91017f4..1781fa0 100644 --- a/.github/workflows/golangci.yml +++ b/.github/workflows/golangci.yml @@ -17,7 +17,7 @@ jobs: steps: - uses: actions/setup-go@v5 with: - go-version: 1.21.0 + go-version: 1.22.2 - uses: actions/checkout@v4.1.7 - name: golangci-lint uses: golangci/golangci-lint-action@v6 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index b067aeb..1fe3b1e 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -20,7 +20,7 @@ jobs: - uses: actions/checkout@v4.1.7 - uses: actions/setup-go@v5 with: - go-version: 1.20.2 # The Go version to download (if necessary) and use. + go-version: 1.22.2 # The Go version to download (if necessary) and use. - name: Build CLI shell: bash run: | diff --git a/.github/workflows/test-unit.yml b/.github/workflows/test-unit.yml index 269ac87..2d45a59 100644 --- a/.github/workflows/test-unit.yml +++ b/.github/workflows/test-unit.yml @@ -13,7 +13,7 @@ jobs: - uses: actions/checkout@v4.1.7 - uses: actions/setup-go@v5 with: - go-version: 1.20.2 # The Go version to download (if necessary) and use. + go-version: 1.22.2 # The Go version to download (if necessary) and use. - name: Test shell: bash run: make test-unit diff --git a/api/client.go b/api/client.go new file mode 100644 index 0000000..5b84218 --- /dev/null +++ b/api/client.go @@ -0,0 +1,70 @@ +package api + +import ( + "context" + "net/http" + + "github.com/JackalLabs/sequoia/api/types" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/desmos-labs/cosmos-go-wallet/client" + storageTypes "github.com/jackalLabs/canine-chain/v4/x/storage/types" + "github.com/rs/zerolog/log" +) + +func SpaceHandler(c *client.Client, address string) func(http.ResponseWriter, *http.Request) { + return func(w http.ResponseWriter, req *http.Request) { + queryClient := storageTypes.NewQueryClient(c.GRPCConn) + + params := &storageTypes.QueryProvider{ + Address: address, + } + res, err := queryClient.Provider(context.Background(), params) + if err != nil { + v := types.ErrorResponse{ + Error: err.Error(), + } + w.WriteHeader(http.StatusInternalServerError) + err = json.NewEncoder(w).Encode(v) + if err != nil { + log.Error().Err(err) + } + return + } + + totalSpace := res.Provider.Totalspace + + fsparams := &storageTypes.QueryFreeSpace{ + Address: address, + } + fsres, err := queryClient.FreeSpace(context.Background(), fsparams) + if err != nil { + v := types.ErrorResponse{ + Error: err.Error(), + } + w.WriteHeader(http.StatusInternalServerError) + err = json.NewEncoder(w).Encode(v) + if err != nil { + log.Error().Err(err) + } + return + } + + freeSpace := fsres.Space + + ttint, ok := sdk.NewIntFromString(totalSpace) + if !ok { + return + } + + v := types.SpaceResponse{ + Total: ttint.Int64(), + Free: freeSpace, + Used: ttint.Int64() - freeSpace, + } + + err = json.NewEncoder(w).Encode(v) + if err != nil { + log.Error().Err(err) + } + } +} diff --git a/api/server.go b/api/server.go index f807d21..fc24679 100644 --- a/api/server.go +++ b/api/server.go @@ -48,7 +48,9 @@ func (a *API) Serve(f *file_system.FileSystem, p *proofs.Prover, wallet *wallet. r.HandleFunc("/download/{merkle}", DownloadFileHandler(f)) r.HandleFunc("/list", ListFilesHandler(f)) + r.HandleFunc("/api/client/list", ListFilesHandler(f)) r.HandleFunc("/api/data/fids", LegacyListFilesHandler(f)) + r.HandleFunc("/api/client/space", SpaceHandler(wallet.Client, wallet.AccAddress())) r.HandleFunc("/ipfs/peers", IPFSListPeers(f)) r.HandleFunc("/ipfs/hosts", IPFSListHosts(f)) diff --git a/api/types/responses.go b/api/types/responses.go index 353d10f..1bae2ac 100644 --- a/api/types/responses.go +++ b/api/types/responses.go @@ -22,6 +22,12 @@ type VersionResponse struct { ChainID string `json:"chain-id"` } +type SpaceResponse struct { + Total int64 `json:"total_space"` + Used int64 `json:"used_space"` + Free int64 `json:"free_space"` +} + type NetworkResponse struct { GRPCStatus string `json:"grpc-status"` RPCStatus *coretypes.ResultStatus `json:"rpc-status"`