-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb3_helper.js
More file actions
42 lines (35 loc) · 1.22 KB
/
web3_helper.js
File metadata and controls
42 lines (35 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const MockWeb3Provider = require('./test/mocks/mock_web3_provider');
class Web3Helper {
constructor(providerUrl) {
if (providerUrl === 'mock') {
this.web3 = new MockWeb3Provider();
} else {
const { Web3 } = require('web3');
this.web3 = new Web3(providerUrl);
}
}
async getBalance(address) {
return this.web3.eth.getBalance(address);
}
async callContract(contractAddress, abi, methodName, params = []) {
const contract = new this.web3.eth.Contract(abi, contractAddress);
return contract.methods[methodName](...params).call();
}
async sendTransaction(from, to, value, privateKey) {
const tx = {
from,
to,
value: this.web3.utils.toWei(value.toString(), 'ether'),
gas: 21000,
};
const signedTx = await this.web3.eth.accounts.signTransaction(tx, privateKey);
return this.web3.eth.sendSignedTransaction(signedTx.rawTransaction);
}
async subscribeToEvent(contractAddress, abi, eventName, callback) {
const contract = new this.web3.eth.Contract(abi, contractAddress);
contract.events[eventName]()
.on('data', event => callback(null, event))
.on('error', error => callback(error, null));
}
}
module.exports = Web3Helper;