File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ import Web3 from 'web3' ;
2
+ import { v4 as uuidv4 } from 'uuid' ;
3
+
4
+ class BlockchainService {
5
+ constructor ( ) {
6
+ this . web3 = new Web3 ( new Web3 . providers . HttpProvider ( 'https://mainnet.infura.io/v3/YOUR_PROJECT_ID' ) ) ;
7
+ this . contracts = { } ;
8
+ }
9
+
10
+ async deployContract ( contractCode ) {
11
+ const contractId = uuidv4 ( ) ;
12
+ const contract = await this . web3 . eth . compile ( contractCode ) ;
13
+ await this . web3 . eth . deployContract ( contract , {
14
+ from : '0x1234567890abcdef' ,
15
+ gas : '2000000' ,
16
+ } ) ;
17
+ this . contracts [ contractId ] = contract ;
18
+ return contractId ;
19
+ }
20
+
21
+ async invokeContractFunction ( contractId , functionName , args ) {
22
+ const contract = this . contracts [ contractId ] ;
23
+ if ( ! contract ) {
24
+ throw new Error ( 'Contract not found' ) ;
25
+ }
26
+ const result = await contract . methods [ functionName ] ( ...args ) . call ( ) ;
27
+ return result ;
28
+ }
29
+
30
+ async getBlockNumber ( ) {
31
+ return await this . web3 . eth . getBlockNumber ( ) ;
32
+ }
33
+
34
+ async getTransactionCount ( address ) {
35
+ return await this . web3 . eth . getTransactionCount ( address ) ;
36
+ }
37
+ }
38
+
39
+ export default BlockchainService ;
You can’t perform that action at this time.
0 commit comments