Skip to content

Commit

Permalink
Merge branch 'dev' into refactor/esm-build-and-deprecating-packages
Browse files Browse the repository at this point in the history
  • Loading branch information
bangjelkoski committed Dec 6, 2024
2 parents ad2fe29 + 08dfc49 commit de460fb
Show file tree
Hide file tree
Showing 21 changed files with 1,898 additions and 1 deletion.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
"publish:mito-proto-ts": "./proto/mito/publish.sh",
"generate:olp-proto-ts": "./proto/olp/gen.sh",
"publish:olp-proto-ts": "./proto/olp/publish.sh",
"generate:abacus-proto-ts": "./proto/abacus/gen.sh",
"publish:abacus-proto-ts": "./proto/abacus/publish.sh",
"build:docs": "yarn typedoc",
"lerna:publish:prerelease": "lerna publish --conventional-commits --conventional-prerelease",
"lerna:publish": "lerna publish patch",
Expand Down
3 changes: 2 additions & 1 deletion packages/exceptions/src/exceptions/types/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ export enum IndexerErrorModule {
ChronosSpot = 'indexer-chronos-spot',
ChronosMarkets = 'indexer-chronos-markets',
Campaign = 'indexer-campaign',
Web3Gw = 'web3-gateway'
Web3Gw = 'web3-gateway',
Abacus = 'abacus'
}

export enum WalletErrorActionModule {
Expand Down
1 change: 1 addition & 0 deletions packages/sdk-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
"@injectivelabs/grpc-web-react-native-transport": "^0.0.2",
"@injectivelabs/indexer-proto-ts": "1.13.3",
"@injectivelabs/mito-proto-ts": "1.13.2",
"@injectivelabs/abacus-proto-ts": "1.13.0",
"@injectivelabs/networks": "^1.14.33",
"@injectivelabs/olp-proto-ts": "1.13.1",
"@injectivelabs/ts-types": "^1.14.33",
Expand Down
123 changes: 123 additions & 0 deletions packages/sdk-ts/src/client/abacus/grpc/AbacusGrpcApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import {
IndexerErrorModule,
UnspecifiedErrorCode,
GrpcUnaryRequestException,
} from '@injectivelabs/exceptions'
import { InjectiveAbacusRpc } from '@injectivelabs/abacus-proto-ts'
import BaseGrpcConsumer from '../../base/BaseGrpcConsumer.js'
import { AbacusGrpcTransformer } from './transformers/index.js'

export class AbacusGrpcApi extends BaseGrpcConsumer {
protected module: string = IndexerErrorModule.Abacus

protected client: InjectiveAbacusRpc.PointsSvcClientImpl

constructor(endpoint: string) {
super(endpoint)

this.client = new InjectiveAbacusRpc.PointsSvcClientImpl(
this.getGrpcWebImpl(endpoint),
)
}

async fetchAccountLatestPoints(address: string) {
const request = InjectiveAbacusRpc.PointsLatestForAccountRequest.create()

request.accountAddress = address

try {
const response =
await this.retry<InjectiveAbacusRpc.PointsLatestForAccountResponse>(
() => this.client.PointsLatestForAccount(request),
)

return AbacusGrpcTransformer.grpcPointsLatestToPointsLatest(response)
} catch (e: unknown) {
if (e instanceof InjectiveAbacusRpc.GrpcWebError) {
throw new GrpcUnaryRequestException(new Error(e.toString()), {
code: e.code,
context: 'PointsStatsLatestForAccount',
contextModule: this.module,
})
}

throw new GrpcUnaryRequestException(e as Error, {
code: UnspecifiedErrorCode,
context: 'PointsStatsLatestForAccount',
contextModule: this.module,
})
}
}

async fetchAccountDailyPoints(address: string, daysLimit?: number) {
const request =
InjectiveAbacusRpc.PointsStatsDailyForAccountRequest.create()

request.accountAddress = address

if (daysLimit) {
request.daysLimit = daysLimit
}

try {
const response =
await this.retry<InjectiveAbacusRpc.PointsStatsDailyForAccountResponse>(
() => this.client.PointsStatsDailyForAccount(request),
)

return AbacusGrpcTransformer.grpcPointsStatsDailyToPointsStatsDaily(
response,
)
} catch (e: unknown) {
if (e instanceof InjectiveAbacusRpc.GrpcWebError) {
throw new GrpcUnaryRequestException(new Error(e.toString()), {
code: e.code,
context: 'PointsStatsDailyForAccount',
contextModule: this.module,
})
}

throw new GrpcUnaryRequestException(e as Error, {
code: UnspecifiedErrorCode,
context: 'PointsStatsDailyForAccount',
contextModule: this.module,
})
}
}

async fetchAccountWeeklyPoints(address: string, weeksLimit?: number) {
const request =
InjectiveAbacusRpc.PointsStatsWeeklyForAccountRequest.create()

request.accountAddress = address

if (weeksLimit) {
request.weeksLimit = weeksLimit
}

try {
const response =
await this.retry<InjectiveAbacusRpc.PointsStatsWeeklyForAccountResponse>(
() => this.client.PointsStatsWeeklyForAccount(request),
)

return AbacusGrpcTransformer.grpcPointsStatsWeeklyToPointsStatsWeekly(
response,
)
} catch (e: unknown) {
if (e instanceof InjectiveAbacusRpc.GrpcWebError) {
throw new GrpcUnaryRequestException(new Error(e.toString()), {
code: e.code,
context: 'PointsStatsWeeklyForAccount',
contextModule: this.module,
})
}

throw new GrpcUnaryRequestException(e as Error, {
code: UnspecifiedErrorCode,
context: 'PointsStatsWeeklyForAccount',
contextModule: this.module,
})
}
}
}
3 changes: 3 additions & 0 deletions packages/sdk-ts/src/client/abacus/grpc/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { AbacusGrpcApi } from './AbacusGrpcApi.js'

export { AbacusGrpcApi }
21 changes: 21 additions & 0 deletions packages/sdk-ts/src/client/abacus/grpc/transformers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { InjectiveAbacusRpc } from '@injectivelabs/abacus-proto-ts'

export class AbacusGrpcTransformer {
static grpcPointsStatsDailyToPointsStatsDaily(
response: InjectiveAbacusRpc.PointsStatsDailyForAccountResponse,
) {
return response.field
}

static grpcPointsStatsWeeklyToPointsStatsWeekly(
response: InjectiveAbacusRpc.PointsStatsWeeklyForAccountResponse,
) {
return response.field
}

static grpcPointsLatestToPointsLatest(
response: InjectiveAbacusRpc.PointsLatestForAccountResponse,
) {
return response
}
}
1 change: 1 addition & 0 deletions packages/sdk-ts/src/client/abacus/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './grpc/index.js'
1 change: 1 addition & 0 deletions packages/sdk-ts/src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './chain/index.js'
export * from './indexer/index.js'
export * from './wasm/index.js'
export * from './olp/index.js'
export * from './abacus/index.js'
3 changes: 3 additions & 0 deletions proto/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ olp/proto-ts
mito/gen/
mito/proto/
mito/proto-ts
abacus/gen/
abacus/proto/
abacus/proto-ts
127 changes: 127 additions & 0 deletions proto/abacus/gen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
ROOT_DIR=./proto/abacus
BUILD_DIR=$ROOT_DIR/gen
PROTO_DIR=$ROOT_DIR/proto
TS_OUTPUT_DIR=$ROOT_DIR/proto-ts
TS_STUB_DIR=$ROOT_DIR/stub
abacus_branch=main

# remove old gen
rm -rf $BUILD_DIR
rm -rf $TS_OUTPUT_DIR
rm -rf $PROTO_DIR

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

# make dirs
mkdir -p $BUILD_DIR
mkdir -p $TS_OUTPUT_DIR
mkdir -p $PROTO_DIR
mkdir -p $PROTO_DIR/danielvladco
mkdir -p $PROTO_DIR/danielvladco/protobuf
mkdir -p $TS_OUTPUT_DIR/proto
mkdir -p $TS_OUTPUT_DIR/esm
mkdir -p $TS_OUTPUT_DIR/cjs

## Clone current proto definitions from core
git clone https://github.com/InjectiveLabs/injective-abacus.git $BUILD_DIR -b $abacus_branch --depth 1 --single-branch > /dev/null

# collecting proto files
find $BUILD_DIR/api/gen/grpc/points_svc/pb -name '*.proto' -exec cp {} $PROTO_DIR \;

# copy imports
cp $TS_STUB_DIR/graphql.stub.proto $PROTO_DIR/danielvladco/protobuf/graphql.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')
done

find $TS_OUTPUT_DIR/proto -name "*.js" -type f -delete
find $TS_OUTPUT_DIR/proto -name "*.d.ts" -type f -delete

########################################
####### POST GENERATION CLEANUP #######
########################################

echo "Compiling npm packages..."

## 1. Replace strings
## 1. Replace packages
search1="@improbable-eng/grpc-web"
replace1="@injectivelabs/grpc-web"

FILES=$( find $TS_OUTPUT_DIR/proto -type f )

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

search1="getExtension():"
replace1="// @ts-ignore \n getExtension():"
search2="setExtension("
replace2="// @ts-ignore \n setExtension("

FILES=$( find $TS_OUTPUT_DIR/proto -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

search3="protobufjs/minimal"
replace3="protobufjs/minimal.js"

FILES=$( find $TS_OUTPUT_DIR/proto -type f )

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

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

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

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

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

## 6. ESM import fixes
npm --prefix $ROOT_DIR run tscEsmFix

# 7. 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
Loading

0 comments on commit de460fb

Please sign in to comment.