Skip to content

albertopeam/cosmos-sdk-swift

Repository files navigation

Cosmos SDK Swift

Cosmos SDK makes usage of protobuf and gRPC to define its APIs.

Using BSR and protoc(optional) we can easily generate Swift code to interact with the Cosmos blockchains. This repository contains only generated code.

Versions

Package protoc-gen-swift protoc-gen-grpc-swift name version Commit hash
1.0.0 1.21.0 1.14.0 cosmos 0.46.11 4074ba39c09e4b4799271a56a053227c
osmosis 12.1.0 81ded5dc0f8f46d8b93b42b66f8b5fdf
evmos 11.0.2 c66c6f141d044e31a4ae77f5b852bcb5
band protocol 2.4.1 2d59c56

Cosmos endpoints

  • Mintscan provides info about the public grpc endpoints available.

Code generation

Custom generation

The script generate-buf-local.sh is ready to make code generation using buf. It downloads and generates code into Sources/CosmosSDK. To regenerate the cosmos-sdk for a custom version, update the property cosmos_commit_hash with the target cosmos-sdk commit hash and run the script.

How code generation works

Tools(Optional)

This step is optional and depends if you want to use remote plugins(Check Remote BSR Plugins)

  1. Protobuf compiler brew install protobuf Github
    • Make sure that protobuf is on the system path protoc --version
  2. Swift protobuf plugin brew install swift-protobuf. Github
    • Make sure that swift protobuf plugin is on the system path protoc-gen-swift --version
  3. Swift gRPC plugin brew install grpc-swift. Github
    • Make sure that gRPC swift protobuf plugin is on the system path protoc-gen-grpc-swift --version

Cosmos code generation procedure

  1. COSMOS-SDK PROTO DOC. Cosmos uses BSR to manage their API.
  2. BSR: Buf Schema Registry
  3. COSMOS SDK PROTOS CODE GENERATION
    • Pick a SDK version commit to download the protos
    • Code generation can be done with local protoc tools or with remote protoc tools:
      • Local tools:
        • Install optional tools
        • Buf export + Buf generate
          • Export a specific version of a remote module to a local directory. buf export buf.build/cosmos/cosmos-sdk:${commit} --output .
          • For Cosmos v0.46.x buf export buf.build/cosmos/cosmos-sdk:8cb30a2c4de74dc9bd8d260b1e75e176 --output .
        • Create a yaml named buf-local.gen.yaml and configure it to generate the swift code for protos and gRPC.
          • Check that the swift-protobuf and grpc-swift versions installed are the same than the libraries that the CosmosSDK package depends on.
            • protoc-gen-swift --version
            • protoc-gen-grpc-swift --version
          • Configure yaml to run swift-protobuf and grpc-swift. Checkout Buf generate for more info
        • Buf generate
          • Generate code with local plugins buf generate --template buf-local.gen.yaml --verbose .
      • Remote tools:
    • Code generation script. Using the generate-cosmos-buf-local.sh script the cosmos sdk will be generated using local tools and bsr
      • Change the property cosmos_commit_hash in the script to update the commit from where the code is being generated.
      • Run the script and the code will be generated on Sources/CosmosSDK

Osmosis code generation procedure

  1. Search osmosis labs on (bsr registry)[https://buf.build/osmosis-labs/osmosis].
  2. Pick a commit (bsr registry commits)[https://buf.build/osmosis-labs/osmosis/commits/], 81ded5dc0f8f46d8b93b42b66f8b5fdf
  3. Replace commit hash on generate-osmosis-buf-local.sh and run it

gCurl

  • List available grpc enpoints grpcurl -plaintext cosmos-grpc.polkachu.com:14990 list
  • List available grpc methods for a service grpcurl -plaintext cosmos-grpc.polkachu.com:14990 list cosmos.auth.v1beta1.Query
  • Describe available grpc methods for a servicegrpcurl -plaintext cosmos-grpc.polkachu.com:14990 describe cosmos.auth.v1beta1.Query

WIP