Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[vsp] Show fee tx hash and fee status on tx details view #3752

Merged
merged 6 commits into from May 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions app/actions/ControlActions.js
Expand Up @@ -881,6 +881,7 @@ export const signMessageAttempt = (address, message, passphrase) => async (
getSignMessageSignature: sig,
type: SIGNMESSAGE_SUCCESS
});
return sig;
} catch (error) {
dispatch({ error, type: SIGNMESSAGE_FAILED });
}
Expand Down
99 changes: 93 additions & 6 deletions app/actions/VSPActions.js
@@ -1,7 +1,8 @@
import {
importScriptAttempt,
rescanAttempt,
unlockAllAcctAndExecFn
unlockAllAcctAndExecFn,
signMessageAttempt
} from "./ControlActions";
import { SETVOTECHOICES_SUCCESS } from "./ClientActions";
import * as sel from "../selectors";
Expand Down Expand Up @@ -694,12 +695,14 @@ export const getVSPsPubkeys = () => async (dispatch) => {
})
);
})
).then((result) =>
).then((result) => {
const availableVSPsPubkeys = result.filter((vsp) => !!vsp?.pubkey);
dispatch({
type: GETVSPSPUBKEYS_SUCCESS,
availableVSPsPubkeys: result.filter((vsp) => !!vsp?.pubkey)
})
);
availableVSPsPubkeys
});
return availableVSPsPubkeys;
});
} catch (error) {
dispatch({ type: GETVSPSPUBKEYS_FAILED, error });
}
Expand All @@ -716,7 +719,11 @@ export const processManagedTickets = (passphrase) => async (
getState
) => {
const walletService = sel.walletService(getState());
const availableVSPsPubkeys = sel.getAvailableVSPsPubkeys(getState());
let availableVSPsPubkeys = sel.getAvailableVSPsPubkeys(getState());

if (!availableVSPsPubkeys) {
availableVSPsPubkeys = await dispatch(getVSPsPubkeys());
}
try {
dispatch({ type: PROCESSMANAGEDTICKETS_ATTEMPT });
let feeAccount, changeAccount;
Expand Down Expand Up @@ -1015,3 +1022,83 @@ export const getUnspentUnexpiredVspTickets = () => async (
});
}
};

export const GETVSP_TICKET_STATUS_ATTEMPT = "GETVSP_TICKET_STATUS_ATTEMPT";
export const GETVSP_TICKET_STATUS_SUCCESS = "GETVSP_TICKET_STATUS_SUCCESS";
export const GETVSP_TICKET_STATUS_FAILED = "GETVSP_TICKET_STATUS_FAILED";

export const getVSPTicketStatus = (passphrase, tx, decodedTx) => async (
dispatch,
getState
) => {
let commitmentAddress;
let json;
try {
if (!tx || !tx.ticketTx || !tx.ticketTx.vspHost || !tx.txHash) {
throw new Error("Invalid tx parameter");
}

if (
!decodedTx ||
!decodedTx.outputs ||
decodedTx.outputs.length < 2 ||
!decodedTx.outputs[1].decodedScript ||
!decodedTx.outputs[1].decodedScript.address
) {
throw new Error("Invalid decodedTx parameter");
}

// This only considers the first commitment address which is the first odd output
commitmentAddress = decodedTx.outputs[1].decodedScript.address;

json = {
tickethash: tx.txHash
};
} catch (error) {
dispatch({ type: GETVSP_TICKET_STATUS_FAILED, error });
return;
}

const sig = await dispatch(
signMessageAttempt(commitmentAddress, JSON.stringify(json), passphrase)
);

if (!sig) {
return;
}

dispatch({ type: GETVSP_TICKET_STATUS_ATTEMPT });
try {
// Check if user allows access to this VSP. This might trigger a confirmation
// dialog.
await wallet.allowVSPHost(tx.ticketTx.vspHost);
const info = await wallet.getVSPTicketStatus({
host: tx.ticketTx.vspHost,
sig,
json
});

if (!info || !info.data) {
throw new Error("Invalid response from the VSP");
}

if (info.data.code) {
throw new Error(`${info.data.message} (code: ${info.data.code})`);
}

if (
(tx.status === LIVE || tx.status === IMMATURE) &&
info.data.feetxstatus === "confirmed" &&
tx.feeStatus != VSP_FEE_PROCESS_CONFIRMED
) {
dispatch(processManagedTickets(passphrase));
}

dispatch({ type: GETVSP_TICKET_STATUS_SUCCESS });
const txURLBuilder = sel.txURLBuilder(getState());
info.data.feetxUrl = txURLBuilder(info.data.feetxhash);
return info.data;
} catch (error) {
dispatch({ type: GETVSP_TICKET_STATUS_FAILED, error });
}
};
6 changes: 0 additions & 6 deletions app/components/views/TicketsPage/PurchaseTab/hooks.js
Expand Up @@ -63,11 +63,6 @@ export const usePurchaseTab = () => {
() => dispatch(ca.ticketBuyerCancel()),
[dispatch]
);
const getTicketStatus = useCallback(
(host, tickethash, passphrase) =>
dispatch(vspa.getVSPTicketStatus(host, tickethash, passphrase)),
[dispatch]
);

const getVSPTicketsByFeeStatus = (feeStatus) => {
dispatch(vspa.getVSPTicketsByFeeStatus(feeStatus));
Expand Down Expand Up @@ -114,7 +109,6 @@ export const usePurchaseTab = () => {
availableVSPs: isVSPListingEnabled ? availableVSPs : [],
availableVSPsError,
onDisableTicketAutoBuyer,
getTicketStatus,
ticketAutoBuyerRunning,
getVSPTicketsByFeeStatus,
isLegacy,
Expand Down
Expand Up @@ -2,7 +2,8 @@ import { Balance, ExternalLink } from "shared";
import {
KeyBlueButton,
RevokeModalButton,
CopyToClipboardButton
CopyToClipboardButton,
PassphraseModalButton
} from "buttons";
import { addSpacingAroundText } from "helpers";
import { FormattedMessage as T } from "react-intl";
Expand Down Expand Up @@ -50,7 +51,10 @@ const TransactionContent = ({
currentBlockHeight,
isSPV,
agendas,
getAgendaSelectedChoice
getAgendaSelectedChoice,
getVSPTicketStatus,
getVSPTicketStatusAttempt,
VSPTicketStatus
}) => {
const {
txHash,
Expand Down Expand Up @@ -260,12 +264,62 @@ const TransactionContent = ({
</div>
)}
{(txType == TICKET || txType == VOTE) && ticketTx.vspHost && (
<div className={styles.topRow}>
<div className={styles.name}>
<T id="txDetails.vspHost" m="VSP host" />:
<>
<div className={styles.topRow}>
<div className={styles.name}>
<T id="txDetails.vspHost" m="VSP host" />:
</div>
<div className={styles.value}>{ticketTx.vspHost}</div>
</div>
<div className={styles.value}>{ticketTx.vspHost}</div>
</div>
{txType == TICKET &&
(VSPTicketStatus ? (
<>
<div className={styles.topRow}>
<div className={styles.name}>
<T id="txDetails.feeTxHashLabel" m="Fee tx hash" />:
</div>
<div className={styles.value}>
<ExternalLink
className={styles.value}
href={VSPTicketStatus.feetxUrl}>
{VSPTicketStatus.feetxhash}
</ExternalLink>
</div>
</div>
<div className={styles.topRow}>
<div className={styles.name}>
<T id="txDetails.feeTxStatusLabel" m="Fee tx status" />:
</div>
<div className={styles.value}>
{VSPTicketStatus.feetxstatus}
</div>
</div>
</>
) : (
<div className={styles.topRow}>
<div className={styles.name}></div>
<div className={styles.value}>
<PassphraseModalButton
modalTitle={
<T
id="txDetails.signMessageModal"
m="Fetch VSP Ticket Status"
/>
}
buttonLabel={
<T
id="txDetails.signMessageBtn"
m="Fetch VSP Ticket Status"
/>
}
loading={getVSPTicketStatusAttempt}
disabled={getVSPTicketStatusAttempt}
onSubmit={getVSPTicketStatus}
/>
</div>
</div>
))}
</>
)}
</div>
{isPending ? (
Expand Down
10 changes: 8 additions & 2 deletions app/components/views/TransactionPage/TransactionPage.jsx
Expand Up @@ -18,7 +18,10 @@ const Transaction = () => {
decodedTx,
isSPV,
agendas,
getAgendaSelectedChoice
getAgendaSelectedChoice,
getVSPTicketStatus,
getVSPTicketStatusAttempt,
VSPTicketStatus
} = useTransactionPage(txHash);

if (!viewedTransaction) return null;
Expand Down Expand Up @@ -48,7 +51,10 @@ const Transaction = () => {
currentBlockHeight,
isSPV,
agendas,
getAgendaSelectedChoice
getAgendaSelectedChoice,
getVSPTicketStatus,
getVSPTicketStatusAttempt,
VSPTicketStatus
}}
/>
</StandalonePage>
Expand Down
19 changes: 18 additions & 1 deletion app/components/views/TransactionPage/hooks.js
Expand Up @@ -6,6 +6,7 @@ import * as sel from "selectors";
import * as ca from "actions/ControlActions";
import * as ta from "actions/TransactionActions";
import * as clia from "actions/ClientActions";
import * as vspa from "actions/VSPActions";
import { find, compose, eq, get } from "fp";

export function useTransactionPage(txHash) {
Expand All @@ -15,6 +16,7 @@ export function useTransactionPage(txHash) {
const decodedTransactions = useSelector(sel.decodedTransactions);
const agendas = useSelector(sel.allAgendas);
const voteChoices = useSelector(sel.voteChoices);
const getVSPTicketStatusAttempt = useSelector(sel.getVSPTicketStatusAttempt);
const getAgendaSelectedChoice = useCallback(
(agendaId) =>
get(
Expand Down Expand Up @@ -52,6 +54,18 @@ export function useTransactionPage(txHash) {
() => dispatch(ca.publishUnminedTransactionsAttempt()),
[dispatch]
);

const [VSPTicketStatus, setVSPTicketStatus] = useState(null);

const getVSPTicketStatus = useCallback(
(passphrase) => {
dispatch(
vspa.getVSPTicketStatus(passphrase, viewedTransaction, decodedTx)
).then((res) => setVSPTicketStatus(res));
},
[dispatch, viewedTransaction, decodedTx]
);

const [state, send] = useMachine(fetchMachine, {
actions: {
initial: () => {
Expand Down Expand Up @@ -102,6 +116,9 @@ export function useTransactionPage(txHash) {
decodedTx,
isSPV,
agendas,
getAgendaSelectedChoice
getAgendaSelectedChoice,
getVSPTicketStatus,
getVSPTicketStatusAttempt,
VSPTicketStatus
};
}
11 changes: 9 additions & 2 deletions app/main_dev/externalRequests.js
Expand Up @@ -143,8 +143,15 @@ export const installSessionHandlers = (mainLogger) => {
statusLine = "OK";
newHeaders["Access-Control-Allow-Headers"] = "Content-Type";
}
}

const globalCfg = getGlobalCfg();
const cfgAllowedVSPs = globalCfg.get(cfgConstants.ALLOWED_VSP_HOSTS, []);
if (cfgAllowedVSPs.some((url) => details.url.includes(url))) {
statusLine = "OK";
newHeaders["Access-Control-Allow-Headers"] =
"Content-Type, VSP-Client-Signature";
}
}
callback({ responseHeaders: newHeaders, statusLine });
});
};
Expand Down Expand Up @@ -208,7 +215,7 @@ export const allowVSPRequests = (stakePoolHost) => {
if (allowedExternalRequests[reqType]) return;

addAllowedURL(stakePoolHost + "/api/v3/vspinfo");
addAllowedURL(stakePoolHost + "/api/ticketstatus");
addAllowedURL(stakePoolHost + "/api/v3/ticketstatus");
};

export const reloadAllowedExternalRequests = () => {
Expand Down
16 changes: 9 additions & 7 deletions app/middleware/vspapi.js
Expand Up @@ -33,11 +33,13 @@ const LEGACY_POST = (path, apiToken, json) => {
};

const POST = (path, vspClientSig, json) => {
const config = {
headers: {
"VSP-CLIENT-SIGNATURE": vspClientSig
}
};
const config = vspClientSig
? {
headers: {
"VSP-Client-Signature": vspClientSig
}
}
: {};
// This json request is strigfied at the call which is making it.
return postJSON(path, json, config);
};
Expand Down Expand Up @@ -174,8 +176,8 @@ export function getVSPInfo(host, cb) {
.catch((error) => cb(null, error, host));
}

export function getTicketStatus({ host, vspClientSig, request }, cb) {
POST(host + "/api/ticketstatus", vspClientSig, request)
export function getVSPTicketStatus({ host, sig, json }, cb) {
POST(host + "/api/v3/ticketstatus", sig, json)
.then((resp) => cb(resp, null, host))
.catch((error) => cb(null, error, host));
}
8 changes: 7 additions & 1 deletion app/reducers/snackbar.js
Expand Up @@ -66,7 +66,8 @@ import {
SYNCVSPTICKETS_SUCCESS,
SYNCVSPTICKETS_FAILED,
PROCESSMANAGEDTICKETS_FAILED,
SETVSPDVOTECHOICE_FAILED
SETVSPDVOTECHOICE_FAILED,
GETVSP_TICKET_STATUS_FAILED
} from "actions/VSPActions";
import {
ABANDONTRANSACTION_SUCCESS,
Expand Down Expand Up @@ -654,6 +655,10 @@ const messages = defineMessages({
id: "set.vspdvote.failed",
defaultMessage: "Set vspd vote choices failed: {originalError}"
},
GETVSP_TICKET_STATUS_FAILED: {
id: "set.getvspticketstatus.failed",
defaultMessage: "Fetch vsp ticket status failed: {originalError}"
},
DEX_STARTUP_FAILED: {
id: "dex.startup.failed",
defaultMessage: "DEX Client Failed to Start: {originalError}"
Expand Down Expand Up @@ -973,6 +978,7 @@ export default function snackbar(state = {}, action) {
case PROCESSMANAGEDTICKETS_FAILED:
case SETVOTECHOICES_FAILED:
case SETVSPDVOTECHOICE_FAILED:
case GETVSP_TICKET_STATUS_FAILED:
case DEX_STARTUP_FAILED:
case DEX_LOGIN_FAILED:
case DEX_ENABLE_FAILED:
Expand Down