Skip to content

Commit

Permalink
Nuke curate to earn program
Browse files Browse the repository at this point in the history
  • Loading branch information
TimDaub committed Oct 24, 2024
1 parent f825077 commit 649f05d
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 198 deletions.
23 changes: 1 addition & 22 deletions src/cache.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { subYears, formatISO } from "date-fns";
import Database from "better-sqlite3";
import { add } from "date-fns";
import normalizeUrl from "normalize-url";
import { ethers } from "ethers";

import log from "./logger.mjs";

Expand Down Expand Up @@ -147,28 +148,6 @@ export function getRandomIndex() {
return index;
}

export function getLeaders() {
const oneWeekAgo = Math.floor(Date.now() / 1000) - 7 * 24 * 60 * 60;
const query = `
SELECT identity, SUM(karma) AS totalKarma
FROM (
SELECT identity, COUNT(*) AS karma
FROM submissions
WHERE timestamp >= ?
GROUP BY identity
UNION ALL
SELECT identity, COUNT(*) AS karma
FROM upvotes
WHERE timestamp >= ?
GROUP BY identity
)
GROUP BY identity
ORDER BY totalKarma DESC
LIMIT 10
`;
return db.prepare(query).all(oneWeekAgo, oneWeekAgo);
}

export function getContributionsData(identity) {
const endDate = new Date();
const startDate = new Date(new Date().getFullYear(), 0, 1);
Expand Down
46 changes: 11 additions & 35 deletions src/frame.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { utils } from "ethers";
import htm from "htm";
import vhtml from "vhtml";

import { getLeaders } from "./cache.mjs";
import * as price from "./price.mjs";
import * as registry from "./chainstate/registry.mjs";

Expand All @@ -28,43 +27,20 @@ const emptyDelegation = [
"0x0000000000000000000000000000000000000000000000000000000000000000",
];

const treasury = "0x1337E2624ffEC537087c6774e9A18031CFEAf0a9";
function computeDistribution(referral, difference) {
const leaders = getLeaders();

let allKarma = leaders.reduce((sum, { totalKarma }) => sum + totalKarma, 0);
if (referral) {
const last = leaders.pop();
allKarma -= last.totalKarma;

// NOTE: We have to give the referrer all the karma earned from everyone
// else as this gives them half of the protocol reward.
// Previuously, we gave the referrer half of all karma, but this lead to
// the referrer only getting a quarter.
leaders.push({ identity: referral, totalKarma: allKarma });
allKarma *= 2;
}

const recipients = [];
const values = [];
if (difference !== 0n) {
const allKarmaBigInt = BigInt(allKarma);
let remainder = difference;

for (const { identity, totalKarma } of leaders) {
const share = (difference * BigInt(totalKarma)) / allKarmaBigInt;
recipients.push(identity);
values.push(share);
remainder -= share;
}

for (let i = 0; i < remainder; i++) {
values[i] += 1n;
}
const beneficiaries = [];
const amounts = [];
if (referral && difference) {
beneficiaries.push(referral);
amounts.push(difference);
} else {
beneficiaries.push(treasury);
amounts.push(difference);
}

return {
beneficiaries: recipients,
amounts: values,
beneficiaries,
amounts,
};
}

Expand Down
18 changes: 1 addition & 17 deletions src/http.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,7 @@ import * as frame from "./frame.mjs";
import * as subscriptions from "./subscriptions.mjs";
import * as telegram from "./telegram.mjs";
import * as price from "./price.mjs";
import {
getRandomIndex,
getSubmission,
trackOutbound,
getLeaders,
} from "./cache.mjs";
import { getRandomIndex, getSubmission, trackOutbound } from "./cache.mjs";

const app = express();
const server = createServer(app);
Expand Down Expand Up @@ -382,17 +377,6 @@ export async function launch(trie, libp2p) {
reply.header("Cache-Control", "no-cache");
return reply.status(200).type("text/html").send(embed);
});
app.get("/api/v1/leaderboard", async (request, reply) => {
const leaders = getLeaders();
const code = 200;
const httpMessage = "OK";
const details = `Get Leaderboard`;
reply.header(
"Cache-Control",
"public, s-maxage=300, max-age=300, must-revalidate, stale-while-revalidate=3600",
);
return sendStatus(reply, code, httpMessage, details, { leaders });
});
app.get("/api/v1/karma/:address", async (request, reply) => {
let address;
try {
Expand Down
14 changes: 0 additions & 14 deletions src/views/components/sidebar.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -597,20 +597,6 @@ const sidebar = (path) => html`
</div>
</a>
</div>
<a
title="Earn"
href="/referral"
style="${path === "/referral"
? " font-weight: bold; background-color: rgba(0,0,0,0.1);"
: "font-weight: normal"}; color: black; text-decoration: none; display: block;"
>
<div style="display: flex; align-items: center;">
<div class="svg-container">
${path === "/referral" ? coinfull : coin}
</div>
<span>Earn</span>
</div>
</a>
<a
title="Community"
href="/community"
Expand Down
86 changes: 3 additions & 83 deletions src/views/referral.mjs
Original file line number Diff line number Diff line change
@@ -1,30 +1,15 @@
//@format
import htm from "htm";
import vhtml from "vhtml";
import { ethers } from "ethers";

import Header from "./components/header.mjs";
import Sidebar from "./components/sidebar.mjs";
import Footer from "./components/footer.mjs";
import Head from "./components/head.mjs";
import InviteRow from "./components/invite-row.mjs";
import * as ens from "../ens.mjs";
import * as price from "../price.mjs";
import * as registry from "../chainstate/registry.mjs";
import { getLeaders } from "../cache.mjs";

const html = htm.bind(vhtml);

export default async function (theme) {
const mints = await registry.mints();
const { reward, percentageOff } = await price.getReferralReward(mints);
const leaders = getLeaders();
const ensLeaders = await Promise.all(
leaders.map(({ identity, totalKarma }) =>
ens.resolve(identity).then((resolved) => ({ ...resolved, totalKarma })),
),
);

return html`
<html lang="en" op="news">
<head>
Expand All @@ -41,77 +26,12 @@ export default async function (theme) {
<tr>
<td style="padding: 1rem; text-align: left;">
<h1 style="color: black; font-size: 1.5rem;">
Curate to earn
</h1>
<p>Let's grow the Kiwi community together!</p>
<p>
We've designed an incentive program for those who curate the
content on Kiwi News.
</p>
<p>
The website charges a slight premium over the smart
contract. We're forwarding this amount to the top karma
earners.
</p>
<div style="display: flex; justify-content: center;">
<img
style="width: 80%; border: 1px solid #828282;"
src="onchainprice.png"
/>
</div>
<h1
style="margin-top: 1.5rem; color: black; font-size: 1.5rem;"
>
How to become a Karma earner
Curate to earn (deprecated)
</h1>
<p>
Through submitting good links and upvoting others' content
you can earn karma.
</p>
<p>
The top 10 karma earners of the last 7 days get a share of
the mint price!
This program has been stopped. We are the onchain Hacker
News and paying curators is not a good policy.
</p>
<table style="width: 100%; border-collapse: collapse;">
<thead>
<tr style="color: black;">
<th style="text-align: left; padding: 8px;">#</th>
<th style="text-align: left; padding: 8px;">Name</th>
<th style="text-align: left; padding: 8px;">
🥝 (7 days)
</th>
</tr>
</thead>
<tbody>
${ensLeaders.map(
(leader, index) => html`
<tr
style="background-color: ${index % 2 === 0
? "transparent"
: "rgba(0,0,0,0.1)"};"
>
<td style="padding: 8px;">${index + 1}</td>
<td style="padding: 8px;">
<div style="display: flex; align-items: center;">
${leader.safeAvatar
? html`<img
src="${leader.safeAvatar}"
style="width: 20px; height: 20px; vertical-align: middle; border: 1px solid #828282; border-radius: 2px; margin-right: 5px;"
/>`
: ""}
<a
href="/upvotes?address=${leader.address}"
style="color: inherit; text-decoration: none;"
>${leader.displayName}</a
>
</div>
</td>
<td style="padding: 8px;">${leader.totalKarma}</td>
</tr>
`,
)}
</tbody>
</table>
</td>
</tr>
</table>
Expand Down
34 changes: 7 additions & 27 deletions src/web/src/BuyButton.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export async function prepare(key) {
const validDiscount = discount === theme.discount.code;

if (validDiscount) {
console.log("Found valid discount");
price = price.min;
} else {
price = price.authoritative;
Expand Down Expand Up @@ -82,16 +83,6 @@ export async function prepare(key) {
const authorize = true;
const payload = await create(key, address, key.address, authorize);

const leaderboard = await fetchLeaderboard();
if (!leaderboard || !leaderboard.leaders) {
throw new Error("Error getting the leaderboard");
}

let allKarma = leaderboard.leaders.reduce(
(sum, { totalKarma }) => sum + totalKarma,
0,
);

const recipients = [];
const values = [];
if (!validDiscount) {
Expand All @@ -104,25 +95,14 @@ export async function prepare(key) {
console.log("Couldn't find referral address in URL bar");
//noop
}
const treasury = "0x1337E2624ffEC537087c6774e9A18031CFEAf0a9";
if (referral !== zeroAddress) {
const reward = difference / 2n;
price -= reward;
price -= difference;
recipients.push(referral);
values.push(reward);
} else if (difference !== 0n) {
const allKarmaBigInt = BigInt(allKarma);
let remainder = difference;

for (const { identity, totalKarma } of leaderboard.leaders) {
const share = (difference * BigInt(totalKarma)) / allKarmaBigInt;
recipients.push(identity);
values.push(share);
remainder -= share;
}

for (let i = 0; i < remainder; i++) {
values[i] += 1n;
}
values.push(difference);
} else {
recipients.push(treasury);
values.push(difference);
}
}

Expand Down

0 comments on commit 649f05d

Please sign in to comment.