-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamountDonatedToProjects.js
More file actions
70 lines (60 loc) · 3.06 KB
/
amountDonatedToProjects.js
File metadata and controls
70 lines (60 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const { connect, keyStores, utils, Contract } = require('near-api-js');
async function checkDonations(accountId) {
// Configure the connection to the NEAR blockchain
const near = await connect({
networkId: "mainnet",
keyStore: new keyStores.InMemoryKeyStore(),
nodeUrl: "https://rpc.mainnet.near.org",
walletUrl: "https://app.mynearwallet.com/"
});
// Use the specified account for contract calls
const account = await near.account(accountId);
// Fetch the list of projects from registry.potlock.near
const projects = await account.viewFunction({
contractId: "registry.potlock.near",
methodName: "get_projects",
args: {}
});
const approvedProjects = projects.filter(project => project.status === "Approved").map(project => project.id);
// Print out each approved project
console.log("Approved Projects:");
approvedProjects.forEach((project, index) => {
console.log(`${index + 1}. Project ID: ${project}`);
});
// Fetch the donation history for the specified account from donate.potlock.near
const donations = await account.viewFunction({
contractId: "donate.potlock.near",
methodName: "get_donations_for_donor",
args: { donor_id: accountId }
});
// Print out each donation
console.log("Donations:");
donations.forEach((donation, index) => {
const donationAmount = parseFloat(utils.format.formatNearAmount(donation.total_amount));
const date = new Date(donation.donated_at_ms);
console.log(`${index + 1}. Donation Amount: ${donationAmount} NEAR, FT ID: ${donation.ft_id} Recipient ID: ${donation.recipient_id} at TIME ${date} `);
});
// Filter donations to approved projects and calculate total
let totalDonations = 0;
donations.forEach(donation => {
if (donation.ft_id === "near" && approvedProjects.includes(donation.recipient_id)) {
// const donationAmount = parseFloat(utils.format.formatNearAmount(donation.total_amount));
const donationAmount = parseFloat(utils.format.formatNearAmount(donation.total_amount));
// totalDonations += donationAmount;
totalDonations += donationAmount;
// Log the donation amount and recipient ID
console.log(`Filtered Donation Amount: ${donationAmount} NEAR, Recipient ID: ${donation.recipient_id}`);
}
});
// Final console log with account name and total donations
console.log(`Account ${accountId} has donated a total of ${totalDonations} NEAR to approved projects`);
// Check if total donations exceed 1 NEAR
return totalDonations > 1; // for other FTs need to change this number
}
// Example usage with "root.near" as the account ID
const accountId = "minorityprogrammers.near"; // Use root.near as the account ID
checkDonations(accountId).then(hasDonatedOverOneNear => {
console.log(`User account '${accountId}' donated over 1 NEAR to approved projects: ${hasDonatedOverOneNear}`);
}).catch(error => {
console.error("Error checking donations:", error);
});