forked from yug49/PyPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-wei-format-fix.js
More file actions
108 lines (95 loc) · 4.02 KB
/
test-wei-format-fix.js
File metadata and controls
108 lines (95 loc) · 4.02 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
const axios = require("axios");
async function demonstrateWeiFix() {
console.log("🔧 Demonstrating Wei Format Fix");
console.log("================================");
const API_BASE_URL = "http://localhost:5001/api";
// Create a test order
const orderId = `0x${Math.random()
.toString(16)
.substr(2, 32)
.padEnd(64, "0")}`;
const transactionHash = `0x${Math.random()
.toString(16)
.substr(2, 32)
.padEnd(64, "0")}`;
const orderData = {
orderId: orderId,
walletAddress: "0x1234567890123456789012345678901234567890",
amount: "100000000000000000000", // 100 INR in wei
tokenAddress: "0x11fE4B6AE13d2a6055C8D9cF65c55bac32B5d844",
startPrice: "95000000000000000000", // 95 INR in wei
endPrice: "85000000000000000000", // 85 INR in wei
recipientUpiAddress: "demo@fix",
transactionHash: transactionHash,
blockNumber: 12345,
};
try {
// Create order
await axios.post(`${API_BASE_URL}/orders`, orderData);
console.log("✅ Order created with wei prices");
console.log(` Start Price: ${orderData.startPrice} wei (95 INR)`);
console.log(` End Price: ${orderData.endPrice} wei (85 INR)`);
// Start auction
const startResponse = await axios.post(
`${API_BASE_URL}/orders/${orderId}/start-auction`
);
console.log("✅ Auction started");
// Wait a bit
await new Promise((resolve) => setTimeout(resolve, 1000));
// Test scenario 1: Resolver sends wei format price (what actually happens)
const weiFormatPrice = "90000000000000000000"; // 90 INR in wei
console.log("\n📤 Scenario 1: Resolver sends wei format price");
console.log(` Sending: ${weiFormatPrice} (90 INR in wei)`);
try {
const response = await axios.post(
`${API_BASE_URL}/orders/${orderId}/accept`,
{
acceptedPrice: weiFormatPrice,
resolverAddress:
"0xb862825240fC768515A26D09FAeB9Ab3236Df09e",
}
);
console.log("✅ SUCCESS: Wei format price accepted correctly!");
console.log(` Response: ${response.data.message}`);
} catch (error) {
console.error("❌ FAILED: Wei format price rejected");
console.error(
` Error: ${error.response?.data?.message || error.message}`
);
}
// Test scenario 2: If someone sends decimal format
console.log("\n📤 Scenario 2: Testing decimal format price");
console.log(` Sending: 89.5 (decimal format)`);
try {
const response = await axios.post(
`${API_BASE_URL}/orders/${orderId}/accept`,
{
acceptedPrice: "89.5",
resolverAddress:
"0xb862825240fC768515A26D09FAeB9Ab3236Df09e",
}
);
console.log("✅ SUCCESS: Decimal format price accepted correctly!");
console.log(` Response: ${response.data.message}`);
} catch (error) {
console.error("❌ FAILED: Decimal format price rejected");
console.error(
` Error: ${error.response?.data?.message || error.message}`
);
}
console.log("\n🎯 Fix Summary:");
console.log(
" Before: Backend always treated incoming price as decimal and multiplied by 1e18"
);
console.log(
" Problem: Resolver sends wei format → Backend converts again → Astronomical number"
);
console.log(
" After: Backend detects format and handles both wei and decimal correctly"
);
console.log(" Result: Smart contract validation passes ✅");
} catch (error) {
console.error("❌ Test failed:", error.response?.data || error.message);
}
}
demonstrateWeiFix().catch(console.error);