Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work in progress, case sensitivity is going to be tricky #171

Draft
wants to merge 45 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
a80eb41
Work in progress, case sensitivity is going to be tricky
johnmark13 Mar 30, 2023
975be71
credbot Mar 31, 2023
8712370
credbot Mar 31, 2023
a64464e
credbot Mar 31, 2023
0d54657
OK, quite a hacky way to match, but, matches
johnmark13 Mar 31, 2023
36a5a38
Merge branch 'automated-ledger-update' of github.com:nation3/nationcr…
johnmark13 Mar 31, 2023
529ff2e
credbot Apr 3, 2023
9cad903
credbot Apr 3, 2023
a4aef76
Can create GitHub identity, have to decide if we want it to?
johnmark13 Apr 3, 2023
08b4eba
Merge branch 'automated-ledger-update' of github.com:nation3/nationcr…
johnmark13 Apr 3, 2023
b27c6ca
credbot Apr 3, 2023
6f9b674
credbot Apr 3, 2023
56e2f55
credbot Apr 3, 2023
4bdf91f
credbot Apr 5, 2023
9e7321d
credbot Apr 5, 2023
c379766
credbot Apr 5, 2023
3edaa7d
credbot Apr 5, 2023
69e2304
credbot Apr 5, 2023
cfb7e02
credbot Apr 5, 2023
714081e
credbot Apr 5, 2023
717eed4
OK, it is messy but Discord in as well, will split across files
johnmark13 Apr 5, 2023
c9da7c3
Merge branch 'automated-ledger-update' of github.com:nation3/nationcr…
johnmark13 Apr 5, 2023
a43e0cb
credbot Apr 5, 2023
3d5fa83
credbot Apr 5, 2023
d41b17d
credbot Apr 5, 2023
ca117c3
Discourse
johnmark13 Apr 5, 2023
bc70695
credbot Apr 5, 2023
f79fc08
Put things back
johnmark13 Apr 5, 2023
fc0b557
Put things back
johnmark13 Apr 5, 2023
10b1944
credbot Apr 5, 2023
d58e184
credbot Apr 5, 2023
c268424
credbot Apr 5, 2023
6c1108c
Rest ledger to Main
johnmark13 Apr 5, 2023
c7b6cf7
credbot Apr 5, 2023
b3038b3
credbot Apr 5, 2023
1a22a13
credbot Apr 5, 2023
7589f32
credbot Apr 6, 2023
962aaf0
credbot Apr 6, 2023
e4e40d0
Dont update ledger if no need
johnmark13 Apr 6, 2023
0f49782
Merge branch 'automated-ledger-update' of github.com:nation3/nationcr…
johnmark13 Apr 6, 2023
3f7a7ec
Change config property name to discordGuildId
johnmark13 Apr 11, 2023
e8c55fc
Update scripts/github-utils.js
johnmark13 Apr 11, 2023
e51fe60
Fix Dicord typo
johnmark13 Apr 11, 2023
d3fa718
Fix Disuorce typo
johnmark13 Apr 11, 2023
97276a8
Merge branch 'automated-ledger-update' of github.com:nation3/nationcr…
johnmark13 Apr 11, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
920 changes: 754 additions & 166 deletions data/ledger.json

Large diffs are not rendered by default.

15,017 changes: 15,017 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@
"better-sqlite3": "^7.6.2",
"csv-parser": "^3.0.0",
"csv-writer": "^1.6.0",
"discord.js": "^14.9.0",
"node-fetch": "^2.6.9",
"sourcecred": "^0.11.2"
},
"scripts": {
"clean": "rimraf cache site",
"clean-all": "yarn clean && rimraf output",
"load": "dotenv sourcecred load",
"start": "dotenv -- sourcecred go --no-load && sourcecred serve",
"grain": "sourcecred grain"
"grain": "sourcecred grain",
"update-ledger-from-chain": "dotenv node scripts/update-ledger-from-chain.js"
},
"devDependencies": {
"dotenv-cli": "^6.0.0",
Expand Down
7 changes: 7 additions & 0 deletions scripts/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
guildId: "690584551239581708",
johnmark13 marked this conversation as resolved.
Show resolved Hide resolved
repo: "nation3/sourcecred-instance",
branch: "automated-ledger-update",
chainId: "1",
tokenAddress: "0x333A4823466879eeF910A04D473505da62142069"
}
134 changes: 134 additions & 0 deletions scripts/discord-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
const { Client, GatewayIntentBits } = require('discord.js');
const sc = require('sourcecred').sourcecred;
const scUtils = require('./sourcecred-utils');

module.exports = {
getDiscordMembers: async (guildId) => {
const token = process.env.SOURCECRED_DISCORD_TOKEN;
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers]
});

client.once('ready', () => {
console.log('Discord Bot is Go!');
});

await client.login(token);

const discordMembers = new Map();

const guild = client.guilds.resolve(guildId);

const rawMembers = await guild.members.fetch();

rawMembers.forEach((member) => {
discordMembers.set(`${member.user.username}#${member.user.discriminator}`, member.id);
});

client.destroy();

return discordMembers;
},

testAndUpdateDiscordAccount: async (ledgerManager, lowerAccountToIdentityMap, discordMemberMap, passportData, chainId, tokenAddress) => {
const {passport_id, owner_address, discord_username,discord_username_ens} = passportData;

console.info(`Reading Discord data for: ${passport_id}`);

if ((!discord_username) && (!discord_username_ens)) {
console.info(`No Discord username for passport ${passport_id}`)
return;
}

//get just the username from one of the two columns
let discordUsernameAndDiscriminator = cleanDiscordUsername(discord_username);
if (!discordUsernameAndDiscriminator) {
discordUsernameAndDiscriminator = cleanDiscordUsername(discord_username_ens);
}

if (!discordUsernameAndDiscriminator) {
console.info(`Invalid Discord username ${discord_username}/${discord_username_ens} provided, skipping Discord for passport ${passport_id}`)
return;
}

//Discord idenitity is stored using the ID of the account, which we do not have, so have to have a Bot which can find it
//N\u0000sourcecred\u0000discord\u0000MEMBER\u0000user\u0000976148126308122644\u0000

const discordUserId = discordMemberMap.get(`${discordUsernameAndDiscriminator[0]}#${discordUsernameAndDiscriminator[1]}`);

if(!discordUserId) {
console.log(`No Discord Member found in Nation3 Dicsord with Username ${discordUsernameAndDiscriminator[0]}#${discordUsernameAndDiscriminator[1]} not setting Discord Identity for ${passport_id}`);
johnmark13 marked this conversation as resolved.
Show resolved Hide resolved
return;
}

const dAddress = scUtils.createDiscordIdentity(discordUsernameAndDiscriminator, discordUserId);

const hasAccount = lowerAccountToIdentityMap.has(dAddress.toLowerCase());
if(hasAccount) {
scUtils.testAndUpdateAccountPayoutAddress(
ledgerManager,
lowerAccountToIdentityMap,
dAddress,
'discord',
discordUsernameAndDiscriminator,
passport_id,
owner_address,
chainId,
tokenAddress);
}
else {
addDiscordIdentityAndSetPayoutAddress(
ledgerManager,
dAddress,
discordUsernameAndDiscriminator,
passport_id,
owner_address,
chainId,
tokenAddress);
}
}
}

function addDiscordIdentityAndSetPayoutAddress(ledgerManager, scAddress, dUsername, passport_id, owner_address, chainId, tokenAddress) {
console.log(`Creating a new SourceCred identity for ${dUsername} for passport ${passport_id}`)

const baseIdentityProposal = scUtils.createDiscordIdentityProposal(scAddress, dUsername);

const baseIdentityId = sc.ledger.utils.ensureIdentityExists(
ledgerManager.ledger,
baseIdentityProposal,
);

console.log(`Base Identity ID ${JSON.stringify(baseIdentityId)}`);

console.log(`Setting payout address for Discord for passport ${passport_id} - discordUsername ${dUsername}`)

ledgerManager.ledger.setPayoutAddress(baseIdentityId, owner_address , chainId, tokenAddress);

console.log(`Updated payout address to ${owner_address} for passport ${passport_id} - discordUsername ${dUsername}`)
}

function cleanDiscordUsername(dUsername) {
//https://discord.gg/grbsArn
//Adelaide Isla#3410
let processing = dUsername;

if(!processing || processing.indexOf('#') < 0) {
return null;
}

if(dUsername.indexOf('/') >= 0) {
const regex = /.*\/(.*)/gm;
processing = processing.replace(regex, '$1');
}

if(processing.startsWith('@')) {
processing = processing.substring(0);
}

const hash = processing.lastIndexOf('#');
const username = processing.substring(0, hash);
const discriminator = processing.substring(hash + 1);

return [username, discriminator];
}
63 changes: 63 additions & 0 deletions scripts/discourse-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const sc = require('sourcecred').sourcecred;
const scUtils = require('./sourcecred-utils');

module.exports = {
//exists - {"action":{"alias":{"address":"N\u0000sourcecred\u0000discourse\u0000user\u0000https://forum.nation3.org\u0000aahna\u0000","description":"discourse/[@aahna](https://forum.nation3.org/u/aahna/)"}
//new - {"action":{"alias":{"address":"N\u0000sourcecred\u0000dicourse\u0000user\u0000https://forum.nation3.org\u0000aahna\u0000","description":"discourse/[@aahna](https://forum.nation3.org/u/aahna)"}
testAndUpdateDiscourseAccount: async (ledgerManager, lowerAccountToIdentityMap, passportData, chainId, tokenAddress) => {
const {passport_id, owner_address, discourse_username} = passportData;

console.info(`Reading Discourse data for: ${passport_id}`);

if ((!discourse_username)) {
console.info(`No Discourse username for passport ${passport_id}`)
return;
}

//construct identity
const discourseAddress = scUtils.createDiscourseIdentity(discourse_username);

const hasAccount = lowerAccountToIdentityMap.has(discourseAddress.toLowerCase());
if(hasAccount) {
scUtils.testAndUpdateAccountPayoutAddress(
ledgerManager,
lowerAccountToIdentityMap,
discourseAddress,
'discourse',
discourse_username,
passport_id,
owner_address,
chainId,
tokenAddress);
}
else {
addDiscourseIdentityAndSetPayoutAddress(
ledgerManager,
discourseAddress,
discourse_username,
passport_id,
owner_address,
chainId,
tokenAddress);
}
}
}

function addDiscourseIdentityAndSetPayoutAddress(ledgerManager, scAddress, dUsername, passport_id, owner_address, chainId, tokenAddress) {
console.log(`Creating a new SourceCred identity for ${dUsername} for passport ${passport_id}`)

const baseIdentityProposal = scUtils.createDiscourseIdentityProposal(scAddress, dUsername);

const baseIdentityId = sc.ledger.utils.ensureIdentityExists(
ledgerManager.ledger,
baseIdentityProposal,
);

console.log(`Base Identity ID ${JSON.stringify(baseIdentityId)}`);

console.log(`Setting payout address for GitHub for passport ${passport_id} - gitHubUsername ${dUsername}`)

ledgerManager.ledger.setPayoutAddress(baseIdentityId, owner_address ,chainId, tokenAddress);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ledgerManager.ledger.setPayoutAddress(baseIdentityId, owner_address ,chainId, tokenAddress);
ledgerManager.ledger.setPayoutAddress(baseIdentityId, owner_address, chainId, tokenAddress);


console.log(`Updated payout address to ${owner_address} for passport ${passport_id} - gitHubUsername ${dUsername}`)
}
82 changes: 82 additions & 0 deletions scripts/github-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const sc = require('sourcecred').sourcecred;
const scUtils = require('./sourcecred-utils');

module.exports = {
testAndUpdateGithubAccount: async (ledgerManager, lowerAccountToIdentityMap, passportData, chainId, tokenAddress) => {
const {passport_id, owner_address, github_username, github_username_ens} = passportData;

console.info(`Reading GitHub data for: ${passport_id}`);

if ((!github_username) && (!github_username_ens)) {
console.info(`No GitHub username for passport ${passport_id}`)
return;
}

//get just the username from one of the two columns
const tidiedGithubUsername = github_username && github_username.length > 0
? cleanGithubUsername(github_username)
: cleanGithubUsername(github_username_ens);

//construct identity
const ghAddress = scUtils.createGitHubIdentity(tidiedGithubUsername);

const hasAccount = lowerAccountToIdentityMap.has(ghAddress.toLowerCase());
if(hasAccount) {
scUtils.testAndUpdateAccountPayoutAddress(
ledgerManager,
lowerAccountToIdentityMap,
ghAddress,
'github',
tidiedGithubUsername,
passport_id,
owner_address,
chainId,
tokenAddress);
}
else {
addGitHubIdentityAndSetPayoutAddress(
ledgerManager,
ghAddress,
tidiedGithubUsername,
passport_id,
owner_address,
chainId,
tokenAddress);
}
}
}

function addGitHubIdentityAndSetPayoutAddress(ledgerManager, scAddress, ghUsername, passport_id, owner_address, chainId, tokenAddress) {
console.log(`Creating a new SourceCred identity for ${ghUsername} for passport ${passport_id}`)

const baseIdentityProposal = scUtils.createGitHubIdentityProposal(scAddress, ghUsername);

const baseIdentityId = sc.ledger.utils.ensureIdentityExists(
ledgerManager.ledger,
baseIdentityProposal,
);

console.log(`Base Identity ID ${JSON.stringify(baseIdentityId)}`);

console.log(`Setting payout address for GitHub for passport ${passport_id} - gitHubUsername ${ghUsername}`)

ledgerManager.ledger.setPayoutAddress(baseIdentityId, owner_address ,chainId, tokenAddress);
johnmark13 marked this conversation as resolved.
Show resolved Hide resolved

console.log(`Updated payout address to ${owner_address} for passport ${passport_id} - gitHubUsername ${ghUsername}`)
}

function cleanGithubUsername(ghUsername) {
//https://github.com/luisivan
//@adelaideisla
let processing = ghUsername;
if(ghUsername.indexOf('/') >= 0) {
const regex = /.*\/(.*)/gm;
processing = processing.replace(regex, '$1');
}

if(processing.startsWith('@')) {
processing = processing.substring(0);
}

return processing;
}
Loading