From 9df77d5b30a90580655c3e79fa7cc53b1a667d7e Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Thu, 31 May 2018 08:46:31 -0400 Subject: [PATCH] Adding heartbeat message andrequired functions for price plugins --- gas-relayer/config/config.json | 18 ++++++++++-- gas-relayer/src/contract-settings.js | 10 +++++++ gas-relayer/src/message-processor.js | 8 ++++++ gas-relayer/src/service.js | 41 ++++++++++++++++++++++++---- 4 files changed, 70 insertions(+), 7 deletions(-) diff --git a/gas-relayer/config/config.json b/gas-relayer/config/config.json index 1f02f5a..347fa95 100644 --- a/gas-relayer/config/config.json +++ b/gas-relayer/config/config.json @@ -22,14 +22,28 @@ } }, + "heartbeat": { + "enabled": true, + "symKey": "0xd0d905c1c62b810b787141430417caf2b3f54cffadb395b7bb39fdeb8f17266b" + }, + "tokens": { "0x0000000000000000000000000000000000000000": { "name": "Ethereum", - "symbol": "ETH" + "symbol": "ETH", + "minAccepted":{ + "value": 1, + "currency": "USD" + } }, "0xD10e6dAe987Dcc0B3ADaA375C9f59690a4C97a27": { "name": "Status Network Token", - "symbol": "SNT" + "symbol": "SNT", + "minAccepted":{ + "value": 1, + "currency": "USD" + }, + "pricePlugin": "../plugins/token-utils.js" } }, diff --git a/gas-relayer/src/contract-settings.js b/gas-relayer/src/contract-settings.js index e9222d8..a58d063 100644 --- a/gas-relayer/src/contract-settings.js +++ b/gas-relayer/src/contract-settings.js @@ -12,9 +12,19 @@ class ContractSettings { } process(){ + this._setTokenPricePlugin(); this._processContracts(); } + _setTokenPricePlugin(){ + for(let token in this.tokens){ + if(this.tokens[token].pricePlugin !== undefined){ + let PricePlugin = require(this.tokens[token].pricePlugin); + this.tokens[token].pricePlugin = new PricePlugin(this.tokens[token]); + } + } + } + getToken(token){ return this.tokens[token]; } diff --git a/gas-relayer/src/message-processor.js b/gas-relayer/src/message-processor.js index 9fee6b9..afe2f66 100644 --- a/gas-relayer/src/message-processor.js +++ b/gas-relayer/src/message-processor.js @@ -96,6 +96,14 @@ class MessageProcessor { } } + _getFactor(input, contract, gasToken){ + if(contract.allowedFunctions[input.functionName].isToken){ + return this.web3.utils.toBN(this.settings.getToken(gasToken).pricePlugin.getFactor()); + } else { + return this.web3.utils.toBN(1); + } + } + async getBalance(token, input, gasToken){ // Determining balances of token used if(token.symbol == "ETH"){ diff --git a/gas-relayer/src/service.js b/gas-relayer/src/service.js index c422963..5d3742f 100644 --- a/gas-relayer/src/service.js +++ b/gas-relayer/src/service.js @@ -31,12 +31,14 @@ events.on('web3:connected', connURL => { }); +// Setting up Whisper options +const shhOptions = { + ttl: config.node.whisper.ttl, + minPow: config.node.whisper.minPow, +}; + events.on('setup:complete', (settings) => { - // Setting up Whisper options - const shhOptions = { - ttl: config.node.whisper.ttl, - minPow: config.node.whisper.minPow, - }; + let kId; let symKId; @@ -58,6 +60,35 @@ events.on('setup:complete', (settings) => { shhOptions.topics = [contract]; events.emit('server:listen', shhOptions, settings); } + + + + if(config.heartbeat.enabled){ + let heartbeatSymKeyId; + web3.shh.addSymKey(config.heartbeat.symKey) + .then(heartbeatSymKeyId => { + + // TODO: define minPriceAccepted + let heartbeatPayload = { + 'minPriceAccepted': 0 + } + + setInterval(() => { + web3.shh.post({ + symKeyID: heartbeatSymKeyId, + sig: keyId, + ttl: config.node.whisper.ttl, + powTarget:config.node.whisper.minPow, + powTime: config.node.whisper.powTime, + // TODO: topic must be a combination of heartbeat + token + topic: web3.utils.toHex("relay-heartbeat").slice(0, 10), + payload: web3.utils.toHex(JSON.stringify(heartbeatPayload)) + }).catch(console.error); + }, 1000); + }); + } + + }); });