-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
66 lines (56 loc) · 2.03 KB
/
server.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
const { ethers } = require("ethers")
const axios = require("axios")
const connectDB = require("./config/db")
const Fixtures = require("./models/Fixtures")
const sendTransaction = require("./utils/sendTransaction")
const bettingAbi_json = require("./contracts/Betting.json")
require("dotenv").config()
const INTERVAL_TIME = 10 * 60 * 1000;
const main = async () => {
const provider = new ethers.JsonRpcProvider(process.env.RPC_URL)
const ownerKey = process.env.CONTRACT_OWNER
const owner = new ethers.Wallet(ownerKey, provider);
const BettingContract = new ethers.Contract(process.env.CONTRACT_ADDRESS, bettingAbi_json, provider)
const gasLimit = 2_000_000
setInterval(async () => {
try {
const fixtures = await Fixtures.find({ finished: false })
fixtures.map(async (fixture) => {
const { fixtureId } = fixture
console.log(fixtureId)
const url = `https://api-football-v1.p.rapidapi.com/v3/fixtures?id=${fixtureId}`
const res = await axios.get(url, {
headers: {
"X-RapidAPI-Key": "4d0108a5a6mshc2e36a7acaf5ecap162acbjsn80103448a735",
"X-RapidAPI-Host": "api-football-v1.p.rapidapi.com"
}
})
if (res.data.response[0].fixture.status.short === "FT") {
const goals = res.data.response[0].goals
const fixtureResult = goals.home > goals.away ? 0 : goals.home < goals.away ? 2 : 1
const transaction = await BettingContract.processGame.populateTransaction(
BigInt(fixtureId),
fixtureResult,
)
const transactionStatus = await sendTransaction(
provider,
owner,
{
...transaction,
gasLimit: gasLimit,
},
)
if (transactionStatus === "sent") {
fixture.updateOne({ finished: true })
}
}
})
} catch (error) {
// eslint-disable-next-line no-console
console.log(error)
}
}, INTERVAL_TIME)
}
// Connect Database
connectDB()
main()