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/proto generation #505

Merged
merged 9 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
"scripts": {
"postinstall": "node etc/bootstrapEnv",
"build": "node etc/bootstrapEnv && lerna run build",
"generate:core-proto-ts": "./proto/core/gen.sh",
"publish:core-proto-ts": "./proto/core/publish.sh",
"generate:indexer-proto-ts": "./proto/indexer/gen.sh",
"publish:indexer-proto-ts": "./proto/indexer/publish.sh",
"build:docs": "yarn typedoc",
"lerna:publish:prerelease": "lerna publish --conventional-commits --conventional-prerelease",
"lerna:publish": "lerna publish patch",
Expand Down
6 changes: 6 additions & 0 deletions proto/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
indexer/proto/
indexer/gen/
indexer/proto-ts/
core/gen/
core/proto/
core/proto-ts
38 changes: 38 additions & 0 deletions proto/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 🌟 TS Proto Generation

Within this folder we have the logic to generate proto definitions from the `injective-core` and `injective-indexer` repos. Within the `core/gen.sh` and `indexer/gen.sh` files, you can find the branches variables and change them as per your need. Then, you can simply just run `yarn generate:core-proto-ts` or `yarn generate:indexer-proto-ts` to generate the TypeScript proto definitions and finally, publish them using `yarn publish:core-proto-ts` or `yarn publish:indexer-proto-ts`

---

## 📜 Contribution

General contribution guidelines can be found on the [Contributing](CONTRIBUTING.md) page. There can be a package-specific contribution guides, please make sure you read them before you open a PR.

---

## ⛑ Support

Reach out to us at one of the following places!

- Website at <a href="https://injective.com" target="_blank">`injective.com`</a>
- Twitter at <a href="https://twitter.com/Injective_" target="_blank">`@Injective`</a>
- Discord at <a href="https://discord.com/invite/NK4qdbv" target="_blank">`Discord`</a>
- Telegram at <a href="https://t.me/joininjective" target="_blank">`Telegram`</a>

---

## 🔓 License

Copyright © 2021 - 2022 Injective Labs Inc. (https://injectivelabs.org/)

<a href="https://iili.io/mNneZN.md.png"><img src="https://iili.io/mNneZN.md.png" style="width: 300px; max-width: 100%; height: auto" />

Originally released by Injective Labs Inc. under: <br />
Apache License <br />
Version 2.0, January 2004 <br />
http://www.apache.org/licenses/

<p>&nbsp;</p>
<div align="center">
<sub><em>Powering the future of decentralized finance.</em></sub>
</div>
147 changes: 147 additions & 0 deletions proto/core/gen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#!/usr/bin/env bash
set -eo pipefail

ROOT_DIR=./proto/core
BUILD_DIR=$ROOT_DIR/gen
PROTO_DIR=$ROOT_DIR/proto
TS_OUTPUT_DIR=$ROOT_DIR/proto-ts
TS_STUB_DIR=$ROOT_DIR/stub
TS_PROTO_TEMPLATE=$PROTO_DIR/buf.gen.ts.yaml
bangjelkoski marked this conversation as resolved.
Show resolved Hide resolved

# remote branches/tags
injective_core_branch=master
cosmos_sdk_branch=v0.50.x-inj
wasmd_branch=v0.50.x-inj
ibc_go_branch=v8.3.2

# remove old gen
rm -rf $BUILD_DIR
rm -rf $PROTO_DIR
rm -rf $TS_OUTPUT_DIR
bangjelkoski marked this conversation as resolved.
Show resolved Hide resolved

# BSD rate limit
counter=0
bangjelkoski marked this conversation as resolved.
Show resolved Hide resolved

########################################
######## TS PROTO GENERATION ###########
########################################
echo "Generating TS proto code..."

## Remove/setup old build dir
mkdir -p $BUILD_DIR
mkdir -p $PROTO_DIR
mkdir -p $PROTO_DIR/proto
mkdir -p $TS_OUTPUT_DIR
mkdir -p $TS_OUTPUT_DIR/cjs
mkdir -p $TS_OUTPUT_DIR/esm
mkdir -p $TS_OUTPUT_DIR/proto

########################################
###### REMOTE PROTO DEFINITIONS ########
########################################

## Clone current proto definitions from core
git clone https://github.com/InjectiveLabs/injective-core.git $BUILD_DIR/injective-core -b $injective_core_branch --depth 1 --single-branch > /dev/null
cp -r $BUILD_DIR/injective-core/proto/injective $PROTO_DIR
bangjelkoski marked this conversation as resolved.
Show resolved Hide resolved

## Third Party proto definitions
git clone https://github.com/InjectiveLabs/cosmos-sdk.git $BUILD_DIR/cosmos-sdk -b $cosmos_sdk_branch --depth 1 --single-branch > /dev/null
git clone https://github.com/InjectiveLabs/wasmd $BUILD_DIR/wasmd -b $wasmd_branch --depth 1 --single-branch > /dev/null
git clone https://github.com/InjectiveLabs/ibc-go $BUILD_DIR/ibc-go -b $ibc_go_branch --depth 1 --single-branch > /dev/null

buf export $BUILD_DIR/cosmos-sdk --output=$PROTO_DIR
buf export $BUILD_DIR/wasmd --exclude-imports --output=$PROTO_DIR
buf export $BUILD_DIR/ibc-go --exclude-imports --output=$PROTO_DIR
buf export https://github.com/cosmos/gogoproto.git --exclude-imports --output=$PROTO_DIR
buf export https://github.com/cometbft/cometbft.git --exclude-imports --output=$PROTO_DIR
buf export https://github.com/cosmos/ics23.git --exclude-imports --output=$PROTO_DIR
buf export https://github.com/cosmos/ibc-apps.git --exclude-imports --output=$PROTO_DIR --path=middleware/packet-forward-middleware/proto/packetforward/v1

# generate TS proto
proto_dirs=$(find $PROTO_DIR -path -prune -o -name '*.proto' -print0 | xargs -0 -n1 dirname | sort | uniq)

# gen using ts-proto
npm --prefix $ROOT_DIR install
for dir in $proto_dirs; do
protoc \
--plugin="$ROOT_DIR/node_modules/.bin/protoc-gen-ts_proto" \
--ts_proto_opt="esModuleInterop=true" \
--ts_proto_opt="forceLong=string" \
--ts_proto_opt="env=both" \
--ts_proto_opt="useExactTypes=false" \
--ts_proto_opt="outputClientImpl=grpc-web" \
--ts_proto_out="$TS_OUTPUT_DIR/proto" \
-I "$PROTO_DIR" \
$(find "${dir}" -maxdepth 1 -name '*.proto')
bangjelkoski marked this conversation as resolved.
Show resolved Hide resolved
done


#######################################
###### NPM PACKAGES GENERATION #######
#######################################

echo "Compiling npm packages..."

## 1. Replace package with our own fork
search1="@improbable-eng/grpc-web"
replace1="@injectivelabs/grpc-web"

FILES=$( find $TS_OUTPUT_DIR -type f )

for file in $FILES
do
sed -ie "s/${search1//\//\\/}/${replace1//\//\\/}/g" "$file"
done

## 2. Replace extension type to ignore on compile time
search1="getExtension():"
replace1="// @ts-ignore \n getExtension():"
search2="setExtension("
replace2="// @ts-ignore \n setExtension("

FILES=$( find $TS_OUTPUT_DIR -type f -name '*.d.ts' )

for file in $FILES
do
sed -ie "s/${search1//\//\\/}/${replace1//\//\\/}/g" "$file"
sed -ie "s/${search2//\//\\/}/${replace2//\//\\/}/g" "$file"
done

ESM_PKG_TEMPLATE=$TS_STUB_DIR/package.json.esm.template
ESM_CFG_TEMPLATE=$TS_STUB_DIR/tsconfig.json.esm.template
CJS_PKG_TEMPLATE=$TS_STUB_DIR/package.json.cjs.template
CJS_CFG_TEMPLATE=$TS_STUB_DIR/tsconfig.json.cjs.template

## 3. Compile TypeScript for ESM package
cp $TS_STUB_DIR/index.ts.template $TS_OUTPUT_DIR/proto/index.ts

### ESM
cp $ESM_PKG_TEMPLATE $TS_OUTPUT_DIR/proto/package.json
cp $ESM_CFG_TEMPLATE $TS_OUTPUT_DIR/proto/tsconfig.json
npm --prefix $TS_OUTPUT_DIR/proto install
npm --prefix $TS_OUTPUT_DIR/proto run gen
cp $ESM_PKG_TEMPLATE $TS_OUTPUT_DIR/esm/package.json

## CJS
cp $CJS_PKG_TEMPLATE $TS_OUTPUT_DIR/proto/package.json
cp $CJS_CFG_TEMPLATE $TS_OUTPUT_DIR/proto/tsconfig.json
npm --prefix $TS_OUTPUT_DIR/proto install
npm --prefix $TS_OUTPUT_DIR/proto run gen
cp $CJS_PKG_TEMPLATE $TS_OUTPUT_DIR/cjs/package.json

## 4. Setup proper package.json for core-proto-ts packages
cp $TS_STUB_DIR/package.json.core-proto-ts.template $TS_OUTPUT_DIR/package.json

## CJS DEPRECATED
# mkdir -p $TS_OUTPUT_DIR/chain-api
# cp $TS_STUB_DIR/package.json.chain-api.template $TS_OUTPUT_DIR/chain-api/package.json

# 5. Clean up folders
rm -rf $BUILD_DIR
rm -rf $PROTO_DIR
rm -rf $TS_OUTPUT_DIR/proto
find $TS_OUTPUT_DIR -name "*.jse" -type f -delete
find $TS_OUTPUT_DIR -name "*.tse" -type f -delete
find $TS_OUTPUT_DIR -name "*.jsone" -type f -delete

echo "Done!"
Loading