quantumcoin 7.0.13 → 7.0.15

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 (213) hide show
  1. package/README-SDK.md +2 -0
  2. package/examples/node_modules/.bin/esbuild +16 -0
  3. package/examples/node_modules/.bin/esbuild.cmd +17 -0
  4. package/examples/node_modules/.bin/esbuild.ps1 +28 -0
  5. package/examples/node_modules/.bin/sdkgen +16 -0
  6. package/examples/node_modules/.bin/sdkgen.cmd +17 -0
  7. package/examples/node_modules/.bin/sdkgen.ps1 +28 -0
  8. package/examples/node_modules/.bin/tsx +16 -0
  9. package/examples/node_modules/.bin/tsx.cmd +17 -0
  10. package/examples/node_modules/.bin/tsx.ps1 +28 -0
  11. package/examples/node_modules/.package-lock.json +144 -0
  12. package/examples/node_modules/@esbuild/win32-x64/README.md +3 -0
  13. package/examples/node_modules/@esbuild/win32-x64/esbuild.exe +0 -0
  14. package/examples/node_modules/@esbuild/win32-x64/package.json +20 -0
  15. package/examples/node_modules/esbuild/LICENSE.md +21 -0
  16. package/examples/node_modules/esbuild/README.md +3 -0
  17. package/examples/node_modules/esbuild/bin/esbuild +223 -0
  18. package/examples/node_modules/esbuild/install.js +289 -0
  19. package/examples/node_modules/esbuild/lib/main.d.ts +716 -0
  20. package/examples/node_modules/esbuild/lib/main.js +2532 -0
  21. package/examples/node_modules/esbuild/package.json +49 -0
  22. package/examples/node_modules/get-tsconfig/LICENSE +21 -0
  23. package/examples/node_modules/get-tsconfig/README.md +235 -0
  24. package/examples/node_modules/get-tsconfig/dist/index.cjs +7 -0
  25. package/examples/node_modules/get-tsconfig/dist/index.d.cts +2088 -0
  26. package/examples/node_modules/get-tsconfig/dist/index.d.mts +2088 -0
  27. package/examples/node_modules/get-tsconfig/dist/index.mjs +7 -0
  28. package/examples/node_modules/get-tsconfig/package.json +46 -0
  29. package/examples/node_modules/quantum-coin-js-sdk/LICENSE +21 -0
  30. package/examples/node_modules/quantum-coin-js-sdk/LICENSE-wasm_exec.js.txt +30 -0
  31. package/examples/node_modules/quantum-coin-js-sdk/README.md +1689 -0
  32. package/examples/node_modules/quantum-coin-js-sdk/example/README.md +14 -0
  33. package/examples/node_modules/quantum-coin-js-sdk/example/conversion-example.js +19 -0
  34. package/examples/node_modules/quantum-coin-js-sdk/example/example-create-contract.js +396 -0
  35. package/examples/node_modules/quantum-coin-js-sdk/example/example-encode-decode-rlp.js +225 -0
  36. package/examples/node_modules/quantum-coin-js-sdk/example/example-event-pack-unpack.js +391 -0
  37. package/examples/node_modules/quantum-coin-js-sdk/example/example-misc.js +100 -0
  38. package/examples/node_modules/quantum-coin-js-sdk/example/example-rpc-send-signRawTransaction.js +318 -0
  39. package/examples/node_modules/quantum-coin-js-sdk/example/example-rpc-send.js +115 -0
  40. package/examples/node_modules/quantum-coin-js-sdk/example/example-send.js +69 -0
  41. package/examples/node_modules/quantum-coin-js-sdk/example/example-token-pack-unpack.js +960 -0
  42. package/examples/node_modules/quantum-coin-js-sdk/example/example-wallet-version4.js +34 -0
  43. package/examples/node_modules/quantum-coin-js-sdk/example/example-wallet.js +43 -0
  44. package/examples/node_modules/quantum-coin-js-sdk/example/example.js +405 -0
  45. package/examples/node_modules/quantum-coin-js-sdk/example/package-lock.json +56 -0
  46. package/examples/node_modules/quantum-coin-js-sdk/example/package.json +15 -0
  47. package/examples/node_modules/quantum-coin-js-sdk/index.d.ts +1047 -0
  48. package/examples/node_modules/quantum-coin-js-sdk/index.js +3182 -0
  49. package/examples/node_modules/quantum-coin-js-sdk/package.json +34 -0
  50. package/examples/node_modules/quantum-coin-js-sdk/tests/encrypted-32.json +1 -0
  51. package/examples/node_modules/quantum-coin-js-sdk/tests/encrypted-36.json +1 -0
  52. package/examples/node_modules/quantum-coin-js-sdk/tests/encrypted-48.json +1 -0
  53. package/examples/node_modules/quantum-coin-js-sdk/tests/generate-verify-vectors.js +91 -0
  54. package/examples/node_modules/quantum-coin-js-sdk/tests/get-gas-price.test.js +59 -0
  55. package/examples/node_modules/quantum-coin-js-sdk/tests/non-transactional.preinit.test.js +41 -0
  56. package/examples/node_modules/quantum-coin-js-sdk/tests/non-transactional.test.js +1389 -0
  57. package/examples/node_modules/quantum-coin-js-sdk/tests/sign-raw-keytype5-context-null.test.js +107 -0
  58. package/examples/node_modules/quantum-coin-js-sdk/tests/sign-raw-transaction.test.js +196 -0
  59. package/examples/node_modules/quantum-coin-js-sdk/tests/sign-verify.test.js +311 -0
  60. package/examples/node_modules/quantum-coin-js-sdk/tests/transactional.relay.test.js +131 -0
  61. package/examples/node_modules/quantum-coin-js-sdk/tests/transactional.rpc.test.js +103 -0
  62. package/examples/node_modules/quantum-coin-js-sdk/tests/verify-vectors.json +95035 -0
  63. package/examples/node_modules/quantum-coin-js-sdk/wasmBase64.d.ts +9 -0
  64. package/examples/node_modules/quantum-coin-js-sdk/wasmBase64.js +16 -0
  65. package/examples/node_modules/quantum-coin-js-sdk/wasm_exec.d.ts +0 -0
  66. package/examples/node_modules/quantum-coin-js-sdk/wasm_exec.js +587 -0
  67. package/examples/node_modules/resolve-pkg-maps/LICENSE +21 -0
  68. package/examples/node_modules/resolve-pkg-maps/README.md +216 -0
  69. package/examples/node_modules/resolve-pkg-maps/dist/index.cjs +1 -0
  70. package/examples/node_modules/resolve-pkg-maps/dist/index.d.cts +11 -0
  71. package/examples/node_modules/resolve-pkg-maps/dist/index.d.mts +11 -0
  72. package/examples/node_modules/resolve-pkg-maps/dist/index.mjs +1 -0
  73. package/examples/node_modules/resolve-pkg-maps/package.json +42 -0
  74. package/examples/node_modules/seed-words/.github/workflows/publish-npmjs.yaml +22 -0
  75. package/examples/node_modules/seed-words/BUILD.md +7 -0
  76. package/examples/node_modules/seed-words/LICENSE +121 -0
  77. package/examples/node_modules/seed-words/README.md +67 -0
  78. package/examples/node_modules/seed-words/dist/seedwords.d.ts +39 -0
  79. package/examples/node_modules/seed-words/package.json +27 -0
  80. package/examples/node_modules/seed-words/seedwords.js +315 -0
  81. package/examples/node_modules/seed-words/seedwords.txt +65536 -0
  82. package/examples/node_modules/seed-words/tsconfig.json +21 -0
  83. package/examples/node_modules/tsx/LICENSE +21 -0
  84. package/examples/node_modules/tsx/README.md +32 -0
  85. package/examples/node_modules/tsx/dist/cjs/api/index.cjs +1 -0
  86. package/examples/node_modules/tsx/dist/cjs/api/index.d.cts +35 -0
  87. package/examples/node_modules/tsx/dist/cjs/api/index.d.mts +35 -0
  88. package/examples/node_modules/tsx/dist/cjs/api/index.mjs +1 -0
  89. package/examples/node_modules/tsx/dist/cjs/index.cjs +1 -0
  90. package/examples/node_modules/tsx/dist/cjs/index.mjs +1 -0
  91. package/examples/node_modules/tsx/dist/cli.cjs +54 -0
  92. package/examples/node_modules/tsx/dist/cli.mjs +55 -0
  93. package/examples/node_modules/tsx/dist/client-BQVF1NaW.mjs +1 -0
  94. package/examples/node_modules/tsx/dist/client-D6NvIMSC.cjs +1 -0
  95. package/examples/node_modules/tsx/dist/esm/api/index.cjs +1 -0
  96. package/examples/node_modules/tsx/dist/esm/api/index.d.cts +35 -0
  97. package/examples/node_modules/tsx/dist/esm/api/index.d.mts +35 -0
  98. package/examples/node_modules/tsx/dist/esm/api/index.mjs +1 -0
  99. package/examples/node_modules/tsx/dist/esm/index.cjs +2 -0
  100. package/examples/node_modules/tsx/dist/esm/index.mjs +2 -0
  101. package/examples/node_modules/tsx/dist/get-pipe-path-BHW2eJdv.mjs +1 -0
  102. package/examples/node_modules/tsx/dist/get-pipe-path-BoR10qr8.cjs +1 -0
  103. package/examples/node_modules/tsx/dist/index-7AaEi15b.mjs +14 -0
  104. package/examples/node_modules/tsx/dist/index-BWFBUo6r.cjs +1 -0
  105. package/examples/node_modules/tsx/dist/index-gbaejti9.mjs +1 -0
  106. package/examples/node_modules/tsx/dist/index-gckBtVBf.cjs +14 -0
  107. package/examples/node_modules/tsx/dist/lexer-DQCqS3nf.mjs +3 -0
  108. package/examples/node_modules/tsx/dist/lexer-DgIbo0BU.cjs +3 -0
  109. package/examples/node_modules/tsx/dist/loader.cjs +1 -0
  110. package/examples/node_modules/tsx/dist/loader.mjs +1 -0
  111. package/examples/node_modules/tsx/dist/node-features-_8ZFwP_x.mjs +1 -0
  112. package/examples/node_modules/tsx/dist/node-features-roYmp9jK.cjs +1 -0
  113. package/examples/node_modules/tsx/dist/package-CeBgXWuR.mjs +1 -0
  114. package/examples/node_modules/tsx/dist/package-Dxt5kIHw.cjs +1 -0
  115. package/examples/node_modules/tsx/dist/patch-repl.cjs +1 -0
  116. package/examples/node_modules/tsx/dist/patch-repl.mjs +1 -0
  117. package/examples/node_modules/tsx/dist/preflight.cjs +1 -0
  118. package/examples/node_modules/tsx/dist/preflight.mjs +1 -0
  119. package/examples/node_modules/tsx/dist/register-2sWVXuRQ.cjs +1 -0
  120. package/examples/node_modules/tsx/dist/register-B7jrtLTO.mjs +1 -0
  121. package/examples/node_modules/tsx/dist/register-CFH5oNdT.mjs +4 -0
  122. package/examples/node_modules/tsx/dist/register-D46fvsV_.cjs +4 -0
  123. package/examples/node_modules/tsx/dist/repl.cjs +3 -0
  124. package/examples/node_modules/tsx/dist/repl.mjs +3 -0
  125. package/examples/node_modules/tsx/dist/require-D4F1Lv60.cjs +1 -0
  126. package/examples/node_modules/tsx/dist/require-DQxpCAr4.mjs +1 -0
  127. package/examples/node_modules/tsx/dist/suppress-warnings.cjs +1 -0
  128. package/examples/node_modules/tsx/dist/suppress-warnings.mjs +1 -0
  129. package/examples/node_modules/tsx/dist/temporary-directory-B83uKxJF.cjs +1 -0
  130. package/examples/node_modules/tsx/dist/temporary-directory-CwHp0_NW.mjs +1 -0
  131. package/examples/node_modules/tsx/dist/types-Cxp8y2TL.d.ts +5 -0
  132. package/examples/node_modules/tsx/package.json +68 -0
  133. package/examples/package-lock.json +6 -6
  134. package/examples/package.json +1 -1
  135. package/generate-sdk.js +30 -9
  136. package/package.json +2 -2
  137. package/src/abi/interface.js +11 -2
  138. package/src/abi/js-abi-coder.js +61 -2
  139. package/src/contract/contract.js +53 -5
  140. package/src/generator/index.js +152 -13
  141. package/src/index.d.ts +1 -0
  142. package/src/providers/index.d.ts +1 -0
  143. package/src/providers/provider.d.ts +16 -0
  144. package/src/providers/provider.js +178 -5
  145. package/src/utils/rlp.js +13 -1
  146. package/src/wallet/wallet.d.ts +10 -0
  147. package/src/wallet/wallet.js +136 -15
  148. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/examples/_test-wallet.js +1 -1
  149. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/examples/_test-wallet.ts +1 -1
  150. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/examples/deploy.js +1 -1
  151. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/examples/deploy.ts +1 -1
  152. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/examples/offline-signing.js +1 -1
  153. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/examples/offline-signing.ts +1 -1
  154. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/examples/write-operations.js +1 -1
  155. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/examples/write-operations.ts +1 -1
  156. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/package-lock.json +6 -6
  157. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/package.json +1 -1
  158. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/src/AllSolidityTypes__factory.js +3 -1
  159. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-js/test/e2e/AllSolidityTypes.e2e.test.js +1 -1
  160. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/examples/_test-wallet.js +1 -1
  161. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/examples/_test-wallet.ts +1 -1
  162. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/examples/deploy.js +1 -1
  163. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/examples/deploy.ts +1 -1
  164. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/examples/offline-signing.js +1 -1
  165. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/examples/offline-signing.ts +1 -1
  166. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/examples/write-operations.js +1 -1
  167. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/examples/write-operations.ts +1 -1
  168. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/package-lock.json +6 -6
  169. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/package.json +1 -1
  170. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/src/AllSolidityTypes__factory.ts +3 -1
  171. package/test/e2e/generated-sdks/all-solidity-types/all-solidity-types-ts/test/e2e/AllSolidityTypes.e2e.test.js +1 -1
  172. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/examples/_test-wallet.js +1 -1
  173. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/examples/_test-wallet.ts +1 -1
  174. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/examples/deploy.js +1 -1
  175. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/examples/deploy.ts +1 -1
  176. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/examples/offline-signing.js +1 -1
  177. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/examples/offline-signing.ts +1 -1
  178. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/examples/write-operations.js +1 -1
  179. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/examples/write-operations.ts +1 -1
  180. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/package-lock.json +6 -6
  181. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/package.json +1 -1
  182. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/src/SimpleERC20.js +9 -3
  183. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/src/SimpleERC20__factory.js +3 -1
  184. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-js/test/e2e/SimpleERC20.e2e.test.js +1 -1
  185. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/examples/_test-wallet.js +1 -1
  186. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/examples/_test-wallet.ts +1 -1
  187. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/examples/deploy.js +1 -1
  188. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/examples/deploy.ts +1 -1
  189. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/examples/offline-signing.js +1 -1
  190. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/examples/offline-signing.ts +1 -1
  191. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/examples/write-operations.js +1 -1
  192. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/examples/write-operations.ts +1 -1
  193. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/package-lock.json +6 -6
  194. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/package.json +1 -1
  195. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/src/SimpleERC20.ts +9 -3
  196. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/src/SimpleERC20__factory.ts +3 -1
  197. package/test/e2e/generated-sdks/simple-erc20/simple-erc20-ts/test/e2e/SimpleERC20.e2e.test.js +1 -1
  198. package/test/e2e/generator-interface.e2e.test.js +6 -4
  199. package/test/e2e/generator-interface.e2e.test.ts +6 -4
  200. package/test/security/abi-decoder-bounds.test.js +122 -0
  201. package/test/security/contract-overrides.test.js +112 -0
  202. package/test/security/generator-injection.test.js +195 -0
  203. package/test/security/malformed-input.test.js +26 -27
  204. package/test/security/rpc-numeric-bounds.test.js +81 -0
  205. package/test/security/rpc-trust.test.js +202 -0
  206. package/test/unit/abi-interface.test.js +12 -5
  207. package/test/unit/abi-interface.test.ts +8 -1
  208. package/test/unit/address-wallet.test.js +53 -0
  209. package/test/unit/address-wallet.test.ts +22 -0
  210. package/test/unit/encoding-units-rlp.test.js +35 -0
  211. package/test/unit/populate-transaction.test.js +33 -0
  212. package/test/unit/providers.test.js +53 -0
  213. package/test/unit/providers.test.ts +53 -0
@@ -0,0 +1,960 @@
1
+ const qcsdk = require('quantum-coin-js-sdk');
2
+
3
+ /**
4
+ * Example: Token Pack/Unpack Operations
5
+ *
6
+ * This example demonstrates how to use packMethodData and unpackMethodData
7
+ * to interact with ERC20 tokens and Router contracts.
8
+ */
9
+
10
+ // Configuration
11
+ var clientConfigVal = new qcsdk.Config("https://sdk.readrelay.quantumcoinapi.com", "https://sdk.writerelay.quantumcoinapi.com", 123123, "", ""); //Mainnet
12
+ //Mainnet Block Explorer: https://scan.quantumcoin.org
13
+
14
+ // RPC endpoint for making contract calls
15
+ const rpcEndpointUrl = 'https://public.rpc.quantumcoinapi.com';
16
+
17
+ // Contract addresses
18
+ const SWAP_ROUTER_ADDRESS = "0x41323EF72662185f44a03ea0ad8094a0C9e925aB1102679D8e957e838054aac5";
19
+ const FACTORY_ADDRESS = "0xbbF45a1B60044669793B444eD01Eb33e03Bb8cf3c5b6ae7887B218D05C5Cbf1d"; // Replace with actual Factory contract address
20
+ const TOKEN_A_ADDRESS = "0x1Bd75060B22686a9f32Af80BC02348c1BAeDBba06f47ad723885c92a6566B65d";
21
+ const TOKEN_B_ADDRESS = "0x12b803EC8529b26deEa8D7ff37B9457Ea15C05bD3fD0Ba2F3A4b95D53c82403a";
22
+ const ACCOUNT_HOLDER_ADDRESS = "0xd51773b5dde3f8e4d29ae42b5046510e2a11fd0c8e4175853d6227896eb445c6";
23
+
24
+ // ERC20 Token Standard ABI
25
+ const erc20ABI = [
26
+ {
27
+ "name": "balanceOf",
28
+ "type": "function",
29
+ "inputs": [
30
+ {"name": "account", "type": "address"}
31
+ ],
32
+ "outputs": [
33
+ {"name": "", "type": "uint256"}
34
+ ],
35
+ "stateMutability": "view"
36
+ },
37
+ {
38
+ "name": "name",
39
+ "type": "function",
40
+ "inputs": [],
41
+ "outputs": [
42
+ {"name": "", "type": "string"}
43
+ ],
44
+ "stateMutability": "view"
45
+ },
46
+ {
47
+ "name": "symbol",
48
+ "type": "function",
49
+ "inputs": [],
50
+ "outputs": [
51
+ {"name": "", "type": "string"}
52
+ ],
53
+ "stateMutability": "view"
54
+ },
55
+ {
56
+ "name": "decimals",
57
+ "type": "function",
58
+ "inputs": [],
59
+ "outputs": [
60
+ {"name": "", "type": "uint8"}
61
+ ],
62
+ "stateMutability": "view"
63
+ },
64
+ {
65
+ "name": "totalSupply",
66
+ "type": "function",
67
+ "inputs": [],
68
+ "outputs": [
69
+ {"name": "", "type": "uint256"}
70
+ ],
71
+ "stateMutability": "view"
72
+ },
73
+ {
74
+ "name": "owner",
75
+ "type": "function",
76
+ "inputs": [],
77
+ "outputs": [
78
+ {"name": "", "type": "address"}
79
+ ],
80
+ "stateMutability": "view"
81
+ }
82
+ ];
83
+
84
+ // QuantumSwap Router ABI
85
+ const quantumswapV2RouterABI = [
86
+ {
87
+ "name": "getAmountsIn",
88
+ "type": "function",
89
+ "inputs": [
90
+ {"name": "amountOut", "type": "uint256"},
91
+ {"name": "path", "type": "address[]"}
92
+ ],
93
+ "outputs": [
94
+ {"name": "amounts", "type": "uint256[]"}
95
+ ],
96
+ "stateMutability": "view"
97
+ },
98
+ {
99
+ "name": "getAmountsOut",
100
+ "type": "function",
101
+ "inputs": [
102
+ {"name": "amountIn", "type": "uint256"},
103
+ {"name": "path", "type": "address[]"}
104
+ ],
105
+ "outputs": [
106
+ {"name": "amounts", "type": "uint256[]"}
107
+ ],
108
+ "stateMutability": "view"
109
+ },
110
+ {
111
+ "name": "swapExactTokensForTokens",
112
+ "type": "function",
113
+ "inputs": [
114
+ {"name": "amountIn", "type": "uint256"},
115
+ {"name": "amountOutMin", "type": "uint256"},
116
+ {"name": "path", "type": "address[]"},
117
+ {"name": "to", "type": "address"},
118
+ {"name": "deadline", "type": "uint256"}
119
+ ],
120
+ "outputs": [
121
+ {"name": "amounts", "type": "uint256[]"}
122
+ ],
123
+ "stateMutability": "nonpayable"
124
+ },
125
+ {
126
+ "name": "swapTokensForExactTokens",
127
+ "type": "function",
128
+ "inputs": [
129
+ {"name": "amountOut", "type": "uint256"},
130
+ {"name": "amountInMax", "type": "uint256"},
131
+ {"name": "path", "type": "address[]"},
132
+ {"name": "to", "type": "address"},
133
+ {"name": "deadline", "type": "uint256"}
134
+ ],
135
+ "outputs": [
136
+ {"name": "amounts", "type": "uint256[]"}
137
+ ],
138
+ "stateMutability": "nonpayable"
139
+ }
140
+ ];
141
+
142
+ // Factory Contract ABI (for getting pair addresses)
143
+ const factoryABI = [
144
+ {
145
+ "name": "getPair",
146
+ "type": "function",
147
+ "inputs": [
148
+ {"name": "tokenA", "type": "address"},
149
+ {"name": "tokenB", "type": "address"}
150
+ ],
151
+ "outputs": [
152
+ {"name": "pair", "type": "address"}
153
+ ],
154
+ "stateMutability": "view"
155
+ },
156
+ {
157
+ "name": "allPairs",
158
+ "type": "function",
159
+ "inputs": [
160
+ {"name": "", "type": "uint256"}
161
+ ],
162
+ "outputs": [
163
+ {"name": "", "type": "address"}
164
+ ],
165
+ "stateMutability": "view"
166
+ },
167
+ {
168
+ "name": "allPairsLength",
169
+ "type": "function",
170
+ "inputs": [],
171
+ "outputs": [
172
+ {"name": "", "type": "uint256"}
173
+ ],
174
+ "stateMutability": "view"
175
+ }
176
+ ];
177
+
178
+ /**
179
+ * Helper function to make RPC calls
180
+ */
181
+ async function rpcCall(method, params) {
182
+ try {
183
+ const response = await fetch(rpcEndpointUrl, {
184
+ method: 'POST',
185
+ headers: {
186
+ 'Content-Type': 'application/json',
187
+ },
188
+ body: JSON.stringify({
189
+ jsonrpc: '2.0',
190
+ id: 1,
191
+ method: method,
192
+ params: params,
193
+ }),
194
+ });
195
+
196
+ if (!response.ok) {
197
+ throw new Error(`HTTP error! status: ${response.status}`);
198
+ }
199
+
200
+ const result = await response.json();
201
+ return result;
202
+ } catch (error) {
203
+ console.error('RPC call failed:', error);
204
+ throw error;
205
+ }
206
+ }
207
+
208
+ /**
209
+ * Helper function to get transaction count (nonce) for an address
210
+ */
211
+ async function getTransactionCount(address) {
212
+ const result = await rpcCall("eth_getTransactionCount", [address, "latest"]);
213
+ if (result.error) {
214
+ throw new Error(`Failed to get transaction count: ${result.error.message}`);
215
+ }
216
+ return parseInt(result.result, 16);
217
+ }
218
+
219
+ /**
220
+ * Helper function to send raw transaction
221
+ */
222
+ async function sendRawTransaction(signedTxData) {
223
+ const result = await rpcCall("eth_sendRawTransaction", [signedTxData]);
224
+ if (result.error) {
225
+ throw new Error(`Failed to send transaction: ${result.error.message}`);
226
+ }
227
+ return result.result; // Returns transaction hash
228
+ }
229
+
230
+ /**
231
+ * Helper function to convert wei to coins (ETH equivalent)
232
+ */
233
+ function weiToCoins(wei) {
234
+ const oneCoinInWei = BigInt("1000000000000000000");
235
+ const coins = Number(wei) / Number(oneCoinInWei);
236
+ return coins;
237
+ }
238
+
239
+ /**
240
+ * Example 1: Get token balance of an address
241
+ */
242
+ async function example1_GetTokenBalance() {
243
+ console.log('\n=== Example 1: Get Token Balance ===\n');
244
+
245
+ // Token contract address
246
+ const tokenAddress = TOKEN_A_ADDRESS;
247
+ // User address
248
+ const userAddress = ACCOUNT_HOLDER_ADDRESS;
249
+
250
+ const abiJSON = JSON.stringify(erc20ABI);
251
+
252
+ // Pack the balanceOf method call
253
+ const packResult = qcsdk.packMethodData(abiJSON, "balanceOf", userAddress);
254
+
255
+ if (packResult.error) {
256
+ console.error("Error packing balanceOf:", packResult.error);
257
+ return;
258
+ }
259
+
260
+ console.log("Packed data (hex):", packResult.result);
261
+
262
+ // Make RPC call to get the balance
263
+ const rpcResult = await rpcCall("eth_call", [
264
+ {
265
+ to: tokenAddress,
266
+ data: packResult.result
267
+ },
268
+ "latest"
269
+ ]);
270
+
271
+ if (rpcResult.error) {
272
+ console.error("RPC error:", rpcResult.error);
273
+ return;
274
+ }
275
+
276
+ const returnData = rpcResult.result;
277
+ console.log("Return data (hex):", returnData);
278
+
279
+ // Unpack the return value
280
+ const unpackResult = qcsdk.unpackMethodData(abiJSON, "balanceOf", returnData);
281
+
282
+ if (unpackResult.error) {
283
+ console.error("Error unpacking balanceOf:", unpackResult.error);
284
+ return;
285
+ }
286
+
287
+ // Parse the JSON result
288
+ const balance = JSON.parse(unpackResult.result)[0];
289
+ const balanceWei = BigInt(balance);
290
+ const balanceCoins = weiToCoins(balanceWei);
291
+ console.log("Token Contract Address:", TOKEN_A_ADDRESS);
292
+ console.log("ACCOUNT_HOLDER_ADDRESS:", ACCOUNT_HOLDER_ADDRESS);
293
+ console.log("Token balance (wei):", balance);
294
+ console.log("Token balance (coins):", balanceCoins.toFixed(9));
295
+ }
296
+
297
+ /**
298
+ * Example 2: Get token name, totalSupply, decimals, owner, and symbol
299
+ */
300
+ async function example2_GetTokenInfo() {
301
+ console.log('\n=== Example 2: Get Token Information ===\n');
302
+
303
+ // Token contract address
304
+ const tokenAddress = TOKEN_A_ADDRESS;
305
+ const abiJSON = JSON.stringify(erc20ABI);
306
+
307
+ // Helper function to call a view function
308
+ async function callViewFunction(methodName, ...args) {
309
+ const packResult = qcsdk.packMethodData(abiJSON, methodName, ...args);
310
+
311
+ if (packResult.error) {
312
+ console.error(`Error packing ${methodName}:`, packResult.error);
313
+ return null;
314
+ }
315
+
316
+ const rpcResult = await rpcCall("eth_call", [
317
+ {
318
+ to: tokenAddress,
319
+ data: packResult.result
320
+ },
321
+ "latest"
322
+ ]);
323
+
324
+ if (rpcResult.error) {
325
+ console.error(`RPC error for ${methodName}:`, rpcResult.error);
326
+ return null;
327
+ }
328
+
329
+ const unpackResult = qcsdk.unpackMethodData(abiJSON, methodName, rpcResult.result);
330
+
331
+ if (unpackResult.error) {
332
+ console.error(`Error unpacking ${methodName}:`, unpackResult.error);
333
+ return null;
334
+ }
335
+
336
+ return JSON.parse(unpackResult.result);
337
+ }
338
+
339
+ // Get token name
340
+ console.log("Getting token name...");
341
+ const nameResult = await callViewFunction("name");
342
+ if (nameResult) {
343
+ console.log("Token name:", nameResult[0]);
344
+ }
345
+
346
+ // Get token symbol
347
+ console.log("Getting token symbol...");
348
+ const symbolResult = await callViewFunction("symbol");
349
+ if (symbolResult) {
350
+ console.log("Token symbol:", symbolResult[0]);
351
+ }
352
+
353
+ // Get token decimals
354
+ console.log("Getting token decimals...");
355
+ const decimalsResult = await callViewFunction("decimals");
356
+ if (decimalsResult) {
357
+ console.log("Token decimals:", parseInt(decimalsResult[0], 10));
358
+ }
359
+
360
+ // Get total supply
361
+ console.log("Getting total supply...");
362
+ const totalSupplyResult = await callViewFunction("totalSupply");
363
+ if (totalSupplyResult) {
364
+ const totalSupply = totalSupplyResult[0];
365
+ const totalSupplyWei = BigInt(totalSupply);
366
+ const totalSupplyCoins = weiToCoins(totalSupplyWei);
367
+ console.log("Total supply (wei):", totalSupply);
368
+ console.log("Total supply (coins):", totalSupplyCoins.toFixed(9));
369
+ }
370
+
371
+ // Get owner
372
+ console.log("Getting token owner...");
373
+ const ownerResult = await callViewFunction("owner");
374
+ if (ownerResult) {
375
+ console.log("Token owner:", ownerResult[0]);
376
+ }
377
+ }
378
+
379
+ /**
380
+ * Example 3: Get pair address for two tokens
381
+ */
382
+ async function example3_GetPairAddress() {
383
+ console.log('\n=== Example 3: Get Pair Address for Two Tokens ===\n');
384
+
385
+ // Factory contract address
386
+ const factoryAddress = FACTORY_ADDRESS;
387
+ const abiJSON = JSON.stringify(factoryABI);
388
+
389
+ // Token addresses
390
+ const tokenA = TOKEN_A_ADDRESS;
391
+ const tokenB = TOKEN_B_ADDRESS;
392
+
393
+ console.log("Token A:", tokenA);
394
+ console.log("Token B:", tokenB);
395
+ console.log("Factory Address:", factoryAddress);
396
+
397
+ // Pack the getPair method call
398
+ const packResult = qcsdk.packMethodData(abiJSON, "getPair", tokenA, tokenB);
399
+
400
+ if (packResult.error) {
401
+ console.error("Error packing getPair:", packResult.error);
402
+ return;
403
+ }
404
+
405
+ console.log("Packed data (hex):", packResult.result);
406
+
407
+ // Make RPC call
408
+ const rpcResult = await rpcCall("eth_call", [
409
+ {
410
+ to: factoryAddress,
411
+ data: packResult.result
412
+ },
413
+ "latest"
414
+ ]);
415
+
416
+ if (rpcResult.error) {
417
+ console.error("RPC error:", rpcResult.error);
418
+ return;
419
+ }
420
+
421
+ console.log("RPC result (raw):", rpcResult.result);
422
+
423
+ // Check if result is empty or indicates a revert
424
+ if (!rpcResult.result || rpcResult.result === "0x" || rpcResult.result === "") {
425
+ console.error("\n❌ Error: Empty result returned from Factory contract.");
426
+ console.error("Possible reasons:");
427
+ console.error(" 1. Factory contract address does not exist or is incorrect");
428
+ console.error(" 2. Contract execution reverted (contract may not have getPair function)");
429
+ console.error(" 3. Factory contract is not deployed at the specified address");
430
+ console.error("\nPlease verify the Factory contract address:", factoryAddress);
431
+ return null;
432
+ }
433
+
434
+ // Check for revert indicator (some RPCs return "0x" for reverts, others may have specific error data)
435
+ // If the result is just "0x" or very short, it might be a revert
436
+ if (rpcResult.result.length <= 2) {
437
+ console.error("\n❌ Error: Contract execution likely reverted.");
438
+ console.error("The Factory contract may not have the getPair function or the contract address is incorrect.");
439
+ return null;
440
+ }
441
+
442
+ // Unpack the return value
443
+ const unpackResult = qcsdk.unpackMethodData(abiJSON, "getPair", rpcResult.result);
444
+
445
+ if (unpackResult.error) {
446
+ console.error("\n❌ Error unpacking getPair:", unpackResult.error);
447
+ console.error("This usually means:");
448
+ console.error(" 1. The Factory contract address is incorrect");
449
+ console.error(" 2. The contract doesn't have the getPair function");
450
+ console.error(" 3. The execution reverted (empty string returned)");
451
+ console.error("\nPlease verify the Factory contract address:", factoryAddress);
452
+ return null;
453
+ }
454
+
455
+ // Parse the JSON result
456
+ const parsed = JSON.parse(unpackResult.result);
457
+ const pairAddress = parsed[0];
458
+
459
+ console.log("\n✅ Pair Address:", pairAddress);
460
+
461
+ if (pairAddress === "0x0000000000000000000000000000000000000000000000000000000000000000" ||
462
+ pairAddress === null ||
463
+ pairAddress === undefined) {
464
+ console.log("⚠️ Note: Pair does not exist yet. It will be created when liquidity is first added.");
465
+ } else {
466
+ console.log("✅ Pair exists at the address above.");
467
+ }
468
+
469
+ return pairAddress;
470
+ }
471
+
472
+ /**
473
+ * Example 3a: Get amount in using QuantumSwap Router
474
+ */
475
+ async function example3a_GetAmountIn() {
476
+ console.log('\n=== Example 3a: Get Amount In (QuantumSwapV2 Router) ===\n');
477
+
478
+ // QuantumSwapV2 Router contract address
479
+ const routerAddress = SWAP_ROUTER_ADDRESS;
480
+ const abiJSON = JSON.stringify(quantumswapV2RouterABI);
481
+
482
+ // Example: Want 1 token out, calculate how much token in needed
483
+ // amountOut: 1 token (with 18 decimals) = 1000000000000000000 wei
484
+ const amountOut = "1000000000000000000";
485
+ const amountOutWei = BigInt(amountOut);
486
+ const amountOutCoins = weiToCoins(amountOutWei);
487
+
488
+ // Swap path: TokenA -> TokenB
489
+ const path = [TOKEN_A_ADDRESS, TOKEN_B_ADDRESS];
490
+
491
+ // Pack the getAmountsIn method call
492
+ const packResult = qcsdk.packMethodData(abiJSON, "getAmountsIn", amountOut, path);
493
+
494
+ if (packResult.error) {
495
+ console.error("Error packing getAmountsIn:", packResult.error);
496
+ return;
497
+ }
498
+
499
+ console.log("Packed data (hex):", packResult.result);
500
+ console.log("Amount out desired (wei):", amountOut);
501
+ console.log("Amount out desired (coins):", amountOutCoins.toFixed(9));
502
+ console.log("Swap path:", path);
503
+
504
+ // Make RPC call
505
+ const rpcResult = await rpcCall("eth_call", [
506
+ {
507
+ to: routerAddress,
508
+ data: packResult.result
509
+ },
510
+ "latest"
511
+ ]);
512
+
513
+ if (rpcResult.error) {
514
+ console.error("RPC error:", rpcResult.error);
515
+ return;
516
+ }
517
+
518
+ // Unpack the return value
519
+ const unpackResult = qcsdk.unpackMethodData(abiJSON, "getAmountsIn", rpcResult.result);
520
+
521
+ if (unpackResult.error) {
522
+ console.error("Error unpacking getAmountsIn:", unpackResult.error);
523
+ return;
524
+ }
525
+
526
+ // Parse the JSON result (array of amounts)
527
+ const parsed = JSON.parse(unpackResult.result);
528
+ console.log("Parsed result:", parsed);
529
+ const amounts = parsed[0];
530
+ console.log("Amounts array:", amounts);
531
+ console.log("Amounts array type:", typeof amounts, Array.isArray(amounts));
532
+
533
+ // Handle case where amounts might be a string or already an array
534
+ let amountsArray;
535
+ if (typeof amounts === 'string') {
536
+ // If it's a string, try to parse it again
537
+ try {
538
+ amountsArray = JSON.parse(amounts);
539
+ } catch (e) {
540
+ console.error("Failed to parse amounts string:", amounts);
541
+ return;
542
+ }
543
+ } else if (Array.isArray(amounts)) {
544
+ amountsArray = amounts;
545
+ } else {
546
+ console.error("Unexpected amounts type:", typeof amounts, amounts);
547
+ return;
548
+ }
549
+
550
+ const amountInWei = BigInt(amountsArray[0]);
551
+ const amountInCoins = weiToCoins(amountInWei);
552
+ const amountOutResultWei = BigInt(amountsArray[1]);
553
+ const amountOutResultCoins = weiToCoins(amountOutResultWei);
554
+ console.log("Amount in needed (wei):", amountsArray[0]);
555
+ console.log("Amount in needed (coins):", amountInCoins.toFixed(9));
556
+ console.log("Amount out (wei):", amountsArray[1]);
557
+ console.log("Amount out (coins):", amountOutResultCoins.toFixed(9));
558
+
559
+ return amounts;
560
+ }
561
+
562
+ /**
563
+ * Example 3b: Get amount out using QuantumSwapV2 Router
564
+ */
565
+ async function example3b_GetAmountOut() {
566
+ console.log('\n=== Example 3b: Get Amount Out (QuantumSwapV2 Router) ===\n');
567
+
568
+ // QuantumSwapV2 Router contract address
569
+ const routerAddress = SWAP_ROUTER_ADDRESS;
570
+ const abiJSON = JSON.stringify(quantumswapV2RouterABI);
571
+
572
+ // Example: Put in 1 token, calculate how much token out received
573
+ // amountIn: 1 token (with 18 decimals) = 1000000000000000000 wei
574
+ const amountIn = "1000000000000000000";
575
+ const amountInInputWei = BigInt(amountIn);
576
+ const amountInInputCoins = weiToCoins(amountInInputWei);
577
+
578
+ // Swap path: TokenA -> TokenB
579
+ const path = [TOKEN_A_ADDRESS, TOKEN_B_ADDRESS];
580
+
581
+ // Pack the getAmountsOut method call
582
+ const packResult = qcsdk.packMethodData(abiJSON, "getAmountsOut", amountIn, path);
583
+
584
+ if (packResult.error) {
585
+ console.error("Error packing getAmountsOut:", packResult.error);
586
+ return;
587
+ }
588
+
589
+ console.log("Packed data (hex):", packResult.result);
590
+ console.log("Amount in (wei):", amountIn);
591
+ console.log("Amount in (coins):", amountInInputCoins.toFixed(9));
592
+ console.log("Swap path:", path);
593
+
594
+ // Make RPC call
595
+ const rpcResult = await rpcCall("eth_call", [
596
+ {
597
+ to: routerAddress,
598
+ data: packResult.result
599
+ },
600
+ "latest"
601
+ ]);
602
+
603
+ if (rpcResult.error) {
604
+ console.error("RPC error:", rpcResult.error);
605
+ return;
606
+ }
607
+
608
+ // Unpack the return value
609
+ const unpackResult = qcsdk.unpackMethodData(abiJSON, "getAmountsOut", rpcResult.result);
610
+
611
+ if (unpackResult.error) {
612
+ console.error("Error unpacking getAmountsOut:", unpackResult.error);
613
+ return;
614
+ }
615
+
616
+ // Parse the JSON result (array of amounts)
617
+ const parsed = JSON.parse(unpackResult.result);
618
+ console.log("Parsed result:", parsed);
619
+ const amounts = parsed[0];
620
+ console.log("Amounts array:", amounts);
621
+ console.log("Amounts array type:", typeof amounts, Array.isArray(amounts));
622
+
623
+ // Handle case where amounts might be a string or already an array
624
+ let amountsArray;
625
+ if (typeof amounts === 'string') {
626
+ // If it's a string, try to parse it again
627
+ try {
628
+ amountsArray = JSON.parse(amounts);
629
+ } catch (e) {
630
+ console.error("Failed to parse amounts string:", amounts);
631
+ return;
632
+ }
633
+ } else if (Array.isArray(amounts)) {
634
+ amountsArray = amounts;
635
+ } else {
636
+ console.error("Unexpected amounts type:", typeof amounts, amounts);
637
+ return;
638
+ }
639
+
640
+ const amountInWei = BigInt(amountsArray[0]);
641
+ const amountInCoins = weiToCoins(amountInWei);
642
+ const amountOutWei = BigInt(amountsArray[1]);
643
+ const amountOutCoins = weiToCoins(amountOutWei);
644
+ console.log("Amount in (wei):", amountsArray[0]);
645
+ console.log("Amount in (coins):", amountInCoins.toFixed(9));
646
+ console.log("Amount out received (wei):", amountsArray[1]);
647
+ console.log("Amount out received (coins):", amountOutCoins.toFixed(9));
648
+
649
+ return amountsArray;
650
+ }
651
+
652
+ /**
653
+ * Example 4a: Swap exact tokens for tokens (using getAmountOut output)
654
+ * This example uses the output from getAmountOut to perform a swap
655
+ */
656
+ async function example4a_SwapExactTokensForTokens() {
657
+ console.log('\n=== Example 4a: Swap Exact Tokens For Tokens (using getAmountOut) ===\n');
658
+
659
+ // Step 1: Create a new wallet
660
+ console.log("Step 1: Creating a new wallet...");
661
+ const wallet = qcsdk.newWallet();
662
+ if (wallet === null) {
663
+ console.error("Failed to create wallet");
664
+ return;
665
+ }
666
+ console.log("Wallet address:", wallet.address);
667
+
668
+ // Step 2: Get the expected amount out
669
+ console.log("\nStep 2: Getting expected amount out...");
670
+ const amountsOut = await example3b_GetAmountOut();
671
+
672
+ if (!amountsOut || amountsOut.length < 2) {
673
+ console.error("Failed to get amount out");
674
+ return;
675
+ }
676
+
677
+ const amountIn = "1000000000000000000"; // 1 token
678
+ const amountInWei = BigInt(amountIn);
679
+ const amountInCoins = weiToCoins(amountInWei);
680
+ const amountOutMin = amountsOut[1]; // Use the amount out from getAmountsOut (with some slippage tolerance)
681
+
682
+ // Apply 1% slippage tolerance (multiply by 99/100)
683
+ const slippageTolerance = BigInt(amountOutMin) * BigInt(99) / BigInt(100);
684
+ const amountOutMinWithSlippage = slippageTolerance.toString();
685
+ const amountOutMinWei = BigInt(amountOutMin);
686
+ const amountOutMinCoins = weiToCoins(amountOutMinWei);
687
+ const amountOutMinWithSlippageWei = BigInt(amountOutMinWithSlippage);
688
+ const amountOutMinWithSlippageCoins = weiToCoins(amountOutMinWithSlippageWei);
689
+
690
+ console.log("\nStep 3: Preparing swap transaction...");
691
+ console.log("Amount in (wei):", amountIn);
692
+ console.log("Amount in (coins):", amountInCoins.toFixed(9));
693
+ console.log("Expected amount out (wei):", amountsOut[1]);
694
+ console.log("Expected amount out (coins):", amountOutMinCoins.toFixed(9));
695
+ console.log("Minimum amount out (with 1% slippage) (wei):", amountOutMinWithSlippage);
696
+ console.log("Minimum amount out (with 1% slippage) (coins):", amountOutMinWithSlippageCoins.toFixed(9));
697
+
698
+ // QuantumSwapV2 Router contract address
699
+ const routerAddress = SWAP_ROUTER_ADDRESS;
700
+ const abiJSON = JSON.stringify(quantumswapV2RouterABI);
701
+
702
+ // Swap path
703
+ const path = [TOKEN_A_ADDRESS, TOKEN_B_ADDRESS];
704
+
705
+ // Recipient address (use the wallet address we created)
706
+ const to = wallet.address;
707
+
708
+ // Deadline: 20 minutes from now (Unix timestamp in seconds, UTC)
709
+ // Date.now() already returns UTC milliseconds, so no conversion needed
710
+ const deadline = Math.floor(Date.now() / 1000) + 20 * 60;
711
+ const deadlineStr = deadline.toString();
712
+
713
+ // Pack the swapExactTokensForTokens method call
714
+ const packResult = qcsdk.packMethodData(
715
+ abiJSON,
716
+ "swapExactTokensForTokens",
717
+ amountIn,
718
+ amountOutMinWithSlippage,
719
+ path,
720
+ to,
721
+ deadlineStr
722
+ );
723
+
724
+ if (packResult.error) {
725
+ console.error("Error packing swapExactTokensForTokens:", packResult.error);
726
+ return;
727
+ }
728
+
729
+ console.log("Packed swap transaction data (hex):", packResult.result);
730
+
731
+ // Step 4: Get nonce for the wallet
732
+ console.log("\nStep 4: Getting transaction count (nonce)...");
733
+ let nonce;
734
+ try {
735
+ nonce = await getTransactionCount(wallet.address);
736
+ console.log("Nonce:", nonce);
737
+ } catch (error) {
738
+ console.error("Failed to get nonce:", error.message);
739
+ return;
740
+ }
741
+
742
+ // Step 5: Sign the transaction
743
+ console.log("\nStep 5: Signing transaction...");
744
+ const gasLimit = 300000; // Gas limit for swap transaction
745
+ const valueInWeiHex = "0x0"; // No native token value for token swaps
746
+
747
+ const transactionRequest = new qcsdk.TransactionSigningRequest(
748
+ wallet,
749
+ routerAddress,
750
+ valueInWeiHex,
751
+ nonce,
752
+ packResult.result,
753
+ gasLimit,
754
+ null // remarks
755
+ );
756
+
757
+ const signResult = qcsdk.signRawTransaction(transactionRequest);
758
+
759
+ if (signResult.resultCode !== 0) {
760
+ console.error("Failed to sign transaction. Result code:", signResult.resultCode);
761
+ return;
762
+ }
763
+
764
+ console.log("Transaction signed successfully");
765
+ console.log("Transaction hash:", signResult.txnHash);
766
+ console.log("Transaction data:", signResult.txnData);
767
+
768
+ // Step 6: Send the transaction
769
+ console.log("\nStep 6: Sending transaction to network...");
770
+ try {
771
+ const txHash = await sendRawTransaction(signResult.txnData);
772
+ console.log("Transaction sent successfully!");
773
+ console.log("Transaction ID (hash):", txHash);
774
+ console.log("You can view this transaction on the block explorer");
775
+ } catch (error) {
776
+ console.error("Failed to send transaction:", error.message);
777
+ }
778
+ }
779
+
780
+ /**
781
+ * Example 4b: Swap tokens for exact tokens (using getAmountIn output)
782
+ * This example uses the output from getAmountIn to perform a swap
783
+ */
784
+ async function example4b_SwapTokensForExactTokens() {
785
+ console.log('\n=== Example 4b: Swap Tokens For Exact Tokens (using getAmountIn) ===\n');
786
+
787
+ // Step 1: Create a new wallet
788
+ console.log("Step 1: Creating a new wallet...");
789
+ const wallet = qcsdk.newWallet();
790
+ if (wallet === null) {
791
+ console.error("Failed to create wallet");
792
+ return;
793
+ }
794
+ console.log("Wallet address:", wallet.address);
795
+
796
+ // Step 2: Get the required amount in
797
+ console.log("\nStep 2: Getting required amount in...");
798
+ const amountsIn = await example3a_GetAmountIn();
799
+
800
+ if (!amountsIn || amountsIn.length < 2) {
801
+ console.error("Failed to get amount in");
802
+ return;
803
+ }
804
+
805
+ const amountOut = "1000000000000000000"; // 1 token desired out
806
+ const amountOutWei = BigInt(amountOut);
807
+ const amountOutCoins = weiToCoins(amountOutWei);
808
+ const amountInMax = amountsIn[0]; // Use the amount in from getAmountsIn
809
+
810
+ // Apply 2% slippage tolerance (multiply by 102/100)
811
+ const slippageTolerance = BigInt(amountInMax) * BigInt(102) / BigInt(100);
812
+ const amountInMaxWithSlippage = slippageTolerance.toString();
813
+ const amountInMaxWei = BigInt(amountInMax);
814
+ const amountInMaxCoins = weiToCoins(amountInMaxWei);
815
+ const amountInMaxWithSlippageWei = BigInt(amountInMaxWithSlippage);
816
+ const amountInMaxWithSlippageCoins = weiToCoins(amountInMaxWithSlippageWei);
817
+
818
+ console.log("\nStep 3: Preparing swap transaction...");
819
+ console.log("Desired amount out (wei):", amountOut);
820
+ console.log("Desired amount out (coins):", amountOutCoins.toFixed(9));
821
+ console.log("Required amount in (wei):", amountsIn[0]);
822
+ console.log("Required amount in (coins):", amountInMaxCoins.toFixed(9));
823
+ console.log("Maximum amount in (with 2% slippage) (wei):", amountInMaxWithSlippage);
824
+ console.log("Maximum amount in (with 2% slippage) (coins):", amountInMaxWithSlippageCoins.toFixed(9));
825
+
826
+ // QuantumSwapV2 Router contract address
827
+ const routerAddress = SWAP_ROUTER_ADDRESS;
828
+ const abiJSON = JSON.stringify(quantumswapV2RouterABI);
829
+
830
+ // Swap path
831
+ const path = [TOKEN_A_ADDRESS, TOKEN_B_ADDRESS];
832
+
833
+ // Recipient address (use the wallet address we created)
834
+ const to = wallet.address;
835
+
836
+ // Deadline: 20 minutes from now (Unix timestamp in seconds, UTC)
837
+ // Date.now() already returns UTC milliseconds, so no conversion needed
838
+ const deadline = Math.floor(Date.now() / 1000) + 20 * 60;
839
+ const deadlineStr = deadline.toString();
840
+
841
+ // Pack the swapTokensForExactTokens method call
842
+ const packResult = qcsdk.packMethodData(
843
+ abiJSON,
844
+ "swapTokensForExactTokens",
845
+ amountOut,
846
+ amountInMaxWithSlippage,
847
+ path,
848
+ to,
849
+ deadlineStr
850
+ );
851
+
852
+ if (packResult.error) {
853
+ console.error("Error packing swapTokensForExactTokens:", packResult.error);
854
+ return;
855
+ }
856
+
857
+ console.log("Packed swap transaction data (hex):", packResult.result);
858
+
859
+ // Step 4: Get nonce for the wallet
860
+ console.log("\nStep 4: Getting transaction count (nonce)...");
861
+ let nonce;
862
+ try {
863
+ nonce = await getTransactionCount(wallet.address);
864
+ console.log("Nonce:", nonce);
865
+ } catch (error) {
866
+ console.error("Failed to get nonce:", error.message);
867
+ return;
868
+ }
869
+
870
+ // Step 5: Sign the transaction
871
+ console.log("\nStep 5: Signing transaction...");
872
+ const gasLimit = 300000; // Gas limit for swap transaction
873
+ const valueInWeiHex = "0x0"; // No native token value for token swaps
874
+
875
+ const transactionRequest = new qcsdk.TransactionSigningRequest(
876
+ wallet,
877
+ routerAddress,
878
+ valueInWeiHex,
879
+ nonce,
880
+ packResult.result,
881
+ gasLimit,
882
+ null // remarks
883
+ );
884
+
885
+ const signResult = qcsdk.signRawTransaction(transactionRequest);
886
+
887
+ if (signResult.resultCode !== 0) {
888
+ console.error("Failed to sign transaction. Result code:", signResult.resultCode);
889
+ return;
890
+ }
891
+
892
+ console.log("Transaction signed successfully");
893
+ console.log("Transaction hash:", signResult.txnHash);
894
+ console.log("Transaction data:", signResult.txnData);
895
+
896
+ // Step 6: Send the transaction
897
+ console.log("\nStep 6: Sending transaction to network...");
898
+ try {
899
+ const txHash = await sendRawTransaction(signResult.txnData);
900
+ console.log("Transaction sent successfully!");
901
+ console.log("Transaction ID (hash):", txHash);
902
+ console.log("You can view this transaction on the block explorer");
903
+ } catch (error) {
904
+ console.error("Failed to send transaction:", error.message);
905
+ }
906
+ }
907
+
908
+ /**
909
+ * Main execution function
910
+ */
911
+ async function main() {
912
+ // Initialize the SDK
913
+ const initResult = await qcsdk.initialize(clientConfigVal);
914
+ if (initResult === false) {
915
+ console.error("Initialize failed");
916
+ return;
917
+ }
918
+ console.log("SDK initialized successfully\n");
919
+
920
+ try {
921
+ // Example 1: Get token balance
922
+ await example1_GetTokenBalance();
923
+
924
+ // Example 2: Get token information
925
+ await example2_GetTokenInfo();
926
+
927
+ // Example 3: Get pair address for two tokens
928
+ await example3_GetPairAddress();
929
+
930
+ // Example 3a: Get amount in
931
+ await example3a_GetAmountIn();
932
+
933
+ // Example 3b: Get amount out
934
+ await example3b_GetAmountOut();
935
+
936
+ // Note: Swap examples (4a and 4b) create wallets, sign transactions, and send them
937
+ // Uncomment below to test swap examples:
938
+ await example4a_SwapExactTokensForTokens();
939
+ await example4b_SwapTokensForExactTokens();
940
+
941
+ } catch (error) {
942
+ console.error("Error running examples:", error);
943
+ }
944
+ }
945
+
946
+ // Run the examples
947
+ if (require.main === module) {
948
+ main().catch(console.error);
949
+ }
950
+
951
+ module.exports = {
952
+ example1_GetTokenBalance,
953
+ example2_GetTokenInfo,
954
+ example3_GetPairAddress,
955
+ example3a_GetAmountIn,
956
+ example3b_GetAmountOut,
957
+ example4a_SwapExactTokensForTokens,
958
+ example4b_SwapTokensForExactTokens
959
+ };
960
+