Published
Edited
May 7, 2021
3 forks
1 star
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(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
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();
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) => {
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

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more