// Copyright 2015, 2016 Parity Technologies (UK) Ltd. // This file is part of Parity. // Parity is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // Parity is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with Parity. If not, see . import { sha3, api } from '../parity.js'; import postTx from '../util/post-tx'; const alreadyQueued = (queue, action, name) => !!queue.find((entry) => entry.action === action && entry.name === name); export const reserveStart = (name) => ({ type: 'names reserve start', name }); export const reserveSuccess = (name) => ({ type: 'names reserve success', name }); export const reserveFail = (name) => ({ type: 'names reserve fail', name }); export const reserve = (name) => (dispatch, getState) => { const state = getState(); const account = state.accounts.selected; const contract = state.contract; const fee = state.fee; if (!contract || !account) { return; } name = name.toLowerCase(); if (alreadyQueued(state.names.queue, 'reserve', name)) { return; } const reserve = contract.functions.find((f) => f.name === 'reserve'); dispatch(reserveStart(name)); const options = { from: account.address, value: fee }; const values = [ sha3(name) ]; postTx(api, reserve, options, values) .then((txHash) => { dispatch(reserveSuccess(name)); }) .catch((err) => { console.error(`could not reserve ${name}`); if (err) { console.error(err.stack); } dispatch(reserveFail(name)); }); }; export const dropStart = (name) => ({ type: 'names drop start', name }); export const dropSuccess = (name) => ({ type: 'names drop success', name }); export const dropFail = (name) => ({ type: 'names drop fail', name }); export const drop = (name) => (dispatch, getState) => { const state = getState(); const account = state.accounts.selected; const contract = state.contract; if (!contract || !account) { return; } name = name.toLowerCase(); if (alreadyQueued(state.names.queue, 'drop', name)) { return; } const drop = contract.functions.find((f) => f.name === 'drop'); dispatch(dropStart(name)); const options = { from: account.address }; const values = [ sha3(name) ]; postTx(api, drop, options, values) .then((txhash) => { dispatch(dropSuccess(name)); }) .catch((err) => { console.error(`could not drop ${name}`); if (err) { console.error(err.stack); } dispatch(reserveFail(name)); }); };