import * as React from "react"; import * as Strings from "~/common/strings"; import * as Actions from "~/common/actions"; import * as Constants from "~/common/constants"; import * as System from "~/components/system"; import * as SVG from "~/common/svg"; import * as Window from "~/common/window"; import * as Messages from "~/common/messages"; import * as FileUtilities from "~/common/file-utilities"; import { css } from "@emotion/react"; import { createState } from "~/scenes/SceneSettings"; import { LoaderSpinner } from "~/components/system/components/Loaders"; import { FilecoinNumber, Converter } from "@openworklabs/filecoin-number"; import { dispatchCustomEvent } from "~/common/custom-events"; import Section from "~/components/core/Section"; import ScenePage from "~/components/core/ScenePage"; import ScenePageHeader from "~/components/core/ScenePageHeader"; import TestnetBanner from "~/components/core/TestnetBanner"; const STYLES_FILE_HIDDEN = css` height: 1px; width: 1px; opacity: 0; visibility: hidden; position: fixed; top: -1px; left: -1px; `; const STYLES_ROW = css` display: flex; align-items: flex-start; justify-content: space-between; `; const STYLES_LEFT = css` color: ${Constants.system.black}; transition: 200ms ease all; min-width: 10%; width: 100%; :visited { color: ${Constants.system.black}; } `; const STYLES_RIGHT = css` flex-shrink: 0; transition: 200ms ease all; cursor: pointer; :hover { color: ${Constants.system.brand}; } `; const DEFAULT_ERROR_MESSAGE = "We could not make your deal. Please try again later."; let mounted = false; export default class SceneMakeFilecoinDeal extends React.Component { state = {}; async componentDidMount() { if (mounted) { return; } mounted = true; let networkViewer; try { const response = await fetch("/api/network"); const json = await response.json(); networkViewer = json.data; } catch (e) {} this.setState({ networkViewer, ...createState(networkViewer.powerInfo.defaultStorageConfig), settings_cold_default_max_price: 1000000000000000, }); } _handleUpload = async (e) => { e.persist(); if (!e.target.files) { return null; } if (!e.target.files.length) { return null; } this.setState({ loading: true }); for (let i = 0; i < e.target.files.length; i++) { const file = e.target.files[i]; const response = await FileUtilities.upload({ bucketName: "deal", file, }); } let networkViewer; try { const response = await fetch("/api/network"); const json = await response.json(); networkViewer = json.data; } catch (e) {} this.setState({ networkViewer, loading: false, }); }; _handleArchive = async (e) => { this.setState({ archiving: true }); const response = await Actions.archive({ bucketName: "deal" }); if (!response) { this.setState({ archiving: false }); return dispatchCustomEvent({ name: "create-alert", detail: { alert: { message: DEFAULT_ERROR_MESSAGE, }, }, }); } if (response.error) { this.setState({ archiving: false }); if (response.message) { return dispatchCustomEvent({ name: "create-alert", detail: { alert: { message: `From Textile: ${response.message}`, }, }, }); } return dispatchCustomEvent({ name: "create-alert", detail: { alert: { message: Messages.error[response.decorator] ? Messages.error[response.decorator] : DEFAULT_ERROR_MESSAGE, }, }, }); } await Window.delay(5000); alert("The storage deal was made!"); this.props.onAction({ type: "NAVIGATE", value: "V1_NAVIGATION_ARCHIVE" }); }; _handleRemove = async (cid) => { this.setState({ loading: true }); await Actions.removeFromBucket({ bucketName: "deal", cid }); let networkViewer; try { const response = await fetch("/api/network"); const json = await response.json(); networkViewer = json.data; } catch (e) {} this.setState({ networkViewer, loading: false, }); }; _handleChange = (e) => { this.setState({ [e.target.name]: e.target.value }); }; componentWillUnmount() { mounted = false; } render() { const { networkViewer } = this.state; const addressMap = {}; const addresses = []; let selected = null; let balance = 0; if (networkViewer) { networkViewer.powerInfo.balancesList.forEach((a) => { addressMap[a.addr.addr] = { ...a.addr, balance: a.balance }; addresses.push({ ...a.addr, balance: a.balance }); }); if (addresses.length) { selected = addresses[0]; } let transactions = []; if (selected.transactions) { transactions = [...selected.transactions]; } balance = Strings.formatAsFilecoinConversion(selected.balance); } let inFil = 0; if (networkViewer) { const filecoinNumber = new FilecoinNumber( `${this.state.settings_cold_default_max_price}`, "attofil" ); inFil = filecoinNumber.toFil(); } return ( This is a simple tool to upload data and make one-off storage deals in the Filecoin network. {this.state.networkViewer ? (
{this.state.loading ? (
) : ( {this.state.networkViewer.deal.length ? ( { return { cid: (
{file.cid} this._handleRemove(file.cid)} >
), }; }), }} /> ) : (
No files have been added.
)}
)}
{ return { miner, }; } ), }} />
Make storage deal
) : ( )}
); } }