Skip to content

Commit

Permalink
add bounty update script to auto close the ended bounties
Browse files Browse the repository at this point in the history
  • Loading branch information
hyifeng authored and wliyongfeng committed Jul 24, 2023
1 parent c7eaa1e commit bbdf706
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
async function fetchBountyState(network, bountyIndex) {
const url = `https://${network}.subsquare.io/api/treasury/bounties/${bountyIndex}`;
console.log(`Fetch from: ${url}`);

const resp = await fetch(url);
if (!resp.ok) {
throw new Error(`Failed to fetch bounty status from subsquare: ${resp.status}`);
}

const data = await resp.json();

const bounty = data.onchainData;
const state = bounty?.state?.state;

return { state };
}

module.exports = {
fetchBountyState,
};
48 changes: 48 additions & 0 deletions backend/packages/server/src/scripts/update-bounty-status/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const dotenv = require("dotenv");
dotenv.config();

const { Bounty } = require("../../models");
const { BountyStatus } = require("../../utils/constants");
const { fetchBountyState } = require("./fetchBountyState");

async function updateBountyToClosed(bounty) {
console.log(`Update status to Closed`);
await Bounty.updateOne(
{ _id: bounty._id },
{
status: BountyStatus.Closed,
},
);
}

async function updateBountyStatus(bounty) {
console.log(`Update bounty: ${bounty.network}/${bounty.bountyIndex}`);

const { state } = await fetchBountyState(bounty.network, bounty.bountyIndex);

if (["Canceled", "Cancelled", "Claimed"].includes(state)) {
return await updateBountyToClosed(bounty);
}
}

async function main() {
console.log(`Start update at:`, new Date());

const bounties = await Bounty.find({
status: BountyStatus.Open,
});

console.log(`Updating ${bounties.length} bounties`);

for (const bounty of bounties) {
try {
await updateBountyStatus(bounty);
} catch (e) {
console.error(e.message);
}
}
}

main()
.catch(console.error)
.finally(() => process.exit());
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ async function fetchChildBountyState(network, parentBountyIndex, index) {
console.log(`Fetch from: ${url}`);

const resp = await fetch(url);
if (!resp.ok) {
throw new Error(`Failed to fetch child bounty status from subsquare: ${resp.status}`);
}

const data = await resp.json();

const childBounty = data.onchainData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async function updateChildBountyToAwarded(childBounty, beneficiary) {
{
beneficiary,
status: ChildBountyStatus.Awarded,
}
},
);
}

Expand All @@ -22,17 +22,19 @@ async function updateChildBountyToClosed(childBounty) {
{ _id: childBounty._id },
{
status: ChildBountyStatus.Closed,
}
},
);
}

async function updateChildBountyStatus(childBounty) {
console.log(`Update child bounty: ${childBounty.network}/${childBounty.parentBountyIndex}/${childBounty.index}`);
console.log(
`Update child bounty: ${childBounty.network}/${childBounty.parentBountyIndex}/${childBounty.index}`,
);

const { beneficiary, state } = await fetchChildBountyState(
childBounty.network,
childBounty.parentBountyIndex,
childBounty.index
childBounty.index,
);

if (["Awarded", "Claimed"].includes(state)) {
Expand All @@ -49,21 +51,21 @@ async function main() {

const childBounties = await ChildBounty.find({
status: {
$in: [
ChildBountyStatus.Open,
ChildBountyStatus.Assigned,
]
}
$in: [ChildBountyStatus.Open, ChildBountyStatus.Assigned],
},
});

console.log(`Updating ${childBounties.length} child bounties`);

for (const childBounty of childBounties) {
await updateChildBountyStatus(childBounty);
try {
await updateChildBountyStatus(childBounty);
} catch (e) {
console.error(e.message);
}
}
}

main()
.catch(console.error)
.finally(() => process.exit());

0 comments on commit bbdf706

Please sign in to comment.