-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpayment-validator-delayed.js
More file actions
125 lines (122 loc) · 4.89 KB
/
payment-validator-delayed.js
File metadata and controls
125 lines (122 loc) · 4.89 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
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
119
120
121
122
123
124
125
// Validator program ffor the confirmation in the queue
const Web3 = require('web3');
const fs = require('fs');
const { ApiPromise, WsProvider } = require('@polkadot/api');
const { Keyring } = require('@polkadot/keyring');
const { Client } = require('pg');
// read environment variables
const TOKENADDRESS = process.env.TOKENADDRESS;
if (typeof TOKENADDRESS==='undefined'){
console.log("TOKENADDRESS variable is not set, please set it for launching the validator. It's the address of the the token to validate");
process.exit();
}
const ABI = process.env.ABI;
if (typeof ABI==='undefined'){
console.log("ABI variable is not set, please set it for launching the validator. It's the file where to read the ABI of the contract");
process.exit();
}
const ABIJSON=fs.readFileSync(ABI,"ascii");
//console.log("ABIJSON: ",ABIJSON);
const WALLETADDRESS = process.env.WALLETADDRESS;
if (typeof WALLETADDRESS==='undefined'){
console.log("WALLETADDRESS variable is not set, please set it for launching the validator. It's the address of the the recipient wallet");
process.exit();
}
const BLOCKCHAIN = process.env.BLOCKCHAIN;
if (typeof BLOCKCHAIN==='undefined'){
console.log("BLOCKCHAIN variable is not set, please set it for launching the validator");
process.exit();
}
const BLOCKCHAINCODE = process.env.BLOCKCHAINCODE;
if (typeof BLOCKCHAINCODE==='undefined'){
console.log("BLOCKCHAINCODE variable is not set, please set it for launching the validator");
process.exit();
}
const MNEMONIC = process.env.MNEMONIC;
if (typeof MNEMONIC==='undefined'){
console.log("MNEMONIC variable is not set, please set it for launching the validator");
process.exit();
}
const SUBSTRATECHAIN = process.env.SUBSTRATECHAIN;
if (typeof SUBSTRATECHAIN=='=undefined'){
console.log("SUBSTRATECHAIN variable is not set, please set it for launching the validator");
process.exit();
}
const BLOCKSCONFIRMATION = process.env.BLOCKSCONFIRMATION;
if (typeof BLOCKSCONFIRMATION=='=undefined'){
console.log("BLOCKSCONFIRMATION variable is not set, please set it for launching the validator");
process.exit();
}
//console.log(BLOCKCHAIN);
const client = new Client();
// connect EVM blockchain
const web3 = new Web3(BLOCKCHAIN || "ws://localhost:8545");
console.log("Payment Validator Queue Processor v.1.0 - token:", TOKENADDRESS," for wallet: ",WALLETADDRESS);
// execute a main loop for async oeprations
mainloop();
async function mainloop(){
//connect SUBSTRATE CHAIN
const wsProvider = new WsProvider(SUBSTRATECHAIN);
const api = await ApiPromise.create({ provider: wsProvider });
const keyring = new Keyring({ type: 'sr25519' });
let keys=keyring.createFromUri(MNEMONIC);
console.log("Validator Address: ",keys.address);
// connect to database
await client.connect();
// read decimals and symbol
let contract = new web3.eth.Contract(JSON.parse(ABIJSON), TOKENADDRESS);
const [decimals, symbol] = await Promise.all([
contract.methods.decimals().call(),
contract.methods.symbol().call()
]);
console.log("decimals: ",decimals);
console.log("symbol: ",symbol);
// select the records in the queue belonging to the current validator
let rs;
try{
const queryText="SELECT * from validationsqueue where validatoraddress=$1";
rs=await client.query(queryText, [keys.address]);
} catch (e) {
throw e;
}
for(i in rs.rows){
const txhash=rs.rows[i].txhash;
// check for blocks confirmed on EVM chain
// fetch current block number
const lastbn=await web3.eth.getBlockNumber();
// fetch the blocknumber of the txash
const tx=await web3.eth.getTransaction(txhash);
if(tx.blockNumber == null)
continue;
// checl for enough confirmations
if(lastbn-tx.blockNumber>=BLOCKSCONFIRMATION){
// validate on chain
validate_payment(rs.rows[i].buyorderid,BLOCKCHAINCODE,txhash,keys,api);
// remove record from the queue
try{
const queryText="delete from validationsqueue where validatoraddress=$1 and txhash=$2";
rs=await client.query(queryText, [keys.address],txhash);
} catch (e) {
throw e;
}
}
}
//end of program
process.exit();
}
// function to submit the transaction to the blockchain
async function validate_payment(orderid,blockchainid,tx,keys,api){
let ao=[];
if(orderid.search(",")==-1)
ao.push(orderid);
else
ao=orderid.split(",");
for(x in ao){
if(ao[x].length==0)
continue;
const validate = api.tx.dex.validateBuyOrder(ao[x],blockchainid,tx);
// Sign and send the transaction using our account with nonce to consider the queue
const hash = await validate.signAndSend(keys,{ nonce: -1 });
console.log("Validation submitted tx: ",hash.toHex());
}
}