Skip to content

Commit

Permalink
updates batch
Browse files Browse the repository at this point in the history
  • Loading branch information
FlacoJones committed Mar 20, 2023
1 parent bff7ed3 commit 0993e6c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 53 deletions.
12 changes: 7 additions & 5 deletions __tests__/Batch/batch.test.js
Original file line number Diff line number Diff line change
@@ -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'
);
});
});
51 changes: 32 additions & 19 deletions lib/batchUtils.js
Original file line number Diff line number Diff line change
@@ -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 }
export { convertPayoutScheduleToBigInt, convertCsvToJson, abiEncodeTieredFixed };
65 changes: 36 additions & 29 deletions pages/batch.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
Expand All @@ -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));
};
Expand All @@ -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];
Expand All @@ -67,8 +72,6 @@ function Batch() {
// Populate the transaction template
const transactions = [];

console.log('jsonData', jsonData);

for (const transactionData of jsonData) {
const {
githubIssueUrl,
Expand All @@ -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,
Expand All @@ -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);
Expand All @@ -119,6 +124,7 @@ function Batch() {

const mintBountyTemplateCopy = _.cloneDeep(mintBountyTemplate);
mintBountyTemplateCopy.transactions = transactions;
console.log(transactions);

setMintBountyBatchData(mintBountyTemplateCopy);
};
Expand All @@ -134,6 +140,7 @@ function Batch() {

const [payoutSchedule, payoutTokenAddress, , , , , sponsorOrganizationName, sponsorOrganizationLogo] =
abiCoder.decode(initializationSchema, initializationOp);

return {
...githubData,
payoutSchedule,
Expand Down

0 comments on commit 0993e6c

Please sign in to comment.