australian_capital_dao/parser.js

131 lines
4.1 KiB
JavaScript
Raw Normal View History

2024-07-19 12:33:57 +10:00
function hexToDecimal(hex) {
return parseInt(hex, 16);
}
function stripExtraZeros(hexString) {
// Remove leading zeros while keeping at least "0x" if present
hexString = hexString.replace(/^0x0+/, '');
return "0x"+hexString;
}
const topics = {
"0xb9956173924f9c1204bae41dd3737d7ed1161846d13183879cdc03c4b9f8d019":"Submit Proposal",
"0x786755545a7e27c12c90cc7f0934514d03fdacfe3684a340b8c4100531e7ecd5":"Submit Vote",
"0xb4571f7e4e2c2b6e6185e47ab5caa5fe34087299bd49fbae945a4583101ee3f0":"Process Proposal",
"0xd45ad122361f16d6f50d7c4a73638f20ee48eff6186af15224e2a4a1f6d50171":"Sponsor Proposal"
};
var Logs = $('HTTP Request').first().json.result.logs;
var topicsFromLogs = [];
for (var i = 0; i < Logs.length; i++) {
var log = Logs[i];
topicsFromLogs.push(...log.topics); // Using spread operator to append topics array
}
var data = {
"method":"Unknown method" // Default value
};
const opts = {
"Submit Vote":{
1:"member",
2:"proposal",
3:"vote"
},
"Submit Proposal":{
1:"proposal",
2:"dataHash"
},
"Process Proposal":{
1:"member",
2:"proposal",
3:"vote"
},
"Sponsor Proposal":{
1:"member",
2:"proposal",
3:"votingStarts"
}
}
// Check each topic in the logs against the topics object
for (let i = 0; i < topicsFromLogs.length; i++) {
const topic = topicsFromLogs[i];
if (topics.hasOwnProperty(topic)) {
data["method"] = topics[topic];
console.log('Matched Option:', data["method"]);
} else {
if (opts.hasOwnProperty(data["method"])){
var keys = opts[data["method"]];
if (keys[i] != "member"){
data[keys[i]] = hexToDecimal(topic);
} else {
data[keys[i]] = stripExtraZeros(topic);
}
}
}
}
function hexToUtf8(hexStr) {
return Buffer.from(hexStr, 'hex').toString('utf8');
}
// Extracting data for specific methods
if (data["method"]== "Submit Proposal"){
const hexData = $('HTTP Request').first().json.result.logs[0].data;
// Extracting segments from hexData (assuming it's already defined)
const votingPeriodHex = hexData.slice(2, 66); // 64 characters (32 bytes) for votingPeriod
const proposalDataHex = hexData.slice(66, 130); // 64 characters (32 bytes) for proposalData
const expirationHex = hexData.slice(130, 194); // 64 characters (32 bytes) for expiration
const baalGasHex = hexData.slice(194, 258); // 64 characters (32 bytes) for baalGas
const selfSponsorHex = hexData.slice(258, 322); // 64 characters (32 bytes) for selfSponsor
const timestampHex = hexData.slice(322, 386); // 64 characters (32 bytes) for timestamp
const detailsHex = hexData.slice(386); // Remaining part for details
// Decode hex segments into decimal or string format
const votingPeriod = hexToDecimal(votingPeriodHex);
const proposalData = proposalDataHex;
const expiration = hexToDecimal(expirationHex);
const baalGas = hexToDecimal(baalGasHex);
const selfSponsor = hexToDecimal(selfSponsorHex);
const timestamp = hexToDecimal(timestampHex);
let detailsObj;
detailsObj = hexToUtf8(detailsHex);
const jsonStringStart = detailsObj.indexOf('{"');
if (jsonStringStart !== -1) {
detailsObj = detailsObj.slice(jsonStringStart);
}
// Construct the JSON object
const jsonObject = {
votingPeriod: votingPeriod,
proposalData: proposalData,
expiration: expiration,
baalGas: baalGas,
selfSponsor: selfSponsor,
timestamp: timestamp,
details: detailsObj
};
data['data'] = jsonObject;
} else if (data["method"]== "Submit Vote"){
const hexData = $('HTTP Request').first().json.result.logs[0].data;
data["votes"]= hexToDecimal(hexData)/1000000000000000000;
} else if (data["method"]== "Process Proposal"){
var logData = []
for (var i = 0; i < Logs.length; i++) {
if (Logs[i].topics[0]=="0xb4571f7e4e2c2b6e6185e47ab5caa5fe34087299bd49fbae945a4583101ee3f0"){
data["proposal"] = hexToDecimal(Logs[i].topics[1]);
}
}
}
console.log(data);
return data;