目的:

此次调研为了解清楚:通过estimateGas能否预先判断合约转账失败与否,以此作为技术实现,优化转账造成的失败比率。

思路:

通过调用estimateGas来估算不同场景下的合约代币转账Gas,记录报错异常,通过对异常结果分析总结,得出结论。

实现记录:

在MacOs中部署Nodejs环境,安装完node、npm后安装web3的库,npm安装web3库会出现较多问题,为避免环境带来影响,这里安装以下库:

npm install web3@^0.20.1

npm install ethereumjs-tx@^1.3.7

如:通过estimateGas估算Eth转账Gas代码:

  1. web3.eth.estimateGas({  
    
        to: "0xc4abd0339eb8d57087278718986382264244252f",   
    
        data: "0xc6888fa10000000000000000000000000000000000000000000000000000000000000003"  
    
    },function (err, res){  
    
        if(!err){  
    
        console.log(res);  
    
    }else{  
    
        console.log(err);  
    
    }  
    
    })  
    
     


如:通过estimateGas估算合约转账Gas代码:

  1. let Web3 = require("web3");  
    
    let Tx = require("ethereumjs-tx");  
    
    //合约abi  
    
    let abi = [AbiArray]  
    
    //合约地址  
    
    let contractAddr = "0x43317CC.........2D9E085AA";  
    
    //创建web3对象  
    
    let web3 = new Web3;  
    
    let fromAddress = "0x52bb.......38A17698602";  
    
    let toAddress = "0xe123E96.......E18c456E9954B";  
    
    //合约对象  
    
    let MyContract = web3.eth.contract(abi);  
    
    let contractInstance = MyContract.at(contractAddr);  
    
    //default address  
    
    web3.eth.defaultAccount = '0x52bb1......E4e38A17698602';  
    
    console.log(web3.eth.defaultAccount);   
    
    //连接到ropsten  
    
    let INFURA_api = "https://mainnet.infura.io/v3/46a1c998adc.....f5aa7c6ebd";  
    
    web3.setProvider(new Web3.providers.HttpProvider(INFURA_api));  
    
    //调用合约函数blanceOf  
    
    let result = contractInstance.balanceOf(fromAddress);  
    
    console.log(result)  
    
    //估算Gas  
    
    let rgas = contractInstance.transfer.estimateGas(toAddress,0,{from: fromAddress});  
    
    console.log(rgas)  
    

调研流程:

****


附:Web3调用合约转账Code

let Web3 = require("web3");  

let Tx = require("ethereumjs-tx");  

  

//合约abi  

let abi = [AbiArray]  

//合约地址  

let contractAddr = "0xC12D1c73e.......DDFA38907E";  

//创建web3对象  

let web3 = new Web3;  

let fromAddress = "0x5bBBc..........A0541CcFb467";  

let toAddress = "0x5891........15a15f1cc6";  

let count = web3.eth.getTransactionCount(fromAddress);  

let gasPrice = web3.eth.gasPrice;  

let gasLimit = 90000;  

//合约对象  

let MyContract = web3.eth.contract(abi);  

let contractInstance = MyContract.at(contractAddr);  

//default address  

web3.eth.defaultAccount = '0x52bb1e..........38A17698602';  

console.log(web3.eth.defaultAccount);   

//连接到ropsten  

let INFURA_api = "https://mainnet.infura.io/v3/46a1c998a.....5aa7c6ebd";  

web3.setProvider(new Web3.providers.HttpProvider(INFURA_api));  

//调用合约函数  

let result = contractInstance.balanceOf(fromAddress);  

console.log(result)  

//估算Gas  

let rgas = contractInstance.transfer.estimateGas(toAddress,0,{from: fromAddress});  

console.log(rgas)  

  

let data = contractInstance.transfer.getData(toAddress,0,{from: fromAddress});  

var rawTransaction = {  

    "from": fromAddress,  

    "nonce": web3.toHex(count),  

    "gasPrice": web3.toHex(gasPrice),  

    "gasLimit": web3.toHex(gasLimit),  

    "to": contractAddr,  

    "value": "0x0",  

    "data": data,  

};  

//私钥  

let priv_key = new Buffer.from("23BC.........08C4", "hex");  

let tx = new Tx(rawTransaction);  

//用私钥签名交易信息  

tx.sign(priv_key);  

let serializedTx = tx.serialize();  

// 发送交易  

web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'),  

    function(err, hash) {  

        if (!err)  

            console.log(hash);  

        else  

            console.log(err);  

});  








个性签名:Make it
收藏  
参考:https://www.cnblogs.com/KRDecad3/p/11745568.html
Make it