UNPKG

@wttp/site

Version:

Web3 Transfer Protocol (WTTP) - Site Contracts and deployment tools

96 lines • 5.25 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const config_1 = require("hardhat/config"); const fs_1 = __importDefault(require("fs")); const crypto_1 = __importDefault(require("crypto")); // Hardcoded contract fallback for estimation to test change to a contract you have permission to write to const DEFAULT_ESTIMATION_SITE = "0x8de4FAA55d6521Ff79a45bCB05359c177c7CD46E"; // Helper function to check if a path is a directory function isDirectory(sourcePath) { return fs_1.default.statSync(sourcePath).isDirectory(); } // Helper function to generate a random URL-safe string for destination path function generateRandomDestinationPath() { // Generate 16 random bytes and convert to base64url (URL-safe base64) const randomBytes = crypto_1.default.randomBytes(16); // Use base64url encoding which is URL-safe (no + or / characters, uses - and _ instead) const randomString = randomBytes.toString('base64') .replace(/\+/g, '-') .replace(/\//g, '_') .replace(/=/g, ''); // Remove padding return `/${randomString}`; } (0, config_1.task)("site:estimate", "Estimate gas costs for uploading a file or directory to a WTTP site") .addParam("source", "The source file or directory path") .addOptionalParam("site", "The address of the WTTP site (uses default empty contract if not provided)") .addOptionalParam("destination", "The destination path on the WTTP site (auto-generated from source if not provided)") .addOptionalParam("gasprice", "Custom gas price in gwei (otherwise uses current price multiplied by rate)", undefined, config_1.types.int) .addOptionalParam("rate", "Multiplier rate for current gas price (default: 2, accepts decimals like 1.5)", 2, config_1.types.float) .addOptionalParam("min", "Minimum gas price in gwei (default: 150)", 150, config_1.types.float) .addOptionalParam("nodefaults", "Disable default ignore patterns", false, config_1.types.boolean) .setAction(async (taskArgs, hre) => { const { site, source, destination, gasprice, rate, min, nodefaults } = taskArgs; // Use default site if not provided const siteAddress = site || DEFAULT_ESTIMATION_SITE; if (!site) { console.log(`šŸ“‹ Using default estimation site: ${siteAddress} (empty contract for testing)`); } else { console.log(`šŸ“‹ Using site: ${siteAddress}`); } // Generate random destination path if not provided to avoid conflicts const destPath = destination || generateRandomDestinationPath(); if (!destination) { console.log(`šŸ“‹ Using random destination path: ${destPath} (to avoid conflicts)`); } // Connect to the WTTP site const wtppSite = await hre.ethers.getContractAt("Web3Site", siteAddress); // Validate that this is a valid WTTP site by testing DPS/DPR calls try { const dprAddress = await wtppSite.DPR(); console.log(`šŸ“‹ DPR address: ${dprAddress}`); const dpsAddress = await wtppSite.DPS(); console.log(`šŸ“‹ DPS address: ${dpsAddress}`); } catch (error) { throw new Error("Not a WTTPSite"); } // Check if source is a file or directory if (isDirectory(source)) { console.log(`Source ${source} is a directory, estimating directory upload...`); // Import the directory estimation function const { estimateDirectory } = require("../scripts/uploadDirectory"); // Configure ignore options const ignoreOptions = { includeDefaults: !nodefaults }; // Estimate the directory const result = await estimateDirectory(wtppSite, source, destPath, gasprice, ignoreOptions, parseFloat(rate), parseFloat(min)); // Get currency symbol const { getChainSymbol } = require("../scripts/uploadFile"); const currencySymbol = await getChainSymbol(); console.log(`\nāœ… Estimation complete!`); console.log(` Total cost: ${hre.ethers.formatEther(result.totalCost)} ${currencySymbol}`); console.log(` Total gas: ${result.totalGas.toString()}`); console.log(` Total royalty: ${hre.ethers.formatEther(result.totalRoyaltyCost)} ${currencySymbol}`); } else { console.log(`Source ${source} is a file, estimating file upload...`); // Import the file estimation function const { estimateFile } = require("../scripts/uploadFile"); // Estimate the file const result = await estimateFile(wtppSite, source, destPath, gasprice, parseFloat(rate), parseFloat(min)); // Get currency symbol const { getChainSymbol } = require("../scripts/uploadFile"); const currencySymbol = await getChainSymbol(); console.log(`\nāœ… Estimation complete!`); console.log(` Total cost: ${hre.ethers.formatEther(result.totalCost)} ${currencySymbol}`); console.log(` Total gas: ${result.totalGas.toString()}`); console.log(` Total royalty: ${hre.ethers.formatEther(result.royaltyCost)} ${currencySymbol}`); } }); exports.default = {}; //# sourceMappingURL=estimate.js.map