Skip to content

Commit

Permalink
split tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
amiecorso committed Mar 18, 2024
1 parent e0c0c7c commit 5738efe
Show file tree
Hide file tree
Showing 6 changed files with 696 additions and 160 deletions.
1 change: 1 addition & 0 deletions bayer_batches_for_retirement.csv

Large diffs are not rendered by default.

148 changes: 148 additions & 0 deletions tasks/aggregate-removals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/* eslint-disable no-await-in-loop */

Check warning on line 1 in tasks/aggregate-removals.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected undescribed directive comment. Include descriptions to explain why the comment is necessary
/* eslint-disable no-restricted-syntax */

Check warning on line 2 in tasks/aggregate-removals.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected undescribed directive comment. Include descriptions to explain why the comment is necessary
import { BigNumber } from 'ethers';
import { task, types } from 'hardhat/config';
import { Utils } from 'alchemy-sdk';
import { readJsonSync, writeJsonSync } from 'fs-extra';

import { getMarket, getRemoval } from '@/utils/contracts';

export const GET_AGGREGATE_REMOVALS_TASK = () =>
({
name: 'aggregate-removals',
description: 'Utility to aggregate removals to a consignor',
run: async (
options: {
aggregateremovals: boolean;
batchfilename: string;
},
_: CustomHardHatRuntimeEnvironment
): Promise<void> => {
const {
aggregateremovals,
batchfilename = 'bayer_batches_for_retirement.csv',
} = options;
const BLOCK_CONFIRMATIONS = 5;
const SUPPLIER_WALLET_ADDRESS =
'0xdca851dE155B20CC534b887bD2a1D780D0DEc077'; // Bayer

const network = hre.network.name;
if (![`localhost`, `mumbai`, `polygon`].includes(network)) {
throw new Error(
`Network ${network} is not supported. Please use localhost, mumbai, or polygon.`
);
}
const [signer] = await hre.getSigners();

const marketContract = await getMarket({

Check warning on line 37 in tasks/aggregate-removals.ts

View workflow job for this annotation

GitHub Actions / lint

Unsafe assignment of an `any` value
hre,
signer,
});
const removalContract = await getRemoval({

Check warning on line 41 in tasks/aggregate-removals.ts

View workflow job for this annotation

GitHub Actions / lint

Unsafe assignment of an `any` value
hre,
signer,
});
console.log('MARKET CONTRACT ADDRESS', marketContract.address);

Check warning on line 45 in tasks/aggregate-removals.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement

Check warning on line 45 in tasks/aggregate-removals.ts

View workflow job for this annotation

GitHub Actions / lint

Unsafe member access .address on an `any` value

const signerAddress = await signer.getAddress();
console.log('SIGNER ADDRESS', signerAddress);

Check warning on line 48 in tasks/aggregate-removals.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement

// await checkRoles(signerAddress);

// Gas stuff ==========================
const latestFastGasPrice = await hre.ethers.provider.getGasPrice();
const fastGasPriceHexString = Utils.hexStripZeros(
latestFastGasPrice.toHexString()
);
// console.log('LATEST BLOCK GAS LIMIT: ', latestBlockGasLimit);
// console.log('LATEST FAST GAS PRICE: ', fastGasPriceHexString);

// Get token data =============================================
// const bayerMintedTokens = await removalContract.getOwnedTokenIds(
// SUPPLIER_WALLET_ADDRESS
// );
// console.log(`Bayer has ${bayerMintedTokens.length} minted tokens`);

const rawBatchData = readJsonSync(batchfilename);

Check warning on line 66 in tasks/aggregate-removals.ts

View workflow job for this annotation

GitHub Actions / lint

Unsafe assignment of an `any` value
console.log('RAW BATCH DATA', rawBatchData.slice(0, 5));

Check warning on line 67 in tasks/aggregate-removals.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement

Check warning on line 67 in tasks/aggregate-removals.ts

View workflow job for this annotation

GitHub Actions / lint

Unsafe member access .slice on an `any` value

const batches = rawBatchData.map((batch) => {

Check failure on line 69 in tasks/aggregate-removals.ts

View workflow job for this annotation

GitHub Actions / analyze

Parameter 'batch' implicitly has an 'any' type.
return {
removalIdsForRetirement: batch.removalIds.map((id) => {

Check failure on line 71 in tasks/aggregate-removals.ts

View workflow job for this annotation

GitHub Actions / analyze

Parameter 'id' implicitly has an 'any' type.
return BigNumber.from(id);
}),
balancesForRetirement: batch.balances.map((balance) => {

Check failure on line 74 in tasks/aggregate-removals.ts

View workflow job for this annotation

GitHub Actions / analyze

Parameter 'balance' implicitly has an 'any' type.
return ethers.utils.parseUnits(balance.toString(), 18);
}),
};
});
console.log('BATCH DATA', batches);

if (aggregateremovals) {
const timestamp = new Date().toISOString();
// Aggregate removals with consignor ========================
const aggregationOutputFilename = `aggregation_txn_receipts_${timestamp}.json`;
const aggregationTransactionOutput = [];
let pendingTx;
let maybePendingTx;
let txReceipt;
for (let i = 0; i < batches.length; i++) {
maybePendingTx =
await removalContract.callStatic.consignorBatchTransfer(
SUPPLIER_WALLET_ADDRESS,
signerAddress,
batches[i].removalIdsForRetirement,
batches[i].balancesForRetirement,
{
gasPrice: fastGasPriceHexString,
}
);
if (maybePendingTx === undefined) {
throw new Error(`No pending transaction returned`);
} else {
pendingTx = maybePendingTx;
}
if (pendingTx !== undefined) {
console.info(
`📝 Awaiting aggregation transaction ${i}/${batches.length}: ${pendingTx.hash}`

Check failure on line 107 in tasks/aggregate-removals.ts

View workflow job for this annotation

GitHub Actions / analyze

Property 'hash' does not exist on type 'never'.
);
const txResult = await pendingTx.wait(BLOCK_CONFIRMATIONS);
console.info('Getting txReceipt...');
txReceipt = await removalContract.provider.getTransactionReceipt(
txResult.transactionHash
);
aggregationTransactionOutput.push(txReceipt);
if (txReceipt.status !== 1) {
console.error(
`❌ Transaction ${pendingTx.hash} failed with failure status ${txReceipt.status} - exiting early`
);
writeJsonSync(
aggregationOutputFilename,
aggregationTransactionOutput
);
throw new Error(
'Transaction failed with unsuccessful status - exiting early'
);
}
}
}
console.log(
'Successfully aggregated all batches of removals to consignor! Writing transaction receipts to file...'
);
writeJsonSync(aggregationOutputFilename, aggregationTransactionOutput);
}
},
} as const);

(() => {
const { name, description, run } = GET_AGGREGATE_REMOVALS_TASK();
task(name, description, run)
.addFlag('aggregateremovals', 'Aggregate removals to consignor')
.addParam(
'batchfilename',
'Path to the file containing the batch data',
undefined,
types.string,
false
);
})();
Loading

0 comments on commit 5738efe

Please sign in to comment.