Mutate tx with fee estimation

This commit is contained in:
Valtteri Karesto
2022-05-24 15:27:20 +03:00
parent 1a1d4901aa
commit 32445dbebf

View File

@@ -6,13 +6,14 @@ import calculateHookOn, { TTS } from "../../utils/hookOnCalculator";
import { SetHookData } from "../../components/SetHookDialog"; import { SetHookData } from "../../components/SetHookDialog";
import { Link } from "../../components"; import { Link } from "../../components";
import { ref } from "valtio"; import { ref } from "valtio";
import estimateFee from "../../utils/estimateFee";
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;
}; };
@@ -72,12 +73,12 @@ export const deployHook = async (
return; return;
} }
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( const filteredHookParameters = HookParameters.filter(
hp => (hp) =>
hp.HookParameter.HookParameterName && hp.HookParameter.HookParameterValue hp.HookParameter.HookParameterName && hp.HookParameter.HookParameterValue
)?.map(aa => ({ )?.map((aa) => ({
HookParameter: { HookParameter: {
HookParameterName: toHex(aa.HookParameter.HookParameterName || ""), HookParameterName: toHex(aa.HookParameter.HookParameterName || ""),
HookParameterValue: aa.HookParameter.HookParameterValue || "", HookParameterValue: aa.HookParameter.HookParameterValue || "",
@@ -119,14 +120,22 @@ export const deployHook = async (
}; };
const keypair = derive.familySeed(account.secret); const keypair = derive.familySeed(account.secret);
try {
// Update tx Fee value with network estimation
await estimateFee(tx, keypair);
} catch (err) {
// use default value what you defined earlier
console.log(err);
}
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;
} }
let submitRes; let submitRes;
try { try {
submitRes = await state.client.send({ submitRes = await state.client.send({
command: "submit", command: "submit",
@@ -183,7 +192,7 @@ 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;
@@ -205,6 +214,13 @@ export const deleteHook = async (account: IAccount & { name?: string }) => {
}; };
const keypair = derive.familySeed(account.secret); const keypair = derive.familySeed(account.secret);
try {
// Update tx Fee value with network estimation
await estimateFee(tx, keypair);
} catch (err) {
// use default value what you defined earlier
console.log(err);
}
const { signedTransaction } = sign(tx, keypair); const { signedTransaction } = sign(tx, keypair);
if (currentAccount) { if (currentAccount) {