From 58bb735142c91fcf7d7c3dfe2578bd9555bf00b2 Mon Sep 17 00:00:00 2001 From: lza_menace Date: Fri, 18 Nov 2022 12:43:21 -0800 Subject: [PATCH] improvements to ux --- src/Contracts.svelte | 82 ++++++++++++++++++++++++++++++++------------ 1 file changed, 60 insertions(+), 22 deletions(-) diff --git a/src/Contracts.svelte b/src/Contracts.svelte index b00ae39..1ddade5 100644 --- a/src/Contracts.svelte +++ b/src/Contracts.svelte @@ -11,19 +11,27 @@ let contractAddress = ''; let contractApproved = true; let checked = false; - let gasCalculation = ''; + let checksPending = false; + let gasCalculation = []; let selectedStandard = 1; let tokenStandards = [ { id: 1, text: 'ERC-721' }, { id: 2, text: 'ERC-1155' } ] - let amt = 10; let gasLimit = 0; let si_gasLimit = 0; evm.attachContract('sendit', sendit, SendIt.abi); + function clearMessages() { + gasCalculation = []; + checked = false; + checksPending = false; + errorMessage = ''; + successMessage = ''; + } + const approveSendIt = async () => { await $contracts.nft.methods.setApprovalForAll(sendit, true).send({from: $selectedAccount}); } @@ -38,6 +46,7 @@ const performCheck = async () => { let tokenIds = []; let recipients = []; + checksPending = true; errorMessage = ''; // Determine ABI @@ -52,13 +61,14 @@ $web3.utils.toChecksumAddress(contractAddress) } catch { errorMessage = 'Invalid contract address supplied'; + checksPending = false; return; } // Check textarea syntax let info = document.getElementById('recipientInfo').value; let lines = info.split(/(\s+)/); - if (lines.length < 2) { errorMessage = 'Invalid recipient info.'; return; } + if (lines.length < 2) { errorMessage = 'Invalid recipient info; should be sending 2 or more tokens'; checksPending = false; return; } for (let i = 0; i < lines.length; i++) { let line = lines[i].split(','); if (line.length == 2) { @@ -69,6 +79,7 @@ recipients.push(recipient); } catch { errorMessage = `Invalid recipient address supplied (line ${i + 1})`; + checksPending = false; return; } @@ -81,6 +92,7 @@ tokenIds.push(tokenId); } catch(e) { errorMessage = e; + checksPending = false; return; } } else { @@ -91,6 +103,7 @@ tokenIds.push(tokenId); } catch(e) { errorMessage = e; + checksPending = false; return; } } @@ -103,18 +116,20 @@ let approved = await $contracts.nft.methods.isApprovedForAll($selectedAccount, sendit).call(); if (!approved) { errorMessage = 'SendIt requires approval to bulk transfer tokens; click the "Approve" button below'; + checksPending = false; contractApproved = false; return; } } catch { errorMessage = 'Unable to check contract approvals'; + checksPending = false; return; } - // Check gas consumption forecasts + // Show gas consumption forecasts await estimateGas(recipients, tokenIds); - // Show results + checked = true; } async function estimateCBT(recipients, tokens) { @@ -138,14 +153,29 @@ if (recipients.length != tokens.length) { errorMessage = 'Invalid recipient/token IDs provided; please review'; return; } await estimateCBT(recipients, tokens); await estimateSTF(recipients, tokens); - // let gasPrice = await $web3.eth.getGasPrice(); - let gasPrice = 20000000000; - let gasCostEth = await $web3.utils.fromWei((gasPrice * gasLimit).toString()); + let gasPrice = await $web3.eth.getGasPrice(); + // let gasPrice = 50000000000; + let gasPriceGwei = await $web3.utils.fromWei(gasPrice.toString(), 'gwei'); + let gasCostWei = gasPrice * gasLimit; + let gasCostEth = await $web3.utils.fromWei(gasCostWei.toString()); let feeWei = await $contracts.sendit.methods.usageFee().call(); let totalFeeWei = feeWei * recipients.length; + let totalFeeEth = $web3.utils.fromWei(totalFeeWei.toString()); let si_gasCostWei = gasPrice * si_gasLimit + totalFeeWei; let si_gasCostEth = await $web3.utils.fromWei(si_gasCostWei.toString()); - gasCalculation = `Transferring each token individual would require ${gasLimit} gas (${gasCostEth} Ξ). SendIt can do it for ${si_gasLimit} gas + a fee (${si_gasCostEth} Ξ).`; + let diffWei = gasCostWei - si_gasCostWei; + let diffEth = await $web3.utils.fromWei(diffWei.toString()); + let diffPerc = 100 - ((si_gasCostWei / gasCostWei) * 100); + gasCalculation.push(`Current network gas price is ~${gasPriceGwei} gwei.`); + gasCalculation.push(`Transferring ${recipients.length} tokens individually would cost ~${gasCostEth} Ξ (${gasLimit} gas).`); + gasCalculation.push(`SendIt can bulk transfer ${recipients.length} tokens for ${si_gasCostEth} Ξ (${si_gasLimit} gas + a small fee of ${totalFeeEth} Ξ).`); + if (diffPerc < 0) { + gasCalculation.push(`That is an additional cost of ${diffEth * -1} Ξ to transfer in one go to save you the time`); + } else { + gasCalculation.push(`That is a savings of ${diffEth} Ξ (saved ~${Math.round(diffPerc)}%)`); + } + + gasCalculation = gasCalculation; // trigger recheck } @@ -155,13 +185,13 @@ {#if $selectedAccount}
-
+
-
+
- {#each tokenStandards as s}
- +
{#if checked} {:else} - + {/if} {#if !contractApproved} @@ -185,23 +217,29 @@ {/if} -{#if gasCalculation} -

{gasCalculation}

-{/if} +
    + {#each gasCalculation as m, i} + {#if i == 3} +
  • {m}
  • + {:else} +
  • {m}
  • + {/if} + {/each} +
{#if errorMessage} -

{errorMessage}

+

{errorMessage}

{/if} {#if errorMessage} -

{successMessage}

+

{successMessage}

{/if} \ No newline at end of file