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