Skip to content

Commit

Permalink
feat: cosmwasmclient extension & signingcosmwasmclient implementation (
Browse files Browse the repository at this point in the history
…#379)

* feat: nibicosmwasmclient

* feat: nibi signing cosm wasm client

* refactor: adding nibi account parser to nibi signingcosmwasmclient

* test: remove unused test file

* test: take signingcosmwasmclient from coverage

* chore: fix coverage
  • Loading branch information
CalicoNino authored Oct 18, 2024
1 parent b79eae5 commit b89a700
Show file tree
Hide file tree
Showing 6 changed files with 916 additions and 14 deletions.
2 changes: 2 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const config: Config = {
"!jest.config.ts",
"!**/src/gql/utils/generated.ts",
"!**/src/sdk/utils/testutil.ts",
"!**/src/sdk/core/cosmwasmclient.ts", // Implementation from Cosmjs
"!**/src/sdk/core/signingcosmwasmclient.ts", // Implementation from Cosmjs
],
testPathIgnorePatterns: ["/node_modules/", "/dist/", "/nibiru/"],
coverageReporters: ["json-summary", "text", "html", "lcov"],
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"bignumber.js": "^9.1.1",
"cross-fetch": "4.0.0",
"graphql": "^16.7.1",
"graphql-ws": "^5.14.0"
"graphql-ws": "^5.14.0",
"pako": "^2.1.0"
},
"peerDependencies": {
"@cosmjs/cosmwasm-stargate": "^0.32.3",
Expand All @@ -67,6 +68,7 @@
"@types/jest": "^29.1.2",
"@types/long": "^4.0.0",
"@types/node": "^16.11.7",
"@types/pako": "^2.0.3",
"@typescript-eslint/eslint-plugin": "^5.59.7",
"@typescript-eslint/parser": "^5.30.7",
"barrelsby": "^2.8.1",
Expand Down
62 changes: 62 additions & 0 deletions src/sdk/core/cosmwasmclient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { CosmWasmClient } from "@cosmjs/cosmwasm-stargate"
import {
Account,
accountFromAny,
AccountParser,
HttpEndpoint,
SequenceResponse,
} from "@cosmjs/stargate"
import { CometClient } from "@cosmjs/tendermint-rpc"

export interface NibiCosmWasmClientOptions {
readonly accountParser?: AccountParser
}

export class NibiCosmWasmClient extends CosmWasmClient {
private readonly accountParser: AccountParser

protected constructor(
cometClient: CometClient | undefined,
options: NibiCosmWasmClientOptions = {}
) {
super(cometClient)
const { accountParser = accountFromAny } = options
this.accountParser = accountParser
}

public static async connect(
endpoint: string | HttpEndpoint,
options: NibiCosmWasmClientOptions = {}
): Promise<NibiCosmWasmClient> {
const cosmWasmClient = await CosmWasmClient.connect(endpoint)
return new NibiCosmWasmClient(cosmWasmClient["cometClient"], options)
}

public async getAccount(searchAddress: string): Promise<Account | null> {
try {
const account = await this.forceGetQueryClient().auth.account(
searchAddress
)
return account ? this.accountParser(account) : null
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
if (/rpc error: code = NotFound/i.test(error.toString())) {
return null
}
throw error
}
}

public async getSequence(address: string): Promise<SequenceResponse> {
const account = await this.getAccount(address)
if (!account) {
throw new Error(
`Account '${address}' does not exist on chain. Send some tokens there before trying to query sequence.`
)
}
return {
accountNumber: account.accountNumber,
sequence: account.sequence,
}
}
}
Loading

0 comments on commit b89a700

Please sign in to comment.