Skip to content

Commit 21f10df

Browse files
authored
Create blockchain-service.js
1 parent ba8bd3b commit 21f10df

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

services/blockchain-service.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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;

0 commit comments

Comments
 (0)