diff --git a/.github/workflows/test-ts-sdk.yaml b/.github/workflows/test-ts-sdk.yaml index 33830222..a72691ff 100644 --- a/.github/workflows/test-ts-sdk.yaml +++ b/.github/workflows/test-ts-sdk.yaml @@ -38,7 +38,6 @@ jobs: with: node-version: "lts/hydrogen" cache: "yarn" - cache-dependency-path: "**/yarn.lock" - name: yarn install uses: borales/actions-yarn@v4 diff --git a/.gitignore b/.gitignore index 3c400e39..a35f63ba 100644 --- a/.gitignore +++ b/.gitignore @@ -8,8 +8,8 @@ src/gql/utils/generated.ts src/gql/utils/schema.graphql dist **/node_modules/* -nibiru/ - +/nibiru +/cosmos-sdk reports junit.xml diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000..f82158ce --- /dev/null +++ b/.gitmodules @@ -0,0 +1,6 @@ +[submodule "cosmos-sdk"] + path = cosmos-sdk + url = https://github.com/cosmos/cosmos-sdk +[submodule "nibiru"] + path = nibiru + url = https://github.com/NibiruChain/nibiru diff --git a/README.md b/README.md index 7e013e1a..d7bab78c 100644 --- a/README.md +++ b/README.md @@ -245,7 +245,14 @@ console.log("perpPositions: %o", perpPositions) yarn ``` -4. Lastly, compile the code in each package. +4. Initialize git submodules + + ```sh + git submodule init + git submodule update + ``` + +5. Lastly, compile the code in each package. ```sh yarn build diff --git a/package.json b/package.json index 06d4c777..9bafc309 100644 --- a/package.json +++ b/package.json @@ -117,5 +117,6 @@ "@commitlint/config-conventional" ] }, - "gitHead": "bac1729be8575a9e75bf9e04447a63c45e227c8c" + "gitHead": "bac1729be8575a9e75bf9e04447a63c45e227c8c", + "packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e" } diff --git a/scripts/build.sh b/scripts/build.sh index 014725d5..26377f7b 100644 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -1,22 +1,8 @@ #!/bin/bash set -e -# Test for ./nibiru/ directory -if [ -d "./nibiru/" ]; then - echo "The ./nibiru/ directory exists." - cd nibiru - git checkout main - cd .. -elif [ -d "../nibiru/" ]; then - cd ../nibiru - git checkout main - git pull - cd ../ts-sdk -else - cd .. - git clone git@github.com:NibiruChain/nibiru.git - cd ts-sdk -fi +git submodule init +git submodule update yarn gql-generate & yarn proto-gen yarn build:tsc diff --git a/scripts/protocgen.sh b/scripts/protocgen.sh index 7a68c8e8..7d238ea2 100755 --- a/scripts/protocgen.sh +++ b/scripts/protocgen.sh @@ -11,11 +11,7 @@ set -eo pipefail # PKG_OUT_DIR: Output path where the generated code will go upon # successful execution. NIBIRU_REPO="./nibiru" - -if [ -d "../nibiru/" ]; then - NIBIRU_REPO="../nibiru" -fi - +COSMOS_SDK_REPO="./cosmos-sdk" PKG_OUT_DIR="./src/protojs" # ----------------------------------------------------------------------------- @@ -25,19 +21,20 @@ echo "Removing old src files" rm -rf $PKG_OUT_DIR/ mkdir -p $PKG_OUT_DIR/ -echo "Copying protos from the 'nibiru' repo" -rm -rf proto/ -mkdir -p proto/ -cp -r $NIBIRU_REPO/proto/ proto/ +for dir in $(find $NIBIRU_REPO/proto -path -prune -o -name '*.proto' -print0 | xargs -0 -n1 dirname | uniq | sort); do \ + for file in $(find "${dir}" -maxdepth 1 -name '*.proto'); do + echo "Generating ts proto code for $file" + buf generate --template $NIBIRU_REPO/proto/buf.gen.ts.yaml -o $PKG_OUT_DIR $file + done +done; -for dir in $(find ./proto -path -prune -o -name '*.proto' -print0 | xargs -0 -n1 dirname | uniq | sort); do \ +for dir in $(find $COSMOS_SDK_REPO/x/staking/proto -path -prune -o -name '*.proto' -print0 | xargs -0 -n1 dirname | uniq | sort); do \ for file in $(find "${dir}" -maxdepth 1 -name '*.proto'); do echo "Generating ts proto code for $file" - buf generate --template proto/proto/buf.gen.ts.yaml -o $PKG_OUT_DIR $file + buf generate --template $NIBIRU_REPO/proto/buf.gen.ts.yaml -o $PKG_OUT_DIR $file done done; -rm -rf proto/ yarn generate-barrels # # the `descriptor.ts` file is only used for gogoproto, and it causes issues with TS-generated code rm $PKG_OUT_DIR/google/protobuf/descriptor.ts @@ -50,12 +47,15 @@ rm -rf $PKG_OUT_DIR/cosmos/msg rm $PKG_OUT_DIR/google/api/annotations.ts rm $PKG_OUT_DIR/index.gogoproto.ts rm $PKG_OUT_DIR/index.amino.ts +rm $PKG_OUT_DIR/index.google.ts rm $PKG_OUT_DIR/index.cosmos.msg.v1.ts rm $PKG_OUT_DIR/index.cosmos.msg.ts +rm $PKG_OUT_DIR/index.cosmos.query.v1.ts +rm $PKG_OUT_DIR/index.cosmos.query.ts sed 's/export \* as gogoproto from \"\.\/index\.gogoproto\"\;//' $PKG_OUT_DIR/index.ts > tmpfile && mv tmpfile $PKG_OUT_DIR/index.ts sed 's/export \* as amino from \"\.\/index\.amino\"\;//' $PKG_OUT_DIR/index.ts > tmpfile && mv tmpfile $PKG_OUT_DIR/index.ts sed 's/export \* as google from \"\.\/index\.google\"\;//' $PKG_OUT_DIR/index.ts > tmpfile && mv tmpfile $PKG_OUT_DIR/index.ts sed 's/export \* from \"\.\/google\/api\/annotations\"\;//' $PKG_OUT_DIR/index.google.api.ts > tmpfile && mv tmpfile $PKG_OUT_DIR/index.google.api.ts -sed 's/export \* as protobuf from \"\.\/index.google.protobuf\"\;//' $PKG_OUT_DIR/index.google.ts > tmpfile && mv tmpfile $PKG_OUT_DIR/index.google.ts +# sed 's/export \* as protobuf from \"\.\/index.google.protobuf\"\;//' $PKG_OUT_DIR/index.google.ts > tmpfile && mv tmpfile $PKG_OUT_DIR/index.google.ts sed 's/export \* as msg from \"\.\/index.cosmos.msg\"\;//' $PKG_OUT_DIR/index.cosmos.ts > tmpfile && mv tmpfile $PKG_OUT_DIR/index.cosmos.ts diff --git a/src/sdk/query/index.ts b/src/sdk/query/index.ts index bba5ece6..22e95d02 100644 --- a/src/sdk/query/index.ts +++ b/src/sdk/query/index.ts @@ -7,4 +7,5 @@ export * from "./epochs" export * from "./inflation" export * from "./oracle" export * from "./query" +export * from "./staking" export * from "./sudo" diff --git a/src/sdk/query/query.ts b/src/sdk/query/query.ts index 691c243e..1e8b7081 100644 --- a/src/sdk/query/query.ts +++ b/src/sdk/query/query.ts @@ -8,8 +8,6 @@ import { setupDistributionExtension, setupGovExtension, setupIbcExtension, - setupStakingExtension, - StakingExtension, StargateClient, StargateClientOptions, } from "@cosmjs/stargate" @@ -29,11 +27,13 @@ import { SudoExtension, InflationExtension, setupInflationExtension, + setupStakingExtension, Result, bytesToHex, hexToBytes, setupDevgasExtension, DevgasExtension, + StakeExtension, } from ".." export type NibiruExtensions = StargateQueryClient & @@ -44,7 +44,7 @@ export type NibiruExtensions = StargateQueryClient & DevgasExtension & DistributionExtension & GovExtension & - StakingExtension & + StakeExtension & IbcExtension & WasmExtension & AuthExtension @@ -90,7 +90,8 @@ export class NibiruQuerier extends StargateClient { setupStakingExtension, setupIbcExtension, setupWasmExtension, - setupAuthExtension + setupAuthExtension, + setupStakingExtension ) } diff --git a/src/sdk/query/staking.test.ts b/src/sdk/query/staking.test.ts new file mode 100644 index 00000000..8181c4d4 --- /dev/null +++ b/src/sdk/query/staking.test.ts @@ -0,0 +1,39 @@ +import { QueryClient, coin } from "@cosmjs/stargate" +import * as query from "../../protojs/cosmos/staking/v1beta1/tx" +import { setupStakingExtension } from "." +import Long from "long" + +describe("setupStakingExtension", () => { + const mockBaseQueryClient = {} as QueryClient + + jest.spyOn(query, "MsgClientImpl").mockReturnValue({ + QuerySudoers: jest.fn().mockResolvedValue({ staking: {} }), + } as unknown as query.MsgClientImpl) + + test("should setup sudo extension correctly", () => { + const extension = setupStakingExtension(mockBaseQueryClient) + + expect(extension.staking).toBeDefined() + expect(extension.staking.cancelUnbondingDelegation).toBeInstanceOf(Function) + }) + + describe("sudo.querySudoers", () => { + test("should call QuerySudoersRequest and return the response", async () => { + const querySudoersRequest = jest + .spyOn(query.MsgCancelUnbondingDelegation, "fromPartial") + .mockReturnValue({} as query.MsgCancelUnbondingDelegation) + + const extension = setupStakingExtension(mockBaseQueryClient) + const testArgs = { + validatorAddress: "", + delegatorAddress: "", + amount: coin(1, "unibi"), + creationHeight: new Long(0), + } + const result = await extension.staking.cancelUnbondingDelegation(testArgs) + + expect(querySudoersRequest).toHaveBeenCalledWith(testArgs) + expect(result).toEqual({ staking: {} }) + }) + }) +}) diff --git a/src/sdk/query/staking.ts b/src/sdk/query/staking.ts new file mode 100644 index 00000000..5b09c137 --- /dev/null +++ b/src/sdk/query/staking.ts @@ -0,0 +1,29 @@ +import { createProtobufRpcClient, QueryClient } from "@cosmjs/stargate" +import { + MsgCancelUnbondingDelegation, + MsgCancelUnbondingDelegationResponse, + MsgClientImpl, +} from "../../protojs/cosmos/staking/v1beta1/tx" + +export interface StakeExtension { + readonly staking: Readonly<{ + cancelUnbondingDelegation: ( + args: MsgCancelUnbondingDelegation + ) => Promise + }> +} + +export const setupStakingExtension = (base: QueryClient): StakeExtension => { + const rpcClient = createProtobufRpcClient(base) + const queryService = new MsgClientImpl(rpcClient) + + return { + staking: { + cancelUnbondingDelegation: async (args: MsgCancelUnbondingDelegation) => { + const req = MsgCancelUnbondingDelegation.fromPartial(args) + const resp = await queryService.CancelUnbondingDelegation(req) + return resp + }, + }, + } +}