australian_capital_dao/message.js

75 lines
2.5 KiB
JavaScript

function cleanJsonString(jsonString) {
// Remove invisible and non-printable characters (excluding newlines, carriage returns, and tabs)
const cleanedString = jsonString.replace(/[^\x20-\x7E\r\n\t]+/g, '');
// Remove leading or trailing whitespace
const trimmedString = cleanedString.trim();
// Ensure the string is enclosed within curly braces if it's a JSON object
if (!trimmedString.startsWith('{') || !trimmedString.endsWith('}')) {
throw new Error('Invalid JSON format: Missing opening or closing brace.');
}
return trimmedString;
}
const addressNames = {
"0x6cb4b39bec23a921c9a20d061bf17d4640b0d39e":"woodburn.au"
};
function addressName(address) {
if (addressNames.hasOwnProperty(address)){
return addressNames[address];
}
return address;
}
var message = "New DAO transaction: ";
const parsed = $('Parser').first().json;
message += parsed.method;
if (parsed.method == "Submit Vote"){
message += "\n"+ addressName(parsed.member) + " voted ";
if (parsed.vote == 1){
message += "for";
} else {
message += "against";
}
message += " proposal #"+parsed.proposal;
message += " using " + parsed.votes + " votes";
}
else if (parsed.method == "Sponsor Proposal"){
message += "\n"+ addressName(parsed.member) + " sponsored ";
message += "proposal #"+parsed.proposal;
}
else if (parsed.method == "Submit Proposal"){
message += "\n"+ addressName($('HTTP Request').first().json.result.from);
message += " created proposal #" + parsed.proposal;
try {
const cleanedJsonString = cleanJsonString(parsed.data.details);
const details = JSON.parse(cleanedJsonString);
message += "\nProposal: " + details.title;
message += "\nDescription: " + details.description;
if (details.contentURI != ""){
message += "\nURL: " + details.contentURI;
}
}
catch (error){
message += "\nProposal details failed to parse";
message += error;
}
}
else if (parsed.method == "Process Proposal"){
message += "\n"+ addressName($('HTTP Request').first().json.result.from);
message += " executed proposal #" + parsed.proposal;
}
return {
"message":message,
"explorer":"https://optimistic.etherscan.io/tx/"+$('HTTP Request').first().json.result.transactionHash,
"proposalURL":"https://admin.daohaus.club/#/molochV3/0xa/0xf4604948ad5365840803297bf81cd9a46c36fce7/proposal/"+parsed.proposal
};