2019-01-09 工作日志:记录web3连接 respon

版权声明:忠于祖国,忠于人民 https://blog.csdn.net/boss2967/article/details/86252131

var Web3 = require(“web3”);
var Tx = require(‘ethereumjs-tx’);
var fs = require(‘fs’);
// 合约地址
var contractAddress = “0xaA3A01dBa149B109d5e9090f1ad1f2cEbA1C272a”;
// 创建web3对象
var web3 = new Web3();
// 连接到 ropsten 测试节点
web3.setProvider(new Web3.providers.HttpProvider(“https://ropsten.infura.io/v3/ee23e7aa14846d88eb5cad3d59e37f2”));
// 通过ABI和地址获取已部署的合约对象
let abi = [
{
“constant”: false,
“inputs”: [
{
“name”: “_spender”,
“type”: “address”
},
{
“name”: “_value”,
“type”: “uint256”
}
],
“name”: “approve”,
“outputs”: [
{
“name”: “success”,
“type”: “bool”
}
],
“payable”: false,
“stateMutability”: “nonpayable”,
“type”: “function”
},
{
“constant”: true,
“inputs”: [],
“name”: “totalSupply”,
“outputs”: [
{
“name”: “”,
“type”: “uint256”
}
],
“payable”: false,
“stateMutability”: “view”,
“type”: “function”
},
{
“constant”: false,
“inputs”: [
{
“name”: “_from”,
“type”: “address”
},
{
“name”: “_to”,
“type”: “address”
},
{
“name”: “_value”,
“type”: “uint256”
}
],
“name”: “transferFrom”,
“outputs”: [
{
“name”: “success”,
“type”: “bool”
}
],
“payable”: false,
“stateMutability”: “nonpayable”,
“type”: “function”
},
{
“constant”: true,
“inputs”: [
{
“name”: “_owner”,
“type”: “address”
}
],
“name”: “balanceOf”,
“outputs”: [
{
“name”: “balance”,
“type”: “uint256”
}
],
“payable”: false,
“stateMutability”: “view”,
“type”: “function”
},
{
“constant”: false,
“inputs”: [
{
“name”: “_to”,
“type”: “address”
},
{
“name”: “_value”,
“type”: “uint256”
}
],
“name”: “transfer”,
“outputs”: [
{
“name”: “success”,
“type”: “bool”
}
],
“payable”: false,
“stateMutability”: “nonpayable”,
“type”: “function”
},
{
“constant”: true,
“inputs”: [
{
“name”: “_owner”,
“type”: “address”
},
{
“name”: “_spender”,
“type”: “address”
}
],
“name”: “allowance”,
“outputs”: [
{
“name”: “remaining”,
“type”: “uint256”
}
],
“payable”: false,
“stateMutability”: “view”,
“type”: “function”
},
{
“anonymous”: false,
“inputs”: [
{
“indexed”: true,
“name”: “_from”,
“type”: “address”
},
{
“indexed”: true,
“name”: “_to”,
“type”: “address”
},
{
“indexed”: false,
“name”: “_value”,
“type”: “uint256”
}
],
“name”: “Transfer”,
“type”: “event”
},
{
“anonymous”: false,
“inputs”: [
{
“indexed”: true,
“name”: “_owner”,
“type”: “address”
},
{
“indexed”: true,
“name”: “_spender”,
“type”: “address”
},
{
“indexed”: false,
“name”: “_value”,
“type”: “uint256”
}
],
“name”: “Approval”,
“type”: “event”
}
];
var coinContract = web3.eth.contract(abi).at(contractAddress);

var fromAddress = “0x38a8DC14edE1DEf9C437bB3647445eEec06fF105”;
var toAddress = “0xd2580AB2EB3313B0972e9e47b05eE4c15320A6D1”;
var count = web3.eth.getTransactionCount(fromAddress);
var gasPrice = web3.eth.gasPrice;
var gasLimit = 90000;

// 查询余额
var tokenBalance = coinContract.balanceOf(fromAddress);
console.log(tokenBalance)

// 转账
var data = coinContract.transfer.getData(toAddress,
1000000000000000000000,
{from: fromAddress});
var rawTransaction = {
“from”: fromAddress,
“nonce”: web3.toHex(count),
“gasPrice”: web3.toHex(gasPrice),
“gasLimit”: web3.toHex(gasLimit),
“to”: contractAddress,
“value”: “0x0”,
“data”: data,
};

//
// 读取私钥,这里不包含‘0x’两个字符
var privKey = new Buffer.from(‘F9B224ECF9161EEA3A815338FA70EF11F82AC1C5CAB145D264ADC1E110FA0907’, ‘hex’);
var tx = new Tx(rawTransaction);

// 用私钥签名交易信息
tx.sign(privKey);
var serializedTx = tx.serialize();

// 发送交易
web3.eth.sendRawTransaction(‘0x’ + serializedTx.toString(‘hex’),
function(err, hash) {
if (!err)
console.log(hash);
else
console.log(err);
});
// contractAddress = ‘0x35790593c3c92b99a8357a9a64749ca5841d6e51’;
// data = coinContract.approve.getData(contractAddress,10000000000000000000000,{from: fromAddress});

猜你喜欢

转载自blog.csdn.net/boss2967/article/details/86252131
今日推荐