1
- ' use strict' ;
1
+ " use strict" ;
2
2
3
3
import Web3 from "web3" ;
4
- import { default as Tx } from "ethereumjs-tx"
4
+ import { default as Tx } from "ethereumjs-tx" ;
5
5
6
6
export default class BlockchainService {
7
7
8
- protected WEB3
9
- protected SCA
10
- protected gasPrice
11
- protected ABI
8
+ protected WEB3 ;
9
+ protected SCA ;
10
+ protected gasPrice ;
11
+ protected ABI ;
12
12
13
- /**
13
+ /**
14
14
* The constructor function is used to initialize the web3 object, gas price, smart contract
15
15
* address, and smart contract ABI
16
16
* @param {string } RPC - The RPC address of the Ethereum node you want to connect to.
17
17
* @param {number } gasPrice - The gas price you want to use for your transactions.
18
18
* @param {string } SCA - The address of the smart contract you want to interact with.
19
19
* @param {any } ABI - The ABI of the contract you want to interact with.
20
20
*/
21
- constructor ( RPC :string , gasPrice :number , SCA :string , ABI :any ) {
22
- this . WEB3 = new Web3 (
23
- new Web3 . providers . HttpProvider ( RPC )
24
- ) ;
21
+ constructor ( RPC :string , gasPrice :number , SCA :string , ABI :any ) {
22
+ this . WEB3 = new Web3 (
23
+ new Web3 . providers . HttpProvider ( RPC )
24
+ ) ;
25
25
26
- this . gasPrice = gasPrice ;
27
- this . SCA = SCA ;
28
- this . ABI = ABI ;
29
- }
26
+ this . gasPrice = gasPrice ;
27
+ this . SCA = SCA ;
28
+ this . ABI = ABI ;
29
+ }
30
30
31
- /**
31
+ /**
32
32
* It takes a function name, parameters, and a from address, and returns a raw transaction object
33
33
* @param [funcName] - The name of the function you want to call
34
34
* @param params - The parameters of the function you want to call.
35
35
* @param [from] - The address of the account that will be sending the transaction.
36
36
* @returns A raw transaction object.
37
37
*/
38
- public async createRaw ( funcName = "" , params , from = "" , value = 0 ) {
38
+ public async createRaw ( funcName = "" , params , from = "" , value = 0 ) {
39
39
40
- let ABI = JSON . parse ( JSON . stringify ( this . ABI ) )
40
+ const ABI = JSON . parse ( JSON . stringify ( this . ABI ) ) ;
41
41
42
- const contractDeployed = new this . WEB3 . eth . Contract (
43
- ABI ,
44
- this . SCA
45
- ) ;
42
+ const contractDeployed = new this . WEB3 . eth . Contract (
43
+ ABI ,
44
+ this . SCA
45
+ ) ;
46
46
47
- const dataFunc = await contractDeployed . methods [ funcName ] (
48
- ...params
49
- ) . encodeABI ( ) ;
47
+ const dataFunc = await contractDeployed . methods [ funcName ] (
48
+ ...params
49
+ ) . encodeABI ( ) ;
50
50
51
- const gasLimit = await contractDeployed . methods [ funcName ] (
52
- ...params
53
- ) . estimateGas ( { from } )
51
+ const gasLimit = await contractDeployed . methods [ funcName ] (
52
+ ...params
53
+ ) . estimateGas ( { from } ) ;
54
54
55
- const nonce = await this . WEB3 . eth . getTransactionCount ( from )
55
+ const nonce = await this . WEB3 . eth . getTransactionCount ( from ) ;
56
56
57
- const rawTx = {
58
- from : from ,
59
- to : this . SCA ,
60
- gasLimit,
61
- gasPrice : this . gasPrice ,
62
- nonce : nonce ,
63
- data : dataFunc ,
64
- value : value
65
- } ;
57
+ const rawTx = {
58
+ from : from ,
59
+ to : this . SCA ,
60
+ gasLimit,
61
+ gasPrice : this . gasPrice ,
62
+ nonce : nonce ,
63
+ data : dataFunc ,
64
+ value : value
65
+ } ;
66
66
67
- return rawTx ;
68
- }
67
+ return rawTx ;
68
+ }
69
69
70
- /**
70
+ /**
71
71
* It takes a raw transaction object, a private key, and a chain ID, and returns a transaction hash
72
72
* and the transaction object
73
73
* @param rawTx - The raw transaction object.
74
74
* @param privateKey - The private key of the account you want to send the transaction from.
75
75
* @param [chainId=97] - The chain ID of the network you're sending to.
76
76
* @returns The transaction hash and the transaction object.
77
77
*/
78
- public async signRaw ( rawTx = { } , privateKey , chainId = 97 ) {
79
-
80
- privateKey = Buffer . from ( privateKey , 'hex' )
81
-
82
- var transaction = new Tx ( rawTx , { chainId : chainId } )
83
-
84
- await transaction . sign ( privateKey )
85
-
86
- let signedTx = '0x' + transaction . serialize ( ) . toString ( 'hex' )
87
-
88
- return signedTx ;
89
- }
78
+ public async signRaw ( rawTx = { } , privateKey , chainId = 97 ) {
79
+
80
+ privateKey = Buffer . from ( privateKey , "hex" ) ;
81
+
82
+ const transaction = new Tx ( rawTx , { chainId : chainId } ) ;
83
+
84
+ await transaction . sign ( privateKey ) ;
85
+
86
+ const signedTx = "0x" + transaction . serialize ( ) . toString ( "hex" ) ;
87
+
88
+ return signedTx ;
89
+ }
90
+
91
+ /**
92
+ * It takes a raw transaction string, and returns an object with the transaction's details
93
+ * @param {string } raw - The raw transaction string
94
+ * @param [chainId=97] - The chainId of the network you're on.
95
+ * @returns The rawTx object is being returned.
96
+ */
97
+ public async decodeRaw ( raw : string ) {
98
+ const tx = new Tx ( raw ) ;
99
+
100
+ const [
101
+ nonce , gasPrice ,
102
+ gasLimit , to ,
103
+ value , data ,
104
+ v , r ,
105
+ s
106
+ ] = tx . toJSON ( ) ;
107
+
108
+ const rawTx = {
109
+ nonce, gasPrice,
110
+ gasLimit, to,
111
+ value, data,
112
+ v, r,
113
+ s,
114
+ chainId : tx . _chainId ,
115
+ from : "0x" + tx . getSenderAddress ( ) . toString ( "hex" ) ,
116
+ txHash : "0x" + tx . hash ( ) . toString ( "hex" )
117
+ } ;
118
+
119
+ return rawTx ;
120
+ }
90
121
91
- /**
122
+ /**
92
123
* It sends a signed transaction to the blockchain.
93
124
* @param {string } signedTx - The signed transaction in hex format.
94
125
* @returns The txHash and the tx object.
95
126
*/
96
- public async sendSignedRaw ( signedTx : string ) {
127
+ public async sendSignedRaw ( signedTx : string ) {
97
128
98
- let txHash = this . WEB3 . utils . keccak256 ( signedTx ) // ALIAS
129
+ const txHash = this . WEB3 . utils . keccak256 ( signedTx ) ; // ALIAS
99
130
100
- const tx = await this . WEB3 . eth . sendSignedTransaction ( signedTx ) ;
131
+ const tx = await this . WEB3 . eth . sendSignedTransaction ( signedTx ) ;
101
132
102
- return { txHash, tx} ;
103
- }
133
+ return { txHash, tx} ;
134
+ }
104
135
105
- /**
136
+ /**
106
137
* It takes in a function name, parameters, and a from address, and returns the data from the
107
138
* function
108
139
* @param [funcName] - The name of the function you want to call.
109
140
* @param params - an array of parameters that the function takes.
110
141
* @param from - The address of the account that will be used to call the function.
111
142
* @returns The return value of the function.
112
143
*/
113
- public async readFunc ( funcName = "" , params , from ) {
144
+ public async readFunc ( funcName = "" , params , from ) {
114
145
115
- let ABI = JSON . parse ( JSON . stringify ( this . ABI ) )
146
+ const ABI = JSON . parse ( JSON . stringify ( this . ABI ) ) ;
116
147
117
- const contractDeployed = new this . WEB3 . eth . Contract (
118
- ABI ,
119
- this . SCA
120
- ) ;
148
+ const contractDeployed = new this . WEB3 . eth . Contract (
149
+ ABI ,
150
+ this . SCA
151
+ ) ;
121
152
122
- const dataFunc = await contractDeployed . methods [ funcName ] (
123
- ...params
124
- ) . call ( { from } ) ;
153
+ const dataFunc = await contractDeployed . methods [ funcName ] (
154
+ ...params
155
+ ) . call ( { from } ) ;
125
156
126
- return dataFunc ;
157
+ return dataFunc ;
127
158
128
- }
159
+ }
129
160
130
- /**
161
+ /**
131
162
* This function returns the receipt of a transaction
132
163
* @param {string } txHash - The transaction hash of the transaction you want to get the receipt
133
164
* for.
134
165
* @returns The receipt of the transaction.
135
166
*/
136
- public async getReceipt ( txHash : string ) {
137
- const receipt = this . WEB3 . eth . getTransactionReceipt ( txHash ) ;
167
+ public async getReceipt ( txHash : string ) {
168
+ const receipt = this . WEB3 . eth . getTransactionReceipt ( txHash ) ;
138
169
139
- return receipt ;
140
- }
170
+ return receipt ;
171
+ }
141
172
142
- /**
173
+ /**
143
174
* This function returns event logs is published from a smart contract
144
175
* @param {array } topics - The topics of log
145
176
* topics[0]: signature event
146
177
* topics[1-3]: indexed params
147
178
* @returns The event log.
148
179
*/
149
- public async getEvent ( topics , fromBlock = 0 , toBlock = 499 ) {
150
- const event = await this . WEB3 . eth . getPastLogs ( {
151
- address : this . SCA , //smart contract address published event
152
- topics,
153
- fromBlock,
154
- toBlock
155
- } )
180
+ public async getEvent ( topics , fromBlock = 0 , toBlock = 499 ) {
181
+ const event = await this . WEB3 . eth . getPastLogs ( {
182
+ address : this . SCA , //smart contract address published event
183
+ topics,
184
+ fromBlock,
185
+ toBlock
186
+ } ) ;
156
187
157
188
return event ;
158
- }
189
+ }
159
190
160
- /**
191
+ /**
161
192
* It takes the ABI of the contract, the data from the log, and the topics from the log, and
162
193
* returns the decoded log
163
194
* @param [data] - The data parameter is the data that was emitted from the contract.
164
195
* @param topics - An array of values generated by the: event.raw.topics
165
196
* @returns The decoded log.
166
197
*/
167
- public async decodeLog ( data = "" , topics ) {
198
+ public async decodeLog ( data = "" , topics ) {
168
199
169
- let ABI = JSON . parse ( JSON . stringify ( this . ABI ) )
200
+ const ABI = JSON . parse ( JSON . stringify ( this . ABI ) ) ;
170
201
171
- const result = await this . WEB3 . eth . abi . decodeLog ( ABI , data , topics )
202
+ const result = await this . WEB3 . eth . abi . decodeLog ( ABI , data , topics ) ;
172
203
173
- return result ;
174
- }
204
+ return result ;
205
+ }
175
206
176
207
}
0 commit comments