-
-
Notifications
You must be signed in to change notification settings - Fork 198
/
xdairelay.js
118 lines (102 loc) Β· 3.51 KB
/
xdairelay.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"use strict";
const express = require('express');
const helmet = require('helmet');
const app = express();
const fs = require('fs');
require('dotenv').config()
const ContractLoader = function(contractList,web3){
let contracts = []
for(let c in contractList){
try{
let abi = require("./src/contracts/"+contractList[c]+".abi.js")
let address = require("./src/contracts/"+contractList[c]+".address.js")
console.log(contractList[c],address,abi.length)
contracts[contractList[c]] = new web3.eth.Contract(abi,address)
console.log("contract")
contracts[contractList[c]].blockNumber = require("./src/contracts/"+contractList[c]+".blocknumber.js")
console.log("@ Block",contracts[contractList[c]].blockNumber)
}catch(e){console.log(e)}
}
return contracts
}
var bodyParser = require('body-parser')
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(helmet());
var cors = require('cors')
app.use(cors())
let contracts;
var Web3 = require('web3');
let relayHttpProvider = fs.readFileSync("relayhttpprovider.env").toString()
let transactions = {}
const HDWalletProvider = require("truffle-hdwallet-provider")
let DESKTOPMINERACCOUNT = 0
let web3
if(relayHttpProvider.indexOf("localhost")>=0||relayHttpProvider.indexOf("dai.poa.network")>=0){
web3 = new Web3(
new HDWalletProvider(
process.env.mnemonic,
relayHttpProvider
)
);
}else{
web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider(relayHttpProvider));
}
let accounts
web3.eth.getAccounts().then((_accounts)=>{
accounts=_accounts
console.log("ACCOUNTS",accounts)
})
console.log("LOADING CONTRACTS")
contracts = ContractLoader(["Links"],web3);
app.get('/', (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*');
console.log("/")
res.set('Content-Type', 'application/json');
res.end(JSON.stringify({hello:"world"}));
});
app.get('/miner', (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*');
console.log("/miner")
res.set('Content-Type', 'application/json');
res.end(JSON.stringify({address:accounts[DESKTOPMINERACCOUNT]}));
});
app.post('/link', async (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*');
console.log("/link",req.body)
let validClaim = await contracts.Links.methods.isClaimValid(req.body.id).call()
let expiredClaim = await contracts.Links.methods.isClaimExpired(req.body.id,req.body.sig,req.body.claimHash,req.body.dest).call()
if(expiredClaim || !validClaim){
console.log("INVALID CLAIM!!!!")
res.set('Content-Type', 'application/json');
res.end(JSON.stringify({invalid:"claim"}));
}else{
console.log("CLAIM IS VALID...")
contracts.Links.methods.claim(req.body.id,req.body.sig,req.body.claimHash,req.body.dest)
.send({from: accounts[DESKTOPMINERACCOUNT],gas: 240000,gasPrice: 1010101010},
(error, transactionHash)=>{
console.log("TX CALLBACK",error,transactionHash)
res.set('Content-Type', 'application/json');
res.end(JSON.stringify({transactionHash:transactionHash}));
}
)
.on('error',(err,receiptMaybe)=>{
console.log("TX ERROR",err,receiptMaybe)
})
.on('transactionHash',(transactionHash)=>{
console.log("TX HASH",transactionHash)
})
.on('receipt',(receipt)=>{
console.log("TX RECEIPT",receipt)
})
.then((receipt)=>{
console.log("TX THEN",receipt)
})
.catch((error) => {
console.log(error); //Tx promise
});
}
});
app.listen(18462);
console.log(`http listening on 18462`);