diff --git a/CHANGELOG.md b/CHANGELOG.md index e039e715..85feb853 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -55,6 +55,10 @@ - @cosmjs/utils: Add `assertDefined`. - @cosmjs/faucet: Rename binary from `cosmwasm-faucet` to `cosmos-faucet`. +## 0.22.3 (2020-09-15) + +- @cosmjs/math: Add `Decimal.minus`. + ## 0.22.2 (2020-08-11) - @cosmjs/faucet: Log errors for failed send transactions. diff --git a/lerna.json b/lerna.json index ac8ee370..006447bc 100644 --- a/lerna.json +++ b/lerna.json @@ -1,5 +1,5 @@ { - "version": "0.22.2", + "version": "0.22.3", "useWorkspaces": true, "npmClient": "yarn" } diff --git a/packages/cli/nonces/1600158182 b/packages/cli/nonces/1600158182 new file mode 100644 index 00000000..e69de29b diff --git a/packages/cli/package.json b/packages/cli/package.json index 4dc2dff7..dc5449da 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -1,6 +1,6 @@ { "name": "@cosmjs/cli", - "version": "0.22.2", + "version": "0.22.3", "description": "Command line interface", "contributors": [ "IOV SAS ", @@ -40,12 +40,12 @@ "!**/testdata/" ], "dependencies": { - "@cosmjs/cosmwasm": "^0.22.2", - "@cosmjs/crypto": "^0.22.2", - "@cosmjs/encoding": "^0.22.2", - "@cosmjs/launchpad": "^0.22.2", - "@cosmjs/math": "^0.22.2", - "@cosmjs/utils": "^0.22.2", + "@cosmjs/cosmwasm": "^0.22.3", + "@cosmjs/crypto": "^0.22.3", + "@cosmjs/encoding": "^0.22.3", + "@cosmjs/launchpad": "^0.22.3", + "@cosmjs/math": "^0.22.3", + "@cosmjs/utils": "^0.22.3", "axios": "^0.19.2", "babylon": "^6.18.0", "colors": "^1.3.3", diff --git a/packages/cosmwasm/nonces/1600158182 b/packages/cosmwasm/nonces/1600158182 new file mode 100644 index 00000000..e69de29b diff --git a/packages/cosmwasm/package.json b/packages/cosmwasm/package.json index 8af6770e..b868447b 100644 --- a/packages/cosmwasm/package.json +++ b/packages/cosmwasm/package.json @@ -1,6 +1,6 @@ { "name": "@cosmjs/cosmwasm", - "version": "0.22.2", + "version": "0.22.3", "description": "CosmWasm SDK", "author": "Ethan Frey ", "license": "Apache-2.0", @@ -40,11 +40,11 @@ "pack-web": "yarn build-or-skip && webpack --mode development --config webpack.web.config.js" }, "dependencies": { - "@cosmjs/crypto": "^0.22.2", - "@cosmjs/encoding": "^0.22.2", - "@cosmjs/launchpad": "^0.22.2", - "@cosmjs/math": "^0.22.2", - "@cosmjs/utils": "^0.22.2", + "@cosmjs/crypto": "^0.22.3", + "@cosmjs/encoding": "^0.22.3", + "@cosmjs/launchpad": "^0.22.3", + "@cosmjs/math": "^0.22.3", + "@cosmjs/utils": "^0.22.3", "axios": "^0.19.0", "pako": "^1.0.11" }, diff --git a/packages/crypto/nonces/1600158182 b/packages/crypto/nonces/1600158182 new file mode 100644 index 00000000..e69de29b diff --git a/packages/crypto/package.json b/packages/crypto/package.json index fcfa0591..03a28417 100644 --- a/packages/crypto/package.json +++ b/packages/crypto/package.json @@ -1,6 +1,6 @@ { "name": "@cosmjs/crypto", - "version": "0.22.2", + "version": "0.22.3", "description": "Cryptography resources for blockchain projects", "contributors": [ "IOV SAS ", @@ -45,9 +45,9 @@ "pack-web": "yarn build-or-skip && webpack --mode development --config webpack.web.config.js" }, "dependencies": { - "@cosmjs/encoding": "^0.22.2", - "@cosmjs/math": "^0.22.2", - "@cosmjs/utils": "^0.22.2", + "@cosmjs/encoding": "^0.22.3", + "@cosmjs/math": "^0.22.3", + "@cosmjs/utils": "^0.22.3", "bip39": "^3.0.2", "bn.js": "^4.11.8", "elliptic": "^6.5.3", diff --git a/packages/encoding/nonces/1600158182 b/packages/encoding/nonces/1600158182 new file mode 100644 index 00000000..e69de29b diff --git a/packages/encoding/package.json b/packages/encoding/package.json index 900c76bf..bef6625b 100644 --- a/packages/encoding/package.json +++ b/packages/encoding/package.json @@ -1,6 +1,6 @@ { "name": "@cosmjs/encoding", - "version": "0.22.2", + "version": "0.22.3", "description": "Encoding helpers for blockchain projects", "contributors": [ "IOV SAS " diff --git a/packages/faucet/nonces/1600158182 b/packages/faucet/nonces/1600158182 new file mode 100644 index 00000000..e69de29b diff --git a/packages/faucet/package.json b/packages/faucet/package.json index eb6ad47e..faaf81a9 100644 --- a/packages/faucet/package.json +++ b/packages/faucet/package.json @@ -1,6 +1,6 @@ { "name": "@cosmjs/faucet", - "version": "0.22.2", + "version": "0.22.3", "description": "The faucet", "contributors": [ "Ethan Frey ", @@ -40,11 +40,11 @@ "start-coralnet": "FAUCET_ADDRESS_PREFIX=coral FAUCET_TOKENS=\"SHELL=10^6ushell, REEF=10^6ureef\" FAUCET_CREDIT_AMOUNT_USHELL=10000000 FAUCET_CREDIT_AMOUNT_UREEF=2000000 FAUCET_CONCURRENCY=3 FAUCET_MNEMONIC=\"economy stock theory fatal elder harbor betray wasp final emotion task crumble siren bottom lizard educate guess current outdoor pair theory focus wife stone\" ./bin/cosmos-faucet start \"https://lcd.coralnet.cosmwasm.com\"" }, "dependencies": { - "@cosmjs/crypto": "^0.22.2", - "@cosmjs/encoding": "^0.22.2", - "@cosmjs/launchpad": "^0.22.2", - "@cosmjs/math": "^0.22.2", - "@cosmjs/utils": "^0.22.2", + "@cosmjs/crypto": "^0.22.3", + "@cosmjs/encoding": "^0.22.3", + "@cosmjs/launchpad": "^0.22.3", + "@cosmjs/math": "^0.22.3", + "@cosmjs/utils": "^0.22.3", "@koa/cors": "^3.0.0", "koa": "^2.11.0", "koa-bodyparser": "^4.2.1" diff --git a/packages/json-rpc/nonces/1600158182 b/packages/json-rpc/nonces/1600158182 new file mode 100644 index 00000000..e69de29b diff --git a/packages/json-rpc/package.json b/packages/json-rpc/package.json index 5a3c5a78..752e579c 100644 --- a/packages/json-rpc/package.json +++ b/packages/json-rpc/package.json @@ -1,6 +1,6 @@ { "name": "@cosmjs/json-rpc", - "version": "0.22.2", + "version": "0.22.3", "description": "Framework for implementing a JSON-RPC 2.0 API", "contributors": [ "IOV SAS ", @@ -46,7 +46,7 @@ "pack-web": "yarn build-or-skip && webpack --mode development --config webpack.web.config.js" }, "dependencies": { - "@cosmjs/stream": "^0.22.2", + "@cosmjs/stream": "^0.22.3", "xstream": "^11.10.0" } } diff --git a/packages/launchpad-ledger/package.json b/packages/launchpad-ledger/package.json index 0fdc2129..0d6a3185 100644 --- a/packages/launchpad-ledger/package.json +++ b/packages/launchpad-ledger/package.json @@ -1,6 +1,6 @@ { "name": "@cosmjs/launchpad-ledger", - "version": "0.22.2", + "version": "0.22.3", "description": "A library for interacting with the Cosmos Launchpad Ledger Nano App", "contributors": [ "Will Clark " diff --git a/packages/launchpad/nonces/1600158182 b/packages/launchpad/nonces/1600158182 new file mode 100644 index 00000000..e69de29b diff --git a/packages/launchpad/package.json b/packages/launchpad/package.json index 3ef5c5c8..55c23f44 100644 --- a/packages/launchpad/package.json +++ b/packages/launchpad/package.json @@ -1,6 +1,6 @@ { "name": "@cosmjs/launchpad", - "version": "0.22.2", + "version": "0.22.3", "description": "A client library for the Cosmos SDK 0.37 (cosmoshub-3), 0.38 and 0.39 (Launchpad)", "contributors": [ "Ethan Frey ", @@ -43,10 +43,10 @@ "pack-web": "yarn build-or-skip && webpack --mode development --config webpack.web.config.js" }, "dependencies": { - "@cosmjs/crypto": "^0.22.2", - "@cosmjs/encoding": "^0.22.2", - "@cosmjs/math": "^0.22.2", - "@cosmjs/utils": "^0.22.2", + "@cosmjs/crypto": "^0.22.3", + "@cosmjs/encoding": "^0.22.3", + "@cosmjs/math": "^0.22.3", + "@cosmjs/utils": "^0.22.3", "axios": "^0.19.0" }, "devDependencies": { diff --git a/packages/math/nonces/1600158182 b/packages/math/nonces/1600158182 new file mode 100644 index 00000000..e69de29b diff --git a/packages/math/package.json b/packages/math/package.json index 2a8414f1..c3a118bf 100644 --- a/packages/math/package.json +++ b/packages/math/package.json @@ -1,6 +1,6 @@ { "name": "@cosmjs/math", - "version": "0.22.2", + "version": "0.22.3", "description": "Math helpers for blockchain projects", "contributors": [ "IOV SAS " diff --git a/packages/math/src/decimal.spec.ts b/packages/math/src/decimal.spec.ts index 9f9dee5b..e12d1f96 100644 --- a/packages/math/src/decimal.spec.ts +++ b/packages/math/src/decimal.spec.ts @@ -212,6 +212,54 @@ describe("Decimal", () => { }); }); + describe("minus", () => { + it("returns correct values", () => { + const zero = Decimal.fromUserInput("0", 5); + expect(Decimal.fromUserInput("0", 5).minus(zero).toString()).toEqual("0"); + expect(Decimal.fromUserInput("1", 5).minus(zero).toString()).toEqual("1"); + expect(Decimal.fromUserInput("2", 5).minus(zero).toString()).toEqual("2"); + expect(Decimal.fromUserInput("2.8", 5).minus(zero).toString()).toEqual("2.8"); + expect(Decimal.fromUserInput("0.12345", 5).minus(zero).toString()).toEqual("0.12345"); + + const one = Decimal.fromUserInput("1", 5); + expect(Decimal.fromUserInput("1", 5).minus(one).toString()).toEqual("0"); + expect(Decimal.fromUserInput("2", 5).minus(one).toString()).toEqual("1"); + expect(Decimal.fromUserInput("3", 5).minus(one).toString()).toEqual("2"); + expect(Decimal.fromUserInput("3.8", 5).minus(one).toString()).toEqual("2.8"); + expect(Decimal.fromUserInput("1.12345", 5).minus(one).toString()).toEqual("0.12345"); + + const oneDotFive = Decimal.fromUserInput("1.5", 5); + expect(Decimal.fromUserInput("1.5", 5).minus(oneDotFive).toString()).toEqual("0"); + expect(Decimal.fromUserInput("2.5", 5).minus(oneDotFive).toString()).toEqual("1"); + expect(Decimal.fromUserInput("3.5", 5).minus(oneDotFive).toString()).toEqual("2"); + expect(Decimal.fromUserInput("4.3", 5).minus(oneDotFive).toString()).toEqual("2.8"); + expect(Decimal.fromUserInput("1.62345", 5).minus(oneDotFive).toString()).toEqual("0.12345"); + + // original value remain unchanged + expect(zero.toString()).toEqual("0"); + expect(one.toString()).toEqual("1"); + expect(oneDotFive.toString()).toEqual("1.5"); + }); + + it("throws for different fractional digits", () => { + const zero = Decimal.fromUserInput("0", 5); + expect(() => Decimal.fromUserInput("1", 1).minus(zero)).toThrowError(/do not match/i); + expect(() => Decimal.fromUserInput("1", 2).minus(zero)).toThrowError(/do not match/i); + expect(() => Decimal.fromUserInput("1", 3).minus(zero)).toThrowError(/do not match/i); + expect(() => Decimal.fromUserInput("1", 4).minus(zero)).toThrowError(/do not match/i); + + expect(() => Decimal.fromUserInput("1", 6).minus(zero)).toThrowError(/do not match/i); + expect(() => Decimal.fromUserInput("1", 7).minus(zero)).toThrowError(/do not match/i); + }); + + it("throws for negative results", () => { + const one = Decimal.fromUserInput("1", 5); + expect(() => Decimal.fromUserInput("0", 5).minus(one)).toThrowError(/must not be negative/i); + expect(() => Decimal.fromUserInput("0.5", 5).minus(one)).toThrowError(/must not be negative/i); + expect(() => Decimal.fromUserInput("0.98765", 5).minus(one)).toThrowError(/must not be negative/i); + }); + }); + describe("multiply", () => { it("returns correct values for Uint32", () => { const zero = Decimal.fromUserInput("0", 5); diff --git a/packages/math/src/decimal.ts b/packages/math/src/decimal.ts index 75ddf103..3f548e5b 100644 --- a/packages/math/src/decimal.ts +++ b/packages/math/src/decimal.ts @@ -126,6 +126,19 @@ export class Decimal { return new Decimal(sum.toString(), this.fractionalDigits); } + /** + * a.minus(b) returns a-b. + * + * Both values need to have the same fractional digits. + * The resulting difference needs to be non-negative. + */ + public minus(b: Decimal): Decimal { + if (this.fractionalDigits !== b.fractionalDigits) throw new Error("Fractional digits do not match"); + const difference = this.data.atomics.sub(new BN(b.atomics)); + if (difference.ltn(0)) throw new Error("Difference must not be negative"); + return new Decimal(difference.toString(), this.fractionalDigits); + } + /** * a.multiply(b) returns a*b. * diff --git a/packages/math/types/decimal.d.ts b/packages/math/types/decimal.d.ts index e2828055..c2f43165 100644 --- a/packages/math/types/decimal.d.ts +++ b/packages/math/types/decimal.d.ts @@ -25,6 +25,13 @@ export declare class Decimal { * Both values need to have the same fractional digits. */ plus(b: Decimal): Decimal; + /** + * a.minus(b) returns a-b. + * + * Both values need to have the same fractional digits. + * The resulting difference needs to be non-negative. + */ + minus(b: Decimal): Decimal; /** * a.multiply(b) returns a*b. * diff --git a/packages/proto-signing/nonces/1600158182 b/packages/proto-signing/nonces/1600158182 new file mode 100644 index 00000000..e69de29b diff --git a/packages/proto-signing/package.json b/packages/proto-signing/package.json index dd1ce24a..447e4587 100644 --- a/packages/proto-signing/package.json +++ b/packages/proto-signing/package.json @@ -1,7 +1,7 @@ { "name": "@cosmjs/proto-signing", "private": true, - "version": "0.22.2", + "version": "0.22.3", "description": "Utilities for protobuf based signing (Cosmos SDK 0.40+)", "contributors": [ "Will Clark ", @@ -50,8 +50,8 @@ "protobufjs": "~6.10.0" }, "devDependencies": { - "@cosmjs/encoding": "^0.22.2", - "@cosmjs/launchpad": "^0.22.2", - "@cosmjs/utils": "^0.22.2" + "@cosmjs/encoding": "^0.22.3", + "@cosmjs/launchpad": "^0.22.3", + "@cosmjs/utils": "^0.22.3" } } diff --git a/packages/socket/nonces/1600158182 b/packages/socket/nonces/1600158182 new file mode 100644 index 00000000..e69de29b diff --git a/packages/socket/package.json b/packages/socket/package.json index 66142393..aff8b0ac 100644 --- a/packages/socket/package.json +++ b/packages/socket/package.json @@ -1,6 +1,6 @@ { "name": "@cosmjs/socket", - "version": "0.22.2", + "version": "0.22.3", "description": "Utility functions for working with WebSockets", "contributors": [ "IOV SAS ", @@ -46,7 +46,7 @@ "pack-web": "yarn build-or-skip && webpack --mode development --config webpack.web.config.js" }, "dependencies": { - "@cosmjs/stream": "^0.22.2", + "@cosmjs/stream": "^0.22.3", "isomorphic-ws": "^4.0.1", "ws": "^6.2.0", "xstream": "^11.10.0" diff --git a/packages/stargate/package.json b/packages/stargate/package.json index c4fb5fd8..30889085 100644 --- a/packages/stargate/package.json +++ b/packages/stargate/package.json @@ -1,7 +1,7 @@ { "name": "@cosmjs/stargate", "private": true, - "version": "0.22.2", + "version": "0.22.3", "description": "Utilities for Cosmos SDK 0.40", "contributors": [ "Simon Warta " @@ -46,13 +46,13 @@ }, "dependencies": { "@confio/ics23": "^0.6.3", - "@cosmjs/encoding": "^0.22.2", - "@cosmjs/launchpad": "^0.22.2", - "@cosmjs/math": "^0.22.2", - "@cosmjs/proto-signing": "^0.22.2", - "@cosmjs/stream": "^0.22.2", - "@cosmjs/tendermint-rpc": "^0.22.2", - "@cosmjs/utils": "^0.22.2", + "@cosmjs/encoding": "^0.22.3", + "@cosmjs/launchpad": "^0.22.3", + "@cosmjs/math": "^0.22.3", + "@cosmjs/proto-signing": "^0.22.3", + "@cosmjs/stream": "^0.22.3", + "@cosmjs/tendermint-rpc": "^0.22.3", + "@cosmjs/utils": "^0.22.3", "long": "^4.0.0", "protobufjs": "~6.10.0" } diff --git a/packages/stream/nonces/1600158182 b/packages/stream/nonces/1600158182 new file mode 100644 index 00000000..e69de29b diff --git a/packages/stream/package.json b/packages/stream/package.json index 81d82e1a..123f797b 100644 --- a/packages/stream/package.json +++ b/packages/stream/package.json @@ -1,6 +1,6 @@ { "name": "@cosmjs/stream", - "version": "0.22.2", + "version": "0.22.3", "description": "Utility functions for producing and consuming streams", "contributors": [ "IOV SAS ", diff --git a/packages/tendermint-rpc/nonces/1600158182 b/packages/tendermint-rpc/nonces/1600158182 new file mode 100644 index 00000000..e69de29b diff --git a/packages/tendermint-rpc/package.json b/packages/tendermint-rpc/package.json index 17bec13e..f4aa430d 100644 --- a/packages/tendermint-rpc/package.json +++ b/packages/tendermint-rpc/package.json @@ -1,6 +1,6 @@ { "name": "@cosmjs/tendermint-rpc", - "version": "0.22.2", + "version": "0.22.3", "description": "Tendermint RPC clients", "contributors": [ "IOV SAS ", @@ -46,18 +46,18 @@ "pack-web": "yarn build-or-skip && webpack --mode development --config webpack.web.config.js" }, "dependencies": { - "@cosmjs/crypto": "^0.22.2", - "@cosmjs/encoding": "^0.22.2", - "@cosmjs/json-rpc": "^0.22.2", - "@cosmjs/math": "^0.22.2", - "@cosmjs/socket": "^0.22.2", - "@cosmjs/stream": "^0.22.2", + "@cosmjs/crypto": "^0.22.3", + "@cosmjs/encoding": "^0.22.3", + "@cosmjs/json-rpc": "^0.22.3", + "@cosmjs/math": "^0.22.3", + "@cosmjs/socket": "^0.22.3", + "@cosmjs/stream": "^0.22.3", "axios": "^0.19.0", "readonly-date": "^1.0.0", "type-tagger": "^1.0.0", "xstream": "^11.10.0" }, "devDependencies": { - "@cosmjs/utils": "^0.22.2" + "@cosmjs/utils": "^0.22.3" } } diff --git a/packages/utils/nonces/1600158182 b/packages/utils/nonces/1600158182 new file mode 100644 index 00000000..e69de29b diff --git a/packages/utils/package.json b/packages/utils/package.json index 5cb2367d..72b830ec 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -1,6 +1,6 @@ { "name": "@cosmjs/utils", - "version": "0.22.2", + "version": "0.22.3", "description": "Utility tools, primarily for testing code", "contributors": [ "IOV SAS "