From 7a02cadb0978d1b2aab64f725e6d9920fc721ff6 Mon Sep 17 00:00:00 2001 From: Tawnee Date: Thu, 27 Jan 2022 15:19:20 -0700 Subject: [PATCH] add addChallengeTypes task --- .../data/challengeTypes/challengeTypes.ts | 54 ++++++ packages/contracts/hardhat.config.ts | 4 +- .../contracts/scripts/testChallengeTypes.js | 183 +++++++++--------- packages/contracts/tasks/addChallengeTypes.ts | 146 ++++++++++++++ 4 files changed, 294 insertions(+), 93 deletions(-) create mode 100644 packages/contracts/data/challengeTypes/challengeTypes.ts create mode 100644 packages/contracts/tasks/addChallengeTypes.ts diff --git a/packages/contracts/data/challengeTypes/challengeTypes.ts b/packages/contracts/data/challengeTypes/challengeTypes.ts new file mode 100644 index 0000000..cf983e2 --- /dev/null +++ b/packages/contracts/data/challengeTypes/challengeTypes.ts @@ -0,0 +1,54 @@ +import { ChallengeTypeInput } from "../../tasks/addChallengeTypes"; + +export const challengeTypes: ChallengeTypeInput[] = [ + { + id: 0, + name: "The Void", + description: "The Void", + prtclePrice: 0, + maxQuantity: 0, + canPurchaseWithPrtcle: false, + canBeTransferred: false, + totalQuantity: 0, + }, + { + id: 1, + name: "BWW Blazin Wing Challenge", + description: "Eat 10 wings in 5 minutes; no water", + prtclePrice: 5, + maxQuantity: "1000", + canPurchaseWithPrtcle: false, + canBeTransferred: true, + totalQuantity: 0, + }, + { + id: 2, + name: "Do a Backflip", + description: "Do a Backflip Description", + prtclePrice: 5, + maxQuantity: "1000", + canPurchaseWithPrtcle: false, + canBeTransferred: true, + totalQuantity: 0, + }, + { + id: 3, + name: "Hold My Breathe for 5 minutes", + description: "Hold My Breathe for 5 minutes", + prtclePrice: 5, + maxQuantity: "1000", + canPurchaseWithPrtcle: false, + canBeTransferred: true, + totalQuantity: 0, + }, + { + id: 4, + name: "Dunk a Basketball", + description: "Dunk", + prtclePrice: 50, + maxQuantity: "250", + canPurchaseWithPrtcle: true, + canBeTransferred: true, + totalQuantity: 0, + }, +]; diff --git a/packages/contracts/hardhat.config.ts b/packages/contracts/hardhat.config.ts index fa12e75..bacf36c 100644 --- a/packages/contracts/hardhat.config.ts +++ b/packages/contracts/hardhat.config.ts @@ -12,7 +12,7 @@ require("./tasks/verifyFacet.ts"); require("./tasks/generateDiamondABI.ts"); // require("./tasks/generateDiamondABI_eth.ts"); require("./tasks/batchDeposit.ts"); -// require("./tasks/addItemTypes.ts"); +require("./tasks/addChallengeTypes.ts"); // You have to export an object to set up your config // This object can have the following optional entries: @@ -36,7 +36,7 @@ export default { }, // xdai: { // url: process.env.XDAI_URL, - + // // url: 'https://rpc-mainnet.maticvigil.com/', // accounts: [process.env.CHALLENGE_MANAGER], // // blockGasLimit: 20000000, diff --git a/packages/contracts/scripts/testChallengeTypes.js b/packages/contracts/scripts/testChallengeTypes.js index edb6804..52c7bca 100644 --- a/packages/contracts/scripts/testChallengeTypes.js +++ b/packages/contracts/scripts/testChallengeTypes.js @@ -1,98 +1,99 @@ -const { isThrowStatement } = require('typescript') +const { isThrowStatement } = require("typescript"); +const { ethers } = require("ethers"); const challengeTypes = [ - { - id: 0, - name: 'The Void', - description: 'The Void', - prtclePrice: 0, - maxQuantity: 0, - canPurchaseWithPrtcle: false, - canBeTransferred: false, - totalQuantity: 0, - }, - { - id: 1, - name: 'BWW Blazin Wing Challenge', - description: 'Eat 10 wings in 5 minutes; no water', - prtclePrice: 5, - maxQuantity: '1000', - canPurchaseWithPrtcle: false, - canBeTransferred: true, - totalQuantity: 0, - }, - { - id: 2, - name: 'Do a Backflip', - description: 'Do a Backflip Description', - prtclePrice: 5, - maxQuantity: '1000', - canPurchaseWithPrtcle: false, - canBeTransferred: true, - totalQuantity: 0, - }, - { - id: 3, - name: 'Hold My Breathe for 5 minutes', - description: 'Hold My Breathe for 5 minutes', - prtclePrice: 5, - maxQuantity: '1000', - canPurchaseWithPrtcle: false, - canBeTransferred: true, - totalQuantity: 0, - }, - { - id: 4, - name: 'Dunk a Basketball', - description: 'Dunk', - prtclePrice: 50, - maxQuantity: '250', - canPurchaseWithPrtcle: true, - canBeTransferred: true, - totalQuantity: 0 - } - ] - - function eightBitIntArrayToUint(array) { - if (array.length === 0) { - return ethers.BigNumber.from(0) - } - const uint = [] - for (const num of array) { - if (num > 127) { - throw (Error('Value beyond signed 8 int ')) - } - const value = ethers.BigNumber.from(num).toTwos(8) - uint.unshift(value.toHexString().slice(2)) - } - return ethers.BigNumber.from('0x' + uint.join('')) + { + id: 0, + name: "The Void", + description: "The Void", + prtclePrice: 0, + maxQuantity: 0, + canPurchaseWithPrtcle: false, + canBeTransferred: false, + totalQuantity: 0, + }, + { + id: 1, + name: "BWW Blazin Wing Challenge", + description: "Eat 10 wings in 5 minutes; no water", + prtclePrice: 5, + maxQuantity: "1000", + canPurchaseWithPrtcle: false, + canBeTransferred: true, + totalQuantity: 0, + }, + { + id: 2, + name: "Do a Backflip", + description: "Do a Backflip Description", + prtclePrice: 5, + maxQuantity: "1000", + canPurchaseWithPrtcle: false, + canBeTransferred: true, + totalQuantity: 0, + }, + { + id: 3, + name: "Hold My Breathe for 5 minutes", + description: "Hold My Breathe for 5 minutes", + prtclePrice: 5, + maxQuantity: "1000", + canPurchaseWithPrtcle: false, + canBeTransferred: true, + totalQuantity: 0, + }, + { + id: 4, + name: "Dunk a Basketball", + description: "Dunk", + prtclePrice: 50, + maxQuantity: "250", + canPurchaseWithPrtcle: true, + canBeTransferred: true, + totalQuantity: 0, + }, +]; + +function eightBitIntArrayToUint(array) { + if (array.length === 0) { + return ethers.BigNumber.from(0); } - - function boolsArrayToUint16(bools) { - const uint = [] - for (const b of bools) { - if (b) { - uint.push('1') - } else { - uint.push('0') - } + const uint = []; + for (const num of array) { + if (num > 127) { + throw Error("Value beyond signed 8 int "); } - // console.log(bools) - // console.log(uint.join('')) - // console.log(uint.join('').padStart(16, '0')) - // console.log('-------------') - return parseInt(uint.join('').padStart(16, '0'), 2) + const value = ethers.BigNumber.from(num).toTwos(8); + uint.unshift(value.toHexString().slice(2)); } - - - function getChallengeTypes() { - const result = [] - for (const challengeType of challengeTypes) { - challengeType.prtclePrice = ethers.utils.parseEther(challengeType.prtclePrice.toString()) - result.push(challengeType) - + return ethers.BigNumber.from("0x" + uint.join("")); +} + +function boolsArrayToUint16(bools) { + const uint = []; + for (const b of bools) { + if (b) { + uint.push("1"); + } else { + uint.push("0"); } - return result } - - exports.challengeTypes = getChallengeTypes() \ No newline at end of file + // console.log(bools) + // console.log(uint.join('')) + // console.log(uint.join('').padStart(16, '0')) + // console.log('-------------') + return parseInt(uint.join("").padStart(16, "0"), 2); +} + +function getChallengeTypes() { + const result = []; + for (const challengeType of challengeTypes) { + challengeType.prtclePrice = ethers.utils.parseEther( + challengeType.prtclePrice.toString() + ); + result.push(challengeType); + } + return result; +} + +exports.challengeTypes = getChallengeTypes(); diff --git a/packages/contracts/tasks/addChallengeTypes.ts b/packages/contracts/tasks/addChallengeTypes.ts new file mode 100644 index 0000000..05e9d0e --- /dev/null +++ b/packages/contracts/tasks/addChallengeTypes.ts @@ -0,0 +1,146 @@ +import { LedgerSigner } from "@ethersproject/hardware-wallets"; + +import { task } from "hardhat/config"; +import { + Contract, + ContractReceipt, + ContractTransaction, +} from "@ethersproject/contracts"; +import { Signer } from "@ethersproject/abstract-signer"; + +import { DAOFacet } from "../typechain/DAOFacet"; +import { BigNumberish } from "@ethersproject/bignumber"; +import { gasPrice } from "../scripts/helperFunctions"; +import { HardhatRuntimeEnvironment } from "hardhat/types"; + +export interface ChallengeTypeOutput { + name: string; + description: string; + id: BigNumberish; + canBeTransferred: boolean; + totalQuantity: BigNumberish; + maxQuantity: BigNumberish; + prtclePrice: BigNumberish | BigNumberish; + canPurchaseWithPrtcle: boolean; +} + +export interface ChallengeTypeInput { + name: string; + description: string; + id: BigNumberish; + canBeTransferred: boolean; + totalQuantity: BigNumberish; + maxQuantity: BigNumberish; + prtclePrice: BigNumberish | BigNumberish; + canPurchaseWithPrtcle: boolean; +} + +export interface AddChallengeTypesTaskArgs { + challengemanager: string; + diamondaddress: string; + challengefile: string; + uploadchallengetypes: boolean; + sendtochallengemanager: boolean; +} + +task("addChallengeTypes", "Adds challengeTypes") + .addParam("challengemanager", "Address of the challenge manager", "0") + .addParam("diamondaddress", "Address of the Diamond to upgrade") + .addParam("challengefile", "File name of the challenges to add") + .addFlag("uploadchallengetypes", "Upload challengeTypes") + .addFlag( + "sendtochallengemanager", + "Mint and send the challenges to challengeManager" + ) + + .setAction( + async ( + taskArgs: AddChallengeTypesTaskArgs, + hre: HardhatRuntimeEnvironment + ) => { + const challengeFile: string = taskArgs.challengefile; + const diamondAddress: string = taskArgs.diamondaddress; + const challengeManager = taskArgs.challengemanager; + const sendToChallengeManager = taskArgs.sendtochallengemanager; + const uploadChallengeTypes = taskArgs.uploadchallengetypes; + + const { + challengeTypes: currentChallengeTypes, + } = require(`../scripts/testChallengeTypes.js`); + + const challengeTypesArray: ChallengeTypeOutput[] = currentChallengeTypes; + + let signer: Signer; + + let owner = challengeManager; + const testing = ["hardhat", "localhost"].includes(hre.network.name); + if (testing) { + await hre.network.provider.request({ + method: "hardhat_impersonateAccount", + params: [owner], + }); + signer = await hre.ethers.provider.getSigner(owner); + } + // else if (hre.network.name === "matic") { + // // signer = new LedgerSigner( + // // hre.ethers.provider, + // // "hid", + // // "m/44'/60'/2'/0/0" + // // ); + // } + else { + throw Error("Incorrect network selected"); + } + + let tx: ContractTransaction; + let receipt: ContractReceipt; + + let daoFacet = (await hre.ethers.getContractAt( + "DAOFacet", + diamondAddress, + signer + )) as DAOFacet; + + if (uploadChallengeTypes) { + console.log("Adding challenges", 0, "to", currentChallengeTypes.length); + + tx = await daoFacet.addChallengeTypes(challengeTypesArray, { + gasPrice: gasPrice, + }); + + receipt = await tx.wait(); + if (!receipt.status) { + throw Error(`Error:: ${tx.hash}`); + } + console.log("Challenges were added:", tx.hash); + } + + if (sendToChallengeManager) { + const challengeIds: BigNumberish[] = []; + const quantities: BigNumberish[] = []; + challengeTypesArray.forEach((challengeType: ChallengeTypeOutput) => { + challengeIds.push(challengeType.id); + quantities.push(challengeType.maxQuantity); + }); + + console.log("final quantities:", challengeIds, quantities); + + console.log(`Mint challenges to Challenge Manager ${challengeManager}`); + + tx = await daoFacet.mintChallenges( + challengeManager, + challengeIds, + quantities, + { + gasPrice: gasPrice, + } + ); + receipt = await tx.wait(); + if (!receipt.status) { + throw Error(`Error:: ${tx.hash}`); + } + + console.log("Prize challenges minted:", tx.hash); + } + } + );