This repository has been archived by the owner on Sep 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
maticBridge.js
72 lines (62 loc) · 2.14 KB
/
maticBridge.js
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const api = require('./httpApi')
let Web3 = require('web3')
// let sleep = require('sleep')
let bridgeBuild = require('./build/contracts/Bridge.json')
let provider = 'https://rpc-mumbai.matic.today'
let web3 = new Web3(new Web3.providers.HttpProvider(provider))
let bridge = new web3.eth.Contract(bridgeBuild['abi'])
const fs = require('fs')
// var Tx = require('ethereumjs-tx').Transaction
// var ethjs = require('ethereumjs-util')
async function main() {
copy()
setInterval(() => copy(), 120000)
}
const privateKey = fs.readFileSync('.bridgeKey').toString().trim()
web3.eth.accounts.wallet.add(privateKey)
console.log('using address ', web3.eth.accounts.wallet[0].address)
async function setResult(i) {
let result
let nonce
let dataTx
result = await api.getResult(i)
console.log('result', i, result)
dataTx = bridge.methods.setResult(Number(i), Number(result)).encodeABI()
nonce = await web3.eth.getTransactionCount(web3.eth.accounts.wallet[0].address, 'pending')
console.log('nonce', nonce)
var rawTx = {
from: web3.eth.accounts.wallet[0].address,
to: "0xBeB35335472FE13375a653e3eB27853a9Df319c0",
gas: 80000,
data: dataTx
}
await web3.eth.sendTransaction(rawTx)
}
async function setJob(i) {
let job
let nonce
let dataTx
job = await api.getJob(i)
console.log('job', i, job)
console.log('setting', Number(i), job.url, job.selector, job.name, Number(job.result))
dataTx = await bridge.methods.setJob(Number(i), job.url, job.selector, job.name, Number(job.result)).encodeABI()
nonce = await web3.eth.getTransactionCount(web3.eth.accounts.wallet[0].address, 'pending')
console.log('nonce', nonce)
var rawTx = {
from: web3.eth.accounts.wallet[0].address,
to: "0xBeB35335472FE13375a653e3eB27853a9Df319c0",
gas: 2000000,
data: dataTx
}
await web3.eth.sendTransaction(rawTx)
}
async function copy() {
let numJobs = await api.getNumJobs()
console.log('numJobs', numJobs)
for (let i = 1; i <= numJobs; i++) {
await setResult(i)
await setJob(i)
}
console.log('synced', Date())
}
main()