sendSignedTransaction调用菩提合约

web3使用sendSignedTransaction方法(发送交易签名)调用菩提预测项目智能合约

更多区块链技术应用内容分类:

区块链应用    区块链开发

以太坊 | Fabric | BCOS | 密码技术 | 共识算法 | 比特币其他链

通证经济传统金融场景 | 去中心化金融 | 防伪溯源 | 数据共享 | 可信存证

“菩提预测”项目合约测试调用:
var Web3 = require("web3");
var Tx = require('ethereumjs-tx');
 
var provider = new Web3.providers.HttpProvider("http://localhost:8548");
var web3Client = new Web3(provider);
var time = Math.round(new Date().getTime()/1000);//秒
// var time = new Date().getTime();
 
var eventFactoryJson = require("../build/contracts/EventFactory.json");
 
//my address //
var rawTx;
var createAddress = '0x2286f9b57b65e402cad2939576126cabea6b9e2b'
var gasPrice = 0;
var nonce = 0;
 
 
// exe();
testDecode();
 
//使用私钥签名交易并发送向以太坊
async function exe(){
    await web3Client.eth.getTransactionCount(createAddress).then(res=>{
        nonce = res;
    });
    await web3Client.eth.getGasPrice().then(res=>{
        gasPrice = res;
        rawTx = {
            from: createAddress,
            nonce: nonce,  //web3.eth.getTransactionCount(address, 'pending').then()
            gasPrice: '0x4e20',//web3.eth.getGasPrice
            gasLimit: '0x5b8d80',
            to: '0xf286113fdf2a43efb61270433a7e1fbf7070c29b',//contract address
            value: '0x00', //0 for contact,
            data: '' //contract method and parameters
        };
    });
 
    var privatekey = new Buffer('49feb9d5f221253b9d64ed0b54bdcdde6bc3c66ffaf5c4f01141682f54366868','hex');
    console.log("nonce:"+nonce);
 
    var funcABI = eventFactoryJson['abi'].filter(per=>{ //返回数组
        return per.name == 'createStandardEvent'
    });
 
    var topic = ["Who will be the next president i", "n the 2020 election?","","","","","","","",""];
 
    topic.forEach(function (res,index) {
        topic[index] =  web3Client.eth.abi.encodeParameter('bytes32', web3Client.utils.asciiToHex(res));
    });
    console.log(topic);
 
    var select = ["Trump", "The Rock", "Hilary","","","","","","",""];
    select.forEach(function (res,index) {
        select[index] =  web3Client.eth.abi.encodeParameter('bytes32', web3Client.utils.asciiToHex(res));
    });
    console.log(select);
    var encodefunc = web3Client.eth.abi.encodeFunctionCall(funcABI[0],
        [
            '0xe1b200a224db7ab6583647072681477ae4a2b229',
            topic,// [byte32,byte32...]要够10个=>byte32[10]
            select,
            web3Client.eth.abi.encodeParameter('uint256', time),
            web3Client.eth.abi.encodeParameter('uint256', time+ 15),
            web3Client.eth.abi.encodeParameter('uint256', time+ 16),
            web3Client.eth.abi.encodeParameter('uint256', time+ 300)
        ]);
 
    rawTx.data = encodefunc;
    console.log('rawTx.data:'+rawTx.data);
 
    var tx = new Tx(rawTx);
    console.log("tx1---"+JSON.stringify(tx));
    tx.sign(privatekey);
    console.log("tx2---"+JSON.stringify(tx));
    var serializedTx = tx.serialize();
    console.log("---------------------");
    console.log("sendData---"+'0x' + serializedTx.toString('hex'));
    web3Client.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
        .on('receipt', console.log);
 
}
//拿到交易的input后解码方法
function testDecode() {
    var inputData = '0xf6c1dab6000000000000000000000000e1b200a224db7ab6583647072681477ae4a2b22957686f2077696c6c20626520746865206e65787420707265736964656e7420696e20746865203230323020656c656374696f6e3f000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005472756d7000000000000000000000000000000000000000000000000000000054686520526f636b00000000000000000000000000000000000000000000000048696c61727900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005b7d5364000000000000000000000000000000000000000000000000000000005b7d5373000000000000000000000000000000000000000000000000000000005b7d5374000000000000000000000000000000000000000000000000000000005b7d5490';
    var abi = eventFactoryJson['abi'];
    var checkTypes = [
        { name: '_oracle', type: 'address' },
        { name: '_name', type: 'bytes32[10]' },
        { name: '_resultNames', type: 'bytes32[10]' },
        { name: '_bettingStartTime', type: 'uint256' },
        { name: '_bettingEndTime', type: 'uint256' },
        { name: '_resultSettingStartTime', type: 'uint256' },
        { name: '_resultSettingEndTime', type: 'uint256' } ];
    // var checkTypes = ['address' ,
    //     'bytes32[10]',
    //     'bytes32[10]',
    //     'uint256',
    //     'uint256',
    //     'uint256',
    //     'uint256' ];
 
    for (let i = 0; i < abi.length; i++) {
        var fsign = web3Client.eth.abi.encodeFunctionSignature(abi[i]);
        abi[i].signature = fsign;
        console.log(fsign);
    }
    var funcABI = eventFactoryJson['abi'].filter(per =>{ //返回数组
        return per.name == 'createStandardEvent';
    });
    console.log(funcABI[0]);
    var decodeData = web3Client.eth.abi.decodeParameters(checkTypes, inputData.substr(10));//decodeParameters只针对参数,故要去掉方法名
    console.log("------------------------");
    console.log(decodeData);
 
}

原文链接:sendSignedTransaction调用菩提合约

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值