Published
Edited
Nov 16, 2020
1 fork
Insert cell
md`# SourceCred Ledger Inspection`
Insert cell
sc = (await require("sourcecred")).default
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
totalGrain = {
let totalGrain = "0";
for (const { balance } of ledger.accounts()) {
totalGrain = G.add(totalGrain, balance);
}
return G.format(totalGrain);
}
Insert cell
activeContributors = ledger.accounts().filter(x => x.active).length
Insert cell
G.format(totalGrain)
Insert cell
START_TS = +new Date("2020-08-01")
Insert cell
END_TS = +new Date("2020-09-01")
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
ledger.accounts()
Insert cell
allocations = {
const allocs = [];
for (const { action } of distributions) {
const distribution = action.distribution;
for (const alloc of distribution.allocations) {
allocs.push(alloc);
}
}
return allocs;
}
Insert cell
allocationByType = {
const byType = { SPECIAL: "0", BALANCED: 0, IMMEDIATE: 0 };
for (const { policy } of allocations) {
byType[policy.policyType] = G.add(byType[policy.policyType], policy.budget);
}
return byType;
}
Insert cell
G.format(allocationByType["SPECIAL"])
Insert cell
G.format(allocationByType["IMMEDIATE"])
Insert cell
G.format(allocationByType["BALANCED"])
Insert cell
specialAllocs = allocations.filter(x => x.policy.policyType === "SPECIAL")
Insert cell
specialAllocs.map(x => ({
name: ledger.account(x.policy.recipient).identity.name,
amt: G.format(x.policy.budget),
memo: x.policy.memo
}))
Insert cell
transfers = {
const logs = ledger.eventLog();
return logs.filter(x => x.action.type === "TRANSFER_GRAIN");
}
Insert cell
transfers.map(t => ({
name: ledger.account(t.action.from).identity.name,
amount: G.format(t.action.amount),
date: new Date(t.ledgerTimestamp),
memo: t.action.memo
}))
Insert cell
currentBalance = {
let bal = "0";
for (const { identity, balance } of ledger.accounts()) {
if (identity.name == "dai-redemptions") continue;
bal = G.add(bal, balance);
}
return G.format(bal);
}
Insert cell
md`

# Notes

## June: Distributed $5k / Paid $1169

## July: Distributed 15k Total / Paid $2734

## August Distributed 25k Total

`
Insert cell
treasuryId = ledger.accountByName("dai-redemptions").identity.id
Insert cell
ledger.accounts()
Insert cell
{
const led = await loadLedger("sourcecred/makerdao-cred");
for (const { identity, balance, active } of led.accounts()) {
if (identity.id === treasuryId) continue;
if (!active) continue;
led.transferGrain({
from: identity.id,
to: treasuryId,
amount: balance,
memo:
"https://etherscan.io/tx/0xd572db896b138c43e42be93348c718a41ad65b54ba467f15ce4078b192d7da16"
});
}
const log = led.eventLog();
return led.serialize();
}
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