From bf25bc6409b840acbebaaf7a39129f07c9ecc845 Mon Sep 17 00:00:00 2001 From: Milan Steiner Date: Fri, 11 Mar 2022 10:06:47 +0100 Subject: [PATCH 01/10] getContracts(): Support more than 100 per code ID --- CHANGELOG.md | 4 ++- .../cosmwasm-stargate/src/cosmwasmclient.ts | 28 ++++++++++++++++--- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cc7e910b..9c2dd21a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,7 +44,8 @@ and this project adheres to dependencies. This should also reduce the bundle size as only the English wordlist is shipped. ([#966]) - @cosmjs/cli: Rename binary `cosmwasm-cli` to `cosmjs-cli` ([#1033]). -- @cosmjs/stargate & @cosmjs/cosmwasm-stargate: Removed default types from AminoTypes. ([1079]) +- @cosmjs/stargate & @cosmjs/cosmwasm-stargate: Removed default types from AminoTypes. ([#1079]) +- @cosmjs/cosmwasm-stargate: getCodes() automatically loops through all pagination pages now. ([#1077]) [#927]: https://github.com/cosmos/cosmjs/issues/927 [#955]: https://github.com/cosmos/cosmjs/issues/955 @@ -56,6 +57,7 @@ and this project adheres to [#1026]: https://github.com/cosmos/cosmjs/issues/1026 [#1033]: https://github.com/cosmos/cosmjs/issues/1033 [#1053]: https://github.com/cosmos/cosmjs/issues/1053 +[#1077]: https://github.com/cosmos/cosmjs/issues/1077 [#1079]: https://github.com/cosmos/cosmjs/issues/1079 ### Removed diff --git a/packages/cosmwasm-stargate/src/cosmwasmclient.ts b/packages/cosmwasm-stargate/src/cosmwasmclient.ts index c1a39ed2..b1da085d 100644 --- a/packages/cosmwasm-stargate/src/cosmwasmclient.ts +++ b/packages/cosmwasm-stargate/src/cosmwasmclient.ts @@ -25,7 +25,7 @@ import { } from "@cosmjs/stargate"; import { Tendermint34Client, toRfc3339WithNanoseconds } from "@cosmjs/tendermint-rpc"; import { assert, sleep } from "@cosmjs/utils"; -import { CodeInfoResponse } from "cosmjs-types/cosmwasm/wasm/v1/query"; +import { CodeInfoResponse, QueryContractsByCodeResponse } from "cosmjs-types/cosmwasm/wasm/v1/query"; import { ContractCodeHistoryOperationType } from "cosmjs-types/cosmwasm/wasm/v1/types"; import { JsonObject, setupWasmExtension, WasmExtension } from "./modules"; @@ -334,10 +334,30 @@ export class CosmWasmClient { return codeDetails; } + /** + * getContracts() returns all contracts and is just looping through all pagination pages. + * + * This is potentially inefficient and advanced apps should consider creating + * their own query client to handle pagination together with the app's screens. + */ public async getContracts(codeId: number): Promise { - // TODO: handle pagination - accept as arg or auto-loop - const { contracts } = await this.forceGetQueryClient().wasm.listContractsByCodeId(codeId); - return contracts; + const allContracts = []; + + try { + let startAtKey: Uint8Array | undefined = undefined; + do { + const { contracts, pagination }: QueryContractsByCodeResponse = + await this.forceGetQueryClient().wasm.listContractsByCodeId(codeId, startAtKey); + const loadedContracts = contracts || []; + loadedContracts.reverse(); + allContracts.unshift(...loadedContracts); + startAtKey = pagination?.nextKey; + } while (startAtKey?.length !== 0); + } catch (_e: any) { + throw new Error(_e); + } + + return allContracts; } /** From 6d6fe29ad05a22c4ca2c3d15567ebfa6c71a44e9 Mon Sep 17 00:00:00 2001 From: Milan Steiner Date: Fri, 11 Mar 2022 10:25:05 +0100 Subject: [PATCH 02/10] Fix tests --- packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts b/packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts index 669602dd..1ba4794c 100644 --- a/packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts +++ b/packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts @@ -275,9 +275,9 @@ describe("CosmWasmClient", () => { const expectedAddresses = deployedHackatom.instances.map((info) => info.address); // Test first 3 instances we get from scripts/wasmd/init.sh. There may me more than that in the result. - expect(result[0]).toEqual(expectedAddresses[0]); + expect(result[0]).toEqual(expectedAddresses[2]); expect(result[1]).toEqual(expectedAddresses[1]); - expect(result[2]).toEqual(expectedAddresses[2]); + expect(result[2]).toEqual(expectedAddresses[0]); }); }); From 38e1d0e075d1799291744f5fbdfc7bb0c7807559 Mon Sep 17 00:00:00 2001 From: Milan Steiner Date: Fri, 11 Mar 2022 10:31:54 +0100 Subject: [PATCH 03/10] Document webpack configs --- HACKING.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/HACKING.md b/HACKING.md index a8f96965..1f094684 100644 --- a/HACKING.md +++ b/HACKING.md @@ -37,6 +37,37 @@ sha256sum -c checksums.sha256 1. Install dependencies: `yarn install` 2. Install SDKs (to make IDE integration work): `yarn dlx @yarnpkg/sdks` +## Webpack Configs +With WebPack 5, you have to be explicit about the usage of Node.js types and modules that were simply replaced with re-implementations for browsers in Webpack 4. + +Configs for 0.28 and later: +```ts +resolve: { + fallback: { + buffer: false, + crypto: false, + events: false, + path: false, + stream: false, + string_decoder: false, + }, + }, +``` + +Configs for CosmJS < 0.28 +```ts +resolve: { + fallback: { + buffer: false, + crypto: false, + events: false, + path: false, + stream: require.resolve("stream-browserify"), + string_decoder: false, + }, + }, +``` + ## Running tests For unit tests that don't connect to any blockchain, just do: From c0820b7597361c9805cfbef06abb7e9cf21bd06d Mon Sep 17 00:00:00 2001 From: Milan Steiner <69144826+msteiner96@users.noreply.github.com> Date: Mon, 14 Mar 2022 11:09:43 +0100 Subject: [PATCH 04/10] Update HACKING.md Co-authored-by: Simon Warta <2603011+webmaster128@users.noreply.github.com> --- HACKING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HACKING.md b/HACKING.md index 1f094684..677b7531 100644 --- a/HACKING.md +++ b/HACKING.md @@ -56,7 +56,7 @@ resolve: { Configs for CosmJS < 0.28 ```ts -resolve: { + resolve: { fallback: { buffer: false, crypto: false, From 4ed6ee140d20a7442cfc10e5e710766093b37e25 Mon Sep 17 00:00:00 2001 From: Milan Steiner <69144826+msteiner96@users.noreply.github.com> Date: Mon, 14 Mar 2022 11:09:46 +0100 Subject: [PATCH 05/10] Update HACKING.md Co-authored-by: Simon Warta <2603011+webmaster128@users.noreply.github.com> --- HACKING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HACKING.md b/HACKING.md index 677b7531..3448a0ef 100644 --- a/HACKING.md +++ b/HACKING.md @@ -42,7 +42,7 @@ With WebPack 5, you have to be explicit about the usage of Node.js types and mod Configs for 0.28 and later: ```ts -resolve: { + resolve: { fallback: { buffer: false, crypto: false, From 03f783194fbfc8e90013e1871693a665ded9950a Mon Sep 17 00:00:00 2001 From: Milan Steiner Date: Mon, 14 Mar 2022 11:16:44 +0100 Subject: [PATCH 06/10] Fix Tests & Beautify Code --- .../src/cosmwasmclient.spec.ts | 4 ++-- .../cosmwasm-stargate/src/cosmwasmclient.ts | 22 +++++++------------ 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts b/packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts index 1ba4794c..669602dd 100644 --- a/packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts +++ b/packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts @@ -275,9 +275,9 @@ describe("CosmWasmClient", () => { const expectedAddresses = deployedHackatom.instances.map((info) => info.address); // Test first 3 instances we get from scripts/wasmd/init.sh. There may me more than that in the result. - expect(result[0]).toEqual(expectedAddresses[2]); + expect(result[0]).toEqual(expectedAddresses[0]); expect(result[1]).toEqual(expectedAddresses[1]); - expect(result[2]).toEqual(expectedAddresses[0]); + expect(result[2]).toEqual(expectedAddresses[2]); }); }); diff --git a/packages/cosmwasm-stargate/src/cosmwasmclient.ts b/packages/cosmwasm-stargate/src/cosmwasmclient.ts index b1da085d..0baa38bc 100644 --- a/packages/cosmwasm-stargate/src/cosmwasmclient.ts +++ b/packages/cosmwasm-stargate/src/cosmwasmclient.ts @@ -342,20 +342,14 @@ export class CosmWasmClient { */ public async getContracts(codeId: number): Promise { const allContracts = []; - - try { - let startAtKey: Uint8Array | undefined = undefined; - do { - const { contracts, pagination }: QueryContractsByCodeResponse = - await this.forceGetQueryClient().wasm.listContractsByCodeId(codeId, startAtKey); - const loadedContracts = contracts || []; - loadedContracts.reverse(); - allContracts.unshift(...loadedContracts); - startAtKey = pagination?.nextKey; - } while (startAtKey?.length !== 0); - } catch (_e: any) { - throw new Error(_e); - } + let startAtKey: Uint8Array | undefined = undefined; + do { + const { contracts, pagination }: QueryContractsByCodeResponse = + await this.forceGetQueryClient().wasm.listContractsByCodeId(codeId, startAtKey); + const loadedContracts = contracts || []; + allContracts.unshift(...loadedContracts); + startAtKey = pagination?.nextKey; + } while (startAtKey?.length !== 0 && startAtKey !== undefined); return allContracts; } From d4b382a1af38f3c5e33eeae5ba26a720be782314 Mon Sep 17 00:00:00 2001 From: Milan Steiner Date: Mon, 14 Mar 2022 11:17:25 +0100 Subject: [PATCH 07/10] Using push instead of unshift --- packages/cosmwasm-stargate/src/cosmwasmclient.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cosmwasm-stargate/src/cosmwasmclient.ts b/packages/cosmwasm-stargate/src/cosmwasmclient.ts index 0baa38bc..968a7f1a 100644 --- a/packages/cosmwasm-stargate/src/cosmwasmclient.ts +++ b/packages/cosmwasm-stargate/src/cosmwasmclient.ts @@ -347,7 +347,7 @@ export class CosmWasmClient { const { contracts, pagination }: QueryContractsByCodeResponse = await this.forceGetQueryClient().wasm.listContractsByCodeId(codeId, startAtKey); const loadedContracts = contracts || []; - allContracts.unshift(...loadedContracts); + allContracts.push(...loadedContracts); startAtKey = pagination?.nextKey; } while (startAtKey?.length !== 0 && startAtKey !== undefined); From b385e53e1691bb831a3f54151dede36a617d10c5 Mon Sep 17 00:00:00 2001 From: Milan Steiner <69144826+msteiner96@users.noreply.github.com> Date: Mon, 14 Mar 2022 15:12:41 +0100 Subject: [PATCH 08/10] Update HACKING.md --- HACKING.md | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/HACKING.md b/HACKING.md index 3448a0ef..c75e2b8c 100644 --- a/HACKING.md +++ b/HACKING.md @@ -38,10 +38,17 @@ sha256sum -c checksums.sha256 2. Install SDKs (to make IDE integration work): `yarn dlx @yarnpkg/sdks` ## Webpack Configs -With WebPack 5, you have to be explicit about the usage of Node.js types and modules that were simply replaced with re-implementations for browsers in Webpack 4. + +With WebPack 5, you have to be explicit about the usage of Node.js types and +modules that were simply replaced with re-implementations for browsers in +Webpack 4. Configs for 0.28 and later: -```ts + +```js +module.exports = [ + { + ..., resolve: { fallback: { buffer: false, @@ -52,10 +59,16 @@ Configs for 0.28 and later: string_decoder: false, }, }, + }, +]; ``` Configs for CosmJS < 0.28 -```ts + +```js +module.exports = [ + { + ..., resolve: { fallback: { buffer: false, @@ -66,6 +79,25 @@ Configs for CosmJS < 0.28 string_decoder: false, }, }, + }, +]; +``` + +Also, in both cases you need the Buffer plugin: + +```ts +module.exports = [ + { + ..., + plugins: [ + ..., + new webpack.ProvidePlugin({ + Buffer: ["buffer", "Buffer"], + }), + ], + ... + }, +]; ``` ## Running tests From 656e02374898afe755e980e93390591b4b65fd86 Mon Sep 17 00:00:00 2001 From: Milan Steiner Date: Mon, 14 Mar 2022 15:21:10 +0100 Subject: [PATCH 09/10] Moving Webpack confs from hacking to readme --- HACKING.md | 63 ----------------------------------------------------- README.md | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 63 deletions(-) diff --git a/HACKING.md b/HACKING.md index c75e2b8c..a8f96965 100644 --- a/HACKING.md +++ b/HACKING.md @@ -37,69 +37,6 @@ sha256sum -c checksums.sha256 1. Install dependencies: `yarn install` 2. Install SDKs (to make IDE integration work): `yarn dlx @yarnpkg/sdks` -## Webpack Configs - -With WebPack 5, you have to be explicit about the usage of Node.js types and -modules that were simply replaced with re-implementations for browsers in -Webpack 4. - -Configs for 0.28 and later: - -```js -module.exports = [ - { - ..., - resolve: { - fallback: { - buffer: false, - crypto: false, - events: false, - path: false, - stream: false, - string_decoder: false, - }, - }, - }, -]; -``` - -Configs for CosmJS < 0.28 - -```js -module.exports = [ - { - ..., - resolve: { - fallback: { - buffer: false, - crypto: false, - events: false, - path: false, - stream: require.resolve("stream-browserify"), - string_decoder: false, - }, - }, - }, -]; -``` - -Also, in both cases you need the Buffer plugin: - -```ts -module.exports = [ - { - ..., - plugins: [ - ..., - new webpack.ProvidePlugin({ - Buffer: ["buffer", "Buffer"], - }), - ], - ... - }, -]; -``` - ## Running tests For unit tests that don't connect to any blockchain, just do: diff --git a/README.md b/README.md index 686cb5ed..6c8b9316 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,70 @@ We're happy to adjust this list according to users' needs as long as you don't ask for Internet Explorer support. If your environment does not support Wasm, we can work on a solution with swappable implementations. + +## Webpack Configs + +With WebPack 5, you have to be explicit about the usage of Node.js types and +modules that were simply replaced with re-implementations for browsers in +Webpack 4. + +Configs for 0.28 and later: + +```js +module.exports = [ + { + ..., + resolve: { + fallback: { + buffer: false, + crypto: false, + events: false, + path: false, + stream: false, + string_decoder: false, + }, + }, + }, +]; +``` + +Configs for CosmJS < 0.28 + +```js +module.exports = [ + { + ..., + resolve: { + fallback: { + buffer: false, + crypto: false, + events: false, + path: false, + stream: require.resolve("stream-browserify"), + string_decoder: false, + }, + }, + }, +]; +``` + +Also, in both cases you need the Buffer plugin: + +```ts +module.exports = [ + { + ..., + plugins: [ + ..., + new webpack.ProvidePlugin({ + Buffer: ["buffer", "Buffer"], + }), + ], + ... + }, +]; +``` + ## Roadmap We maintain a [development board](https://github.com/orgs/cosmos/projects/6), From 9d9989403bfb9b9974117c7da82bb1e5b9fb99b4 Mon Sep 17 00:00:00 2001 From: Milan Steiner <69144826+msteiner96@users.noreply.github.com> Date: Tue, 15 Mar 2022 16:25:36 +0100 Subject: [PATCH 10/10] Update packages/cosmwasm-stargate/src/cosmwasmclient.ts Co-authored-by: Simon Warta <2603011+webmaster128@users.noreply.github.com> --- packages/cosmwasm-stargate/src/cosmwasmclient.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cosmwasm-stargate/src/cosmwasmclient.ts b/packages/cosmwasm-stargate/src/cosmwasmclient.ts index 968a7f1a..1a68e43d 100644 --- a/packages/cosmwasm-stargate/src/cosmwasmclient.ts +++ b/packages/cosmwasm-stargate/src/cosmwasmclient.ts @@ -335,7 +335,7 @@ export class CosmWasmClient { } /** - * getContracts() returns all contracts and is just looping through all pagination pages. + * getContracts() returns all contract instances for one code and is just looping through all pagination pages. * * This is potentially inefficient and advanced apps should consider creating * their own query client to handle pagination together with the app's screens.