1
1
2
- // https://ethereum.stackexchange.com/questions/42929/send-payment-from-wallet-using-web3
3
- // https://web3js.readthedocs.io/en/v1.2.7/web3-utils.html#tohex
4
-
5
2
6
3
const fs = require ( 'fs-sync' )
7
4
const path = require ( 'path' )
8
5
const express = require ( 'express' )
9
- const axios = require ( 'axios' )
10
6
const Web3 = require ( 'web3' ) ;
11
7
const Tx = require ( 'ethereumjs-tx' ) . Transaction ;
12
8
const http = require ( 'http' )
@@ -17,7 +13,6 @@ const configFileId = path.join(path.resolve(''), './web3-server/.env.json')
17
13
const config = fs . readJSON ( configFileId )
18
14
19
15
const web3 = new Web3 ( new Web3 . providers . HttpProvider ( `https://mainnet.infura.io/v3/${ config . infuraProjectId } ` ) ) ;
20
- // let web3 = new Web3(Web3.givenProvider)
21
16
22
17
executeMasterplan ( )
23
18
. then ( ( result ) => {
@@ -35,18 +30,8 @@ async function executeMasterplan() {
35
30
function defineRoutes ( app ) {
36
31
37
32
app . get ( '/sendSignedTransaction' , async ( req , res ) => {
38
- // var privateKey = Buffer.from('e331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109', 'hex');
39
33
var privateKey = Buffer . from ( req . query . privateKey , 'hex' ) ;
40
34
41
- // var rawTx = {
42
- // nonce: '0x00',
43
- // gasPrice: '0x09184e72a000',
44
- // gasLimit: '0x2710',
45
- // to: '0x0000000000000000000000000000000000000000',
46
- // value: '0x00',
47
- // data: '0x7f7465737432000000000000000000000000000000000000000000000000000000600057'
48
- // }
49
-
50
35
var nonce = await web3 . eth . getTransactionCount ( req . query . fromAddress ) ;
51
36
console . log ( `\n\nnonce: ${ nonce } ` )
52
37
@@ -59,31 +44,21 @@ function defineRoutes(app) {
59
44
const myAmount = web3 . utils . toHex ( req . query . amountToBeSent )
60
45
console . log ( `\n\nmyAmount: ${ myAmount } ` )
61
46
62
- // console.log(amountToBeSent)
63
-
64
- // const rawTransaction = {
65
- // "from": fromAddress,
66
- // "nonce": web3.utils.toHex(nonce),
67
47
var rawTx = {
68
48
nonce,
69
49
gasPrice : myGasPrice ,
70
50
gasLimit : myGasLimit ,
71
51
fromAddress : req . query . fromAddress ,
72
52
to : req . query . toAddress ,
73
53
value : myAmount ,
74
- // data: '0x7f7465737432000000000000000000000000000000000000000000000000000000600057'
75
54
}
76
55
77
- // var tx = new Tx(rawTx, { 'chain': 'mainnet' });
78
56
var tx = new Tx ( rawTx ) ;
79
57
80
58
tx . sign ( privateKey ) ;
81
59
82
60
var serializedTx = tx . serialize ( ) ;
83
61
84
- console . log ( serializedTx . toString ( 'hex' ) ) ;
85
- // 0xf889808609184e72a00082271094000000000000000000000000000000000000000080a47f74657374320000000000000000000000000000000000000000000000000000006000571ca08a8bbf888cfa37bbf0bb965423625641fc956967b81d12e23709cead01446075a01ce999b56a8a88504be365442ea61239198e23d1fce7d00fcfc5cd3b44b7215f
86
-
87
62
web3 . eth . sendSignedTransaction ( '0x' + serializedTx . toString ( 'hex' ) )
88
63
. on ( 'receipt' , console . log ) ;
89
64
@@ -117,45 +92,3 @@ function startListening(app) {
117
92
console . log ( `listening on : http://localhost:${ config . httpPort } ` )
118
93
}
119
94
}
120
-
121
-
122
- async function transfer ( fromAddress , toAddress , amountToBeSent , privateKey , chainId = 4 ) {
123
-
124
- console . log ( `preparing transaction with \n${ fromAddress } \n${ toAddress } \n${ amountToBeSent } \n${ privateKey } \n${ chainId } ` )
125
-
126
- const EthereumTx = require ( 'ethereumjs-tx' ) . Transaction
127
- const privateKeyBuffer = Buffer . from ( privateKey , 'hex' )
128
-
129
- var nonce = web3 . eth . getTransactionCount ( fromAddress ) ;
130
-
131
- const myGasPrice = ( await web3 . eth . getGasPrice ( ) ) * 10
132
-
133
- console . log ( amountToBeSent )
134
-
135
- const rawTransaction = {
136
- "from" : fromAddress ,
137
- "nonce" : web3 . utils . toHex ( nonce ) ,
138
- "gasPrice" : web3 . utils . toHex ( myGasPrice ) ,
139
- "gasLimit" : web3 . utils . toHex ( 33000 ) ,
140
- // "gasLimit": '0x2710',
141
- "to" : toAddress ,
142
- "value" : amountToBeSent ,
143
- // "chainId": chainId //remember to change this
144
- } ;
145
-
146
- const tx = new EthereumTx ( rawTransaction )
147
- tx . sign ( privateKeyBuffer )
148
-
149
- const serializedTx = tx . serialize ( )
150
-
151
- console . log ( `this is getting serious :)` )
152
-
153
- web3 . eth . sendTransaction ( '0x' + serializedTx . toString ( 'hex' ) , function ( err , hash ) {
154
- if ( ! err ) {
155
- console . log ( 'Txn Sent and hash is ' + hash ) ;
156
- }
157
- else {
158
- console . error ( err ) ;
159
- }
160
- } ) ;
161
- }
0 commit comments