-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathlib.js
More file actions
56 lines (50 loc) · 1.53 KB
/
lib.js
File metadata and controls
56 lines (50 loc) · 1.53 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const Web3 = require('web3')
var web3, account
class Contract {
constructor(address, abi) {
this.address = address
this.abi = abi
this.object = new web3.eth.Contract(abi, address)
}
async view(name, ...args) {
return await this.object.methods[name](...args).call()
}
async call(tx, name, ...args) {
if (typeof tx === 'string') {
if (name) {
args = [name, ...args]
}
name = tx
}
let func = this.object.methods[name](...args)
let s = await web3.eth.accounts.signTransaction({
to: this.address,
data: func.encodeABI(),
nonce: await web3.eth.getTransactionCount(account.address),
gasLimit: 1000000,
...tx
}, account.privateKey)
await new Promise((resolve, reject) => {
web3.eth.sendSignedTransaction(s.rawTransaction)
.on('transactionHash', (txHash) => {
console.log(`Transaction -> https://ropsten.etherscan.io/tx/${txHash}`)
})
.on('error', reject)
.then(resolve)
})
}
async storage(position) {
return await web3.eth.getStorageAt(this.address, position)
}
}
module.exports = function (config) {
web3 = new Web3(config.network)
account = config.account
return {
web3: web3,
account: account,
contract: function (address, abi) {
return new Contract(address, abi)
}
}
}