diff --git a/__tests__/Batch/batch.test.js b/__tests__/Batch/batch.test.js index 28ca94b0b..53faeb11b 100644 --- a/__tests__/Batch/batch.test.js +++ b/__tests__/Batch/batch.test.js @@ -1,11 +1,13 @@ -import { convertPayoutScheduleToBigInt } from '../../lib/batchUtils' +import { convertPayoutScheduleToBigInt } from '../../lib/batchUtils'; describe('Batch Mint', () => { it('should pass', async () => { - const payoutScheduleString = '[375.0, 375.0, 375.0, 375.0, 750.0, 750.0, 750.0, 750.0, 833.33, 833.33, 833.33]' - - const convertedToBigInt = convertPayoutScheduleToBigInt(payoutScheduleString, 6) + const payoutScheduleString = '[375.0, 375.0, 375.0, 375.0, 750.0, 750.0, 750.0, 750.0, 833.33, 833.33, 833.33]'; - expect(convertedToBigInt.toString()).toEqual('375000000,375000000,375000000,375000000,750000000,750000000,750000000,750000000,833330000,833330000,833330000'); + const convertedToBigInt = convertPayoutScheduleToBigInt(payoutScheduleString, 6); + + expect(convertedToBigInt.toString()).toEqual( + '375000000,375000000,375000000,375000000,750000000,750000000,750000000,750000000,833330000,833330000,833330000' + ); }); }); diff --git a/lib/batchUtils.js b/lib/batchUtils.js index 50a840f62..46fe54726 100644 --- a/lib/batchUtils.js +++ b/lib/batchUtils.js @@ -1,27 +1,40 @@ import { ethers } from 'ethers'; +const convertCsvToJson = (csvData) => { + const headers = csvData[0]; + const rows = csvData.slice(1); + const jsonData = rows.map((row) => { + const jsonObject = {}; + row.forEach((cell, index) => { + jsonObject[headers[index]] = cell; + }); + return jsonObject; + }); + return jsonData; +}; + const convertPayoutScheduleToBigInt = (payoutSchedule, decimals) => { - const payoutScheduleParsed = payoutSchedule && JSON.parse(payoutSchedule); + const payoutScheduleParsed = payoutSchedule && JSON.parse(payoutSchedule); - const newPayoutSchedule = payoutScheduleParsed.map((tierVolume) => { - let formattedVolume = tierVolume * 10 ** decimals; - return ethers.BigNumber.from(formattedVolume.toLocaleString('fullwide', { useGrouping: false })); - }); + const newPayoutSchedule = payoutScheduleParsed.map((tierVolume) => { + let formattedVolume = tierVolume * 10 ** decimals; + return ethers.BigNumber.from(formattedVolume.toLocaleString('fullwide', { useGrouping: false })); + }); - return newPayoutSchedule; -} + return newPayoutSchedule; +}; -const convertCsvToJson = (csvData) => { - const headers = csvData[0]; - const rows = csvData.slice(1); - const jsonData = rows.map((row) => { - const jsonObject = {}; - row.forEach((cell, index) => { - jsonObject[headers[index]] = cell; - }); - return jsonObject; - }); - return jsonData; +const abiEncodeTieredFixed = (initializationData) => { + let abiCoder = new ethers.utils.AbiCoder(); + const initializationSchema = ['uint256[]', 'address', 'bool', 'bool', 'bool', 'string', 'string', 'string']; + + console.log(initializationData); + + const tieredFixedEncoded = abiCoder.encode(initializationSchema, initializationData); + + let tieredFixed = [3, `\"${tieredFixedEncoded}\"`]; + + return tieredFixed; }; -export { convertPayoutScheduleToBigInt, convertCsvToJson } \ No newline at end of file +export { convertPayoutScheduleToBigInt, convertCsvToJson, abiEncodeTieredFixed }; diff --git a/pages/batch.js b/pages/batch.js index 413fe9e09..cef735636 100644 --- a/pages/batch.js +++ b/pages/batch.js @@ -1,6 +1,6 @@ import React, { useState, useContext, useEffect } from 'react'; -import Papa from 'papaparse'; import { ethers } from 'ethers'; +import Papa from 'papaparse'; import StoreContext from '../store/Store/StoreContext'; import _ from 'lodash'; import mintBountyTemplate from '../constants/mintBountyTemplate.json'; @@ -9,7 +9,7 @@ import md4 from 'js-md4'; import Link from 'next/link'; import Image from 'next/image'; import BountyCardLean from '../components/BountyCard/BountyCardLean'; -import { convertCsvToJson, convertPayoutScheduleToBigInt } from '../lib/batchUtils' +import { convertCsvToJson, convertPayoutScheduleToBigInt, abiEncodeTieredFixed } from '../lib/batchUtils'; function Batch() { const [mintBountyBatchData, setMintBountyBatchData] = useState(null); @@ -18,6 +18,9 @@ function Batch() { const { accountData } = appState; const [file, setFile] = useState(null); + let abiCoder = new ethers.utils.AbiCoder(); + const initializationSchema = ['uint256[]', 'address', 'bool', 'bool', 'bool', 'string', 'string', 'string']; + const handleCopyToClipboard = () => { navigator.clipboard.writeText(JSON.stringify(mintBountyBatchData)); }; @@ -37,21 +40,23 @@ function Batch() { document.body.removeChild(element); }; - let abiCoder = new ethers.utils.AbiCoder(); - const initializationSchema = ['uint256[]', 'address', 'bool', 'bool', 'bool', 'string', 'string', 'string']; - - const loadGithubData = async (githubIssueUrl) => { - const resource = await appState.githubRepository.fetchIssueByUrl(githubIssueUrl); - const githubSponsorResource = await appState.githubRepository.getOrgByUrl(githubSponsorUrl); + const loadGithubData = async (githubIssueUrl, githubSponsorUrl) => { + const resource = await appState.githubRepository.fetchIssueByUrl(githubIssueUrl); + const githubSponsorResource = await appState.githubRepository.getOrgByUrl(githubSponsorUrl); - const bountyId = resource.id; - const organizationId = resource.repository.owner.id; + const bountyId = resource.id; + const organizationId = resource.repository.owner.id; - const sponsorOrganizationName = githubSponsorResource.login; - const sponsorOrganizationLogo = githubSponsorResource.avatarUrl; + const sponsorOrganizationName = githubSponsorResource.login; + const sponsorOrganizationLogo = githubSponsorResource.avatarUrl; - return { bountyId, organizationId, sponsorOrganizationName, sponsorOrganizationLogo}; - } + return { + bountyId, + organizationId, + sponsorOrganizationName, + sponsorOrganizationLogo, + }; + }; const handleFileUpload = (event) => { const file = event.target.files[0]; @@ -67,8 +72,6 @@ function Batch() { // Populate the transaction template const transactions = []; - console.log('jsonData', jsonData); - for (const transactionData of jsonData) { const { githubIssueUrl, @@ -81,19 +84,16 @@ function Batch() { } = transactionData; try { - const { decimals } = await appState.tokenClient.getToken(payoutTokenAddress); - - const newPayoutSchedule = convertPayoutScheduleToBigInt(payoutSchedule, decimals); + const { decimals } = await appState.tokenClient.getToken(payoutTokenAddress); + console.log('decimals', decimals) - // Fetch Github Issue ID and Organization ID - const { bountyId, organizationId, sponsorOrganizationName, sponsorOrganizationLogo } = await loadGithubData(githubIssueUrl) + const newPayoutSchedule = convertPayoutScheduleToBigInt(payoutSchedule, decimals); - // Overwrite contractInputsValues on mintBountyTransactionTemplate - const mintBountyTransactionTemplateCopy = _.cloneDeep(mintBountyTransactionTemplate); - - mintBountyTransactionTemplateCopy.contractInputsValues._bountyId = bountyId; - mintBountyTransactionTemplateCopy.contractInputsValues._organization = organizationId; - mintBountyTransactionTemplateCopy.to = process.env.NEXT_PUBLIC_OPENQ_PROXY_ADDRESS; + // Fetch Github Issue ID and Organization ID + const { bountyId, organizationId, sponsorOrganizationName, sponsorOrganizationLogo } = await loadGithubData( + githubIssueUrl, + githubSponsorUrl + ); const initializationData = [ newPayoutSchedule, @@ -106,9 +106,14 @@ function Batch() { sponsorOrganizationLogo, ]; - const tieredFixedEncoded = abiCoder.encode(initializationSchema, initializationData); - let tieredFixed = [3, `\"${tieredFixedEncoded}\"`]; + let tieredFixed = abiEncodeTieredFixed(initializationData); + // Overwrite contractInputsValues on mintBountyTransactionTemplate + const mintBountyTransactionTemplateCopy = _.cloneDeep(mintBountyTransactionTemplate); + + mintBountyTransactionTemplateCopy.contractInputsValues._bountyId = bountyId; + mintBountyTransactionTemplateCopy.contractInputsValues._organization = organizationId; + mintBountyTransactionTemplateCopy.to = process.env.NEXT_PUBLIC_OPENQ_PROXY_ADDRESS; mintBountyTransactionTemplateCopy.contractInputsValues._initOperation = `[${tieredFixed}]`; transactions.push(mintBountyTransactionTemplateCopy); @@ -119,6 +124,7 @@ function Batch() { const mintBountyTemplateCopy = _.cloneDeep(mintBountyTemplate); mintBountyTemplateCopy.transactions = transactions; + console.log(transactions); setMintBountyBatchData(mintBountyTemplateCopy); }; @@ -134,6 +140,7 @@ function Batch() { const [payoutSchedule, payoutTokenAddress, , , , , sponsorOrganizationName, sponsorOrganizationLogo] = abiCoder.decode(initializationSchema, initializationOp); + return { ...githubData, payoutSchedule,