UNPKG

hardhat-deal

Version:

🎩🪄 Easily deal any amount of any ERC20 tokens to any account on the hardhat network

85 lines (84 loc) • 4.7 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.deal = void 0; const cache_1 = require("./cache"); const types_1 = require("./types"); const ethers_1 = require("ethers"); const balanceOfIfc = new ethers_1.Interface(["function balanceOf(address) external view returns (uint256)"]); const getBalanceOfSlot = (type, slot, recipient) => { if (type === types_1.StorageLayoutType.VYPER) return (0, ethers_1.toQuantity)((0, ethers_1.keccak256)(ethers_1.AbiCoder.defaultAbiCoder().encode(["uint256", "address"], [slot, recipient]))); return (0, ethers_1.toQuantity)((0, ethers_1.keccak256)(ethers_1.AbiCoder.defaultAbiCoder().encode(["address", "uint256"], [recipient, slot]))); }; const getAddress = (address) => __awaiter(void 0, void 0, void 0, function* () { const awaited = yield address; if (typeof awaited === "string") return awaited.toLowerCase(); return awaited.getAddress(); }); const deal = (erc20_1, recipient_1, amount_1, ...args_1) => __awaiter(void 0, [erc20_1, recipient_1, amount_1, ...args_1], void 0, function* (erc20, recipient, amount, maxSlot = 256, hre) { if (!hre) hre = require("hardhat"); if (!hre) throw Error("Could not instanciate Hardhat Runtime Environment"); const [erc20Address, recipientAddress] = yield Promise.all([ getAddress(erc20), getAddress(recipient), ]); const hexAmount = (0, ethers_1.toBeHex)(amount, 32); const balanceOfCall = [ { to: erc20Address, data: balanceOfIfc.encodeFunctionData("balanceOf", [recipientAddress]), }, ]; const configDealSlot = hre.config.dealSlots[erc20Address]; const cached = configDealSlot != null; let dealSlot = typeof configDealSlot === "object" && "type" in configDealSlot ? Object.assign({}, configDealSlot) : { type: types_1.StorageLayoutType.SOLIDITY, slot: configDealSlot !== null && configDealSlot !== void 0 ? configDealSlot : 0 }; const trySlot = () => __awaiter(void 0, void 0, void 0, function* () { var _a, _b, _c, _d; let balanceOfSlot = getBalanceOfSlot(dealSlot.type, dealSlot.slot, recipientAddress); const storageBefore = !cached ? yield hre.network.provider.send("eth_getStorageAt", [erc20Address, balanceOfSlot]) : null; yield hre.network.provider.send((_b = (_a = hre.network.config.rpcEndpoints) === null || _a === void 0 ? void 0 : _a.setStorageAt) !== null && _b !== void 0 ? _b : "hardhat_setStorageAt", [erc20Address, balanceOfSlot, hexAmount]); if (cached) return true; const balance = yield hre.network.provider.send("eth_call", balanceOfCall); if (balance === hexAmount) return true; yield hre.network.provider.send((_d = (_c = hre.network.config.rpcEndpoints) === null || _c === void 0 ? void 0 : _c.setStorageAt) !== null && _d !== void 0 ? _d : "hardhat_setStorageAt", [erc20Address, balanceOfSlot, storageBefore]); return false; }); const getNextDealSlot = () => { const { slot } = dealSlot; if (dealSlot.type === types_1.StorageLayoutType.SOLIDITY) return { type: types_1.StorageLayoutType.VYPER, slot }; return { type: types_1.StorageLayoutType.SOLIDITY, slot: slot + 1 }; }; // Hotfix: warm up EDR account storage // https://github.com/NomicFoundation/edr/issues/503#issuecomment-2165233608 yield hre.network.provider.send("eth_getCode", [erc20Address]); let success = yield trySlot(); while (!success && dealSlot.slot <= maxSlot) { dealSlot = getNextDealSlot(); success = yield trySlot(); } if (!success) throw Error(`Could not brute-force storage slot for ERC20 at: ${erc20Address}`); if (cached) return; hre.config.dealSlots[erc20Address] = dealSlot; (0, cache_1.saveCache)((0, cache_1.getCachePath)(hre.config.paths.cache), hre.config.dealSlots); }); exports.deal = deal;