Published
Edited
Oct 5, 2021
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
actionTypes = Array.from(new Set(augmentedLedgerLog.map(event => event.action.type)))
Insert cell
augmentedLedgerLog = truncatedLedgerLog.map(event => {
if (event.action.type === "TRANSFER_GRAIN") {
event.action.from = getAccountName(event.action.from);
event.action.to = getAccountName(event.action.to);
event.action.amount = g(event.action.amount);
}
if (event.action.type === "TOGGLE_ACTIVATION") {
event.action.identityId = getAccountName(event.action.identityId);
}
if (event.action.type === "MERGE_IDENTITIES") {
const base = getAccount(event.action.base);
event.action.base = base ? base.identity.name : "IDENTITY MERGED";
event.action.newAliases = base ? base.identity.aliases.map(a => a.description) : "ERROR";
event.action.target = getAccountName(event.action.target);
}
if (event.action.type === "DISTRIBUTE_GRAIN") {
if (event.action.distribution) {
event.action.distribution.allocations.forEach(allocation => {
allocation.policy.budget = g(allocation.policy.budget);
allocation.receipts.sort((a, b) => a.amount.padStart(30, '0') < b.amount.padStart(30, '0') ? 1 : -1);
allocation.receipts.forEach(receipt => {
receipt.id = getAccountName(receipt.id);
receipt.amount = g(receipt.amount);
});
});
}
}
event.ledgerTimestamp = new Date(event.ledgerTimestamp).toLocaleString("en", {
month: "numeric",
day: "numeric",
hour: "numeric",
minute: "numeric",
year: "2-digit",
});
return event;
})
Insert cell
truncatedLedgerLog = ledger
? JSON.parse(JSON.stringify(diffedEventLog.slice(-limit).reverse()))
: []
Insert cell
ledger = repoAndBranch ? loadLedger(repoAndBranch) : null
Insert cell
firstDiff = {
if (!ledger || !baseLedgerLogIds) return null;
const result = ledger
.eventLog()
.findIndex(({ uuid }) => !baseLedgerLogIds.includes(uuid));
return result == -1 ? ledger.eventLog().length : result;
}
Insert cell
diffedEventLog = ledger
? shouldDiff
? ledger.eventLog().slice(firstDiff)
: ledger.eventLog()
: null
Insert cell
repo = {
if (branch && location.search.includes("?repo=")) {
const match = location.search.match(/\\?repo=(.*)/);
if (match) return match[1];
}
return null;
}
Insert cell
baseLedgerLogIds = baseLedger ? baseLedger.eventLog().map((x) => x.uuid) : null
Insert cell
baseLedger = repo ? loadLedger(repo + "/" + main) : null
Insert cell
repoAndBranch = repo + "/" + branch
Insert cell
sc = (await require("sourcecred@latest"))
Insert cell
async function loadLedger(repoAndBranch) {
const ledgerFile = `https://raw.githubusercontent.com/${repoAndBranch}/data/ledger.json`;
const ledgerRaw = await (await fetch(ledgerFile)).text();
try {
return sc.ledger.ledger.Ledger.parse(ledgerRaw);
} catch {
return null;
}
}
Insert cell
g = grain => {
return sc.ledger.grain.format(grain, 2);
}
Insert cell
getAccountName = (identityId) => {
const account = getAccount(identityId);
return account ? account.identity.name : "ERROR";
}
Insert cell
reversedLog = ledger ? ledger.eventLog().slice().reverse() : []
Insert cell
getAccount = (identityId) => {
const mergeIndex = reversedLog.findIndex(event => {
return event.action.type == "MERGE_IDENTITIES" && event.action.target == identityId
})
if (mergeIndex === -1) return ledger.account(identityId)
const slicedLog = reversedLog.slice(mergeIndex + 1).reverse()
const slicedLedger = sc.ledger.ledger.Ledger.fromEventLog(slicedLog)
return slicedLedger.account(identityId);
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more