UNPKG

frakt-client

Version:

Client library for interacting with FRAKT solana program

48 lines (47 loc) 2.83 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.getAssetCostToStore = exports.filterModalSolTokens = exports.LAMPORT_MULTIPLIER = void 0; exports.LAMPORT_MULTIPLIER = Math.pow(10, 9); const WINSTON_MULTIPLIER = Math.pow(10, 12); exports.filterModalSolTokens = (tokens) => { return tokens; }; function getAssetCostToStore(files) { return __awaiter(this, void 0, void 0, function* () { const totalBytes = files.reduce((sum, f) => (sum += f.size), 0); console.log('Total bytes', totalBytes); const txnFeeInWinstons = parseInt(yield (yield fetch('https://arweave.net/price/0')).text()); console.log('txn fee', txnFeeInWinstons); const byteCostInWinstons = parseInt(yield (yield fetch('https://arweave.net/price/' + totalBytes.toString())).text()); console.log('byte cost', byteCostInWinstons); const totalArCost = (txnFeeInWinstons * files.length + byteCostInWinstons) / WINSTON_MULTIPLIER; console.log('total ar', totalArCost); let conversionRates = JSON.parse(localStorage.getItem('conversionRates') || '{}'); if (!conversionRates || !conversionRates.expiry || conversionRates.expiry < Date.now()) { console.log('Calling conversion rate'); conversionRates = { value: JSON.parse(yield (yield fetch('https://api.coingecko.com/api/v3/simple/price?ids=solana,arweave&vs_currencies=usd')).text()), expiry: Date.now() + 5 * 60 * 1000, }; if (conversionRates.value.solana) localStorage.setItem('conversionRates', JSON.stringify(conversionRates)); } // To figure out how many lamports are required, multiply ar byte cost by this number const arMultiplier = conversionRates.value.arweave.usd / conversionRates.value.solana.usd; console.log('Ar mult', arMultiplier); // We also always make a manifest file, which, though tiny, needs payment. return exports.LAMPORT_MULTIPLIER * totalArCost * arMultiplier * 1.1; }); } exports.getAssetCostToStore = getAssetCostToStore;