Skip to content

Commit

Permalink
Merge pull request #2797 from Sifchain/scripts/update-reward-period-gen
Browse files Browse the repository at this point in the history
  • Loading branch information
timlind authored May 23, 2022
2 parents 50300e8 + 892c9a8 commit 20e79e1
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 15 deletions.
4 changes: 3 additions & 1 deletion scripts/rewards/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
entries.json
pools.csv
rewards.json
rewards.json
node_modules
RP_*
14 changes: 14 additions & 0 deletions scripts/rewards/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "rewards",
"version": "1.0.0",
"main": "index.js",
"author": "Caner Candan",
"license": "MIT",
"type": "module",
"scripts": {
"rewards-period": "node rewards-period.js"
},
"dependencies": {
"node-fetch": "^3.2.4"
}
}
94 changes: 80 additions & 14 deletions scripts/rewards/rewards-period.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,71 @@
#!/usr/bin/env node
import fs from "fs";
import fetch from "node-fetch";

const fs = require("fs");
function formatAllocationAmount(allocation) {
return `${allocation.trim().split(",").join("")}${"0".repeat(18)}`;
}
function doesPoolExist(base_denom, pool) {
return (
base_denom === `c${pool}` ||
base_denom === `u${pool}` ||
base_denom === `e${pool}` ||
base_denom === `a${pool}` ||
base_denom === `${pool}`
);
}

async function createAssetRewardsFileFromParams({
periodId,
startBlock,
endBlock,
allocation,
defaultMultiplier = "0.0",
pools = [],
}) {
const entries = (
await (
await fetch("https://api.sifchain.finance/tokenregistry/entries")
).json()
).result.registry.entries;

const multipliers = pools.map(({ pool, multiplier }) => {
const entry = entries.find(({ base_denom }) =>
doesPoolExist(base_denom, pool)
);

const asset = entry ? entry.denom : `???${pool}???`;

return {
pool_multiplier_asset: asset,
multiplier,
};
});

const rewardPeriod = {
reward_period_id: periodId,
reward_period_start_block: startBlock,
reward_period_end_block: endBlock,
reward_period_allocation: formatAllocationAmount(allocation),
reward_period_pool_multipliers: multipliers,
reward_period_default_multiplier: defaultMultiplier,
};

return rewardPeriod;
}

async function createAssetRewardsFile(periodId, startBlock, endBlock) {
const csv = fs.readFileSync(`./${periodId}.csv`, "utf-8");
const entries = JSON.parse(fs.readFileSync("./entries.json", "utf-8")).result
.registry.entries;
const lines = csv.split("\r\n").filter((line) => line.split(",")[1] !== "");
const lines = csv.split("\r\n").filter((line) => line.split(",")[0] !== "");

let [, allocation] = lines[1].split('"');
allocation = `${allocation.trim().split(",").join("")}${"0".repeat(18)}`;
let [, allocation] = lines[0].split('"');

const multipliers = lines.slice(1).map((line) => {
const [, poolName, multiplier] = line.split(",");

const entry = entries.find(
({ base_denom }) =>
base_denom === `c${poolName}` ||
base_denom === `u${poolName}` ||
base_denom === `e${poolName}` ||
base_denom === `${poolName}`
const entry = entries.find(({ base_denom }) =>
doesPoolExist(base_denom, poolName)
);

const asset = entry ? entry.denom : `???${poolName}???`;
Expand All @@ -34,7 +80,7 @@ async function createAssetRewardsFile(periodId, startBlock, endBlock) {
reward_period_id: periodId,
reward_period_start_block: startBlock,
reward_period_end_block: endBlock,
reward_period_allocation: allocation,
reward_period_allocation: formatAllocationAmount(allocation),
reward_period_pool_multipliers: multipliers,
reward_period_default_multiplier: "0.0",
};
Expand All @@ -44,8 +90,28 @@ async function createAssetRewardsFile(periodId, startBlock, endBlock) {

async function start() {
const rewardPeriods = [
await createAssetRewardsFile("RP_2", 6586931, 6687730),
await createAssetRewardsFile("RP_1", 6486131, 6586930),
await createAssetRewardsFileFromParams({
periodId: "RP_6",
startBlock: 6982961,
endBlock: 7081748,
allocation: "35040462",
defaultMultiplier: "0.602",
pools: [
{ pool: "atom", multiplier: "1.075" },
{ pool: "eth", multiplier: "1.075" },
{ pool: "usdc", multiplier: "1.075" },
{ pool: "juno", multiplier: "1.075" },
{ pool: "uscrt", multiplier: "1.075" },
{ pool: "luna", multiplier: "0.0" },
{ pool: "ust", multiplier: "0.0" },
{ pool: "usd", multiplier: "0.0" },
],
}),
await createAssetRewardsFile("RP_5", 6885850, 6982960),
// await createAssetRewardsFile("RP_4", 6788531, 6885841),
// await createAssetRewardsFile("RP_3", 6687731, 6788530),
// await createAssetRewardsFile("RP_2", 6586931, 6687730),
// await createAssetRewardsFile("RP_1", 6486131, 6586930),
];

fs.writeFileSync("./rewards.json", JSON.stringify(rewardPeriods, null, 2));
Expand Down
42 changes: 42 additions & 0 deletions scripts/rewards/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


data-uri-to-buffer@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/data-uri-to-buffer/-/data-uri-to-buffer-4.0.0.tgz#b5db46aea50f6176428ac05b73be39a57701a64b"
integrity sha512-Vr3mLBA8qWmcuschSLAOogKgQ/Jwxulv3RNE4FXnYWRGujzrRWQI4m12fQqRkwX06C0KanhLr4hK+GydchZsaA==

fetch-blob@^3.1.2, fetch-blob@^3.1.4:
version "3.1.5"
resolved "https://registry.yarnpkg.com/fetch-blob/-/fetch-blob-3.1.5.tgz#0077bf5f3fcdbd9d75a0b5362f77dbb743489863"
integrity sha512-N64ZpKqoLejlrwkIAnb9iLSA3Vx/kjgzpcDhygcqJ2KKjky8nCgUQ+dzXtbrLaWZGZNmNfQTsiQ0weZ1svglHg==
dependencies:
node-domexception "^1.0.0"
web-streams-polyfill "^3.0.3"

formdata-polyfill@^4.0.10:
version "4.0.10"
resolved "https://registry.yarnpkg.com/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz#24807c31c9d402e002ab3d8c720144ceb8848423"
integrity sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==
dependencies:
fetch-blob "^3.1.2"

node-domexception@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/node-domexception/-/node-domexception-1.0.0.tgz#6888db46a1f71c0b76b3f7555016b63fe64766e5"
integrity sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==

node-fetch@^3.2.4:
version "3.2.4"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-3.2.4.tgz#3fbca2d8838111048232de54cb532bd3cf134947"
integrity sha512-WvYJRN7mMyOLurFR2YpysQGuwYrJN+qrrpHjJDuKMcSPdfFccRUla/kng2mz6HWSBxJcqPbvatS6Gb4RhOzCJw==
dependencies:
data-uri-to-buffer "^4.0.0"
fetch-blob "^3.1.4"
formdata-polyfill "^4.0.10"

web-streams-polyfill@^3.0.3:
version "3.2.1"
resolved "https://registry.yarnpkg.com/web-streams-polyfill/-/web-streams-polyfill-3.2.1.tgz#71c2718c52b45fd49dbeee88634b3a60ceab42a6"
integrity sha512-e0MO3wdXWKrLbL0DgGnUV7WHVuw9OUvL4hjgnPkIeEvESk74gAITi5G606JtZPp39cd8HA9VQzCIvA49LpPN5Q==

0 comments on commit 20e79e1

Please sign in to comment.