-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerateTransactions.js
More file actions
235 lines (209 loc) · 8.92 KB
/
generateTransactions.js
File metadata and controls
235 lines (209 loc) · 8.92 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import fs from 'fs';
import { ethers } from 'ethers';
const FUNCTION_SELECTORS = {
approve: "0x095ea7b3",
buy: "0xd6febde8",
transfer: "0xa9059cbb",
pushPayment: "0x8028b82f"
};
const ORCHESTRATOR_ABI = [{
"inputs": [],
"name": "fundingManager",
"outputs": [{ "internalType": "address", "name": "", "type": "address" }],
"stateMutability": "view",
"type": "function"
}];
const FUNDING_MANAGER_ABI = [{
"inputs": [
{ "internalType": "uint256", "name": "_depositAmount", "type": "uint256" }
],
"name": "calculatePurchaseReturn",
"outputs": [{ "internalType": "uint256", "name": "mintAmount", "type": "uint256" }],
"stateMutability": "view",
"type": "function"
}];
const ORCHESTRATOR_LIST_MODULES_ABI = [{
"inputs": [],
"name": "listModules",
"outputs": [{ "internalType": "address[]", "name": "", "type": "address[]" }],
"stateMutability": "view",
"type": "function"
}];
const MODULE_ABI = [{
"inputs": [],
"name": "title",
"outputs": [{ "internalType": "string", "name": "", "type": "string" }],
"stateMutability": "view",
"type": "function"
}];
async function getPaymentRouterAddress(orchestratorAddress) {
try {
const provider = new ethers.JsonRpcProvider("https://polygon-rpc.com");
const orchestratorContract = new ethers.Contract(
orchestratorAddress,
ORCHESTRATOR_LIST_MODULES_ABI,
provider
);
// Get list of modules
const modules = await orchestratorContract.listModules();
console.log("Found modules:", modules);
// Iterate through modules to find payment router
for (const module of modules) {
const moduleContract = new ethers.Contract(
module,
MODULE_ABI,
provider
);
try {
const moduleName = await moduleContract.title();
console.log(`Module ${module} has title: ${moduleName}`);
if (moduleName === 'LM_PC_PaymentRouter_v1') {
console.log(`Found Payment Router at address: ${module}`);
return module;
}
} catch (error) {
console.log(`Could not get title for module ${module}:`, error.message);
continue;
}
}
throw new Error("Payment Router module not found");
} catch (error) {
console.error("Error getting payment router address:", error);
throw error;
}
}
async function calculatePurchaseReturn(fundingManagerAddress, amount) {
try {
const provider = new ethers.JsonRpcProvider("https://polygon-rpc.com");
const fundingManagerContract = new ethers.Contract(
fundingManagerAddress,
FUNDING_MANAGER_ABI,
provider
);
const amountInWei = ethers.parseUnits(amount.toString(), 18);
const purchaseReturn = await fundingManagerContract.calculatePurchaseReturn(amountInWei);
return purchaseReturn.toString();
} catch (error) {
console.error("Error calculating purchase return:", error);
throw error;
}
}
async function getFundingManagerAddress(orchestratorAddress) {
try {
const provider = new ethers.JsonRpcProvider("https://polygon-rpc.com");
const orchestratorContract = new ethers.Contract(
orchestratorAddress,
ORCHESTRATOR_ABI,
provider
);
const fundingManagerAddress = await orchestratorContract.fundingManager();
console.log(`Funding Manager Address: ${fundingManagerAddress}`);
return fundingManagerAddress;
} catch (error) {
console.error("Error getting funding manager address:", error);
throw error;
}
}
function generateTransactionJson(fundingPotMSAddress, projectName, donationTokenAddress, fundingManagerAddress, tokenAmount, ABCTokenAmount, paymentRouterAddress) {
// Current timestamp in milliseconds
const currentTimestamp = Date.now();
const tokenAmountInWei = ethers.parseUnits(tokenAmount.toString(), 18).toString();
// Create the transaction structure
const transactionData = {
version: "1.0",
chainId: "137", // Polygon Mainnet
createdAt: currentTimestamp,
meta: {
name: `[FUNDING_POT]-[${projectName}]-[BATCH-1]-[TX-0]`,
description: `Batch 1 for ${projectName}`,
txBuilderVersion: "",
createdFromSafeAddress: fundingPotMSAddress,
createdFromOwnerAddress: "",
checksum: ""
},
transactions: [{
to: donationTokenAddress,
value: "0",
data: FUNCTION_SELECTORS.approve + ethers.AbiCoder.defaultAbiCoder().encode(
["address", "uint256"], [fundingManagerAddress, tokenAmountInWei]
).slice(2), // Remove '0x' prefix
contractMethod: "approve(address,uint256)",
contractInputsValues: [
fundingManagerAddress,
tokenAmountInWei
]
},
{
to: fundingManagerAddress,
value: "0",
data: FUNCTION_SELECTORS.buy + ethers.AbiCoder.defaultAbiCoder().encode(
["uint256", "uint256"], [tokenAmountInWei, "1"]
).slice(2),
contractMethod: "buy(uint256,uint256)",
contractInputsValues: [
tokenAmountInWei,
"1"
]
},
{
to: abcTokenAddress,
value: "0",
data: FUNCTION_SELECTORS.transfer + ethers.AbiCoder.defaultAbiCoder().encode(
["address", "uint256"], [paymentRouterAddress, ABCTokenAmount]
).slice(2),
contractMethod: "transfer(address,uint256)",
contractInputsValues: [
paymentRouterAddress,
ABCTokenAmount
]
},
{
to: paymentRouterAddress,
value: "0",
data: FUNCTION_SELECTORS.pushPayment + ethers.AbiCoder.defaultAbiCoder().encode(
["address", "address", "uint256", "uint256", "uint256", "uint256"], [userAddress, abcTokenAddress, ABCTokenAmount, start, cliff, end]
).slice(2),
contractMethod: "pushPayment(address,address,uint256,uint256,uint256,uint256)",
contractInputsValues: [
userAddress,
abcTokenAddress,
ABCTokenAmount,
start.toString(),
cliff.toString(),
end.toString()
]
}
]
};
// Generate filename with timestamp
const timestamp = new Date().toISOString().replace(/[:.]/g, '').replace('T', '_').slice(0, 15);
const filename = `transactions_${timestamp}.json`;
// Write to JSON file
fs.writeFileSync(filename, JSON.stringify(transactionData, null, 2));
console.log(`Transaction file generated: ${filename}`);
}
// Run the function
// const projectName = 'JEFFERYS_PROJECT';
// const orchestratorAddress = "0x64413f88f7a77abc951051e12e8d1c4be00be3d0";
// const fundingPotMSAddress = "0x06ee820a94d7f23d3d3468a159737287059edddf"; // project address that donations goes to it
// const donationTokenAddress = "0xc20CAf8deE81059ec0c8E5971b2AF7347eC131f4"; // TPOL contract address
// const abcTokenAddress = "0x3c042431c55afa630e5af2c25cb7baad8d992a9e";
// const userAddress = "0x23d4B92783975A0B240BD863193B1e162929F4C9";
// const tokenAmount = 6; // we should have or send this amount of donation token to the porject address before execute this script
const projectName = 'TAMS_PROJECT';
// const orchestratorAddress = "0x0899b39163b813b8e5b1e43d549d9e357d598fef";
const fundingPotMSAddress = "0x5527c2391c541f917197bb4b2b9fdd92b3ab12f2";
const donationTokenAddress = "0x806B448d6C5b507727AD715425B744f038E475bc";
const abcTokenAddress = "0x84c4ca1e5487ef0464a17880ee0fb235bd10a8b3";
const userAddress = "0x2148106E53edc98e9196f4aDEAC83EA9916F99d7";
const tokenAmount = 10;
const start = Math.floor(Date.now() / 1000) + 1800; // 30 mins after now
const cliff = 1800; // 30 mins
const end = start + 2 * 60 * 60; // 2 hours after start
// const fundingManagerAddress = await getFundingManagerAddress(orchestratorAddress);
// const ABCTokenAmount = await calculatePurchaseReturn(fundingManagerAddress, tokenAmount);
// console.log("purchaseReturn amount:", ABCTokenAmount);
const orchestratorAddress = "0x097Fea9749186998A4D0835E101eF786484528b8";
const paymentRouterAddress = await getPaymentRouterAddress(orchestratorAddress);
console.log("payment router address:", paymentRouterAddress);
// generateTransactionJson(fundingPotMSAddress, projectName, donationTokenAddress, fundingManagerAddress, tokenAmount, ABCTokenAmount, paymentRouterAddress);