diff --git a/funding-splits/distribution/FUNDING_SPLITS_HIN.csv b/funding-splits/distribution/FUNDING_SPLITS_HIN.csv index 393545f..e2afa6b 100644 --- a/funding-splits/distribution/FUNDING_SPLITS_HIN.csv +++ b/funding-splits/distribution/FUNDING_SPLITS_HIN.csv @@ -1,7 +1,8 @@ ethereum_address,sponsorship_count,distribution_count,impact_percentage -0xA7D1CB88740642DC95774511Cc73f015396Be869,4,0,44.44444444444444 -0x883753Beab357A2c29f3766C6ad158e72A78ce51,1,0,11.11111111111111 -0xFf7e232A44C74b306623f0d20dd6995775773842,1,0,11.11111111111111 -0xEbcF2690AF9e7D17e7dfB7118f65321Fc68d4b97,1,0,11.11111111111111 -0x29d6744eF3C98a00CAE8071D6DC292c53d6c5C75,1,0,11.11111111111111 -0x015B5dF1673499E32D11Cf786A43D1c42b3d725C,1,0,11.11111111111111 +0xA7D1CB88740642DC95774511Cc73f015396Be869,4,2,42.857142857142854 +0x883753Beab357A2c29f3766C6ad158e72A78ce51,1,0,7.142857142857143 +0xFf7e232A44C74b306623f0d20dd6995775773842,1,0,7.142857142857143 +0xEbcF2690AF9e7D17e7dfB7118f65321Fc68d4b97,1,1,14.285714285714286 +0x29d6744eF3C98a00CAE8071D6DC292c53d6c5C75,1,1,14.285714285714286 +0x015B5dF1673499E32D11Cf786A43D1c42b3d725C,1,0,7.142857142857143 +0xAD5f3A0433B25fC3933830D2FA008f6780386D97,0,1,7.142857142857143 diff --git a/funding-splits/distribution/FUNDING_SPLITS_TGL.csv b/funding-splits/distribution/FUNDING_SPLITS_TGL.csv index 08354d2..e2afa6b 100644 --- a/funding-splits/distribution/FUNDING_SPLITS_TGL.csv +++ b/funding-splits/distribution/FUNDING_SPLITS_TGL.csv @@ -1 +1,8 @@ ethereum_address,sponsorship_count,distribution_count,impact_percentage +0xA7D1CB88740642DC95774511Cc73f015396Be869,4,2,42.857142857142854 +0x883753Beab357A2c29f3766C6ad158e72A78ce51,1,0,7.142857142857143 +0xFf7e232A44C74b306623f0d20dd6995775773842,1,0,7.142857142857143 +0xEbcF2690AF9e7D17e7dfB7118f65321Fc68d4b97,1,1,14.285714285714286 +0x29d6744eF3C98a00CAE8071D6DC292c53d6c5C75,1,1,14.285714285714286 +0x015B5dF1673499E32D11Cf786A43D1c42b3d725C,1,0,7.142857142857143 +0xAD5f3A0433B25fC3933830D2FA008f6780386D97,0,1,7.142857142857143 diff --git a/funding-splits/distribution/abis/DistributionQueue.json b/funding-splits/distribution/abis/DistributionQueue.json new file mode 100644 index 0000000..8716d51 --- /dev/null +++ b/funding-splits/distribution/abis/DistributionQueue.json @@ -0,0 +1,230 @@ +{ + "abi": [ + { + "inputs": [ + { + "internalType": "address", + "name": "_attestationHandler", + "type": "address" + } + ], + "stateMutability": "nonpayable", + "type": "constructor" + }, + { + "inputs": [], + "name": "OnlyAttestationHandler", + "type": "error" + }, + { + "inputs": [], + "name": "OnlyOwner", + "type": "error" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "address", + "name": "attestationHandler", + "type": "address" + } + ], + "name": "AttestationHandlerUpdated", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "components": [ + { + "internalType": "uint256", + "name": "timestamp", + "type": "uint256" + }, + { + "internalType": "address", + "name": "distributor", + "type": "address" + }, + { + "internalType": "enum DistributionStatus", + "name": "status", + "type": "uint8" + } + ], + "indexed": false, + "internalType": "struct Distribution", + "name": "distribution", + "type": "tuple" + } + ], + "name": "DistributionAdded", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "components": [ + { + "internalType": "uint256", + "name": "timestamp", + "type": "uint256" + }, + { + "internalType": "address", + "name": "distributor", + "type": "address" + }, + { + "internalType": "enum DistributionStatus", + "name": "status", + "type": "uint8" + } + ], + "indexed": false, + "internalType": "struct Distribution", + "name": "distribution", + "type": "tuple" + } + ], + "name": "DistributionStatusUpdated", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "address", + "name": "owner", + "type": "address" + } + ], + "name": "OwnerUpdated", + "type": "event" + }, + { + "inputs": [], + "name": "addDistribution", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "name": "attestationHandler", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "getQueueCount", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "owner", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "name": "queue", + "outputs": [ + { + "internalType": "uint256", + "name": "timestamp", + "type": "uint256" + }, + { + "internalType": "address", + "name": "distributor", + "type": "address" + }, + { + "internalType": "enum DistributionStatus", + "name": "status", + "type": "uint8" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "_attestationHandler", + "type": "address" + } + ], + "name": "updateAttestationHandler", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "queueIndex", + "type": "uint256" + }, + { + "internalType": "enum DistributionStatus", + "name": "_distributionStatus", + "type": "uint8" + } + ], + "name": "updateDistributionStatus", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "_owner", + "type": "address" + } + ], + "name": "updateOwner", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + } + ] +} diff --git a/funding-splits/distribution/query-events.js b/funding-splits/distribution/query-events.js index 16cf666..5bf1b70 100644 --- a/funding-splits/distribution/query-events.js +++ b/funding-splits/distribution/query-events.js @@ -14,21 +14,27 @@ var __importDefault = (this && this.__importDefault) || function (mod) { Object.defineProperty(exports, "__esModule", { value: true }); const ethers_1 = require("ethers"); const SponsorshipQueue_json_1 = __importDefault(require("./abis/SponsorshipQueue.json")); +const DistributionQueue_json_1 = __importDefault(require("./abis/DistributionQueue.json")); const csv_writer_1 = require("csv-writer"); const rpcServerAddress = 'https://base-sepolia.blockpi.network/v1/rpc/public'; console.log('rpcServerAddress:', rpcServerAddress); const provider = new ethers_1.ethers.JsonRpcProvider(rpcServerAddress); -// console.log('provider:', provider) +/** + * Deployment details: https://github.com/elimu-ai/web3-sponsors/tree/main/backend/ignition/deployments + */ const sponsorshipQueueContract = new ethers_1.ethers.Contract('0x9Af2E73663968fdfb9791b7D6Bd40cd259f0388a', SponsorshipQueue_json_1.default.abi, provider); -// console.log('sponsorshipQueueContract:', sponsorshipQueueContract) +/** + * Deployment details: https://github.com/elimu-ai/web3-sponsors/tree/main/backend/ignition/deployments + */ +const distributionQueueContract = new ethers_1.ethers.Contract('0xB84dcB33eAEAaC81723d91A50674b27f3c5380eD', DistributionQueue_json_1.default.abi, provider); query(); function query() { return __awaiter(this, void 0, void 0, function* () { console.log('query'); - const events = yield sponsorshipQueueContract.queryFilter('*'); - console.log('events.length:', events.length); - const eventData = []; - events.forEach((eventLog) => { + const sponsorshipQueueEvents = yield sponsorshipQueueContract.queryFilter('*'); + console.log('sponsorshipQueueEvents.length:', sponsorshipQueueEvents.length); + const sponsorshipAddedEventData = []; + sponsorshipQueueEvents.forEach((eventLog) => { console.log(''); // console.log('eventLog:', eventLog) const blockNumber = eventLog.blockNumber; @@ -56,32 +62,69 @@ function query() { console.log('timestamp:', timestamp); const sponsorAddress = argResultArray[2]; console.log('sponsorAddress:', sponsorAddress); - eventData.push([estimatedCost, timestamp, sponsorAddress]); + sponsorshipAddedEventData.push([estimatedCost, timestamp, sponsorAddress]); } }); - prepareCsvData(eventData); + const distributionQueueEvents = yield distributionQueueContract.queryFilter('*'); + console.log('distributionQueueEvents.length:', distributionQueueEvents.length); + const distributionAddedEventData = []; + distributionQueueEvents.forEach((eventLog) => { + console.log(''); + // console.log('eventLog:', eventLog) + const blockNumber = eventLog.blockNumber; + console.log('blockNumber:', eventLog.blockNumber); + const eventName = eventLog.fragment.name; + console.log('eventName:', eventName); + const argResult = eventLog.args; + // console.log('argResult:', argResult) + // + // Sample: + // + // Result(1) [ + // Result(3) [ + // 1719830522n, + // '0xAD5f3A0433B25fC3933830D2FA008f6780386D97', + // 0n + // ] + // ] + if (eventName == 'DistributionAdded') { + const argResultArray = argResult[0]; + console.log('argResultArray:', argResultArray); + const timestamp = argResultArray[0]; + console.log('timestamp:', timestamp); + const distributorAddress = argResultArray[1]; + console.log('distributorAddress:', distributorAddress); + const distributionStatus = argResultArray[2]; + console.log('distributionStatus:', distributionStatus); + distributionAddedEventData.push([timestamp, distributorAddress, distributionStatus]); + } + }); + prepareCsvData(sponsorshipAddedEventData, distributionAddedEventData); }); } -function prepareCsvData(eventData) { +/** + * Prepare an array of JSON objects before exporting to CSV. + * + * Sample: + * + * { + * ethereum_address: '0x015B5dF1673499E32D11Cf786A43D1c42b3d725C' + * sponsorship_count: 1 + * distribution_count: 0 + * impact_percentage: 100 + * } + */ +function prepareCsvData(sponsorshipAddedEvents, distributionAddedEvents) { return __awaiter(this, void 0, void 0, function* () { console.log('prepareCsvData'); - console.log('eventData.length:', eventData.length); - // Count events per sponsor address + console.log('sponsorshipAddedEvents.length:', sponsorshipAddedEvents.length); + console.log('distributionAddedEvents.length:', distributionAddedEvents.length); const csvData = []; - // Sample: - // - // [ - // { - // ethereum_address: '0x015B5dF1673499E32D11Cf786A43D1c42b3d725C' - // sponsorship_count: 1 - // distribution_count: 0 - // impact_percentage: 100 - // } - // ] - eventData.forEach(sponsorshipEvent => { - const estimatedCost = sponsorshipEvent[0]; - const timestamp = sponsorshipEvent[1]; - const sponsorAddress = sponsorshipEvent[2]; + // Count 'SponsorshipAdded' events per sponsor address + sponsorshipAddedEvents.forEach(sponsorshipAddedEvent => { + const estimatedCost = sponsorshipAddedEvent[0]; + const timestamp = sponsorshipAddedEvent[1]; + const sponsorAddress = sponsorshipAddedEvent[2]; // Check if data already exists for the sponsor address let existingData = undefined; csvData.forEach(data => { @@ -104,9 +147,36 @@ function prepareCsvData(eventData) { existingData.sponsorship_count++; } }); + // Count 'DistributionAdded' events per distributor address + distributionAddedEvents.forEach(distributionAddedEvent => { + const distributorAddress = distributionAddedEvent[1]; + const distributionStatus = distributionAddedEvent[2]; + // Check if data already exists for the distributor address + let existingData = undefined; + csvData.forEach(data => { + // console.log('data:', data) + if (distributorAddress == data.ethereum_address) { + existingData = data; + } + }); + if (!existingData) { + console.log('Add data for address:', distributorAddress); + const newData = { + ethereum_address: distributorAddress, + sponsorship_count: 0, + distribution_count: 1 + }; + csvData.push(newData); + } + else { + console.log('Update data for address:', distributorAddress); + existingData.distribution_count++; + } + }); console.log('csvData:', csvData); calculateImpactPercentages(csvData); - exportToCsv(csvData); + exportToCsv(csvData, 'HIN'); + exportToCsv(csvData, 'TGL'); }); } function calculateImpactPercentages(csvData) { @@ -129,9 +199,9 @@ function calculateImpactPercentages(csvData) { }); console.log('csvData:', csvData); } -function exportToCsv(csvData) { +function exportToCsv(csvData, languageCode) { console.log('exportToCsv'); - const outputPath = 'FUNDING_SPLITS_HIN.csv'; + const outputPath = `FUNDING_SPLITS_${languageCode}.csv`; console.log('outputPath:', outputPath); const csvWriter = (0, csv_writer_1.createObjectCsvWriter)({ path: outputPath, diff --git a/funding-splits/distribution/query-events.ts b/funding-splits/distribution/query-events.ts index 7dac274..b781b85 100644 --- a/funding-splits/distribution/query-events.ts +++ b/funding-splits/distribution/query-events.ts @@ -1,31 +1,40 @@ -import { Contract, ethers, EventLog, JsonRpcProvider, Log, Result } from 'ethers' +import { Contract, ethers, JsonRpcProvider, Result } from 'ethers' import SponsorshipQueue from './abis/SponsorshipQueue.json' +import DistributionQueue from './abis/DistributionQueue.json' import { createObjectCsvWriter as createCsvWriter } from 'csv-writer' const rpcServerAddress: string = 'https://base-sepolia.blockpi.network/v1/rpc/public' console.log('rpcServerAddress:', rpcServerAddress) const provider: JsonRpcProvider = new ethers.JsonRpcProvider(rpcServerAddress) -// console.log('provider:', provider) +/** + * Deployment details: https://github.com/elimu-ai/web3-sponsors/tree/main/backend/ignition/deployments + */ const sponsorshipQueueContract: Contract = new ethers.Contract( '0x9Af2E73663968fdfb9791b7D6Bd40cd259f0388a', SponsorshipQueue.abi, provider ) -// console.log('sponsorshipQueueContract:', sponsorshipQueueContract) + +/** + * Deployment details: https://github.com/elimu-ai/web3-sponsors/tree/main/backend/ignition/deployments + */ +const distributionQueueContract: Contract = new ethers.Contract( + '0xB84dcB33eAEAaC81723d91A50674b27f3c5380eD', + DistributionQueue.abi, + provider +) query() async function query() { console.log('query') - const events = await sponsorshipQueueContract.queryFilter('*') - console.log('events.length:', events.length) - - const eventData: any[] = [] - - events.forEach((eventLog: any) => { + const sponsorshipQueueEvents = await sponsorshipQueueContract.queryFilter('*') + console.log('sponsorshipQueueEvents.length:', sponsorshipQueueEvents.length) + const sponsorshipAddedEventData: any[] = [] + sponsorshipQueueEvents.forEach((eventLog: any) => { console.log('') // console.log('eventLog:', eventLog) @@ -61,34 +70,81 @@ async function query() { const sponsorAddress: string = argResultArray[2] console.log('sponsorAddress:', sponsorAddress) - eventData.push([estimatedCost, timestamp, sponsorAddress]) + sponsorshipAddedEventData.push([estimatedCost, timestamp, sponsorAddress]) } }) - prepareCsvData(eventData) + const distributionQueueEvents = await distributionQueueContract.queryFilter('*') + console.log('distributionQueueEvents.length:', distributionQueueEvents.length) + const distributionAddedEventData: any[] = [] + distributionQueueEvents.forEach((eventLog: any) => { + console.log('') + // console.log('eventLog:', eventLog) + + const blockNumber: number = eventLog.blockNumber + console.log('blockNumber:', eventLog.blockNumber) + + const eventName: string = eventLog.fragment.name + console.log('eventName:', eventName) + + const argResult: Result = eventLog.args + // console.log('argResult:', argResult) + // + // Sample: + // + // Result(1) [ + // Result(3) [ + // 1719830522n, + // '0xAD5f3A0433B25fC3933830D2FA008f6780386D97', + // 0n + // ] + // ] + + if (eventName == 'DistributionAdded') { + const argResultArray: Result = argResult[0] + console.log('argResultArray:', argResultArray) + + const timestamp: number = argResultArray[0] + console.log('timestamp:', timestamp) + + const distributorAddress: string = argResultArray[1] + console.log('distributorAddress:', distributorAddress) + + const distributionStatus: number = argResultArray[2] + console.log('distributionStatus:', distributionStatus) + + distributionAddedEventData.push([timestamp, distributorAddress, distributionStatus]) + } + }) + + prepareCsvData(sponsorshipAddedEventData, distributionAddedEventData) } -async function prepareCsvData(eventData: any[]) { +/** + * Prepare an array of JSON objects before exporting to CSV. + * + * Sample: + * + * { + * ethereum_address: '0x015B5dF1673499E32D11Cf786A43D1c42b3d725C' + * sponsorship_count: 1 + * distribution_count: 0 + * impact_percentage: 100 + * } + */ +async function prepareCsvData(sponsorshipAddedEvents: any[], distributionAddedEvents: any[]) { console.log('prepareCsvData') - console.log('eventData.length:', eventData.length) + console.log('sponsorshipAddedEvents.length:', sponsorshipAddedEvents.length) + console.log('distributionAddedEvents.length:', distributionAddedEvents.length) - // Count events per sponsor address const csvData: any[] = [] - // Sample: - // - // [ - // { - // ethereum_address: '0x015B5dF1673499E32D11Cf786A43D1c42b3d725C' - // sponsorship_count: 1 - // distribution_count: 0 - // impact_percentage: 100 - // } - // ] - eventData.forEach(sponsorshipEvent => { - const estimatedCost: number = sponsorshipEvent[0] - const timestamp: number = sponsorshipEvent[1] - const sponsorAddress: string = sponsorshipEvent[2] + + // Count 'SponsorshipAdded' events per sponsor address + sponsorshipAddedEvents.forEach(sponsorshipAddedEvent => { + const estimatedCost: number = sponsorshipAddedEvent[0] + const timestamp: number = sponsorshipAddedEvent[1] + const sponsorAddress: string = sponsorshipAddedEvent[2] // Check if data already exists for the sponsor address let existingData: any = undefined @@ -114,11 +170,43 @@ async function prepareCsvData(eventData: any[]) { existingData.sponsorship_count++ } }) + + // Count 'DistributionAdded' events per distributor address + distributionAddedEvents.forEach(distributionAddedEvent => { + const distributorAddress: string = distributionAddedEvent[1] + const distributionStatus: number = distributionAddedEvent[2] + + // Check if data already exists for the distributor address + let existingData: any = undefined + csvData.forEach(data => { + // console.log('data:', data) + if (distributorAddress == data.ethereum_address) { + existingData = data + } + }) + + if (!existingData) { + console.log('Add data for address:', distributorAddress) + + const newData = { + ethereum_address: distributorAddress, + sponsorship_count: 0, + distribution_count: 1 + } + csvData.push(newData) + } else { + console.log('Update data for address:', distributorAddress) + + existingData.distribution_count++ + } + }) + console.log('csvData:', csvData) calculateImpactPercentages(csvData) - exportToCsv(csvData) + exportToCsv(csvData, 'HIN') + exportToCsv(csvData, 'TGL') } function calculateImpactPercentages(csvData: any[]) { @@ -144,10 +232,10 @@ function calculateImpactPercentages(csvData: any[]) { console.log('csvData:', csvData) } -function exportToCsv(csvData: any[]) { +function exportToCsv(csvData: any[], languageCode: string) { console.log('exportToCsv') - const outputPath: string = 'FUNDING_SPLITS_HIN.csv' + const outputPath: string = `FUNDING_SPLITS_${languageCode}.csv` console.log('outputPath:', outputPath) const csvWriter = createCsvWriter({ path: outputPath,