quantumswap 0.0.1 → 0.0.3

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