quantumswap 0.0.1

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 (77) hide show
  1. package/.github/workflows/publish-npmjs.yaml +22 -0
  2. package/LICENSE +21 -0
  3. package/README.md +287 -0
  4. package/examples/_test-wallet.js +17 -0
  5. package/examples/deploy-IERC20.js +23 -0
  6. package/examples/deploy-QuantumSwapV2ERC20.js +23 -0
  7. package/examples/deploy-QuantumSwapV2Factory.js +23 -0
  8. package/examples/deploy-QuantumSwapV2Pair.js +23 -0
  9. package/examples/deploy-QuantumSwapV2Router02.js +23 -0
  10. package/examples/deploy-WQ.js +23 -0
  11. package/examples/events-IERC20.js +20 -0
  12. package/examples/events-QuantumSwapV2ERC20.js +20 -0
  13. package/examples/events-QuantumSwapV2Factory.js +20 -0
  14. package/examples/events-QuantumSwapV2Pair.js +20 -0
  15. package/examples/events-QuantumSwapV2Router02.js +20 -0
  16. package/examples/events-WQ.js +20 -0
  17. package/examples/offline-signing-IERC20.js +29 -0
  18. package/examples/offline-signing-QuantumSwapV2ERC20.js +29 -0
  19. package/examples/offline-signing-QuantumSwapV2Factory.js +29 -0
  20. package/examples/offline-signing-QuantumSwapV2Pair.js +29 -0
  21. package/examples/offline-signing-QuantumSwapV2Router02.js +29 -0
  22. package/examples/offline-signing-WQ.js +29 -0
  23. package/examples/read-operations-IERC20.js +19 -0
  24. package/examples/read-operations-QuantumSwapV2ERC20.js +19 -0
  25. package/examples/read-operations-QuantumSwapV2Factory.js +19 -0
  26. package/examples/read-operations-QuantumSwapV2Pair.js +19 -0
  27. package/examples/read-operations-QuantumSwapV2Router02.js +19 -0
  28. package/examples/read-operations-WQ.js +19 -0
  29. package/examples/walkthrough-dex-full-flow.js +226 -0
  30. package/examples/walkthrough-dex-full-flow.ts +231 -0
  31. package/examples/write-operations-IERC20.js +22 -0
  32. package/examples/write-operations-QuantumSwapV2ERC20.js +22 -0
  33. package/examples/write-operations-QuantumSwapV2Factory.js +22 -0
  34. package/examples/write-operations-QuantumSwapV2Pair.js +22 -0
  35. package/examples/write-operations-QuantumSwapV2Router02.js +22 -0
  36. package/examples/write-operations-WQ.js +22 -0
  37. package/index.d.ts +1 -0
  38. package/index.js +45 -0
  39. package/package.json +35 -0
  40. package/src/IERC20.d.ts +24 -0
  41. package/src/IERC20.js +348 -0
  42. package/src/IERC20__factory.d.ts +10 -0
  43. package/src/IERC20__factory.js +29 -0
  44. package/src/QuantumSwapV2ERC20.d.ts +24 -0
  45. package/src/QuantumSwapV2ERC20.js +353 -0
  46. package/src/QuantumSwapV2ERC20__factory.d.ts +10 -0
  47. package/src/QuantumSwapV2ERC20__factory.js +29 -0
  48. package/src/QuantumSwapV2Factory.d.ts +24 -0
  49. package/src/QuantumSwapV2Factory.js +310 -0
  50. package/src/QuantumSwapV2Factory__factory.d.ts +10 -0
  51. package/src/QuantumSwapV2Factory__factory.js +29 -0
  52. package/src/QuantumSwapV2Pair.d.ts +44 -0
  53. package/src/QuantumSwapV2Pair.js +847 -0
  54. package/src/QuantumSwapV2Pair__factory.d.ts +10 -0
  55. package/src/QuantumSwapV2Pair__factory.js +29 -0
  56. package/src/QuantumSwapV2Router02.d.ts +47 -0
  57. package/src/QuantumSwapV2Router02.js +1109 -0
  58. package/src/QuantumSwapV2Router02__factory.d.ts +10 -0
  59. package/src/QuantumSwapV2Router02__factory.js +29 -0
  60. package/src/WQ.d.ts +28 -0
  61. package/src/WQ.js +435 -0
  62. package/src/WQ__factory.d.ts +10 -0
  63. package/src/WQ__factory.js +29 -0
  64. package/src/index.d.ts +14 -0
  65. package/src/index.js +15 -0
  66. package/src/quantumcoin-shims.d.ts +25 -0
  67. package/src/types.d.ts +3 -0
  68. package/src/types.js +3 -0
  69. package/test/e2e/IERC20.e2e.test.js +79 -0
  70. package/test/e2e/QuantumSwapV2ERC20.e2e.test.js +79 -0
  71. package/test/e2e/QuantumSwapV2Factory.e2e.test.js +79 -0
  72. package/test/e2e/QuantumSwapV2Pair.e2e.test.js +79 -0
  73. package/test/e2e/QuantumSwapV2Router02.e2e.test.js +79 -0
  74. package/test/e2e/WQ.e2e.test.js +79 -0
  75. package/test/e2e/all-contracts.e2e.test.js +103 -0
  76. package/test/e2e/dex-full-flow.e2e.test.js +353 -0
  77. package/tsconfig.json +14 -0
@@ -0,0 +1,29 @@
1
+ const { Initialize } = require("quantumcoin/config");
2
+ const { JsonRpcProvider, Wallet, getCreateAddress } = require("quantumcoin");
3
+ const { TEST_WALLET_ENCRYPTED_JSON, TEST_WALLET_PASSPHRASE } = require("./_test-wallet");
4
+ const { IERC20__factory, IERC20 } = require("..");
5
+
6
+ async function main() {
7
+ const rpcUrl = process.env.QC_RPC_URL;
8
+ if (!rpcUrl) throw new Error("QC_RPC_URL is required");
9
+ const chainId = process.env.QC_CHAIN_ID ? Number(process.env.QC_CHAIN_ID) : 123123;
10
+ await Initialize(null);
11
+
12
+ const provider = new JsonRpcProvider(rpcUrl, chainId);
13
+ const wallet = Wallet.fromEncryptedJsonSync(TEST_WALLET_ENCRYPTED_JSON, TEST_WALLET_PASSPHRASE);
14
+ const from = wallet.address;
15
+
16
+ const factory = new IERC20__factory(wallet);
17
+ const deployTxReq = factory.getDeployTransaction();
18
+ const nonce0 = await provider.getTransactionCount(from, "pending");
19
+ const predicted = getCreateAddress({ from, nonce: nonce0 });
20
+
21
+ const rawDeploy = await wallet.signTransaction({ ...deployTxReq, nonce: nonce0, chainId, gasLimit: 6_000_000, gasPrice: 1n });
22
+ const sentDeploy = await provider.sendRawTransaction(rawDeploy);
23
+ await sentDeploy.wait(1, 600_000);
24
+
25
+ const contract = IERC20.connect(predicted, provider);
26
+ console.log("deployed at:", contract.target);
27
+ }
28
+
29
+ main().catch((e) => { console.error(e); process.exitCode = 1; });
@@ -0,0 +1,29 @@
1
+ const { Initialize } = require("quantumcoin/config");
2
+ const { JsonRpcProvider, Wallet, getCreateAddress } = require("quantumcoin");
3
+ const { TEST_WALLET_ENCRYPTED_JSON, TEST_WALLET_PASSPHRASE } = require("./_test-wallet");
4
+ const { QuantumSwapV2ERC20__factory, QuantumSwapV2ERC20 } = require("..");
5
+
6
+ async function main() {
7
+ const rpcUrl = process.env.QC_RPC_URL;
8
+ if (!rpcUrl) throw new Error("QC_RPC_URL is required");
9
+ const chainId = process.env.QC_CHAIN_ID ? Number(process.env.QC_CHAIN_ID) : 123123;
10
+ await Initialize(null);
11
+
12
+ const provider = new JsonRpcProvider(rpcUrl, chainId);
13
+ const wallet = Wallet.fromEncryptedJsonSync(TEST_WALLET_ENCRYPTED_JSON, TEST_WALLET_PASSPHRASE);
14
+ const from = wallet.address;
15
+
16
+ const factory = new QuantumSwapV2ERC20__factory(wallet);
17
+ const deployTxReq = factory.getDeployTransaction();
18
+ const nonce0 = await provider.getTransactionCount(from, "pending");
19
+ const predicted = getCreateAddress({ from, nonce: nonce0 });
20
+
21
+ const rawDeploy = await wallet.signTransaction({ ...deployTxReq, nonce: nonce0, chainId, gasLimit: 6_000_000, gasPrice: 1n });
22
+ const sentDeploy = await provider.sendRawTransaction(rawDeploy);
23
+ await sentDeploy.wait(1, 600_000);
24
+
25
+ const contract = QuantumSwapV2ERC20.connect(predicted, provider);
26
+ console.log("deployed at:", contract.target);
27
+ }
28
+
29
+ main().catch((e) => { console.error(e); process.exitCode = 1; });
@@ -0,0 +1,29 @@
1
+ const { Initialize } = require("quantumcoin/config");
2
+ const { JsonRpcProvider, Wallet, getCreateAddress } = require("quantumcoin");
3
+ const { TEST_WALLET_ENCRYPTED_JSON, TEST_WALLET_PASSPHRASE } = require("./_test-wallet");
4
+ const { QuantumSwapV2Factory__factory, QuantumSwapV2Factory } = require("..");
5
+
6
+ async function main() {
7
+ const rpcUrl = process.env.QC_RPC_URL;
8
+ if (!rpcUrl) throw new Error("QC_RPC_URL is required");
9
+ const chainId = process.env.QC_CHAIN_ID ? Number(process.env.QC_CHAIN_ID) : 123123;
10
+ await Initialize(null);
11
+
12
+ const provider = new JsonRpcProvider(rpcUrl, chainId);
13
+ const wallet = Wallet.fromEncryptedJsonSync(TEST_WALLET_ENCRYPTED_JSON, TEST_WALLET_PASSPHRASE);
14
+ const from = wallet.address;
15
+
16
+ const factory = new QuantumSwapV2Factory__factory(wallet);
17
+ const deployTxReq = factory.getDeployTransaction();
18
+ const nonce0 = await provider.getTransactionCount(from, "pending");
19
+ const predicted = getCreateAddress({ from, nonce: nonce0 });
20
+
21
+ const rawDeploy = await wallet.signTransaction({ ...deployTxReq, nonce: nonce0, chainId, gasLimit: 6_000_000, gasPrice: 1n });
22
+ const sentDeploy = await provider.sendRawTransaction(rawDeploy);
23
+ await sentDeploy.wait(1, 600_000);
24
+
25
+ const contract = QuantumSwapV2Factory.connect(predicted, provider);
26
+ console.log("deployed at:", contract.target);
27
+ }
28
+
29
+ main().catch((e) => { console.error(e); process.exitCode = 1; });
@@ -0,0 +1,29 @@
1
+ const { Initialize } = require("quantumcoin/config");
2
+ const { JsonRpcProvider, Wallet, getCreateAddress } = require("quantumcoin");
3
+ const { TEST_WALLET_ENCRYPTED_JSON, TEST_WALLET_PASSPHRASE } = require("./_test-wallet");
4
+ const { QuantumSwapV2Pair__factory, QuantumSwapV2Pair } = require("..");
5
+
6
+ async function main() {
7
+ const rpcUrl = process.env.QC_RPC_URL;
8
+ if (!rpcUrl) throw new Error("QC_RPC_URL is required");
9
+ const chainId = process.env.QC_CHAIN_ID ? Number(process.env.QC_CHAIN_ID) : 123123;
10
+ await Initialize(null);
11
+
12
+ const provider = new JsonRpcProvider(rpcUrl, chainId);
13
+ const wallet = Wallet.fromEncryptedJsonSync(TEST_WALLET_ENCRYPTED_JSON, TEST_WALLET_PASSPHRASE);
14
+ const from = wallet.address;
15
+
16
+ const factory = new QuantumSwapV2Pair__factory(wallet);
17
+ const deployTxReq = factory.getDeployTransaction();
18
+ const nonce0 = await provider.getTransactionCount(from, "pending");
19
+ const predicted = getCreateAddress({ from, nonce: nonce0 });
20
+
21
+ const rawDeploy = await wallet.signTransaction({ ...deployTxReq, nonce: nonce0, chainId, gasLimit: 6_000_000, gasPrice: 1n });
22
+ const sentDeploy = await provider.sendRawTransaction(rawDeploy);
23
+ await sentDeploy.wait(1, 600_000);
24
+
25
+ const contract = QuantumSwapV2Pair.connect(predicted, provider);
26
+ console.log("deployed at:", contract.target);
27
+ }
28
+
29
+ main().catch((e) => { console.error(e); process.exitCode = 1; });
@@ -0,0 +1,29 @@
1
+ const { Initialize } = require("quantumcoin/config");
2
+ const { JsonRpcProvider, Wallet, getCreateAddress } = require("quantumcoin");
3
+ const { TEST_WALLET_ENCRYPTED_JSON, TEST_WALLET_PASSPHRASE } = require("./_test-wallet");
4
+ const { QuantumSwapV2Router02__factory, QuantumSwapV2Router02 } = require("..");
5
+
6
+ async function main() {
7
+ const rpcUrl = process.env.QC_RPC_URL;
8
+ if (!rpcUrl) throw new Error("QC_RPC_URL is required");
9
+ const chainId = process.env.QC_CHAIN_ID ? Number(process.env.QC_CHAIN_ID) : 123123;
10
+ await Initialize(null);
11
+
12
+ const provider = new JsonRpcProvider(rpcUrl, chainId);
13
+ const wallet = Wallet.fromEncryptedJsonSync(TEST_WALLET_ENCRYPTED_JSON, TEST_WALLET_PASSPHRASE);
14
+ const from = wallet.address;
15
+
16
+ const factory = new QuantumSwapV2Router02__factory(wallet);
17
+ const deployTxReq = factory.getDeployTransaction();
18
+ const nonce0 = await provider.getTransactionCount(from, "pending");
19
+ const predicted = getCreateAddress({ from, nonce: nonce0 });
20
+
21
+ const rawDeploy = await wallet.signTransaction({ ...deployTxReq, nonce: nonce0, chainId, gasLimit: 6_000_000, gasPrice: 1n });
22
+ const sentDeploy = await provider.sendRawTransaction(rawDeploy);
23
+ await sentDeploy.wait(1, 600_000);
24
+
25
+ const contract = QuantumSwapV2Router02.connect(predicted, provider);
26
+ console.log("deployed at:", contract.target);
27
+ }
28
+
29
+ main().catch((e) => { console.error(e); process.exitCode = 1; });
@@ -0,0 +1,29 @@
1
+ const { Initialize } = require("quantumcoin/config");
2
+ const { JsonRpcProvider, Wallet, getCreateAddress } = require("quantumcoin");
3
+ const { TEST_WALLET_ENCRYPTED_JSON, TEST_WALLET_PASSPHRASE } = require("./_test-wallet");
4
+ const { WQ__factory, WQ } = require("..");
5
+
6
+ async function main() {
7
+ const rpcUrl = process.env.QC_RPC_URL;
8
+ if (!rpcUrl) throw new Error("QC_RPC_URL is required");
9
+ const chainId = process.env.QC_CHAIN_ID ? Number(process.env.QC_CHAIN_ID) : 123123;
10
+ await Initialize(null);
11
+
12
+ const provider = new JsonRpcProvider(rpcUrl, chainId);
13
+ const wallet = Wallet.fromEncryptedJsonSync(TEST_WALLET_ENCRYPTED_JSON, TEST_WALLET_PASSPHRASE);
14
+ const from = wallet.address;
15
+
16
+ const factory = new WQ__factory(wallet);
17
+ const deployTxReq = factory.getDeployTransaction();
18
+ const nonce0 = await provider.getTransactionCount(from, "pending");
19
+ const predicted = getCreateAddress({ from, nonce: nonce0 });
20
+
21
+ const rawDeploy = await wallet.signTransaction({ ...deployTxReq, nonce: nonce0, chainId, gasLimit: 6_000_000, gasPrice: 1n });
22
+ const sentDeploy = await provider.sendRawTransaction(rawDeploy);
23
+ await sentDeploy.wait(1, 600_000);
24
+
25
+ const contract = WQ.connect(predicted, provider);
26
+ console.log("deployed at:", contract.target);
27
+ }
28
+
29
+ main().catch((e) => { console.error(e); process.exitCode = 1; });
@@ -0,0 +1,19 @@
1
+ const { Initialize } = require("quantumcoin/config");
2
+ const { JsonRpcProvider } = require("quantumcoin");
3
+ const { IERC20 } = require("..");
4
+
5
+ async function main() {
6
+ const rpcUrl = process.env.QC_RPC_URL;
7
+ if (!rpcUrl) throw new Error("QC_RPC_URL is required");
8
+ const chainId = process.env.QC_CHAIN_ID ? Number(process.env.QC_CHAIN_ID) : 123123;
9
+ const address = process.env.CONTRACT_ADDRESS;
10
+ if (!address) throw new Error("CONTRACT_ADDRESS is required");
11
+ await Initialize(null);
12
+
13
+ const provider = new JsonRpcProvider(rpcUrl, chainId);
14
+ const contract = IERC20.connect(address, provider);
15
+
16
+ console.log("IERC20:", contract.target);
17
+ }
18
+
19
+ main().catch((e) => { console.error(e); process.exitCode = 1; });
@@ -0,0 +1,19 @@
1
+ const { Initialize } = require("quantumcoin/config");
2
+ const { JsonRpcProvider } = require("quantumcoin");
3
+ const { QuantumSwapV2ERC20 } = require("..");
4
+
5
+ async function main() {
6
+ const rpcUrl = process.env.QC_RPC_URL;
7
+ if (!rpcUrl) throw new Error("QC_RPC_URL is required");
8
+ const chainId = process.env.QC_CHAIN_ID ? Number(process.env.QC_CHAIN_ID) : 123123;
9
+ const address = process.env.CONTRACT_ADDRESS;
10
+ if (!address) throw new Error("CONTRACT_ADDRESS is required");
11
+ await Initialize(null);
12
+
13
+ const provider = new JsonRpcProvider(rpcUrl, chainId);
14
+ const contract = QuantumSwapV2ERC20.connect(address, provider);
15
+
16
+ console.log("QuantumSwapV2ERC20:", contract.target);
17
+ }
18
+
19
+ main().catch((e) => { console.error(e); process.exitCode = 1; });
@@ -0,0 +1,19 @@
1
+ const { Initialize } = require("quantumcoin/config");
2
+ const { JsonRpcProvider } = require("quantumcoin");
3
+ const { QuantumSwapV2Factory } = require("..");
4
+
5
+ async function main() {
6
+ const rpcUrl = process.env.QC_RPC_URL;
7
+ if (!rpcUrl) throw new Error("QC_RPC_URL is required");
8
+ const chainId = process.env.QC_CHAIN_ID ? Number(process.env.QC_CHAIN_ID) : 123123;
9
+ const address = process.env.CONTRACT_ADDRESS;
10
+ if (!address) throw new Error("CONTRACT_ADDRESS is required");
11
+ await Initialize(null);
12
+
13
+ const provider = new JsonRpcProvider(rpcUrl, chainId);
14
+ const contract = QuantumSwapV2Factory.connect(address, provider);
15
+
16
+ console.log("QuantumSwapV2Factory:", contract.target);
17
+ }
18
+
19
+ main().catch((e) => { console.error(e); process.exitCode = 1; });
@@ -0,0 +1,19 @@
1
+ const { Initialize } = require("quantumcoin/config");
2
+ const { JsonRpcProvider } = require("quantumcoin");
3
+ const { QuantumSwapV2Pair } = require("..");
4
+
5
+ async function main() {
6
+ const rpcUrl = process.env.QC_RPC_URL;
7
+ if (!rpcUrl) throw new Error("QC_RPC_URL is required");
8
+ const chainId = process.env.QC_CHAIN_ID ? Number(process.env.QC_CHAIN_ID) : 123123;
9
+ const address = process.env.CONTRACT_ADDRESS;
10
+ if (!address) throw new Error("CONTRACT_ADDRESS is required");
11
+ await Initialize(null);
12
+
13
+ const provider = new JsonRpcProvider(rpcUrl, chainId);
14
+ const contract = QuantumSwapV2Pair.connect(address, provider);
15
+
16
+ console.log("QuantumSwapV2Pair:", contract.target);
17
+ }
18
+
19
+ main().catch((e) => { console.error(e); process.exitCode = 1; });
@@ -0,0 +1,19 @@
1
+ const { Initialize } = require("quantumcoin/config");
2
+ const { JsonRpcProvider } = require("quantumcoin");
3
+ const { QuantumSwapV2Router02 } = require("..");
4
+
5
+ async function main() {
6
+ const rpcUrl = process.env.QC_RPC_URL;
7
+ if (!rpcUrl) throw new Error("QC_RPC_URL is required");
8
+ const chainId = process.env.QC_CHAIN_ID ? Number(process.env.QC_CHAIN_ID) : 123123;
9
+ const address = process.env.CONTRACT_ADDRESS;
10
+ if (!address) throw new Error("CONTRACT_ADDRESS is required");
11
+ await Initialize(null);
12
+
13
+ const provider = new JsonRpcProvider(rpcUrl, chainId);
14
+ const contract = QuantumSwapV2Router02.connect(address, provider);
15
+
16
+ console.log("QuantumSwapV2Router02:", contract.target);
17
+ }
18
+
19
+ main().catch((e) => { console.error(e); process.exitCode = 1; });
@@ -0,0 +1,19 @@
1
+ const { Initialize } = require("quantumcoin/config");
2
+ const { JsonRpcProvider } = require("quantumcoin");
3
+ const { WQ } = require("..");
4
+
5
+ async function main() {
6
+ const rpcUrl = process.env.QC_RPC_URL;
7
+ if (!rpcUrl) throw new Error("QC_RPC_URL is required");
8
+ const chainId = process.env.QC_CHAIN_ID ? Number(process.env.QC_CHAIN_ID) : 123123;
9
+ const address = process.env.CONTRACT_ADDRESS;
10
+ if (!address) throw new Error("CONTRACT_ADDRESS is required");
11
+ await Initialize(null);
12
+
13
+ const provider = new JsonRpcProvider(rpcUrl, chainId);
14
+ const contract = WQ.connect(address, provider);
15
+
16
+ console.log("WQ:", contract.target);
17
+ }
18
+
19
+ main().catch((e) => { console.error(e); process.exitCode = 1; });
@@ -0,0 +1,226 @@
1
+ /**
2
+ * Walkthrough: full DEX flow using Test Release (Dec 2025) contracts.
3
+ *
4
+ * Follows the README "Step by step walkthrough": connect to pre-deployed WQ, Factory, Router;
5
+ * deploy two ERC20 tokens (SimpleERC20), create pair, add liquidity, swap token-for-token,
6
+ * then swap ETH for token.
7
+ *
8
+ * Usage:
9
+ * QC_RPC_URL=http://your-rpc:8545 node examples/walkthrough-dex-full-flow.js
10
+ *
11
+ * Optional: QC_CHAIN_ID (default 123123), QC_WALLET_JSON + QC_WALLET_PASSPHRASE (else uses _test-wallet).
12
+ *
13
+ * Requires: quantumcoin package with examples/sdk-generator-erc20.inline.json (SimpleERC20 artifact).
14
+ */
15
+
16
+ const path = require("node:path");
17
+
18
+ const { Initialize } = require("quantumcoin/config");
19
+ const {
20
+ JsonRpcProvider,
21
+ Wallet,
22
+ Contract,
23
+ ContractFactory,
24
+ getCreateAddress,
25
+ getAddress,
26
+ parseUnits,
27
+ isAddress,
28
+ } = require("quantumcoin");
29
+
30
+ const {
31
+ WQ,
32
+ QuantumSwapV2Factory,
33
+ QuantumSwapV2Router02,
34
+ QuantumSwapV2Pair,
35
+ } = require("..");
36
+
37
+ // Test Release (Dec 2025) — do not deploy these
38
+ const WQ_CONTRACT_ADDRESS = "0x0E49c26cd1ca19bF8ddA2C8985B96783288458754757F4C9E00a5439A7291628";
39
+ const V2_CORE_FACTORY_CONTRACT_ADDRESS = "0xbbF45a1B60044669793B444eD01Eb33e03Bb8cf3c5b6ae7887B218D05C5Cbf1d";
40
+ const SWAP_ROUTER_V2_CONTRACT_ADDRESS = "0x41323EF72662185f44a03ea0ad8094a0C9e925aB1102679D8e957e838054aac5";
41
+
42
+ const DEPLOY_GAS_LIMIT = 6_000_000;
43
+ const TX_GAS_LIMIT = 400_000;
44
+ const DEADLINE_OFFSET = 1200;
45
+
46
+ function deadline() {
47
+ return BigInt(Math.floor(Date.now() / 1000) + DEADLINE_OFFSET);
48
+ }
49
+
50
+ let SIMPLE_ERC20_ABI;
51
+ let SIMPLE_ERC20_BYTECODE;
52
+ try {
53
+ const qcPkg = require.resolve("quantumcoin/package.json");
54
+ const qcRoot = path.dirname(qcPkg);
55
+ const artifact = require(path.join(qcRoot, "examples", "sdk-generator-erc20.inline.json"));
56
+ const simple = Array.isArray(artifact) ? artifact[0] : artifact;
57
+ SIMPLE_ERC20_ABI = simple.abi;
58
+ SIMPLE_ERC20_BYTECODE = simple.bin;
59
+ } catch (e) {
60
+ SIMPLE_ERC20_ABI = null;
61
+ SIMPLE_ERC20_BYTECODE = null;
62
+ }
63
+
64
+ async function main() {
65
+ const rpcUrl = process.env.QC_RPC_URL;
66
+ if (!rpcUrl) {
67
+ console.error("QC_RPC_URL is required. Example: QC_RPC_URL=http://localhost:8545 node examples/walkthrough-dex-full-flow.js");
68
+ process.exit(1);
69
+ }
70
+ if (!SIMPLE_ERC20_ABI || !SIMPLE_ERC20_BYTECODE) {
71
+ console.error("SimpleERC20 artifact not found (quantumcoin examples/sdk-generator-erc20.inline.json). Cannot deploy tokens.");
72
+ process.exit(1);
73
+ }
74
+
75
+ const chainId = process.env.QC_CHAIN_ID ? Number(process.env.QC_CHAIN_ID) : 123123;
76
+ await Initialize(null);
77
+
78
+ const provider = new JsonRpcProvider(rpcUrl, chainId);
79
+ let wallet;
80
+ if (process.env.QC_WALLET_JSON && process.env.QC_WALLET_PASSPHRASE) {
81
+ wallet = Wallet.fromEncryptedJsonSync(process.env.QC_WALLET_JSON, process.env.QC_WALLET_PASSPHRASE, provider);
82
+ } else {
83
+ const { createTestWallet } = require("./_test-wallet");
84
+ wallet = createTestWallet(provider);
85
+ console.log("Using demo test wallet (set QC_WALLET_JSON + QC_WALLET_PASSPHRASE for your own).");
86
+ }
87
+ const walletAddr = getAddress(wallet.address);
88
+
89
+ // Step 1: Connect to pre-deployed WQ, Factory, Router
90
+ console.log("Step 1: Connecting to Test Release contracts...");
91
+ const wq = WQ.connect(WQ_CONTRACT_ADDRESS, wallet);
92
+ const wqAddressNorm = getAddress(WQ_CONTRACT_ADDRESS);
93
+ const factoryContract = QuantumSwapV2Factory.connect(V2_CORE_FACTORY_CONTRACT_ADDRESS, wallet);
94
+ const routerContract = QuantumSwapV2Router02.connect(SWAP_ROUTER_V2_CONTRACT_ADDRESS, wallet);
95
+ const routerAddress = routerContract.target;
96
+ console.log(" WQ:", wqAddressNorm);
97
+ console.log(" Factory:", getAddress(V2_CORE_FACTORY_CONTRACT_ADDRESS));
98
+ console.log(" Router:", getAddress(routerAddress));
99
+
100
+ // Step 2: Deploy two ERC20 tokens (SimpleERC20 with initial supply)
101
+ console.log("Step 2: Deploying TokenA and TokenB (SimpleERC20)...");
102
+ const initialSupply = parseUnits("1000000", 18);
103
+ const simpleErc20Factory = new ContractFactory(SIMPLE_ERC20_ABI, SIMPLE_ERC20_BYTECODE, wallet);
104
+ const deploySimpleErc20 = async (name, symbol) => {
105
+ const tx = simpleErc20Factory.getDeployTransaction(name, symbol, initialSupply);
106
+ const nonce = await provider.getTransactionCount(walletAddr, "pending");
107
+ const address = getCreateAddress({ from: walletAddr, nonce });
108
+ const resp = await wallet.sendTransaction({ ...tx, nonce, gasLimit: DEPLOY_GAS_LIMIT });
109
+ await resp.wait(1, 600_000);
110
+ const contract = new Contract(address, SIMPLE_ERC20_ABI, wallet);
111
+ contract._deployTx = resp;
112
+ return contract;
113
+ };
114
+ const tokenA = await deploySimpleErc20("TokenA", "TKA");
115
+ const tokenB = await deploySimpleErc20("TokenB", "TKB");
116
+ const tokenAAddress = getAddress(tokenA.target);
117
+ const tokenBAddress = getAddress(tokenB.target);
118
+ console.log(" TokenA:", tokenAAddress);
119
+ console.log(" TokenB:", tokenBAddress);
120
+
121
+ // Step 3: Create pair
122
+ console.log("Step 3: Creating pair (TokenA, TokenB)...");
123
+ const createPairTx = await factoryContract.createPair(tokenAAddress, tokenBAddress, { gasLimit: TX_GAS_LIMIT });
124
+ await createPairTx.wait(1, 600_000);
125
+ const pairAddressFromEvent = getAddress(await factoryContract.getPair(tokenAAddress, tokenBAddress));
126
+ const zeroAddress32 = "0x0000000000000000000000000000000000000000000000000000000000000000";
127
+ const pairCreated = pairAddressFromEvent && isAddress(pairAddressFromEvent) && pairAddressFromEvent !== zeroAddress32 && pairAddressFromEvent !== "0x" + "0".repeat(64);
128
+ if (!pairCreated) {
129
+ console.error(" Pair not created (getPair returned zero or invalid address).");
130
+ process.exit(1);
131
+ }
132
+ console.log(" Pair:", pairAddressFromEvent);
133
+
134
+ // Step 4: Add liquidity
135
+ console.log("Step 4: Adding liquidity...");
136
+ const amountADesired = parseUnits("1000", 18);
137
+ const amountBDesired = parseUnits("1000", 18);
138
+ const approveAmount = amountADesired > amountBDesired ? amountADesired : amountBDesired;
139
+ await (await tokenA.approve(routerAddress, approveAmount, { gasLimit: TX_GAS_LIMIT })).wait(1, 600_000);
140
+ await (await tokenB.approve(routerAddress, approveAmount, { gasLimit: TX_GAS_LIMIT })).wait(1, 600_000);
141
+ const addLiqTx = await routerContract.addLiquidity(
142
+ tokenAAddress,
143
+ tokenBAddress,
144
+ amountADesired,
145
+ amountBDesired,
146
+ 0n,
147
+ 0n,
148
+ walletAddr,
149
+ deadline(),
150
+ { gasLimit: TX_GAS_LIMIT },
151
+ );
152
+ await addLiqTx.wait(1, 600_000);
153
+ const pairContract = QuantumSwapV2Pair.connect(pairAddressFromEvent, provider);
154
+ const reservesAfter = await pairContract.getReserves();
155
+ const res0 = Array.isArray(reservesAfter) ? reservesAfter[0] : reservesAfter;
156
+ const res1 = Array.isArray(reservesAfter) ? reservesAfter[1] : 0n;
157
+ console.log(" Reserves after addLiquidity:", res0.toString(), res1.toString());
158
+
159
+ // Step 5: Swap token for token
160
+ console.log("Step 5: Swapping TokenA -> TokenB (swapExactTokensForTokens)...");
161
+ const swapAmountIn = parseUnits("10", 18);
162
+ const pathSwap = [tokenAAddress, tokenBAddress];
163
+ await (await tokenA.approve(routerAddress, swapAmountIn, { gasLimit: TX_GAS_LIMIT })).wait(1, 600_000);
164
+ const swapTx = await routerContract.swapExactTokensForTokens(
165
+ swapAmountIn,
166
+ 0n,
167
+ pathSwap,
168
+ walletAddr,
169
+ deadline(),
170
+ { gasLimit: TX_GAS_LIMIT },
171
+ );
172
+ await swapTx.wait(1, 600_000);
173
+ const tokenBBalanceAfterSwapRaw = await tokenB.balanceOf(walletAddr);
174
+ const tokenBBalanceAfterSwap = typeof tokenBBalanceAfterSwapRaw === "bigint" ? tokenBBalanceAfterSwapRaw : BigInt(String(tokenBBalanceAfterSwapRaw));
175
+ console.log(" TokenB balance after swap:", tokenBBalanceAfterSwap.toString());
176
+
177
+ // Step 6: Swap ETH for token (wrap, add WQ-token liquidity if needed, swapExactETHForTokens)
178
+ console.log("Step 6: Swapping ETH for TokenA (wrap, addLiquidityETH, swapExactETHForTokens)...");
179
+ const ethToWrap = parseUnits("1", 18);
180
+ await (await wq.deposit({ value: ethToWrap, gasLimit: TX_GAS_LIMIT })).wait(1, 600_000);
181
+
182
+ const pairWqTokenA = await factoryContract.getPair(wqAddressNorm, tokenAAddress);
183
+ let pairWqTokenAAddr = null;
184
+ try {
185
+ pairWqTokenAAddr = getAddress(pairWqTokenA);
186
+ } catch {
187
+ pairWqTokenAAddr = null;
188
+ }
189
+ const zeroAddr = "0x0000000000000000000000000000000000000000000000000000000000000000";
190
+ const hasWqPair = pairWqTokenAAddr && isAddress(pairWqTokenAAddr) && pairWqTokenAAddr !== zeroAddr && pairWqTokenAAddr !== "0x" + "0".repeat(64);
191
+ if (!hasWqPair) {
192
+ await (await factoryContract.createPair(wqAddressNorm, tokenAAddress, { gasLimit: TX_GAS_LIMIT })).wait(1, 600_000);
193
+ }
194
+ const tokenForEthLiq = parseUnits("500", 18);
195
+ await (await tokenA.approve(routerAddress, tokenForEthLiq, { gasLimit: TX_GAS_LIMIT })).wait(1, 600_000);
196
+ await (
197
+ await routerContract.addLiquidityETH(
198
+ tokenAAddress,
199
+ tokenForEthLiq,
200
+ 0n,
201
+ 0n,
202
+ walletAddr,
203
+ deadline(),
204
+ { value: ethToWrap, gasLimit: TX_GAS_LIMIT },
205
+ )
206
+ ).wait(1, 600_000);
207
+
208
+ const ethSwapValue = parseUnits("0.1", 18);
209
+ const pathEthToToken = [wqAddressNorm, tokenAAddress];
210
+ const swapEthTx = await routerContract.swapExactETHForTokens(
211
+ 0n,
212
+ pathEthToToken,
213
+ walletAddr,
214
+ deadline(),
215
+ { value: ethSwapValue, gasLimit: TX_GAS_LIMIT },
216
+ );
217
+ await swapEthTx.wait(1, 600_000);
218
+ console.log(" swapExactETHForTokens completed.");
219
+
220
+ console.log("Walkthrough complete.");
221
+ }
222
+
223
+ main().catch((err) => {
224
+ console.error(err);
225
+ process.exit(1);
226
+ });