forked from Remitwise-Org/Remitwise-Contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_bill_tests_v2.js
More file actions
120 lines (107 loc) · 5.58 KB
/
Copy pathfix_bill_tests_v2.js
File metadata and controls
120 lines (107 loc) · 5.58 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
const fs = require('fs');
const path = require('path');
function processDirectory(dir) {
const files = fs.readdirSync(dir);
for (const file of files) {
const fullPath = path.join(dir, file);
if (fs.statSync(fullPath).isDirectory()) {
processDirectory(fullPath);
} else if (fullPath.endsWith('.rs')) {
let content = fs.readFileSync(fullPath, 'utf8');
let modified = false;
// Simple regex to match client.create_bill and try_create_bill.
// Using a recursive string balancer is hard in standard JS regex,
// but we can match up to the semicolon if they are simple calls.
// However, arguments might contain nested parens.
// A simpler approach: replace lines containing create_bill if they don't have None or XLM correctly.
// Let's use a function to balance parentheses securely.
let idx = 0;
while ((idx = content.indexOf('create_bill(', idx)) !== -1) {
// Find where the method call starts
// Could be `client.create_bill(` or `client.try_create_bill(`
let startIdx = idx + 'create_bill('.length;
let openParen = 1;
let endIdx = startIdx;
while (endIdx < content.length && openParen > 0) {
if (content[endIdx] === '(') openParen++;
else if (content[endIdx] === ')') openParen--;
endIdx++;
}
if (openParen === 0) {
// We found the bounds of the arguments: content.substring(startIdx, endIdx - 1)
let argsStr = content.substring(startIdx, endIdx - 1);
// Split by comma, but respect nested parens/strings (rudimentary split)
let args = [];
let currentArg = "";
let nestLevel = 0;
let inString = false;
for (let i = 0; i < argsStr.length; i++) {
let c = argsStr[i];
if (c === '"' && argsStr[i-1] !== '\\') inString = !inString;
if (!inString) {
if (c === '(' || c === '<' || c === '{' || c === '[') nestLevel++;
else if (c === ')' || c === '>' || c === '}' || c === ']') nestLevel--;
}
if (c === ',' && nestLevel === 0 && !inString) {
args.push(currentArg.trim());
currentArg = "";
} else {
currentArg += c;
}
}
if (currentArg.trim().length > 0) {
args.push(currentArg.trim());
}
// Now we have the arguments array.
// The signature we want is 8 arguments.
// owner, name, amount, due_date, recurring, freq, ref, currency
let originalLength = args.length;
if (args.length === 6) {
args.push('&None');
args.push('&String::from_str(&env, "XLM")');
} else if (args.length === 7) {
// Usually the 7th is a currency string or None.
if (args[6].includes("XLM")) {
args[6] = '&None';
args.push('&String::from_str(&env, "XLM")');
} else if (args[6].includes("None")) {
args.push('&String::from_str(&env, "XLM")');
} else {
// Default back to safely appending.
args[6] = '&None';
args.push('&String::from_str(&env, "XLM")');
}
} else if (args.length === 8) {
// Already 8, doing nothing.
} else if (args.length === 9) {
// Sometimes it's duplicated XLM from previous failed script
args = [args[0], args[1], args[2], args[3], args[4], args[5], '&None', '&String::from_str(&env, "XLM")'];
}
if (args.length !== originalLength || originalLength >= 7) {
let newArgsStr = args.join(', ');
content = content.substring(0, startIdx) + newArgsStr + content.substring(endIdx - 1);
modified = true;
idx = startIdx + newArgsStr.length + 1; // Move past this call
} else {
idx = endIdx;
}
} else {
idx += 'create_bill('.length;
}
}
// Also fix set_time to set_ledger_time as found previously
let newContent = content.replace(/set_time\s*\(\s*&env\s*,\s*([0-9a-zA-Z_+\-*/\s]+)\)/g, 'set_ledger_time(&env, 1, $1)');
if (newContent !== content) {
content = newContent;
modified = true;
}
if (modified) {
fs.writeFileSync(fullPath, content);
console.log(`Updated ${fullPath}`);
}
}
}
}
const targetDir = path.join(process.cwd(), 'bill_payments');
processDirectory(targetDir);
console.log("Finished script.");