fix gas settings

enumerate
lza_menace 2 years ago
parent 41e78e6b46
commit 38563d7b69

@ -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,7 +242,7 @@
{#if $selectedAccount}
<form>
<form>
<div class="row">
<div class="eight columns">
<label for="contractAddress">Contract Address</label>
@ -240,7 +264,7 @@
0x653D2d1D10c79017b2eA5F5a6F02D9Ab6e725395,1775" id="recipientInfo" on:change={clearMessages}></textarea>
<br />
{#if checked}
<input class="button-primary" type="submit" value="Transfer">
<input class="button-primary" type="submit" value="Transfer" on:click|preventDefault={executeTransfer}>
{:else}
<button class="button" disabled={checksPending} on:click|preventDefault={performCheck}>
{#if checksPending}checking...{:else}Check{/if}
@ -249,10 +273,9 @@
{#if !contractApproved}
<button class="button-primary" on:click|preventDefault={approveShipIt}>Approve</button>
{/if}
</form>
{/if}
</form>
<ul>
<ul>
{#each gasCalculation as m, i}
{#if i == 3}
<li class="successMessage">{m}</li>
@ -260,16 +283,16 @@
<li>{m}</li>
{/if}
{/each}
</ul>
</ul>
{#if errorMessage}
<p class="errorMessage">{errorMessage}</p>
{/if}
{#if errorMessage}
<p class="errorMessage">{errorMessage}</p>
{/if}
{#if errorMessage}
<p class="successMessage">{successMessage}</p>
{#if errorMessage}
<p class="successMessage">{successMessage}</p>
{/if}
{/if}
<style>
.errorMessage {
color: red;

Loading…
Cancel
Save