Published
Edited
Feb 6, 2021
Insert cell
Insert cell
sc = (await require("sourcecred-publish-test@latest"))
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(ledger.eventLog().slice(-100).reverse())) : []
Insert cell
ledger =
repoAndBranch ? loadLedger(repoAndBranch) : null;
Insert cell
repoAndBranch = {
if (branchUrl && branchUrl.length > 0) {
const match = branchUrl.match(/.*github\.com\/(.*)tree\/(.*)(\/?)/);
if (match) return match[1] + match[2];
}
if (location.search.includes("?branchUrl=")) {
const match = location.search.match(/.*github\.com\/(.*)tree\/(.*)(\/?)/);
if (match) return match[1] + match[2];
}
if (location.search.includes("?repoAndBranch=")) {
const match = location.search.match(/\\?repoAndBranch=(.*)/);
if (match) return match[1];
}
return null;
}
Insert cell
async function loadLedger(repoAndBranch) {
const ledgerFile = `https://raw.githubusercontent.com/${repoAndBranch}/data/ledger.json`;
const ledgerRaw = await (await fetch(ledgerFile)).text();
return sc.ledger.ledger.Ledger.parse(ledgerRaw);
}
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) => {
try {
return ledger.account(identityId)
} catch {
const mergeIndex = reversedLog.findIndex(event => {
return event.action.type == "MERGE_IDENTITIES" && event.action.target == identityId
})
if (mergeIndex === -1) return null
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