Schätzung der Gaskosten der Funktion mit mehreren Eingaben

Ich habe den Vertrag

pragma solidity ^0.4.11;

contract UserBasic {
    struct Record {
        bytes32 _id;
        address _addedBy;
        uint _dateAdded;
        bytes32 _transactionHash;
        bytes32 _type;
        bytes32 _hash;
        bytes32 _signature;
    }
    // Type to records array
    mapping(bytes32 => bytes32[]) typeRecords;
    // Record ID to record
    mapping(bytes32 => Record) idRecord;
    // Search for record
    function searchRecord(bytes32 _id) constant returns (bytes32, address, uint, bytes32, bytes32, bytes32, bytes32) {
        Record storage temp = idRecord[_id];
        return (temp._id, temp._addedBy, temp._dateAdded, temp._transactionHash, temp._type, temp._hash, temp._signature);
    }
    // Add a record
    function addRecord(bytes32 _type, bytes32 _id) {
        typeRecords[_type].push(_id);
        var _new = Record(_id, tx.origin, now, "", _type, "", "");
        idRecord[_id] = _new;
    }
    // Get all records of a given type
    function getRecordsByType(bytes32 _type) constant returns(bytes32[]) {
        return typeRecords[_type];
    }
}

Meine addRecordMethode funktioniert nicht ( ich denke wegen der Benzinkosten ). Ich wollte schätzen, wie viel Benzin diese Methode kostet, bin mir aber nicht sicher, wie. Diese Antwort erklärt einen Weg für eine Funktion mit einem Parameter, aber es scheint ein zu komplexer Prozess zu sein, ich habe mich gefragt, ob es einen einfacheren Weg gibt.

Ich rufe es vom Knoten mit auf

function addRecord(publicAddress, contractAddress, _type, _id) {
    const contract = contractInstance("UserBasic", contractAddress);
    // Interaction with the contract
    contract.addRecord(_type, _id, {from: publicAddress}, (err, res) => {
        // Log transaction to explore
        if (err) {
            console.log(err);
        } else {
            console.log('tx: ' + res);
            helpers.addTransaction(publicAddress, res);
        }
    });
}

Ich habe die Dokumentation gelesen , aber der einzige Teil, den ich nicht weiß, ist, wie man mehrere Parameter in data.

Antworten (2)

Wenn Sie die Vertragsadresse und abi haben, können Sie den Methodenaufruf direkt abschätzen

const Web3 = require('web3');

const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

const contractAbi = [.....] ;// Contract abi goes here
const contractAddress = "0x....."; // Contract address goes here

const contract = new web3.eth.Contract(contractAbi, contractAddress);


// Estimate gas for:
//     contractInstance.methods.MethodToCalll(param1, param2, param3);
const gas = contract.methods.MethodToCall.estimateGas(param1, param2, param3);

console.log(`Gas estimated: ${gas}`);

Wird so etwas wie ausgeben

Gasschätzung: 26818

Data param ist eine Signatur der Methode. In Geth können Sie den folgenden Befehl ausführen, um die Signatur der addRecord(bytes32,bytes32)Funktion zu finden:

> web3.sha3("addRecord(bytes32,bytes32)")
"0x3193195597c87745d51912ca6cbef014b04b9d925f94235ee2ed5c1819afb2cb"

Und nehmen Sie die ersten 4 Bytes, um den Datenparameter zu erstellen:

0x319319550000000000000000000000000000000000000000000000000000000000000001

Jetzt haben Sie einen Datenparameter. Hoffe es hilft~