Skip to content

Commit

Permalink
feat: staking extension and git submodules
Browse files Browse the repository at this point in the history
  • Loading branch information
cgilbe27 committed Jun 18, 2024
1 parent f3dfa3a commit c1421be
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 38 deletions.
1 change: 0 additions & 1 deletion .github/workflows/test-ts-sdk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ src/gql/utils/generated.ts
src/gql/utils/schema.graphql
dist
**/node_modules/*
nibiru/

/nibiru
/cosmos-sdk

reports
junit.xml
Expand Down
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -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
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,6 @@
"@commitlint/config-conventional"
]
},
"gitHead": "bac1729be8575a9e75bf9e04447a63c45e227c8c"
"gitHead": "bac1729be8575a9e75bf9e04447a63c45e227c8c",
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
}
18 changes: 2 additions & 16 deletions scripts/build.sh
Original file line number Diff line number Diff line change
@@ -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 [email protected]:NibiruChain/nibiru.git
cd ts-sdk
fi
git submodule init
git submodule update

yarn gql-generate & yarn proto-gen
yarn build:tsc
Expand Down
26 changes: 13 additions & 13 deletions scripts/protocgen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"

# -----------------------------------------------------------------------------
Expand All @@ -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
Expand All @@ -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
1 change: 1 addition & 0 deletions src/sdk/query/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export * from "./epochs"
export * from "./inflation"
export * from "./oracle"
export * from "./query"
export * from "./staking"
export * from "./sudo"
9 changes: 5 additions & 4 deletions src/sdk/query/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import {
setupDistributionExtension,
setupGovExtension,
setupIbcExtension,
setupStakingExtension,
StakingExtension,
StargateClient,
StargateClientOptions,
} from "@cosmjs/stargate"
Expand All @@ -29,11 +27,13 @@ import {
SudoExtension,
InflationExtension,
setupInflationExtension,
setupStakingExtension,
Result,
bytesToHex,
hexToBytes,
setupDevgasExtension,
DevgasExtension,
StakeExtension,
} from ".."

export type NibiruExtensions = StargateQueryClient &
Expand All @@ -44,7 +44,7 @@ export type NibiruExtensions = StargateQueryClient &
DevgasExtension &
DistributionExtension &
GovExtension &
StakingExtension &
StakeExtension &
IbcExtension &
WasmExtension &
AuthExtension
Expand Down Expand Up @@ -90,7 +90,8 @@ export class NibiruQuerier extends StargateClient {
setupStakingExtension,
setupIbcExtension,
setupWasmExtension,
setupAuthExtension
setupAuthExtension,
setupStakingExtension
)
}

Expand Down
39 changes: 39 additions & 0 deletions src/sdk/query/staking.test.ts
Original file line number Diff line number Diff line change
@@ -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: {} })
})
})
})
29 changes: 29 additions & 0 deletions src/sdk/query/staking.ts
Original file line number Diff line number Diff line change
@@ -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<MsgCancelUnbondingDelegationResponse>
}>
}

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
},
},
}
}

0 comments on commit c1421be

Please sign in to comment.