From 38563d7b694450b018d1b87e314bbbab7bd1d770 Mon Sep 17 00:00:00 2001 From: lza_menace Date: Sat, 19 Nov 2022 01:08:08 -0800 Subject: [PATCH] fix gas settings --- src/Contracts.svelte | 157 +++++++++++++++++++++++++------------------ 1 file changed, 90 insertions(+), 67 deletions(-) diff --git a/src/Contracts.svelte b/src/Contracts.svelte index 4a743ca..e5b6d95 100644 --- a/src/Contracts.svelte +++ b/src/Contracts.svelte @@ -14,14 +14,17 @@ let checksPending = false; let gasCalculation = []; let selectedStandard = 1; + let gasPrice = 0; + let gasLimit = 0; + let si_gasLimit = 0; + let tokenIds = []; + let recipients = []; + let isERC1155 = false; let tokenStandards = [ { id: 1, text: 'ERC-721' }, { id: 2, text: 'ERC-1155' } ] - let gasLimit = 0; - let si_gasLimit = 0; - evm.attachContract('shipit', shipit, ShipIt.abi); function clearMessages() { @@ -44,9 +47,9 @@ } const performCheck = async () => { - let tokenIds = []; - let recipients = []; - let isERC1155 = false; + tokenIds = []; + recipients = []; + isERC1155 = false; checksPending = true; errorMessage = ''; @@ -129,19 +132,19 @@ } // Show gas consumption forecasts - await estimateGas(recipients, tokenIds, isERC1155); + await estimateGas(); checked = true; window.scrollTo(0, document.body.scrollHeight); } - function deriveTokenTotals(recipients, tokens) { + function deriveTokenTotals() { const counts = {}; let new_addr = []; let new_token = []; let new_amount = []; for (var i = 0; i < recipients.length; i++) { - let x = recipients[i] + "_" + tokens[i]; + let x = recipients[i] + "_" + tokenIds[i]; counts[x] = (counts[x] || 0) + 1; } for (const [key, value] of Object.entries(counts)) { @@ -152,45 +155,66 @@ return {new_addr, new_token, new_amount} } - async function estimateCBT(recipients, tokens, isERC1155) { + async function estimateCBT() { si_gasLimit = 0; let fee = await $contracts.shipit.methods.usageFee().call(); if (isERC1155) { - let {new_addr, new_token, new_amount} = deriveTokenTotals(recipients, tokens); + let {new_addr, new_token, new_amount} = deriveTokenTotals(); await $contracts.shipit.methods.erc1155BulkTransfer(contractAddress, new_addr, new_token, new_amount).estimateGas({from: $selectedAccount, value: fee * recipients.length}, function(err, gas){ si_gasLimit += gas; }); } else { - await $contracts.shipit.methods.erc721BulkTransfer(contractAddress, recipients, tokens).estimateGas({from: $selectedAccount, value: fee * recipients.length}, function(err, gas){ + await $contracts.shipit.methods.erc721BulkTransfer(contractAddress, recipients, tokenIds).estimateGas({from: $selectedAccount, value: fee * recipients.length}, function(err, gas){ si_gasLimit += gas; }); } } - async function estimateSTF(recipients, tokens, isERC1155) { + async function executeTransfer() { + console.log('executing transfer') + let fee = await $contracts.shipit.methods.usageFee().call(); + if (isERC1155) { + let {new_addr, new_token, new_amount} = deriveTokenTotals(); + await $contracts.shipit.methods.erc1155BulkTransfer(contractAddress, new_addr, new_token, new_amount).send({ + from: $selectedAccount, + value: fee * recipients.length, + gasPrice: gasPrice, + gas: si_gasLimit + }); + } else { + await $contracts.shipit.methods.erc721BulkTransfer(contractAddress, recipients, tokenIds).send({ + from: $selectedAccount, + value: fee * recipients.length, + gasPrice: gasPrice, + gas: si_gasLimit + }); + } + } + + async function estimateSTF() { gasLimit = 0; if (isERC1155) { - let {new_addr, new_token, new_amount} = deriveTokenTotals(recipients, tokens); + let {new_addr, new_token, new_amount} = deriveTokenTotals(); for (let i = 0; i < new_addr.length; i++) { - await $contracts.nft.methods.safeTransferFrom($selectedAccount, new_addr[i], new_token[i], new_amount[i], 0x0).estimateGas({from: $selectedAccount}, function(err, gas){ + await $contracts.nft.methods.safeTransferFrom($selectedAccount, new_addr[i], new_token[i], new_amount[i], 0x0).estimateGas({from: $selectedAccount, gasPrice: gasPrice}, function(err, gas){ gasLimit += gas; }); } } else { for (let i = 0; i < recipients.length; i++) { - await $contracts.nft.methods.safeTransferFrom($selectedAccount, recipients[i], tokens[i]).estimateGas({from: $selectedAccount}, function(err, gas){ + await $contracts.nft.methods.safeTransferFrom($selectedAccount, recipients[i], tokenIds[i]).estimateGas({from: $selectedAccount, gasPrice: gasPrice}, function(err, gas){ gasLimit += gas; }); } } } - async function estimateGas(recipients, tokens, isERC1155) { - if (recipients.length != tokens.length) { errorMessage = 'Invalid recipient/token IDs provided; please review'; return; } - await estimateCBT(recipients, tokens, isERC1155); - await estimateSTF(recipients, tokens, isERC1155); - let gasPrice = await $web3.eth.getGasPrice(); - gasPrice = 15000000000; // override for testing + async function estimateGas() { + if (recipients.length != tokenIds.length) { errorMessage = 'Invalid recipient/token IDs provided; please review'; return; } + gasPrice = await $web3.eth.getGasPrice(); + // gasPrice = 15000000000; // override for testing + await estimateCBT(); + await estimateSTF(); let gasPriceGwei = await $web3.utils.fromWei(gasPrice.toString(), 'gwei'); let gasCostWei = gasPrice * gasLimit; let gasCostEth = await $web3.utils.fromWei(gasCostWei.toString()); @@ -218,58 +242,57 @@ {#if $selectedAccount} -
-
-
- - -
-
- - + +
+
+ + +
+
+ + +
-
- - -
- {#if checked} - - {:else} - - {/if} - {#if !contractApproved} - - {/if} - -{/if} - -
    - {#each gasCalculation as m, i} - {#if i == 3} -
  • {m}
  • +
    + {#if checked} + {:else} -
  • {m}
  • + {/if} - {/each} -
+ {#if !contractApproved} + + {/if} + -{#if errorMessage} -

{errorMessage}

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

{successMessage}

-{/if} + {#if errorMessage} +

{errorMessage}

+ {/if} + {#if errorMessage} +

{successMessage}

+ {/if} +{/if}