工作日志 2019-01-10
1. 工作计划
- 封装函数
- 修改bug
2. 遇到问题
- web3连接repson测试网络
- 思路错误
3. 编写代码
const Web3 = require("web3");
const solc = require('solc');
const fs = require("fs");
const Koa = require('koa');
const router = require('koa-router')();
const bodyParser = require('koa-bodyparser');
const render = require('koa-art-template');
const path = require('path');
const views = require('koa-views');
const HDWalletProvider = require('truffle-hdwallet-provider');
const walletConfig = require('./walletConfig.json');
const Tx = require('ethereumjs-tx');
const app = new Koa();
var web3 = new Web3();
var Contract_Token;
var Actions_data = {
Type_init:()=>{
}
}
var Actions_Koa = {
render: () => {
render(app, {
root: path.join(__dirname, '../views'),
extname: '.html',
debug: process.env.NODE_ENV !== 'production'
})
},
user: () => {
app.use(views('../views', {
extension: 'ejs'
}));
app.use(bodyParser());
app.use(router.routes());
app.use(router.allowedMethods());
app.use(bodyParser());
app.listen(3003, () => {
console.log("start at port 3003");
});
}
}
var Actions_Router = {
router_get: () => {
router.get('/', (ctx, next) => {
ctx.body = "测试路由111";
});
router.get('/Token/T_transferFrom', (ctx, next) => {
ctx.body = "T_transferFrom";
console.log('ctx=>11112222');
let sFrom = Json_list.ADDRESS_TOKEN;
console.log("sFrom=>",sFrom);
let sTo = Json_list.USE_ADDRESS.User_2;
console.log("sTo=>",sTo);
let sValue =7;
console.log("sValue=>",sValue);
let result = Actions_Contrant_Token.T_transferFrom({
from: sFrom,
to: sTo,
value:sValue
});
return result;
});
router.get('/Token/T_transfer', (ctx, next) => {
ctx.body = "T_transfer";
});
router.get('/Token/T_approve', (ctx, next) => {
ctx.body = "T_approve";
});
router.get('/Token/T_allowance', (ctx, next) => {
ctx.body = "T_allowance";
});
router.get('/Drop/D_setToken', (ctx, next) => {
ctx.body = "D_setToken";
});
router.get('/Drop/D_multiSendandself', (ctx, next) => {
ctx.body = "D_multiSendandself";
});
router.get('/Drop/D_multiSend', (ctx, next) => {
ctx.body = "D_multiSend";
});
router.get('/Drop/D_multiSend2', (ctx, next) => {
ctx.body = "D_multiSend2";
});
router.get('/Drop/D_multiself', (ctx, next) => {
ctx.body = "D_multiself";
});
router.get('/Drop/D_settrustOwner', (ctx, next) => {
ctx.body = "D_settrustOwner";
});
router.get('/Drop/D_seterctypeName', (ctx, next) => {
ctx.body = "D_seterctypeName";
});
router.get('/Drop/D_sethistoricalOwner', (ctx, next) => {
ctx.body = "D_sethistoricalOwner";
});
router.get('/Drop/D_transfer', (ctx, next) => {
ctx.body = "D_transfer";
});
router.get('/Drop/D_approve', (ctx, next) => {
ctx.body = "D_approve";
});
router.get('/Drop/D_transferFrom', (ctx, next) => {
ctx.body = "D_transferFrom";
});
router.get('/Drop/D_balanceOf', (ctx, next) => {
ctx.body = "D_balanceOf";
});
router.get('/Drop/M_prepare', (ctx, next) => {
ctx.body = "M_prepare";
});
router.get('/Drop/M_flyDrop', (ctx, next) => {
ctx.body = "M_flyDrop";
});
},
router_post: () => {
router.post('/', (ctx, next) => {
ctx.body = "测试路由111";
});
router.post('/Token/T_transferFrom', (ctx, next) => {
ctx.body = "T_transferFrom";
});
router.post('/Token/T_transfer', (ctx, next) => {
ctx.body = "T_transfer";
});
router.post('/Token/T_approve', (ctx, next) => {
ctx.body = "T_approve";
});
router.post('/Token/T_allowance', (ctx, next) => {
ctx.body = "T_allowance";
});
router.post('/Drop/D_setToken', (ctx, next) => {
ctx.body = "D_setToken";
});
router.post('/Drop/D_multiSendandself', (ctx, next) => {
ctx.body = "D_multiSendandself";
});
router.post('/Drop/D_multiSend', (ctx, next) => {
ctx.body = "D_multiSend";
});
router.post('/Drop/D_multiSend2', (ctx, next) => {
ctx.body = "D_multiSend2";
});
router.post('/Drop/D_multiself', (ctx, next) => {
ctx.body = "D_multiself";
});
router.post('/Drop/D_settrustOwner', (ctx, next) => {
ctx.body = "D_settrustOwner";
});
router.post('/Drop/D_seterctypeName', (ctx, next) => {
ctx.body = "D_seterctypeName";
});
router.post('/Drop/D_sethistoricalOwner', (ctx, next) => {
ctx.body = "D_sethistoricalOwner";
});
router.post('/Drop/D_transfer', (ctx, next) => {
ctx.body = "D_transfer";
});
router.post('/Drop/D_approve', (ctx, next) => {
ctx.body = "D_approve";
});
router.post('/Drop/D_transferFrom', (ctx, next) => {
ctx.body = "D_transferFrom";
});
router.post('/Drop/D_balanceOf', (ctx, next) => {
ctx.body = "D_balanceOf";
});
router.post('/Drop/M_prepare', (ctx, next) => {
ctx.body = "M_prepare";
});
router.post('/Drop/M_flyDrop', (ctx, next) => {
ctx.body = "M_flyDrop";
});
}
}
var Actions_initWeb3Provider = {
initWeb3: () => {
if (typeof web3 == 'undefined') {
web3 = new Web3(web3.currentProvider);
console.log("web undefined");
} else {
web3 = new Web3(new Web3.providers.HttpProvider("https://ropsten.infura.io/v3/ee23e77aa14846d88eb5cad3d59e37f2"));
}
console.log("web3实例化完成=>");
console.log("web3.currentProvider=>",web3.currentProvider);
console.log("web3是否连接成功=>",web3.isConnected());
console.log("默认账户",web3.eth.defaultAccount);
console.log("默认区块",web3.eth.defaultBlock);
web3.eth.defaultAccount='0x38a8DC14edE1DEf9C437bB3647445eEec06fF105';
console.log("默认账户",web3.eth.defaultAccount);
},
initContract_Token: () => {
let Abi_Token = Json_list.ABI_TOKEN;
let Address_Token = Json_list.ADDRESS_TOKEN;
Contract_Token = web3.eth.contract(Abi_Token).at(Address_Token);
console.log("Token合约实例完成=>");
},
initContract_drop: () => {
let Abi_drop = "";
let Address_drop = "";
var Contract_drop = web_TokenMgr.eth.constant(Abi_drop).at(Address_drop);
},
initContract_TokenMgr: () => {
let Abi_TokenMgr = "";
let Address_TokenMgr = "";
var Contract_drop = web3.eth.constant(Abi_TokenMgr).at(Address_TokenMgr);
}
}
var Actions_Web3jsCommonMethod = {
web_api: () => {
},
web_node: () => {
},
web3_isConnected: () => {
},
web3_currentProvider: () => {
}
}
var Actions_Web3jsUtils = {
web_api: () => {
},
web_node: () => {
},
web3_isConnected: () => {
},
web3_estimateGas: (address,data) => {
let estimateGas = web3.eth.estimateGas({
to: address,
data: data
});
estimateGas = this.web3.toHex(estimateGas);
return estimateGas;
},
web3_getNonce: (address) => {
let nonce = web3.eth.getTransactionCount(address);
return nonce;
},
web3_bufferPrivateKey: (value) => {
const privateKey = new Buffer(value,'hex');
return privateKey;
},
web3_currentProvider: () => {
}
}
var Actions_Contrant_Token = {
T_transferFrom: (ctx,next) => {
let Tx_gasPrice =web3.toHex('0x3B9ACA00'*1000);
let Tx_address = ctx.from;
let Tx_to =ctx.to;
let Tx_value =ctx.value;
let Tx_v = "0x00";
console.log("T_transferFrom => ",ctx);
Contract_Token.methods.transfer(Tx_address,Tx_value).send({from:'0x38a8DC14edE1DEf9C437bB3647445eEec06fF105'},(err,data)=>{
if(!err){
console.log("methds____________",data);
}
});
let Tx_estimateGas = web3.toHex(6000000);
console.log("T_transferFrom预估gas是=>",Tx_estimateGas);
let Tx_nonce =web3.eth.getTransactionCount(Tx_address);
let Tx_nonce1= web3.toHex(Tx_nonce+1);
console.log("区块高度是:",Tx_nonce1);
var rawTx = {
nonce:Tx_nonce1,
gasPrice: Tx_gasPrice,
gasLimit: Tx_estimateGas,
to: Tx_address,
value: Tx_v,
data: Tx_data_Token
}
console.log("rawTx=>",rawTx);
let tx = new Tx(rawTx);
const privateKey = Actions_Web3jsUtils.web3_bufferPrivateKey(Json_list.PRIVATEKEY.Token_privateKey);
tx.sign(privateKey);
let serializeTx = tx.serialize();
let result = web3.eth.sendRawTransaction('0x' + serializeTx.toString('hex'),(err,hash)=>{
if(!err){
console.log(hash);
}else{
console.log("err:1111",err);
}
});
console.log("调用交易返回结果=>",result);
return result;
},
T_transfer: (from, value) => {
var transferData = this.contract.transferFrom.getData(from,to,value);
},
T_balanceOf: (from) => {
},
T_approve: (from, value) => {
},
T_allowance: (owner, spender) => {
}
}
var Actions_Contrant_Drop = {
D_setToken: (address) => {
},
D_multiSendandself: (arr_address, arr_values) => {
},
D_multiSend: (arr_address, arr_values) => {
},
D_multiSend2: (arr_address, arr_values, value) => {
},
D_multiself: (owner, value) => {
},
D_settrustOwner: (address, owntext) => {
},
D_seterctypeName: (address, tokenName) => {
},
D_sethistoricalOwner: (address, histext) => {
},
D_transfer: (address, value) => {
},
D_approve: (address, value) => {
},
D_transferFrom: (from, to, value) => {
},
D_totalSupply: () => {
},
D_balanceOf: (address) => {
}
}
var Actions_Contrant_TokenMgr = {
M_prepare: (rand, form, token, value) => {
},
M_flyDrop: (arr_address, arr_values) => {
}
}
var Actions_Configure = {
Abi_Token:{
},
Abi_drop:{
},
Abi_TokenMgr:{
}
}
var Actions_Starting = {
init:()=>{
Actions_initWeb3Provider.initWeb3();
Actions_initWeb3Provider.initContract_Token();
},
test:()=>{
console.log("log........");
},
log:()=>{
console.log("-");
}
}
var Actions = {
init:(data)=>{
Actions_Koa.render();
Actions_Koa.user();
Actions_Router.router_get();
Actions_Router.router_post();
Actions_Starting.init();
console.log(data);
}
}
const Json_list = {
ABI_TOKEN:[
{
"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"
}
],
ADDRESS_TOKEN: '0xaA3A01dBa149B109d5e9090f1ad1f2cEbA1C272a',
ABI_DROP: [
{
"constant": false,
"inputs": [
{
"name": "spender",
"type": "address"
},
{
"name": "value",
"type": "uint256"
}
],
"name": "approve",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_destAddrs",
"type": "address[]"
},
{
"name": "_values",
"type": "uint256[]"
},
{
"name": "_valuesmyself",
"type": "uint256"
}
],
"name": "multiSendandself",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_token",
"type": "address"
}
],
"name": "setToken",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "totalSupply",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "",
"type": "address"
}
],
"name": "historicalOwner",
"outputs": [
{
"name": "",
"type": "string"
}
],
"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": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "",
"type": "address"
}
],
"name": "snList",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "spender",
"type": "address"
},
{
"name": "addedValue",
"type": "uint256"
}
],
"name": "increaseAllowance",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "",
"type": "address"
}
],
"name": "erctypeName",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_owneraddress",
"type": "address"
}
],
"name": "deltrustOwner",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "",
"type": "address"
}
],
"name": "trustOwner",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_destAddrs",
"type": "address[]"
},
{
"name": "_values",
"type": "uint256[]"
}
],
"name": "multiSend2",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_ownaddress",
"type": "address"
},
{
"name": "_owntext",
"type": "string"
}
],
"name": "settrustOwner",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "owner",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "a",
"type": "string"
}
],
"name": "myself",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "pure",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "a",
"type": "uint256"
},
{
"name": "b",
"type": "uint256"
}
],
"name": "add",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "pure",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "owner",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_tokentype",
"type": "address"
},
{
"name": "_tokenName",
"type": "string"
}
],
"name": "seterctypeName",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "spender",
"type": "address"
},
{
"name": "subtractedValue",
"type": "uint256"
}
],
"name": "decreaseAllowance",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "to",
"type": "address"
},
{
"name": "value",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_destAddrs",
"type": "address[]"
},
{
"name": "_values",
"type": "uint256[]"
}
],
"name": "multiSend",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_values",
"type": "uint256"
},
{
"name": "addres_owner",
"type": "address"
}
],
"name": "multiself",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "owner",
"type": "address"
},
{
"name": "spender",
"type": "address"
}
],
"name": "allowance",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_hisaddress",
"type": "address"
},
{
"name": "_histext",
"type": "string"
}
],
"name": "sethistoricalOwner",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"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"
}
],
ADDRESS_DROP: '0x7Dc6Dc91178a6fEF2Fa5Bd1de32c5642e6Ae421b',
ABI_TOKENMGR:[],
ADDRESS_TOKENMGR:"",
PRIVATEKEY:{
Token_privateKey:"F9B224ECF9161EEA3A815338FA70EF11F82AC1C5CAB145D264ADC1E110FA0907",
TEST:"123"
},
USE_ADDRESS:{
User_1:"0x38a8DC14edE1DEf9C437bB3647445eEec06fF105",
User_2:"0xd2580AB2EB3313B0972e9e47b05eE4c15320A6D1",
User_3:"0xA9af645Ce31AF413b24a3b913f1a5Bf57A7a1C50",
User_4:"0x3846a0e5e3a93900B5c0F8BA1504e0B55Ca201e5"
}
};
const Json_Bz ={};
Actions.init("starting........");
4. 明日计划
- 调试bug
- 数据校验
5. 备注
- 今天主要连接测试网络测试