forked from bcosorg/bcos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendTransaction.js
192 lines (188 loc) · 5.07 KB
/
sendTransaction.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
var fs = require("fs");
var Web3 = require('web3');
var net = require('net');
var conf = require('./config');
//init web3
var web3 = new Web3();
var client = new net.Socket();
web3.setProvider(new web3.providers.IpcProvider(conf.ipc_path,client));
//sendTransaction
function sendTransaction()
{
var postdata = {
data: params_data,
from: conf.account,
to:conf.contract_addr,
value:params_value,
gas: 1000000
}
//发送交易
web3.eth.sendTransaction(postdata, function(err, address) {
if (!err)
{
logger.debug(tx_id + " | 发送交易成功!|",address);
process.exit();
return;
}
else
{
logger.debug(tx_id + "|发送交易失败!",err);
process.exit();
return;
}
});
}
function callContract()
{
var test_params = '';
//调用合约
var abi_obj = conf.abi_arr;
var args = test_params;
console.log(" |args : " + args);
if( typeof(abi_obj) === 'string' || Object.prototype.toString.call(abi_obj) === '[object String]')
{
abi_obj = JSON.parse(abi_obj);
}
if( typeof(args) === 'string' || Object.prototype.toString.call(args) === '[object String]')
{
args = JSON.parse(args);
}
console.log(typeof(abi_obj) + " | " + Object.prototype.toString.call(abi_obj));
console.log(typeof(args) + " | " + Object.prototype.toString.call(args));
if(typeof(abi_obj) !== 'object' || Object.prototype.toString.call(abi_obj) !== '[object Array]' || typeof(args) !== 'object' || Object.prototype.toString.call(args) !== '[object Array]')
{
console.log("参数格式不合法! abi_obj : " + JSON.stringify(abi_obj) +" | args : " + JSON.stringify(args));
process.exit();
return;
}
else
{
console.log(" |args : " + args + " | " + JSON.stringify(args));
var breaked = false;
var abi_obj_size = abi_obj.length;
abi_obj.forEach(function (obj,index)
{
if(breaked) return;;
console.log("obj : " + JSON.stringify(obj) + " | " + abi_obj_size + " | " + index);
if(obj.constant !== undefined && obj.name !== undefined && obj.name === fun)
{
console.log("call fun : " + obj.name);
try{
// creation of contract object
var MyContract = web3.eth.contract(abi_obj);
// initiate contract for an address
if( conf.contract_addr === '' || conf.contract_addr === undefined)
{
console.log("未传入合约地址!");
process.exit();
return;
}
var myContractInstance = MyContract.at(conf.contract_addr);
var f = myContractInstance[fun];
if(obj.constant)
{
console.log(fun+" is a constant function, we should call it.");
if(typeof(f) === 'function')
{
try
{
var call_param = args;
function call_back(err,ret_data){
console.log("err : " + err + " | ret_data : " + JSON.stringify(ret_data));
if( !err )
{
console.log(f + " result : " + JSON.stringify(ret_data));
process.exit();
return;
}
else
{
console.log(" 调用合约失败");
process.exit();
return;
}
}
call_param.push(call_back);
console.log("f:"+f + " | args : " + JSON.stringify(call_param));
f.apply(myContractInstance, call_param);
}
catch(e)
{
console.log("exception:"+e.message);
process.exit();
return;
}
}
else
{
console.log(f + " 不是合约函数!");
process.exit();
return;
}
}
else
{
console.log(fun+" is not a constant function, we should send a Transaction.");
if(typeof(f) === 'function')
{
try
{
var call_param = args;
var fromparam = {
from:conf.account,
gas:100000000
};
//gas: 1000000000
call_param.push(fromparam);
function call_back(err,ret_data)
{
console.log("err : " + err + " | ret_data : " + JSON.stringify(ret_data));
if( !err )
{
console.log(f + " result : " + JSON.stringify(ret_data));
process.exit();
return;
}
else
{
console.log("调用合约失败!");
process.exit();
return;
}
}
call_param.push(call_back);
console.log("call_param : " + JSON.stringify(call_param));
f.apply(myContractInstance, call_param);
}
catch(e)
{
console.log("调用合约失败! | exception:",e);
process.exit();
return;
}
}
else
{
console.log(f + " 不是合约函数!");
process.exit();
return;
}
}
breaked = true;
}
catch(ex)
{
console.log("exception:",ex);
process.exit();
return;
}
}
if( parseInt(abi_obj_size) === (parseInt(index)+1) && !breaked )
{
console.log("合约未包含该函数!" + fun);
process.exit();
return;
}
});
}
}