forked from Remitwise-Org/Remitwise-Contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_lib.py
More file actions
129 lines (120 loc) · 4.46 KB
/
Copy pathfix_lib.py
File metadata and controls
129 lines (120 loc) · 4.46 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
import os
path = r'bill_payments\src\lib.rs'
with open(path, 'r', encoding='utf-8') as f:
content = f.read()
# Fix ArchivedBill struct and duplicate attributes
old_archived_bill = """#[derive(Clone)]
#[contracttype]
#[derive(Clone)]
#[contracttype]
pub struct ArchivedBill {
pub id: u32,
pub owner: Address,
pub name: String,
pub amount: i128,
pub paid_at: u64,
pub archived_at: u64,
pub tags: Vec<String>,
/// Intended currency/asset carried over from the originating `Bill`.
pub currency: String,
}"""
new_archived_bill = """#[derive(Clone)]
#[contracttype]
pub struct ArchivedBill {
pub id: u32,
pub owner: Address,
pub name: String,
pub external_ref: Option<String>,
pub amount: i128,
pub paid_at: u64,
pub archived_at: u64,
pub tags: Vec<String>,
/// Intended currency/asset carried over from the originating `Bill`.
pub currency: String,
}"""
# Handle potential \r\n vs \n
def fix_replace(text, old, new):
# Normalize everything to \n for the search, then try to match the original line endings
if old in text:
return text.replace(old, new)
# try with \r\n
old_rn = old.replace('\n', '\r\n')
new_rn = new.replace('\n', '\r\n')
if old_rn in text:
return text.replace(old_rn, new_rn)
# try mixed or just normalize then replace
return text
content = fix_replace(content, old_archived_bill, new_archived_bill)
# Fix restore_bill instantiation
old_restore = """ let restored_bill = Bill {
id: archived_bill.id,
owner: archived_bill.owner.clone(),
name: archived_bill.name.clone(),
amount: archived_bill.amount,
due_date: env.ledger().timestamp() + 2592000,
recurring: false,
frequency_days: 0,
paid: true,
created_at: archived_bill.paid_at,
paid_at: Some(archived_bill.paid_at),
schedule_id: None,
tags: archived_bill.tags.clone(),
currency: archived_bill.currency.clone(),
};"""
new_restore = """ let restored_bill = Bill {
id: archived_bill.id,
owner: archived_bill.owner.clone(),
name: archived_bill.name.clone(),
external_ref: archived_bill.external_ref.clone(),
amount: archived_bill.amount,
due_date: env.ledger().timestamp() + 2592000,
recurring: false,
frequency_days: 0,
paid: true,
created_at: archived_bill.paid_at,
paid_at: Some(archived_bill.paid_at),
schedule_id: None,
tags: archived_bill.tags.clone(),
currency: archived_bill.currency.clone(),
};"""
content = fix_replace(content, old_restore, new_restore)
# Fix batch_pay_bills instantiation
old_batch_pay = """ let next_bill = Bill {
id: next_id,
owner: bill.owner.clone(),
name: bill.name.clone(),
amount: bill.amount,
due_date: next_due_date,
recurring: true,
frequency_days: bill.frequency_days,
paid: false,
created_at: current_time,
paid_at: None,
schedule_id: bill.schedule_id,
tags: bill.tags.clone(),
currency: bill.currency.clone(),
};"""
new_batch_pay = """ let next_bill = Bill {
id: next_id,
owner: bill.owner.clone(),
name: bill.name.clone(),
external_ref: bill.external_ref.clone(),
amount: bill.amount,
due_date: next_due_date,
recurring: true,
frequency_days: bill.frequency_days,
paid: false,
created_at: current_time,
paid_at: None,
schedule_id: bill.schedule_id,
tags: bill.tags.clone(),
currency: bill.currency.clone(),
};"""
content = fix_replace(content, old_batch_pay, new_batch_pay)
# Fix redundant/broken get_all_bills
broken_get_all = """ pub fn get_all_bills(env: Env) -> Vec<Bill> {"""
clean_get_all = """"""
content = fix_replace(content, broken_get_all, clean_get_all)
with open(path, 'w', encoding='utf-8') as f:
f.write(content)
print("Fixes applied successfully.")