Link ledger index to explorer

This commit is contained in:
muzam1l
2022-04-20 13:45:17 +05:30
parent aff0142870
commit 582fb17c94
3 changed files with 72 additions and 27 deletions

View File

@@ -186,8 +186,17 @@ export const Log: FC<ILog> = ({
}, },
[accounts] [accounts]
); );
_message = _message.trim().replace(/\n /gi, "\n");
const message = enrichAccounts(_message); let message: ReactNode;
if (typeof _message === 'string') {
_message = _message.trim().replace(/\n /gi, "\n");
message = enrichAccounts(_message)
}
else {
message = _message
}
const jsonData = enrichAccounts(_jsonData); const jsonData = enrichAccounts(_jsonData);
return ( return (

View File

@@ -4,19 +4,21 @@ import toast from "react-hot-toast";
import state, { IAccount } from "../index"; import state, { IAccount } from "../index";
import calculateHookOn, { TTS } from "../../utils/hookOnCalculator"; import calculateHookOn, { TTS } from "../../utils/hookOnCalculator";
import { SetHookData } from "../../components/SetHookDialog"; import { SetHookData } from "../../components/SetHookDialog";
import { Link } from "../../components";
import { ref } from "valtio";
export const sha256 = async (string: string) => { export const sha256 = async (string: string) => {
const utf8 = new TextEncoder().encode(string); const utf8 = new TextEncoder().encode(string);
const hashBuffer = await crypto.subtle.digest('SHA-256', utf8); const hashBuffer = await crypto.subtle.digest("SHA-256", utf8);
const hashArray = Array.from(new Uint8Array(hashBuffer)); const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray const hashHex = hashArray
.map((bytes) => bytes.toString(16).padStart(2, '0')) .map(bytes => bytes.toString(16).padStart(2, "0"))
.join(''); .join("");
return hashHex; return hashHex;
} };
function toHex(str: string) { function toHex(str: string) {
var result = ''; var result = "";
for (var i = 0; i < str.length; i++) { for (var i = 0; i < str.length; i++) {
result += str.charCodeAt(i).toString(16); result += str.charCodeAt(i).toString(16);
} }
@@ -51,7 +53,10 @@ function arrayBufferToHex(arrayBuffer?: ArrayBuffer | null) {
* hex string, signs the transaction and deploys it to * hex string, signs the transaction and deploys it to
* Hooks testnet. * Hooks testnet.
*/ */
export const deployHook = async (account: IAccount & { name?: string }, data: SetHookData) => { export const deployHook = async (
account: IAccount & { name?: string },
data: SetHookData
) => {
if ( if (
!state.files || !state.files ||
state.files.length === 0 || state.files.length === 0 ||
@@ -69,7 +74,15 @@ export const deployHook = async (account: IAccount & { name?: string }, data: Se
const HookNamespace = (await sha256(data.HookNamespace)).toUpperCase(); const HookNamespace = (await sha256(data.HookNamespace)).toUpperCase();
const hookOnValues: (keyof TTS)[] = data.Invoke.map(tt => tt.value); const hookOnValues: (keyof TTS)[] = data.Invoke.map(tt => tt.value);
const { HookParameters } = data; const { HookParameters } = data;
const filteredHookParameters = HookParameters.filter(hp => hp.HookParameter.HookParameterName && hp.HookParameter.HookParameterValue)?.map(aa => ({ HookParameter: { HookParameterName: toHex(aa.HookParameter.HookParameterName || ''), HookParameterValue: toHex(aa.HookParameter.HookParameterValue || '') } })); const filteredHookParameters = HookParameters.filter(
hp =>
hp.HookParameter.HookParameterName && hp.HookParameter.HookParameterValue
)?.map(aa => ({
HookParameter: {
HookParameterName: toHex(aa.HookParameter.HookParameterName || ""),
HookParameterValue: toHex(aa.HookParameter.HookParameterValue || ""),
},
}));
// const filteredHookGrants = HookGrants.filter(hg => hg.HookGrant.Authorize || hg.HookGrant.HookHash).map(hg => { // const filteredHookGrants = HookGrants.filter(hg => hg.HookGrant.Authorize || hg.HookGrant.HookHash).map(hg => {
// return { // return {
// HookGrant: { // HookGrant: {
@@ -97,16 +110,18 @@ export const deployHook = async (account: IAccount & { name?: string }, data: Se
HookApiVersion: 0, HookApiVersion: 0,
Flags: 1, Flags: 1,
// ...(filteredHookGrants.length > 0 && { HookGrants: filteredHookGrants }), // ...(filteredHookGrants.length > 0 && { HookGrants: filteredHookGrants }),
...(filteredHookParameters.length > 0 && { HookParameters: filteredHookParameters }), ...(filteredHookParameters.length > 0 && {
} HookParameters: filteredHookParameters,
} }),
] },
},
],
}; };
const keypair = derive.familySeed(account.secret); const keypair = derive.familySeed(account.secret);
const { signedTransaction } = sign(tx, keypair); const { signedTransaction } = sign(tx, keypair);
const currentAccount = state.accounts.find( const currentAccount = state.accounts.find(
(acc) => acc.address === account.address acc => acc.address === account.address
); );
if (currentAccount) { if (currentAccount) {
currentAccount.isLoading = true; currentAccount.isLoading = true;
@@ -125,12 +140,28 @@ export const deployHook = async (account: IAccount & { name?: string }, data: Se
}); });
state.deployLogs.push({ state.deployLogs.push({
type: "success", type: "success",
message: `[${submitRes.engine_result}] ${submitRes.engine_result_message} Validated ledger index: ${submitRes.validated_ledger_index}`, message: ref(
<>
[{submitRes.engine_result}] {submitRes.engine_result_message}{" "}
Validated ledger index:{" "}
<Link
as="a"
href={`https://${process.env.NEXT_PUBLIC_EXPLORER_URL}/${submitRes.validated_ledger_index}`}
target="_blank"
rel="noopener noreferrer"
>
{submitRes.validated_ledger_index}
</Link>
</>
),
// message: `[${submitRes.engine_result}] ${submitRes.engine_result_message} Validated ledger index: ${submitRes.validated_ledger_index}`,
}); });
} else { } else {
state.deployLogs.push({ state.deployLogs.push({
type: "error", type: "error",
message: `[${submitRes.engine_result || submitRes.error}] ${submitRes.engine_result_message || submitRes.error_exception}`, message: `[${submitRes.engine_result || submitRes.error}] ${
submitRes.engine_result_message || submitRes.error_exception
}`,
}); });
} }
} catch (err) { } catch (err) {
@@ -152,10 +183,10 @@ export const deleteHook = async (account: IAccount & { name?: string }) => {
return; return;
} }
const currentAccount = state.accounts.find( const currentAccount = state.accounts.find(
(acc) => acc.address === account.address acc => acc.address === account.address
); );
if (currentAccount?.isLoading || !currentAccount?.hooks.length) { if (currentAccount?.isLoading || !currentAccount?.hooks.length) {
return return;
} }
if (typeof window !== "undefined") { if (typeof window !== "undefined") {
const tx = { const tx = {
@@ -168,9 +199,9 @@ export const deleteHook = async (account: IAccount & { name?: string }) => {
Hook: { Hook: {
CreateCode: "", CreateCode: "",
Flags: 1, Flags: 1,
} },
} },
] ],
}; };
const keypair = derive.familySeed(account.secret); const keypair = derive.familySeed(account.secret);
@@ -188,7 +219,7 @@ export const deleteHook = async (account: IAccount & { name?: string }) => {
}); });
if (submitRes.engine_result === "tesSUCCESS") { if (submitRes.engine_result === "tesSUCCESS") {
toast.success('Hook deleted successfully ✅', { id: toastId }) toast.success("Hook deleted successfully ✅", { id: toastId });
state.deployLogs.push({ state.deployLogs.push({
type: "success", type: "success",
message: "Hook deleted successfully ✅", message: "Hook deleted successfully ✅",
@@ -199,15 +230,20 @@ export const deleteHook = async (account: IAccount & { name?: string }) => {
}); });
currentAccount.hooks = []; currentAccount.hooks = [];
} else { } else {
toast.error(`${submitRes.engine_result_message || submitRes.error_exception}`, { id: toastId }) toast.error(
`${submitRes.engine_result_message || submitRes.error_exception}`,
{ id: toastId }
);
state.deployLogs.push({ state.deployLogs.push({
type: "error", type: "error",
message: `[${submitRes.engine_result || submitRes.error}] ${submitRes.engine_result_message || submitRes.error_exception}`, message: `[${submitRes.engine_result || submitRes.error}] ${
submitRes.engine_result_message || submitRes.error_exception
}`,
}); });
} }
} catch (err) { } catch (err) {
console.log(err); console.log(err);
toast.error('Error occured while deleting hoook', { id: toastId }) toast.error("Error occured while deleting hoook", { id: toastId });
state.deployLogs.push({ state.deployLogs.push({
type: "error", type: "error",
message: "Error occured while deleting hook", message: "Error occured while deleting hook",
@@ -218,4 +254,4 @@ export const deleteHook = async (account: IAccount & { name?: string }) => {
} }
return submitRes; return submitRes;
} }
}; };

View File

@@ -39,7 +39,7 @@ export interface IAccount {
export interface ILog { export interface ILog {
type: "error" | "warning" | "log" | "success"; type: "error" | "warning" | "log" | "success";
message: string; message: string | JSX.Element;
key?: string; key?: string;
jsonData?: any, jsonData?: any,
timestring?: string; timestring?: string;