Published
Edited
Jun 29, 2021
Insert cell
Insert cell
DOM.download(
serialize(processedRedemptions),
"redemptions.csv",
"Download Redemption Data"
)
Insert cell
DOM.download(
serialize(processedDistributions),
"distributions.csv",
"Download Distribution Data"
)
Insert cell
BN = await require('bignumber.js')
Insert cell
sc = await require("sourcecred@latest")
Insert cell
async function loadCredView(repo) {
const credResultFile = `https://raw.githubusercontent.com/${repo}/gh-pages/output/credResult.json`;
const credResultRaw = await (await fetch(credResultFile)).json()
const credResult = sc.analysis.credResult.fromJSON(credResultRaw)
const credView = new sc.analysis.credView.CredView(credResult)
return credView;
}
Insert cell
async function loadLedger(repo) {
const ledgerFile = `https://raw.githubusercontent.com/${repo}/gh-pages/data/ledger.json`;
const ledgerRaw = await (await fetch(ledgerFile)).text();
return sc.ledger.ledger.Ledger.parse(ledgerRaw);
}
Insert cell
ledger = loadLedger("sourcecred/cred")
Insert cell
G = sc.ledger.grain
Insert cell
Insert cell
Insert cell
Insert cell
transfers = {
const logs = ledger.eventLog();
return (
logs
//.filter(x => x.ledgerTimestamp < END_TS && x.ledgerTimestamp > START_TS)
.filter((x) => x.action.type === "TRANSFER_GRAIN")
);
}
Insert cell
redemptions = transfers.filter(
(x) =>
x.action.to === sourcecredId ||
(x.action.to === protocolId && x.action.from !== sourcecredId)
)
Insert cell
processedRedemptions = redemptions.map((x) => ({
timestamp: x.ledgerTimestamp,
date: new Date(x.ledgerTimestamp),
amount: G.format(x.action.amount, 2, ""),
redeemer: idToName.get(x.action.from),
buyer: idToName.get(x.action.to)
}))
Insert cell
distributions = {
const logs = ledger.eventLog();
return (
logs
//.filter(x => x.ledgerTimestamp < END_TS && x.ledgerTimestamp > START_TS)
.filter((x) => x.action.type === "DISTRIBUTE_GRAIN")
);
}
Insert cell
participantDistributions = {
const results = [];
for (const { action, ledgerTimestamp } of distributions) {
for (const { receipts } of action.distribution.allocations) {
for (const { id, amount } of receipts) {
if (amount === "0") continue;
results.push({ id, amount, ledgerTimestamp });
}
}
}
return results;
}
Insert cell
processedDistributions = participantDistributions.map((x) => ({
id: x.id,
name: idToName.get(x.id),
amount: G.format(x.amount, 2, ""),
date: new Date(x.ledgerTimestamp),
timestamp: x.ledgerTimestamp
}))
Insert cell
distributions[0].action.distribution
Insert cell
ledger.accounts()
Insert cell
participantList = ledger.accounts().map(a => a.identity.name)
Insert cell
nameToId = {
const m = new Map();
for (const { identity } of ledger.accounts()) {
m.set(identity.name, identity.id);
}
return m;
}
Insert cell
idToName = {
const m = new Map();
for (const { identity } of ledger.accounts()) {
m.set(identity.id, identity.name);
}
return m;
}
Insert cell
sourcecredId = nameToId.get("sourcecred")
Insert cell
protocolId = nameToId.get("protocol")
Insert cell
json2csv = require("json2csv")
Insert cell
function serialize (data) {
let parser = new json2csv.Parser();
let csv = parser.parse(data);
return new Blob([csv], {type: "text/csv"})
}
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