Public
Edited
Jan 8, 2023
1 fork
Insert cell
Insert cell
Insert cell
sc = require("sourcecred@latest")
// In other environments, you might need to use:
// sc = require("sourcecred").sourcecred
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
createWeightedGraph = () => {
const graph = new sc.core.graph.Graph()
const weights = sc.core.weights.empty()

for (const epoch of coordinape.getEpochs()) {
graph.addNode({
address: addr(epoch),
description: `Epoch from ${epoch.startTime.toISOString()}–${epoch.endTime.toISOString()}`,
timestampMs: epoch.endTime.getTime(),
})

const top = epoch.top
graph.addNode({
address: addr(top),
description: `Top Circle for Epoch ${epoch.startTime.toISOString()}–${epoch.endTime.toISOString()}`,
timestampMs: epoch.endTime.getTime(),
})

graph.addEdge({
address: addr.divided_by([epoch, top]),
timestamp: epoch.endTime.getTime(),
src: addr(epoch),
dst: addr(top),
})

const distribute = (src) => {
for(const dist of src.distribution) {
const dest = dist.destination
graph.addNode({
address: addr(dest),
description: `${dest.type}: ${dest.name}`,
timestampMs: null,
})
graph.addEdge({
address: addr.distributed_to([src, dest]),
timestamp: epoch.endTime.getTime(),
src: addr(src),
dst: addr(dest),
})

weights.edgeWeights.set(
addr.distributed_to([src, dest]),
{ forwards: dist.cost, backwards: 0 },
)

if(dest.distribution) {
distribute(dest)
}
}
}
distribute(top)
}
return { graph, weights }
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
createIdentityProposals = () => (
coordinape.getPlayers().map((player) => ({
// "name" can only contain letters, numbers, and dashes
// You will have to clean your data if it does not already follow that requirement.
// We will soon have a helper function exposed to do that.
name: player.name.replace(/[^\da-z-]/gi, '-'),
pluginName,
type: "USER", // The options are USER, BOT, ORGANIZATION, PROJECT
alias: {
description: `player: ${player.name}`,
address: addr(player),
}
}))
)
Insert cell
identityProposals = createIdentityProposals()
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
graphInput = ({
plugins: [
{
// Use the ConstructorPlugin to pipe your structures into this plugin field. Leave the rest.
plugin: new sc.plugins.ConstructorPlugin({
weightedGraph, declaration, identityProposals
}),
directoryContext: null,
pluginId: pluginName,
}
],
ledger: new sc.ledger.ledger.Ledger()
})
Insert cell
// should not error
graphOutput = sc.api.graph.graph(graphInput)
Insert cell
Insert cell
credrankInput = ({
pluginGraphs: [weightedGraph], // Pass your weightedGraph here, keep the rest as is.
ledger: graphOutput.ledger, // Important to use the same ledger that was generated by your graph API test
dependencies: [],
weightOverrides: sc.core.weights.empty(),
pluginsBudget: null,
personalAttributions: []
})
Insert cell
// should not error
credrankOutput = sc.api.credrank.credrank(credrankInput)
Insert cell
Insert cell
credrankOutput.credGrainView
.participants()
.map(p => `Name: ${p.identity.name}\nTotal Score: ${p.cred}`)
.join('\n\n')
Insert cell
Insert cell
serializedWeightedGraph = JSON.stringify(
sc.core.weightedGraph.toJSON(weightedGraph), null, 2 // Notice, this is a little different than the other two.
)
Insert cell
serializedDeclaration = JSON.stringify(declaration, null, 2)
Insert cell
serializedIdentityProposals = JSON.stringify(identityProposals, null, 2)
Insert cell
Insert cell
Insert cell
coordinape = {
const players = Array.from({ length: 4 }).map((_, idx) => ({
type: 'player',
id: idx + 6,
name: `Player #${idx + 1}`,
}))
const guilds = [
{
type: 'guild',
id: 3,
name: 'Guild #1',
distribution: [
{ destination: players[0], cost: 20 },
{ destination: players[1], cost: 20 },
{ destination: players[2], cost: 20 },
],
},
{
type: 'guild',
id: 4,
name: 'Guild #2',
distribution: [
{ destination: players[1], cost: 20 },
{ destination: players[2], cost: 20 },
{ destination: players[3], cost: 20 },
],
},
{
type: 'guild',
id: 5,
name: 'Guild #3',
distribution: [
{ destination: players[0], cost: 20 },
{ destination: players[3], cost: 20 },
],
},
]
const top = {
type: 'top',
id: 2,
distribution: [
{ destination: guilds[0], cost: 50 },
{ destination: guilds[1], cost: 50 },
{ destination: guilds[2], cost: 50 },
],
}
return ({
getEpochs: () => ([
{
type: 'epoch',
id: 1,
startTime: (() => {
const now = new Date()
const monthAgo = new Date()
monthAgo.setMonth(now.getMonth() - 1)
return monthAgo
})(),
endTime: new Date(),
top,
},
]),
getPlayers: () => players,
})
}
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