Skip to content

Commit 7af4664

Browse files
update UpsertRoom contract
1 parent aa94196 commit 7af4664

File tree

2 files changed

+176
-16
lines changed

2 files changed

+176
-16
lines changed

app/api/wallet.js

+175-15
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,183 @@ const web3 = new Web3();
99
web3.setProvider(new web3.providers.HttpProvider(config.get('geth:host')));
1010
const keythereum = require("keythereum");
1111
const Tx = require('ethereumjs-tx');
12+
const util = require('ethereumjs-util');
13+
1214
const _ = require('lodash');
1315
const SolidityFunction = require('web3/lib/web3/function');
1416
module.exports = (API, redis) => {
15-
let solidityFunction = new SolidityFunction('', _.find(sol_config._abi, {name: 'GetRoomsCount'}), '0x283684994b56861DabBfF1eE012dA9BcabAb6c62');
16-
var hash = web3.sha3("Some string to be hashed");
17-
console.log(hash); // "0xed973b234cf2238052c9ac87072c71bcf33abc1bbd721018e0cca448ef79b379"
17+
API.on('requestFunctionContract', true, (user, param, callback) => {
18+
if (!param.from)return callback && callback(null, {
19+
error: error.api('Request param "from" incorrect', 'param', {
20+
pos: 'api/auth.js(transfer_coin):#1',
21+
param: param
22+
}, 0),
23+
success: false,
24+
25+
});
26+
if (!param.to)return callback && callback(null, {
27+
error: error.api('Request param "to" incorrect', 'param', {
28+
pos: 'api/auth.js(transfer_coin):#2',
29+
param: param
30+
}, 0),
31+
success: false
32+
33+
});
34+
35+
if (!param.amount)return callback && callback(null, {
36+
error: error.api('Request param "amount" incorrect', 'param', {
37+
pos: 'api/auth.js(transfer_coin):#3',
38+
param: param
39+
}, 0),
40+
success: false
41+
});
42+
db.wallets.findOne({
43+
"user": db.mongoose.Types.ObjectId(user._id),
44+
"wallet.address": param.from,
45+
}).then((response_s) => {
46+
if (!response_s) {
47+
callback && callback(null,
48+
{
49+
error: error.api('wallets not found', 'param', {
50+
pos: 'api/auth.js(transfer_coin):#1',
51+
param: param
52+
}, 0),
53+
success: false
54+
});
55+
return false;
56+
}
1857

19-
var hashOfHash = web3.toHex("Some string to be hashed");
20-
console.log(hashOfHash);
21-
API.on('test_smartContract', true, (user, param, callback) => {
58+
db.tx.find({
59+
"wallet": response_s.wallet.address,
60+
"currency": sol_config._symbol,
61+
}).then(function (res) {
62+
let nonce = web3.eth.getTransactionCount(response_s.wallet.address);
63+
// for (let k in res) {
64+
// if (res.hasOwnProperty(k) && nonce < res[k].nonce)
65+
// nonce = res[k].nonce;
66+
// }
67+
let privateKey = new Buffer(response_s.wallet.PrivateKey, 'hex');
68+
let solidityFunction = new SolidityFunction('', _.find(sol_config._abi, {name: 'transfer'}), '');
69+
let payloadData = solidityFunction.toPayload([param.to, (+param.amount * sol_config._contract_fixed)]).data;
70+
let gasPrice = web3.eth.gasPrice;
71+
let gasPriceHex = web3.toHex(21000000000);
72+
let gasLimitHex = web3.toHex(80000);
73+
let nonceHex = web3.toHex(nonce);
74+
let tx = new Tx({
75+
nonce: nonceHex,
76+
gasPrice: gasPriceHex,
77+
gasLimit: gasLimitHex,
78+
to: sol_config._address,
79+
from: response_s.wallet.address,
80+
value: '0x00',
81+
data: payloadData
82+
});
2283

23-
callback && callback(null, {ok: 1});
84+
console.log({
85+
"nonce": "0x02f5",
86+
"gasPrice": "0x04e3b29200",
87+
"gasLimit": "0x013880",
88+
"to": "0x95408930d6323Ac7aa69e6C2CBFE58774D565fa8",
89+
"value": "0x00",
90+
"data": "0xa9059cbb0000000000000000000000007f47c15af9568c04ba44e0d9fceccc67828a8155000000000000000000000000000000000000000000000000000000174876e800",
91+
"chainId": 1
92+
},{
93+
nonce: nonceHex,
94+
gasPrice: gasPriceHex,
95+
gasLimit: gasLimitHex,
96+
to: sol_config._address,
97+
from: response_s.wallet.address,
98+
value: '0x00',
99+
data: payloadData
100+
},nonce);
101+
102+
tx.sign(privateKey);
103+
104+
let serializedTx = tx.serialize();
105+
106+
web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function (err, hash) {
107+
if (err) {
108+
console.log('Error sendRawTransaction:', err);
109+
callback && callback(null,
110+
{
111+
error: error.api('Error send TX', 'web3', err, 0),
112+
success: false
113+
});
114+
}
115+
else {
116+
callback && callback(null,
117+
{
118+
txHash: hash,
119+
success: true
120+
});
121+
new db.tx({
122+
user_id: user.webtransfer_id,
123+
currency: sol_config._symbol,
124+
tx: {
125+
to: param.to,
126+
from: response_s.wallet.address,
127+
hash: hash
128+
},
129+
nonce: +nonce + 1,
130+
wallet: response_s.wallet.address,
131+
callback_url: param.callback_url,
132+
status: 1
133+
}).save();
134+
}
135+
});
136+
}).catch(function (err) {
137+
return callback && callback(null, {
138+
error: error.api(err.message, 'db', err, 5),
139+
success: false
140+
});
141+
142+
});
143+
}).catch(function (err) {
144+
return callback && callback(null, {
145+
error: error.api(err.message, 'db', err, 6),
146+
success: false
147+
});
148+
149+
});
24150
}, {
25-
title: 'Demo function',
26-
description: 'Demo function',
27-
param: [],
28-
response: []
151+
title: 'Call function smart contract',
152+
description: 'Call function smart contract (' + sol_config.sol_config + '):' + sol_config._address,
153+
param: [{
154+
name: 'from',
155+
type: "string",
156+
title: 'address',
157+
necessarily: false,
158+
default: '0x'
159+
}, {
160+
name: 'function',
161+
type: "string",
162+
title: 'name function',
163+
necessarily: false,
164+
default: ''
165+
}, {
166+
name: 'param',
167+
type: "array",
168+
title: 'param function contract',
169+
necessarily: false,
170+
default: '[]'
171+
}, {
172+
name: 'callback_url',
173+
type: "string",
174+
title: 'callback url change status tx',
175+
necessarily: false,
176+
default: 'https://'
177+
}],
178+
response: [
179+
{name: 'success', type: "string", title: 'Success ?', default: 'true, false'},
180+
{
181+
name: 'txHash',
182+
type: "string",
183+
title: 'HASH transaction blockchain',
184+
default: '0x*******'
185+
},
186+
{name: 'error', type: "object", title: '', default: 'ERROR'},
187+
{name: 'latency_ms', type: "int(11)", title: 'Processing time of the request in ms', default: '122'}
188+
]
29189
});
30190
API.on('transfer_coin', (user, param, callback) => {
31191
if (!param.from)return callback && callback(null, {
@@ -184,15 +344,15 @@ module.exports = (API, redis) => {
184344

185345
],
186346
response: [
187-
{name: 'success', type: "string", title: 'Успешно ?', default: 'true, false'},
347+
{name: 'success', type: "string", title: 'Success ?', default: 'true, false'},
188348
{
189349
name: 'txHash',
190350
type: "string",
191-
title: 'хеш транзакции',
351+
title: 'HASH transaction blockchain',
192352
default: '0x*******'
193353
},
194-
{name: 'error', type: "string", title: '', default: 'ERROR account not approve'},
195-
{name: 'latency_ms', type: "int(11)", title: 'Время оброботки запроса в мс', default: '122'}
354+
{name: 'error', type: "object", title: '', default: 'ERROR'},
355+
{name: 'latency_ms', type: "int(11)", title: 'Processing time of the request in ms', default: '122'}
196356
]
197357
});
198358
};

geth_server.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
require('shelljs/global');
3-
var child = exec('~/go-ethereum/build/bin/geth --etherbase "0x89B453ec269D7550B369932a8680e912Ee7550A8" --mine --rpc --rpcaddr "0.0.0.0" --testnet --fast --bootnodes "enode://20c9ad97c081d63397d7b685a412227a40e23c8bdc6688c6f37e97cfbc22d2b4d1db1510d8f61e6a8866ad7f0e17c02b14182d37ea7c3c8b9c2683aeb6b733a1@52.169.14.227:30303,enode://6ce05930c72abc632c58e2e4324f7c7ea478cec0ed4fa2528982cf34483094e9cbc9216e7aa349691242576d552a2a56aaeae426c5303ded677ce455ba1acd9d@13.84.180.240:30303"', {async:true});
3+
var child = exec('~/go-ethereum/build/bin/geth --rpc --rpcaddr "0.0.0.0" --testnet --fast --bootnodes "enode://20c9ad97c081d63397d7b685a412227a40e23c8bdc6688c6f37e97cfbc22d2b4d1db1510d8f61e6a8866ad7f0e17c02b14182d37ea7c3c8b9c2683aeb6b733a1@52.169.14.227:30303,enode://6ce05930c72abc632c58e2e4324f7c7ea478cec0ed4fa2528982cf34483094e9cbc9216e7aa349691242576d552a2a56aaeae426c5303ded677ce455ba1acd9d@13.84.180.240:30303"', {async:true});
44

55
child.stdout.on('data', function(data) {
66
console.log('[Geth] -> ', data);

0 commit comments

Comments
 (0)