-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-rlp-fix.js
More file actions
179 lines (153 loc) Β· 6.27 KB
/
test-rlp-fix.js
File metadata and controls
179 lines (153 loc) Β· 6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env node
/**
* Test RLP Encoding Fix
* Verifies that transactions are properly constructed and serialized
*/
import { ethers } from "ethers";
import "dotenv/config.js";
import * as transactionUtils from "../services/transactionUtils.js";
async function testRLPEncoding() {
console.log("\nπ Testing RLP Encoding Fix...\n");
try {
// Initialize provider
const rpcUrl = process.env.BLOCKCHAIN_RPC_URL;
if (!rpcUrl) {
throw new Error("BLOCKCHAIN_RPC_URL not set in .env");
}
const provider = new ethers.JsonRpcProvider(rpcUrl);
console.log("β Connected to blockchain");
// Test 1: Get fee data
console.log("\nπ Test 1: Retrieving Gas Fee Data");
const feeData = await provider.getFeeData();
if (!feeData) {
throw new Error("Failed to get fee data from provider");
}
console.log(" β Gas Price:", ethers.formatUnits(feeData.gasPrice, "gwei"), "gwei");
console.log(" β Max Fee Per Gas:", ethers.formatUnits(feeData.maxFeePerGas, "gwei"), "gwei");
console.log(
" β Max Priority Fee:",
ethers.formatUnits(feeData.maxPriorityFeePerGas, "gwei"),
"gwei"
);
// Test 2: Build proper transaction options
console.log("\nποΈ Test 2: Building Transaction Options");
const txOptions = {
gasLimit: 500000,
maxFeePerGas: feeData.maxFeePerGas || ethers.parseUnits("100", "gwei"),
maxPriorityFeePerGas: feeData.maxPriorityFeePerGas || ethers.parseUnits("2", "gwei"),
};
console.log(" β Gas Limit:", txOptions.gasLimit);
console.log(" β Max Fee Per Gas:", ethers.formatUnits(txOptions.maxFeePerGas, "gwei"), "gwei");
console.log(
" β Max Priority Fee:",
ethers.formatUnits(txOptions.maxPriorityFeePerGas, "gwei"),
"gwei"
);
// Test 3: Validate transaction structure
console.log("\nβ
Test 3: Validating Transaction Structure");
const testTx = {
to: "0x0000000000000000000000000000000000000000",
from: "0x0000000000000000000000000000000000000001",
data: "0x",
value: ethers.parseEther("0"),
...txOptions,
};
// Try to serialize the transaction
try {
const serialized = ethers.Transaction.from(testTx).serialized;
console.log(" β Transaction serializes correctly");
console.log(" β Serialized length:", serialized.length, "bytes");
} catch (error) {
throw new Error("Failed to serialize transaction: " + error.message);
}
// Test 4: Check signer availability
console.log("\nπ€ Test 4: Checking Signer");
const privateKey = process.env.PRIVATE_KEY;
if (!privateKey) {
console.warn(" β οΈ PRIVATE_KEY not set - skipping signer test");
} else {
// Don't log the actual key!
const signer = new ethers.Wallet(privateKey, provider);
console.log(" β Signer loaded:", signer.address);
const signerBalance = await provider.getBalance(signer.address);
console.log(" β Signer balance:", ethers.formatEther(signerBalance), "ETH");
if (signerBalance === BigInt(0)) {
console.warn(" β οΈ WARNING: Signer has 0 balance - cannot send transactions");
}
}
// Test 5: Validate addresses
console.log("\nπ Test 5: Validating Addresses");
const addresses = {
TIPSCOIN: process.env.TIPSCOIN_ADDRESS,
USDTC: process.env.USDTC_ADDRESS,
DEX_ROUTER: process.env.DEX_ROUTER_ADDRESS,
TRUSTED_FORWARDER: process.env.TRUSTED_FORWARDER_ADDRESS,
};
for (const [name, addr] of Object.entries(addresses)) {
if (!addr) {
console.warn(` β οΈ ${name} not set`);
} else if (!ethers.isAddress(addr)) {
throw new Error(`Invalid address format for ${name}: ${addr}`);
} else {
console.log(` β ${name}: ${addr}`);
}
}
// Test 6: Verify transactionUtils module
console.log("\nπ οΈ Test 6: Verifying Transaction Utilities");
try {
if (!transactionUtils.validateTransactionData) {
throw new Error("validateTransactionData not exported");
}
if (!transactionUtils.buildTransaction) {
throw new Error("buildTransaction not exported");
}
if (!transactionUtils.sendTransaction) {
throw new Error("sendTransaction not exported");
}
console.log(" β validateTransactionData function available");
console.log(" β buildTransaction function available");
console.log(" β sendTransaction function available");
// Test validation with sample data
const { valid, errors } = transactionUtils.validateTransactionData({
to: "0x" + "0".repeat(40),
from: "0x" + "1".repeat(40),
data: "0x",
value: 0,
gasLimit: 21000,
});
if (valid) {
console.log(" β Transaction validation works correctly");
} else {
console.warn(" β οΈ Validation errors:", errors);
}
} catch (error) {
throw new Error("Failed to load transactionUtils: " + error.message);
}
console.log("\n" + "=".repeat(50));
console.log("β
All RLP Encoding Tests PASSED!");
console.log("=".repeat(50));
console.log("\nYour blockchain setup is properly configured.");
console.log("Transactions should serialize and send without RLP errors.\n");
process.exit(0);
} catch (error) {
console.error("\n" + "=".repeat(50));
console.error("β RLP Encoding Test FAILED!");
console.error("=".repeat(50));
console.error("\nError:", error.message);
if (error.message.includes("Cannot read property 'getFeeData'")) {
console.error("\nSolution: Make sure your RPC URL is correct");
console.error(" Example: BLOCKCHAIN_RPC_URL=http://localhost:8545");
} else if (error.message.includes("BLOCKCHAIN_RPC_URL not set")) {
console.error("\nSolution: Set BLOCKCHAIN_RPC_URL in your .env file");
console.error(" cp .env.example .env");
console.error(" # Edit .env and set BLOCKCHAIN_RPC_URL");
} else if (error.message.includes("Invalid address")) {
console.error("\nSolution: Check your contract addresses in .env");
console.error(" Ensure all addresses start with 0x and are 40 hex characters");
}
console.log("\nπ See docs/RLP_ENCODING_FIX.md for more help\n");
process.exit(1);
}
}
// Run tests
testRLPEncoding();