-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add bounty update script to auto close the ended bounties
- Loading branch information
1 parent
c7eaa1e
commit bbdf706
Showing
4 changed files
with
85 additions
and
11 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
backend/packages/server/src/scripts/update-bounty-status/fetchBountyState.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
48
backend/packages/server/src/scripts/update-bounty-status/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters