quantum-coin-js-sdk 2.0.1 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (232) hide show
  1. package/README.md +13 -0
  2. package/index.d.ts +9 -0
  3. package/index.js +37 -4
  4. package/package.json +12 -4
  5. package/wasmBase64.js +3 -2
  6. package/.idea/.name +0 -1
  7. package/.idea/modules.xml +0 -8
  8. package/.idea/quantum-coin-js-sdk.iml +0 -12
  9. package/.idea/vcs.xml +0 -6
  10. package/.vs/ProjectSettings.json +0 -3
  11. package/.vs/VSWorkspaceState.json +0 -8
  12. package/.vs/quantum-coin-js-sdk/FileContentIndex/a9d6de82-cfc3-4c22-ac38-06d706ff3673.vsidx +0 -0
  13. package/.vs/quantum-coin-js-sdk/FileContentIndex/fdc816df-1898-4153-99f2-c69ef4c2283b.vsidx +0 -0
  14. package/.vs/quantum-coin-js-sdk/config/applicationhost.config +0 -1011
  15. package/.vs/quantum-coin-js-sdk/v17/.wsuo +0 -0
  16. package/.vs/quantum-coin-js-sdk/v17/DocumentLayout.backup.json +0 -39
  17. package/.vs/quantum-coin-js-sdk/v17/DocumentLayout.json +0 -57
  18. package/.vs/slnx.sqlite +0 -0
  19. package/example/README.md +0 -14
  20. package/example/conversion-example.js +0 -19
  21. package/example/example-create-contract.js +0 -396
  22. package/example/example-encode-decode-rlp.js +0 -225
  23. package/example/example-event-pack-unpack.js +0 -391
  24. package/example/example-misc.js +0 -96
  25. package/example/example-token-pack-unpack.js +0 -960
  26. package/example/example-wallet-version4.js +0 -34
  27. package/example/example-wallet.js +0 -39
  28. package/example/example.js +0 -115
  29. package/example/node_modules/.bin/sdkgen +0 -16
  30. package/example/node_modules/.bin/sdkgen.cmd +0 -17
  31. package/example/node_modules/.bin/sdkgen.ps1 +0 -28
  32. package/example/node_modules/.package-lock.json +0 -51
  33. package/example/node_modules/quantumcoin/.github/workflows/publish-npmjs.yaml +0 -22
  34. package/example/node_modules/quantumcoin/LICENSE +0 -21
  35. package/example/node_modules/quantumcoin/README-SDK.md +0 -828
  36. package/example/node_modules/quantumcoin/README.md +0 -197
  37. package/example/node_modules/quantumcoin/config.d.ts +0 -50
  38. package/example/node_modules/quantumcoin/config.js +0 -123
  39. package/example/node_modules/quantumcoin/examples/AllSolidityTypes.sol +0 -184
  40. package/example/node_modules/quantumcoin/examples/SimpleIERC20.sol +0 -74
  41. package/example/node_modules/quantumcoin/examples/events.js +0 -41
  42. package/example/node_modules/quantumcoin/examples/events.ts +0 -35
  43. package/example/node_modules/quantumcoin/examples/example-generator-sdk-js.js +0 -100
  44. package/example/node_modules/quantumcoin/examples/example-generator-sdk-js.ts +0 -77
  45. package/example/node_modules/quantumcoin/examples/example-generator-sdk-ts.js +0 -100
  46. package/example/node_modules/quantumcoin/examples/example-generator-sdk-ts.ts +0 -77
  47. package/example/node_modules/quantumcoin/examples/example.js +0 -67
  48. package/example/node_modules/quantumcoin/examples/example.ts +0 -56
  49. package/example/node_modules/quantumcoin/examples/offline-signing.js +0 -77
  50. package/example/node_modules/quantumcoin/examples/offline-signing.ts +0 -65
  51. package/example/node_modules/quantumcoin/examples/package-lock.json +0 -596
  52. package/example/node_modules/quantumcoin/examples/package.json +0 -32
  53. package/example/node_modules/quantumcoin/examples/read-operations.js +0 -32
  54. package/example/node_modules/quantumcoin/examples/read-operations.ts +0 -31
  55. package/example/node_modules/quantumcoin/examples/sdk-generator-erc20.inline.json +0 -251
  56. package/example/node_modules/quantumcoin/examples/solidity-types.ts +0 -43
  57. package/example/node_modules/quantumcoin/examples/wallet-offline.js +0 -36
  58. package/example/node_modules/quantumcoin/examples/wallet-offline.ts +0 -26
  59. package/example/node_modules/quantumcoin/generate-sdk.js +0 -1846
  60. package/example/node_modules/quantumcoin/index.js +0 -12
  61. package/example/node_modules/quantumcoin/package.json +0 -95
  62. package/example/node_modules/quantumcoin/scripts/copy-declarations.js +0 -31
  63. package/example/node_modules/quantumcoin/scripts/run-all-one-by-one.js +0 -151
  64. package/example/node_modules/quantumcoin/src/abi/fragments.d.ts +0 -42
  65. package/example/node_modules/quantumcoin/src/abi/fragments.js +0 -63
  66. package/example/node_modules/quantumcoin/src/abi/index.d.ts +0 -13
  67. package/example/node_modules/quantumcoin/src/abi/index.js +0 -9
  68. package/example/node_modules/quantumcoin/src/abi/interface.d.ts +0 -128
  69. package/example/node_modules/quantumcoin/src/abi/interface.js +0 -605
  70. package/example/node_modules/quantumcoin/src/abi/js-abi-coder.d.ts +0 -8
  71. package/example/node_modules/quantumcoin/src/abi/js-abi-coder.js +0 -538
  72. package/example/node_modules/quantumcoin/src/constants.d.ts +0 -61
  73. package/example/node_modules/quantumcoin/src/constants.js +0 -94
  74. package/example/node_modules/quantumcoin/src/contract/contract-factory.d.ts +0 -28
  75. package/example/node_modules/quantumcoin/src/contract/contract-factory.js +0 -111
  76. package/example/node_modules/quantumcoin/src/contract/contract.d.ts +0 -113
  77. package/example/node_modules/quantumcoin/src/contract/contract.js +0 -408
  78. package/example/node_modules/quantumcoin/src/contract/index.d.ts +0 -9
  79. package/example/node_modules/quantumcoin/src/contract/index.js +0 -9
  80. package/example/node_modules/quantumcoin/src/errors/index.d.ts +0 -92
  81. package/example/node_modules/quantumcoin/src/errors/index.js +0 -200
  82. package/example/node_modules/quantumcoin/src/generator/index.d.ts +0 -81
  83. package/example/node_modules/quantumcoin/src/generator/index.js +0 -1571
  84. package/example/node_modules/quantumcoin/src/index.d.ts +0 -122
  85. package/example/node_modules/quantumcoin/src/index.js +0 -41
  86. package/example/node_modules/quantumcoin/src/internal/hex.d.ts +0 -68
  87. package/example/node_modules/quantumcoin/src/internal/hex.js +0 -180
  88. package/example/node_modules/quantumcoin/src/providers/extra-providers.d.ts +0 -139
  89. package/example/node_modules/quantumcoin/src/providers/extra-providers.js +0 -614
  90. package/example/node_modules/quantumcoin/src/providers/index.d.ts +0 -17
  91. package/example/node_modules/quantumcoin/src/providers/index.js +0 -10
  92. package/example/node_modules/quantumcoin/src/providers/json-rpc-provider.d.ts +0 -12
  93. package/example/node_modules/quantumcoin/src/providers/json-rpc-provider.js +0 -89
  94. package/example/node_modules/quantumcoin/src/providers/provider.d.ts +0 -206
  95. package/example/node_modules/quantumcoin/src/providers/provider.js +0 -524
  96. package/example/node_modules/quantumcoin/src/types/index.d.ts +0 -214
  97. package/example/node_modules/quantumcoin/src/types/index.js +0 -9
  98. package/example/node_modules/quantumcoin/src/utils/address.d.ts +0 -58
  99. package/example/node_modules/quantumcoin/src/utils/address.js +0 -144
  100. package/example/node_modules/quantumcoin/src/utils/encoding.d.ts +0 -120
  101. package/example/node_modules/quantumcoin/src/utils/encoding.js +0 -306
  102. package/example/node_modules/quantumcoin/src/utils/fixednumber.d.ts +0 -57
  103. package/example/node_modules/quantumcoin/src/utils/fixednumber.js +0 -366
  104. package/example/node_modules/quantumcoin/src/utils/hashing.d.ts +0 -76
  105. package/example/node_modules/quantumcoin/src/utils/hashing.js +0 -298
  106. package/example/node_modules/quantumcoin/src/utils/index.d.ts +0 -63
  107. package/example/node_modules/quantumcoin/src/utils/index.js +0 -14
  108. package/example/node_modules/quantumcoin/src/utils/result.d.ts +0 -57
  109. package/example/node_modules/quantumcoin/src/utils/result.js +0 -128
  110. package/example/node_modules/quantumcoin/src/utils/rlp.d.ts +0 -12
  111. package/example/node_modules/quantumcoin/src/utils/rlp.js +0 -215
  112. package/example/node_modules/quantumcoin/src/utils/units.d.ts +0 -29
  113. package/example/node_modules/quantumcoin/src/utils/units.js +0 -107
  114. package/example/node_modules/quantumcoin/src/wallet/index.d.ts +0 -10
  115. package/example/node_modules/quantumcoin/src/wallet/index.js +0 -8
  116. package/example/node_modules/quantumcoin/src/wallet/wallet.d.ts +0 -192
  117. package/example/node_modules/quantumcoin/src/wallet/wallet.js +0 -713
  118. package/example/node_modules/quantumcoin/test/e2e/all-solidity-types.dynamic.test.js +0 -207
  119. package/example/node_modules/quantumcoin/test/e2e/all-solidity-types.dynamic.test.ts +0 -191
  120. package/example/node_modules/quantumcoin/test/e2e/all-solidity-types.fixtures.js +0 -231
  121. package/example/node_modules/quantumcoin/test/e2e/all-solidity-types.generated-sdks.e2e.test.js +0 -387
  122. package/example/node_modules/quantumcoin/test/e2e/all-solidity-types.generated-sdks.e2e.test.ts +0 -350
  123. package/example/node_modules/quantumcoin/test/e2e/generator-interface.e2e.test.js +0 -165
  124. package/example/node_modules/quantumcoin/test/e2e/generator-interface.e2e.test.ts +0 -160
  125. package/example/node_modules/quantumcoin/test/e2e/helpers.js +0 -59
  126. package/example/node_modules/quantumcoin/test/e2e/signing-context-and-fee.e2e.test.js +0 -141
  127. package/example/node_modules/quantumcoin/test/e2e/signing-context-and-fee.e2e.test.ts +0 -128
  128. package/example/node_modules/quantumcoin/test/e2e/simple-erc20.generated-sdks.e2e.test.js +0 -168
  129. package/example/node_modules/quantumcoin/test/e2e/simple-erc20.generated-sdks.e2e.test.ts +0 -141
  130. package/example/node_modules/quantumcoin/test/e2e/transactional.test.js +0 -245
  131. package/example/node_modules/quantumcoin/test/e2e/transactional.test.ts +0 -208
  132. package/example/node_modules/quantumcoin/test/e2e/typed-generator.e2e.test.js +0 -407
  133. package/example/node_modules/quantumcoin/test/e2e/typed-generator.e2e.test.ts +0 -337
  134. package/example/node_modules/quantumcoin/test/fixtures/ConstructorParam.sol +0 -23
  135. package/example/node_modules/quantumcoin/test/fixtures/MultiContracts.sol +0 -37
  136. package/example/node_modules/quantumcoin/test/fixtures/SimpleStorage.sol +0 -18
  137. package/example/node_modules/quantumcoin/test/fixtures/StakingContract.abi.json +0 -1
  138. package/example/node_modules/quantumcoin/test/integration/ipc-provider.test.js +0 -49
  139. package/example/node_modules/quantumcoin/test/integration/ipc-provider.test.ts +0 -44
  140. package/example/node_modules/quantumcoin/test/integration/provider.test.js +0 -88
  141. package/example/node_modules/quantumcoin/test/integration/provider.test.ts +0 -85
  142. package/example/node_modules/quantumcoin/test/integration/ws-provider.test.js +0 -41
  143. package/example/node_modules/quantumcoin/test/integration/ws-provider.test.ts +0 -38
  144. package/example/node_modules/quantumcoin/test/security/abi-decoder-bounds.test.js +0 -122
  145. package/example/node_modules/quantumcoin/test/security/contract-overrides.test.js +0 -112
  146. package/example/node_modules/quantumcoin/test/security/generator-injection.test.js +0 -195
  147. package/example/node_modules/quantumcoin/test/security/malformed-input.test.js +0 -330
  148. package/example/node_modules/quantumcoin/test/security/malformed-input.test.ts +0 -35
  149. package/example/node_modules/quantumcoin/test/security/rpc-numeric-bounds.test.js +0 -81
  150. package/example/node_modules/quantumcoin/test/security/rpc-trust.test.js +0 -202
  151. package/example/node_modules/quantumcoin/test/unit/_encrypted-output.txt +0 -6
  152. package/example/node_modules/quantumcoin/test/unit/_log-encrypted-jsons.js +0 -45
  153. package/example/node_modules/quantumcoin/test/unit/_write-keystore-fixture.js +0 -16
  154. package/example/node_modules/quantumcoin/test/unit/abi-interface.test.js +0 -110
  155. package/example/node_modules/quantumcoin/test/unit/abi-interface.test.ts +0 -109
  156. package/example/node_modules/quantumcoin/test/unit/address-wallet.test.js +0 -923
  157. package/example/node_modules/quantumcoin/test/unit/address-wallet.test.ts +0 -877
  158. package/example/node_modules/quantumcoin/test/unit/browser-provider.test.js +0 -85
  159. package/example/node_modules/quantumcoin/test/unit/browser-provider.test.ts +0 -79
  160. package/example/node_modules/quantumcoin/test/unit/contract.test.js +0 -85
  161. package/example/node_modules/quantumcoin/test/unit/contract.test.ts +0 -83
  162. package/example/node_modules/quantumcoin/test/unit/encoding-units-rlp.test.js +0 -127
  163. package/example/node_modules/quantumcoin/test/unit/encoding-units-rlp.test.ts +0 -91
  164. package/example/node_modules/quantumcoin/test/unit/errors.test.js +0 -77
  165. package/example/node_modules/quantumcoin/test/unit/errors.test.ts +0 -76
  166. package/example/node_modules/quantumcoin/test/unit/filter-by-blockhash.test.js +0 -55
  167. package/example/node_modules/quantumcoin/test/unit/filter-by-blockhash.test.ts +0 -54
  168. package/example/node_modules/quantumcoin/test/unit/fixednumber.test.js +0 -656
  169. package/example/node_modules/quantumcoin/test/unit/fixednumber.test.ts +0 -660
  170. package/example/node_modules/quantumcoin/test/unit/fixtures/encrypted-keystores-48-32-36.js +0 -9
  171. package/example/node_modules/quantumcoin/test/unit/generate-contract-cli.test.js +0 -42
  172. package/example/node_modules/quantumcoin/test/unit/generate-contract-cli.test.ts +0 -41
  173. package/example/node_modules/quantumcoin/test/unit/generate-sdk-artifacts-json.test.js +0 -113
  174. package/example/node_modules/quantumcoin/test/unit/generate-sdk-artifacts-json.test.ts +0 -110
  175. package/example/node_modules/quantumcoin/test/unit/generator.test.js +0 -149
  176. package/example/node_modules/quantumcoin/test/unit/generator.test.ts +0 -148
  177. package/example/node_modules/quantumcoin/test/unit/hashing.test.js +0 -64
  178. package/example/node_modules/quantumcoin/test/unit/hashing.test.ts +0 -63
  179. package/example/node_modules/quantumcoin/test/unit/init.test.js +0 -39
  180. package/example/node_modules/quantumcoin/test/unit/init.test.ts +0 -38
  181. package/example/node_modules/quantumcoin/test/unit/interface.test.js +0 -56
  182. package/example/node_modules/quantumcoin/test/unit/interface.test.ts +0 -54
  183. package/example/node_modules/quantumcoin/test/unit/internal-hex.test.js +0 -81
  184. package/example/node_modules/quantumcoin/test/unit/internal-hex.test.ts +0 -80
  185. package/example/node_modules/quantumcoin/test/unit/populate-transaction.test.js +0 -98
  186. package/example/node_modules/quantumcoin/test/unit/populate-transaction.test.ts +0 -64
  187. package/example/node_modules/quantumcoin/test/unit/providers.test.js +0 -252
  188. package/example/node_modules/quantumcoin/test/unit/providers.test.ts +0 -251
  189. package/example/node_modules/quantumcoin/test/unit/result.test.js +0 -80
  190. package/example/node_modules/quantumcoin/test/unit/result.test.ts +0 -79
  191. package/example/node_modules/quantumcoin/test/unit/solidity-types.test.js +0 -49
  192. package/example/node_modules/quantumcoin/test/unit/solidity-types.test.ts +0 -39
  193. package/example/node_modules/quantumcoin/test/unit/utils.test.js +0 -57
  194. package/example/node_modules/quantumcoin/test/unit/utils.test.ts +0 -56
  195. package/example/node_modules/quantumcoin/test/verbose-logger.js +0 -74
  196. package/example/node_modules/quantumcoin/tsconfig.build.json +0 -14
  197. package/example/node_modules/seed-words/.github/workflows/publish-npmjs.yaml +0 -22
  198. package/example/node_modules/seed-words/BUILD.md +0 -7
  199. package/example/node_modules/seed-words/LICENSE +0 -121
  200. package/example/node_modules/seed-words/README.md +0 -67
  201. package/example/node_modules/seed-words/dist/seedwords.d.ts +0 -39
  202. package/example/node_modules/seed-words/package.json +0 -27
  203. package/example/node_modules/seed-words/seedwords.js +0 -315
  204. package/example/node_modules/seed-words/seedwords.txt +0 -65536
  205. package/example/node_modules/seed-words/tsconfig.json +0 -21
  206. package/example/package-lock.json +0 -60
  207. package/example/package.json +0 -15
  208. package/scripts/build-browser-tests.js +0 -29
  209. package/scripts/build-wasm.js +0 -85
  210. package/tests/browser/bundle.js +0 -97643
  211. package/tests/browser/entry.js +0 -57
  212. package/tests/browser/harness.html +0 -20
  213. package/tests/browser/run-browser.js +0 -50
  214. package/tests/encrypted-32.json +0 -1
  215. package/tests/encrypted-36.json +0 -1
  216. package/tests/encrypted-48.json +0 -1
  217. package/tests/generate-verify-vectors.js +0 -88
  218. package/tests/get-gas-price.test.js +0 -59
  219. package/tests/hashing.test.js +0 -34
  220. package/tests/non-transactional.preinit.test.js +0 -33
  221. package/tests/non-transactional.test.js +0 -1318
  222. package/tests/sign-raw-keytype5-context-null.test.js +0 -105
  223. package/tests/sign-raw-transaction.test.js +0 -194
  224. package/tests/sign-verify.test.js +0 -308
  225. package/tests/specs/assert.js +0 -74
  226. package/tests/specs/crypto.spec.js +0 -104
  227. package/tests/specs/hex.js +0 -31
  228. package/tests/specs/preinit.spec.js +0 -19
  229. package/tests/specs/sign.spec.js +0 -90
  230. package/tests/specs/wallet.spec.js +0 -98
  231. package/tests/transactional.rpc.test.js +0 -102
  232. package/tests/verify-vectors.json +0 -95035
@@ -1,660 +0,0 @@
1
- /**
2
- * @testCategory unit
3
- * @blockchainRequired false
4
- * @transactional false
5
- * @description FixedNumber: creation, arithmetic, comparison, rounding, conversion, and error handling
6
- */
7
-
8
- import { describe, it } from "node:test";
9
- import assert from "node:assert/strict";
10
- import qc from "../../index";
11
-
12
- const { FixedNumber } = qc;
13
-
14
- // ---------------------------------------------------------------------------
15
- // Ported from ethers.js v5 test-utils.ts (FixedNumber section)
16
- // ---------------------------------------------------------------------------
17
-
18
- describe("Address + Wallet (offline)", () => {
19
- // wrapper describe to match test runner expectations
20
- });
21
-
22
- describe("FixedNumber (ethers v5 ported)", () => {
23
- describe("Creation from value", () => {
24
- const tests: Array<{ value: string | number; expected: string }> = [
25
- { value: "0.0", expected: "0.0" },
26
- { value: "-0.0", expected: "0.0" },
27
- { value: "1.0", expected: "1.0" },
28
- { value: "1.00", expected: "1.0" },
29
- { value: "01.00", expected: "1.0" },
30
- { value: 1, expected: "1.0" },
31
- { value: "-1.0", expected: "-1.0" },
32
- { value: "-1.00", expected: "-1.0" },
33
- { value: "-01.00", expected: "-1.0" },
34
- { value: -1, expected: "-1.0" },
35
- ];
36
-
37
- for (const test of tests) {
38
- it(`from(${test.value}) => "${test.expected}"`, () => {
39
- const value = FixedNumber.from(test.value);
40
- assert.equal(value.toString(), test.expected);
41
- });
42
- }
43
- });
44
-
45
- describe("Rounding", () => {
46
- const tests = [
47
- { value: "1.0", round: 1, expected: "1.0" },
48
- { value: "1.4", round: 1, expected: "1.4" },
49
- { value: "1.4", round: 2, expected: "1.4" },
50
- { value: "1.4", round: 0, expected: "1.0" },
51
- { value: "1.5", round: 0, expected: "2.0" },
52
- { value: "1.6", round: 0, expected: "2.0" },
53
- { value: "-1.0", round: 1, expected: "-1.0" },
54
- { value: "-1.4", round: 1, expected: "-1.4" },
55
- { value: "-1.4", round: 2, expected: "-1.4" },
56
- { value: "-1.4", round: 0, expected: "-1.0" },
57
- { value: "-1.5", round: 0, expected: "-2.0" },
58
- { value: "-1.6", round: 0, expected: "-2.0" },
59
- { value: "1.51", round: 1, expected: "1.5" },
60
- { value: "1.55", round: 1, expected: "1.6" },
61
- ];
62
-
63
- for (const test of tests) {
64
- it(`round(${test.value}, ${test.round}) => "${test.expected}"`, () => {
65
- const value = FixedNumber.from(test.value).round(test.round);
66
- assert.equal(value.toString(), test.expected);
67
- });
68
- }
69
- });
70
-
71
- describe("Floor / Ceiling", () => {
72
- const tests = [
73
- { value: "1.0", ceiling: "1.0", floor: "1.0" },
74
- { value: "1.1", ceiling: "2.0", floor: "1.0" },
75
- { value: "1.9", ceiling: "2.0", floor: "1.0" },
76
- { value: "-1.0", ceiling: "-1.0", floor: "-1.0" },
77
- { value: "-1.1", ceiling: "-1.0", floor: "-2.0" },
78
- { value: "-1.9", ceiling: "-1.0", floor: "-2.0" },
79
- ];
80
-
81
- for (const test of tests) {
82
- it(`floor/ceiling(${test.value})`, () => {
83
- const value = FixedNumber.from(test.value);
84
- assert.equal(value.floor().toString(), test.floor);
85
- assert.equal(value.ceiling().toString(), test.ceiling);
86
- });
87
- }
88
- });
89
- });
90
-
91
- // ---------------------------------------------------------------------------
92
- // New v6 API tests
93
- // ---------------------------------------------------------------------------
94
-
95
- describe("FixedNumber (v6 API)", () => {
96
- describe("Properties", () => {
97
- it("exposes format, signed, width, decimals, value", () => {
98
- const f = FixedNumber.fromString("1.5");
99
- assert.equal(f.format, "fixed128x18");
100
- assert.equal(f.signed, true);
101
- assert.equal(f.width, 128);
102
- assert.equal(f.decimals, 18);
103
- assert.equal(typeof f.value, "bigint");
104
- assert.equal(f.value, 1500000000000000000n);
105
- });
106
-
107
- it("unsigned format properties", () => {
108
- const f = FixedNumber.fromString("10", "ufixed32x0");
109
- assert.equal(f.format, "ufixed32x0");
110
- assert.equal(f.signed, false);
111
- assert.equal(f.width, 32);
112
- assert.equal(f.decimals, 0);
113
- });
114
- });
115
-
116
- describe("fromString", () => {
117
- it("parses with custom format", () => {
118
- const f = FixedNumber.fromString("3.14", "fixed128x2");
119
- assert.equal(f.toString(), "3.14");
120
- assert.equal(f.value, 314n);
121
- });
122
-
123
- it("parses zero-decimal format", () => {
124
- const f = FixedNumber.fromString("42", "fixed128x0");
125
- assert.equal(f.toString(), "42");
126
- assert.equal(f.value, 42n);
127
- });
128
-
129
- it("parses unsigned format", () => {
130
- const f = FixedNumber.fromString("255", "ufixed8x0");
131
- assert.equal(f.toString(), "255");
132
- });
133
- });
134
-
135
- describe("fromValue", () => {
136
- it("creates from bigint with decimals", () => {
137
- const f = FixedNumber.fromValue(1500000000000000000n, 18);
138
- assert.equal(f.toString(), "1.5");
139
- });
140
-
141
- it("creates from number", () => {
142
- const f = FixedNumber.fromValue(100, 2, "fixed128x2");
143
- assert.equal(f.toString(), "1.0");
144
- });
145
- });
146
-
147
- describe("fromBytes round-trip", () => {
148
- it("roundtrips through hex bytes", () => {
149
- const f1 = FixedNumber.fromString("42", "ufixed32x0");
150
- const hex = f1.toHexString();
151
- const bytes = Buffer.from(hex.replace(/^0x/, ""), "hex");
152
- const f2 = FixedNumber.fromBytes(bytes, "ufixed32x0");
153
- assert.equal(f2.toString(), "42");
154
- });
155
- });
156
-
157
- describe("from() dispatch", () => {
158
- it("from string", () => {
159
- assert.equal(FixedNumber.from("2.5").toString(), "2.5");
160
- });
161
-
162
- it("from number", () => {
163
- assert.equal(FixedNumber.from(3).toString(), "3.0");
164
- });
165
-
166
- it("from bigint", () => {
167
- assert.equal(FixedNumber.from(5n).toString(), "5.0");
168
- });
169
- });
170
-
171
- describe("Safe arithmetic", () => {
172
- it("add", () => {
173
- const a = FixedNumber.fromString("1.5");
174
- const b = FixedNumber.fromString("2.3");
175
- assert.equal(a.add(b).toString(), "3.8");
176
- });
177
-
178
- it("sub", () => {
179
- const a = FixedNumber.fromString("5.0");
180
- const b = FixedNumber.fromString("2.3");
181
- assert.equal(a.sub(b).toString(), "2.7");
182
- });
183
-
184
- it("mul", () => {
185
- const a = FixedNumber.fromString("2.0");
186
- const b = FixedNumber.fromString("3.5");
187
- assert.equal(a.mul(b).toString(), "7.0");
188
- });
189
-
190
- it("div", () => {
191
- const a = FixedNumber.fromString("7.0");
192
- const b = FixedNumber.fromString("2.0");
193
- assert.equal(a.div(b).toString(), "3.5");
194
- });
195
- });
196
-
197
- describe("Unsafe arithmetic (silent overflow)", () => {
198
- it("addUnsafe wraps on overflow in fixed8x0", () => {
199
- const a = FixedNumber.fromString("127", "fixed8x0");
200
- const b = FixedNumber.fromString("1", "fixed8x0");
201
- const result = a.addUnsafe(b);
202
- assert.equal(typeof result.toString(), "string");
203
- });
204
-
205
- it("subUnsafe wraps on underflow in fixed8x0", () => {
206
- const a = FixedNumber.fromString("-128", "fixed8x0");
207
- const b = FixedNumber.fromString("1", "fixed8x0");
208
- const result = a.subUnsafe(b);
209
- assert.equal(typeof result.toString(), "string");
210
- });
211
- });
212
-
213
- describe("Comparison", () => {
214
- it("cmp returns -1, 0, 1", () => {
215
- const a = FixedNumber.fromString("1.0");
216
- const b = FixedNumber.fromString("2.0");
217
- const c = FixedNumber.fromString("1.0");
218
- assert.equal(a.cmp(b), -1);
219
- assert.equal(b.cmp(a), 1);
220
- assert.equal(a.cmp(c), 0);
221
- });
222
-
223
- it("eq, lt, lte, gt, gte", () => {
224
- const a = FixedNumber.fromString("1.0");
225
- const b = FixedNumber.fromString("2.0");
226
- assert.equal(a.eq(a), true);
227
- assert.equal(a.eq(b), false);
228
- assert.equal(a.lt(b), true);
229
- assert.equal(a.lte(b), true);
230
- assert.equal(a.lte(a), true);
231
- assert.equal(b.gt(a), true);
232
- assert.equal(b.gte(a), true);
233
- assert.equal(b.gte(b), true);
234
- });
235
-
236
- it("cmp across different decimal formats", () => {
237
- const a = FixedNumber.fromString("1.5", "fixed128x2");
238
- const b = FixedNumber.fromString("1.5", "fixed128x18");
239
- assert.equal(a.cmp(b), 0);
240
- });
241
- });
242
-
243
- describe("isZero / isNegative", () => {
244
- it("isZero", () => {
245
- assert.equal(FixedNumber.fromString("0.0").isZero(), true);
246
- assert.equal(FixedNumber.fromString("1.0").isZero(), false);
247
- });
248
-
249
- it("isNegative", () => {
250
- assert.equal(FixedNumber.fromString("-1.0").isNegative(), true);
251
- assert.equal(FixedNumber.fromString("0.0").isNegative(), false);
252
- assert.equal(FixedNumber.fromString("1.0").isNegative(), false);
253
- });
254
- });
255
-
256
- describe("toUnsafeFloat", () => {
257
- it("returns approximate float", () => {
258
- const f = FixedNumber.fromString("3.14");
259
- assert.ok(Math.abs(f.toUnsafeFloat() - 3.14) < 0.001);
260
- });
261
- });
262
-
263
- describe("toFormat", () => {
264
- it("converts between formats", () => {
265
- const f = FixedNumber.fromString("1.5", "fixed128x18");
266
- const g = f.toFormat("fixed128x2");
267
- assert.equal(g.toString(), "1.5");
268
- assert.equal(g.format, "fixed128x2");
269
- });
270
- });
271
-
272
- describe("isFixedNumber", () => {
273
- it("returns true for FixedNumber", () => {
274
- assert.equal(FixedNumber.isFixedNumber(FixedNumber.from("1.0")), true);
275
- });
276
-
277
- it("returns false for non-FixedNumber", () => {
278
- assert.equal(FixedNumber.isFixedNumber("1.0"), false);
279
- assert.equal(FixedNumber.isFixedNumber(1), false);
280
- assert.equal(FixedNumber.isFixedNumber({}), false);
281
- assert.equal(FixedNumber.isFixedNumber(null), false);
282
- });
283
- });
284
-
285
- describe("toHexString", () => {
286
- it("returns hex for unsigned value", () => {
287
- const f = FixedNumber.fromString("255", "ufixed8x0");
288
- assert.equal(f.toHexString(), "0xff");
289
- });
290
-
291
- it("returns hex for positive signed value", () => {
292
- const f = FixedNumber.fromString("1", "fixed8x0");
293
- assert.equal(f.toHexString(), "0x01");
294
- });
295
- });
296
- });
297
-
298
- // ---------------------------------------------------------------------------
299
- // Negative tests -- invalid inputs
300
- // ---------------------------------------------------------------------------
301
-
302
- describe("FixedNumber negative tests -- invalid inputs", () => {
303
- it("fromString rejects empty string", () => {
304
- assert.throws(() => FixedNumber.fromString(""), /invalid/);
305
- });
306
-
307
- it("fromString rejects non-numeric string", () => {
308
- assert.throws(() => FixedNumber.fromString("abc"), /invalid/);
309
- });
310
-
311
- it("fromString rejects multiple decimal points", () => {
312
- assert.throws(() => FixedNumber.fromString("1.2.3"), /invalid/);
313
- });
314
-
315
- it("fromString rejects whitespace-only string", () => {
316
- assert.throws(() => FixedNumber.fromString(" "), /invalid/);
317
- });
318
-
319
- it("fromString rejects too many decimals for format", () => {
320
- assert.throws(() => FixedNumber.fromString("1.123", "fixed128x2"), /too many decimals/i);
321
- });
322
-
323
- it("fromValue rejects NaN", () => {
324
- assert.throws(() => FixedNumber.fromValue(NaN, 0), /invalid/);
325
- });
326
-
327
- it("fromValue rejects Infinity", () => {
328
- assert.throws(() => FixedNumber.fromValue(Infinity, 0), /invalid/);
329
- });
330
-
331
- it("fromValue rejects object", () => {
332
- assert.throws(() => FixedNumber.fromValue({} as any, 0), /invalid/);
333
- });
334
-
335
- it("from() rejects boolean", () => {
336
- assert.throws(() => FixedNumber.from(true), /invalid/);
337
- });
338
-
339
- it("from() rejects function", () => {
340
- assert.throws(() => FixedNumber.from(() => {}), /invalid/);
341
- });
342
-
343
- it("constructor is guarded", () => {
344
- assert.throws(() => new (FixedNumber as any)({}, 0n, {}), /cannot use FixedNumber constructor/);
345
- });
346
- });
347
-
348
- // ---------------------------------------------------------------------------
349
- // Negative tests -- overflow
350
- // ---------------------------------------------------------------------------
351
-
352
- describe("FixedNumber negative tests -- overflow", () => {
353
- it("add throws on signed overflow (127 + 1 in fixed8x0)", () => {
354
- const a = FixedNumber.fromString("127", "fixed8x0");
355
- const b = FixedNumber.fromString("1", "fixed8x0");
356
- assert.throws(() => a.add(b), (e: any) => e.code === "NUMERIC_FAULT" && e.fault === "overflow");
357
- });
358
-
359
- it("sub throws on signed underflow (-128 - 1 in fixed8x0)", () => {
360
- const a = FixedNumber.fromString("-128", "fixed8x0");
361
- const b = FixedNumber.fromString("1", "fixed8x0");
362
- assert.throws(() => a.sub(b), (e: any) => e.code === "NUMERIC_FAULT" && e.fault === "overflow");
363
- });
364
-
365
- it("mul throws on overflow", () => {
366
- const a = FixedNumber.fromString("127", "fixed8x0");
367
- const b = FixedNumber.fromString("2", "fixed8x0");
368
- assert.throws(() => a.mul(b), (e: any) => e.code === "NUMERIC_FAULT" && e.fault === "overflow");
369
- });
370
-
371
- it("unsigned sub throws on 0 - 1 (ufixed8x0)", () => {
372
- const a = FixedNumber.fromString("0", "ufixed8x0");
373
- const b = FixedNumber.fromString("1", "ufixed8x0");
374
- assert.throws(() => a.sub(b), (e: any) => e.code === "NUMERIC_FAULT" && e.fault === "overflow");
375
- });
376
-
377
- it("fromString throws overflow for 256 in ufixed8x0", () => {
378
- assert.throws(
379
- () => FixedNumber.fromString("256", "ufixed8x0"),
380
- (e: any) => e.code === "NUMERIC_FAULT" && e.fault === "overflow",
381
- );
382
- });
383
-
384
- it("fromString throws overflow for 128 in fixed8x0", () => {
385
- assert.throws(
386
- () => FixedNumber.fromString("128", "fixed8x0"),
387
- (e: any) => e.code === "NUMERIC_FAULT" && e.fault === "overflow",
388
- );
389
- });
390
-
391
- it("fromValue throws overflow when scaled value exceeds range", () => {
392
- assert.throws(
393
- () => FixedNumber.fromValue(256n, 0, "ufixed8x0"),
394
- (e: any) => e.code === "NUMERIC_FAULT" && e.fault === "overflow",
395
- );
396
- });
397
- });
398
-
399
- // ---------------------------------------------------------------------------
400
- // Negative tests -- underflow (precision loss)
401
- // ---------------------------------------------------------------------------
402
-
403
- describe("FixedNumber negative tests -- underflow", () => {
404
- it("mulSignal throws on precision loss", () => {
405
- const a = FixedNumber.fromString("0.1", "fixed128x1");
406
- const b = FixedNumber.fromString("0.1", "fixed128x1");
407
- assert.throws(() => a.mulSignal(b), (e: any) => e.code === "NUMERIC_FAULT" && e.fault === "underflow");
408
- });
409
-
410
- it("divSignal throws when division is not exact", () => {
411
- const a = FixedNumber.fromString("1.0");
412
- const b = FixedNumber.fromString("3.0");
413
- assert.throws(() => a.divSignal(b), (e: any) => e.code === "NUMERIC_FAULT" && e.fault === "underflow");
414
- });
415
-
416
- it("mul (non-signal) silently truncates", () => {
417
- const a = FixedNumber.fromString("0.1", "fixed128x1");
418
- const b = FixedNumber.fromString("0.1", "fixed128x1");
419
- const result = a.mul(b);
420
- assert.equal(result.toString(), "0.0");
421
- });
422
-
423
- it("div (non-signal) silently truncates", () => {
424
- const a = FixedNumber.fromString("1.0");
425
- const b = FixedNumber.fromString("3.0");
426
- const result = a.div(b);
427
- assert.equal(typeof result.toString(), "string");
428
- });
429
- });
430
-
431
- // ---------------------------------------------------------------------------
432
- // Negative tests -- division by zero
433
- // ---------------------------------------------------------------------------
434
-
435
- describe("FixedNumber negative tests -- division by zero", () => {
436
- it("div throws on zero divisor", () => {
437
- const a = FixedNumber.fromString("1.0");
438
- const b = FixedNumber.fromString("0.0");
439
- assert.throws(() => a.div(b), (e: any) => e.code === "NUMERIC_FAULT" && e.fault === "divide-by-zero");
440
- });
441
-
442
- it("divUnsafe throws on zero divisor", () => {
443
- const a = FixedNumber.fromString("1.0");
444
- const b = FixedNumber.fromString("0.0");
445
- assert.throws(() => a.divUnsafe(b), (e: any) => e.code === "NUMERIC_FAULT" && e.fault === "divide-by-zero");
446
- });
447
-
448
- it("divSignal throws on zero divisor", () => {
449
- const a = FixedNumber.fromString("1.0");
450
- const b = FixedNumber.fromString("0.0");
451
- assert.throws(() => a.divSignal(b), (e: any) => e.code === "NUMERIC_FAULT" && e.fault === "divide-by-zero");
452
- });
453
- });
454
-
455
- // ---------------------------------------------------------------------------
456
- // Negative tests -- unsigned format rejects negatives
457
- // ---------------------------------------------------------------------------
458
-
459
- describe("FixedNumber negative tests -- unsigned rejects negatives", () => {
460
- it("fromString rejects negative in unsigned format", () => {
461
- assert.throws(
462
- () => FixedNumber.fromString("-1", "ufixed128x18"),
463
- (e: any) => e.code === "NUMERIC_FAULT" && e.fault === "overflow",
464
- );
465
- });
466
-
467
- it("fromValue rejects negative in unsigned format", () => {
468
- assert.throws(
469
- () => FixedNumber.fromValue(-1n, 0, "ufixed128x18"),
470
- (e: any) => e.code === "NUMERIC_FAULT" && e.fault === "overflow",
471
- );
472
- });
473
- });
474
-
475
- // ---------------------------------------------------------------------------
476
- // Negative tests -- format parsing
477
- // ---------------------------------------------------------------------------
478
-
479
- describe("FixedNumber negative tests -- format parsing", () => {
480
- it("rejects invalid format string", () => {
481
- assert.throws(() => FixedNumber.fromString("1", "fixd128x18"), /invalid fixed format/);
482
- });
483
-
484
- it("rejects non-byte-aligned width", () => {
485
- assert.throws(() => FixedNumber.fromString("1", "fixed7x0"), /not byte aligned/);
486
- });
487
-
488
- it("rejects decimals > 80", () => {
489
- assert.throws(() => FixedNumber.fromString("1", "fixed128x81"), /too large/);
490
- });
491
-
492
- it("rejects invalid format object (wrong type for signed)", () => {
493
- assert.throws(() => FixedNumber.fromString("1", { signed: "yes" } as any), /not boolean/);
494
- });
495
-
496
- it("rejects invalid format object (non-byte-aligned width)", () => {
497
- assert.throws(() => FixedNumber.fromString("1", { width: 7 }), /not byte aligned/);
498
- });
499
-
500
- it("rejects invalid format object (decimals too large)", () => {
501
- assert.throws(() => FixedNumber.fromString("1", { decimals: 81 }), /too large/);
502
- });
503
- });
504
-
505
- // ---------------------------------------------------------------------------
506
- // Negative tests -- incompatible format arithmetic
507
- // ---------------------------------------------------------------------------
508
-
509
- describe("FixedNumber negative tests -- incompatible formats", () => {
510
- it("add throws with different formats", () => {
511
- const a = FixedNumber.fromString("1.0", "fixed128x18");
512
- const b = FixedNumber.fromString("1.0", "fixed128x2");
513
- assert.throws(() => a.add(b), /incompatible format/);
514
- });
515
-
516
- it("sub throws with different formats", () => {
517
- const a = FixedNumber.fromString("1.0", "fixed128x18");
518
- const b = FixedNumber.fromString("1.0", "fixed128x2");
519
- assert.throws(() => a.sub(b), /incompatible format/);
520
- });
521
-
522
- it("mul throws with different formats", () => {
523
- const a = FixedNumber.fromString("1.0", "fixed128x18");
524
- const b = FixedNumber.fromString("1.0", "fixed128x2");
525
- assert.throws(() => a.mul(b), /incompatible format/);
526
- });
527
-
528
- it("div throws with different formats", () => {
529
- const a = FixedNumber.fromString("1.0", "fixed128x18");
530
- const b = FixedNumber.fromString("1.0", "fixed128x2");
531
- assert.throws(() => a.div(b), /incompatible format/);
532
- });
533
- });
534
-
535
- // ---------------------------------------------------------------------------
536
- // Boundary condition tests
537
- // ---------------------------------------------------------------------------
538
-
539
- describe("FixedNumber boundary conditions", () => {
540
- describe("fixed8x0 range [-128, 127]", () => {
541
- it("accepts min value -128", () => {
542
- const f = FixedNumber.fromString("-128", "fixed8x0");
543
- assert.equal(f.toString(), "-128");
544
- });
545
-
546
- it("accepts max value 127", () => {
547
- const f = FixedNumber.fromString("127", "fixed8x0");
548
- assert.equal(f.toString(), "127");
549
- });
550
-
551
- it("rejects 128", () => {
552
- assert.throws(() => FixedNumber.fromString("128", "fixed8x0"),
553
- (e: any) => e.code === "NUMERIC_FAULT");
554
- });
555
-
556
- it("rejects -129", () => {
557
- assert.throws(() => FixedNumber.fromString("-129", "fixed8x0"),
558
- (e: any) => e.code === "NUMERIC_FAULT");
559
- });
560
- });
561
-
562
- describe("ufixed8x0 range [0, 255]", () => {
563
- it("accepts 0", () => {
564
- assert.equal(FixedNumber.fromString("0", "ufixed8x0").toString(), "0");
565
- });
566
-
567
- it("accepts 255", () => {
568
- assert.equal(FixedNumber.fromString("255", "ufixed8x0").toString(), "255");
569
- });
570
-
571
- it("rejects 256", () => {
572
- assert.throws(() => FixedNumber.fromString("256", "ufixed8x0"),
573
- (e: any) => e.code === "NUMERIC_FAULT");
574
- });
575
-
576
- it("rejects -1", () => {
577
- assert.throws(() => FixedNumber.fromString("-1", "ufixed8x0"),
578
- (e: any) => e.code === "NUMERIC_FAULT");
579
- });
580
- });
581
-
582
- describe("fixed16x2 range [-327.68, 327.67]", () => {
583
- it("accepts 327.67", () => {
584
- assert.equal(FixedNumber.fromString("327.67", "fixed16x2").toString(), "327.67");
585
- });
586
-
587
- it("accepts -327.68", () => {
588
- assert.equal(FixedNumber.fromString("-327.68", "fixed16x2").toString(), "-327.68");
589
- });
590
-
591
- it("rejects 327.68", () => {
592
- assert.throws(() => FixedNumber.fromString("327.68", "fixed16x2"),
593
- (e: any) => e.code === "NUMERIC_FAULT");
594
- });
595
-
596
- it("arithmetic at edges: 327.67 + 0.01 overflows", () => {
597
- const a = FixedNumber.fromString("327.67", "fixed16x2");
598
- const b = FixedNumber.fromString("0.01", "fixed16x2");
599
- assert.throws(() => a.add(b), (e: any) => e.code === "NUMERIC_FAULT");
600
- });
601
- });
602
-
603
- describe("zero-decimal format (fixed128x0)", () => {
604
- it("toString does not append .0", () => {
605
- const f = FixedNumber.fromString("42", "fixed128x0");
606
- assert.equal(f.toString(), "42");
607
- });
608
- });
609
-
610
- describe("high decimals (fixed256x40)", () => {
611
- it("construction works", () => {
612
- const f = FixedNumber.fromString("1.0", "fixed256x40");
613
- assert.equal(f.decimals, 40);
614
- assert.ok(f.toString().startsWith("1.0"));
615
- });
616
- });
617
-
618
- describe("rounding edge cases", () => {
619
- it("round(0) on 0.5 rounds up", () => {
620
- assert.equal(FixedNumber.from("0.5").round(0).toString(), "1.0");
621
- });
622
-
623
- it("round(0) on -0.5 rounds away from zero", () => {
624
- assert.equal(FixedNumber.from("-0.5").round(0).toString(), "-1.0");
625
- });
626
- });
627
-
628
- describe("floor/ceiling at exact integers", () => {
629
- it("floor of integer returns same value", () => {
630
- const f = FixedNumber.from("5.0");
631
- assert.equal(f.floor().toString(), "5.0");
632
- });
633
-
634
- it("ceiling of integer returns same value", () => {
635
- const f = FixedNumber.from("5.0");
636
- assert.equal(f.ceiling().toString(), "5.0");
637
- });
638
- });
639
-
640
- describe("toFormat precision loss", () => {
641
- it("throws when converting to lower precision with data loss", () => {
642
- const f = FixedNumber.fromString("1.23", "fixed128x18");
643
- assert.throws(() => f.toFormat("fixed128x1"), /too many decimals/i);
644
- });
645
- });
646
-
647
- describe("large values near 2^127 - 1 for fixed128x0", () => {
648
- it("accepts 2^127 - 1", () => {
649
- const maxVal = (2n ** 127n - 1n).toString();
650
- const f = FixedNumber.fromString(maxVal, "fixed128x0");
651
- assert.equal(f.toString(), maxVal);
652
- });
653
-
654
- it("rejects 2^127", () => {
655
- const overVal = (2n ** 127n).toString();
656
- assert.throws(() => FixedNumber.fromString(overVal, "fixed128x0"),
657
- (e: any) => e.code === "NUMERIC_FAULT");
658
- });
659
- });
660
- });