From 13a0dfe98fa1e85b22b29d93ce2d7785fd5b8162 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Mon, 18 Nov 2019 10:48:26 -0400 Subject: [PATCH] Support querying smart contracts for template data --- api/controller.js | 1 - dapps/teller-network/config.js | 39 +++++++++++++++++++++++++++++++++- watcher/index.js | 26 ++++++++++++++--------- 3 files changed, 54 insertions(+), 12 deletions(-) diff --git a/api/controller.js b/api/controller.js index b11b1c0..a5ee308 100644 --- a/api/controller.js +++ b/api/controller.js @@ -78,7 +78,6 @@ class Controller { static unsubscribe(dappConfig) { return async (req, res) => { - // TODO: const { params: { dappId }, body: { address, signature } diff --git a/dapps/teller-network/config.js b/dapps/teller-network/config.js index 516f3e0..cd9c9a9 100644 --- a/dapps/teller-network/config.js +++ b/dapps/teller-network/config.js @@ -1,3 +1,32 @@ + +const ERC20_ABI = [ + { + constant: true, + inputs: [], + name: "name", + outputs: [ + { + name: "", + type: "string" + } + ], + payable: false, + stateMutability: "view", + type: "function" + }, + { + constant: true, + type: "function", + name: "balanceOf", + inputs: [ + { name: "owner", type: "address" } + ], + outputs: [{ name: "balance", type: "uint256" }], + stateMutability: "view" + }, +]; + + module.exports = { from: { email: "noreply@teller.exchange", @@ -27,7 +56,15 @@ module.exports = { template: { subject: "New trade!", html: "escrow-creation.html", - text: "escrow-creation.txt" + text: "escrow-creation.txt", + data: async (web3, returnValues) => { + // Example obtaining contract data + const SNT = new web3.eth.Contract(ERC20_ABI, "0x3C36db79598e7902b5D726af7C7d406d5Da8aF14"); + return { + 'tokenName': await SNT.methods.name().call(), + 'balance': await SNT.methods.balanceOf(returnValues.seller).call() + }; + } } }, "escrow-funded": { diff --git a/watcher/index.js b/watcher/index.js index d16c9df..229ed71 100644 --- a/watcher/index.js +++ b/watcher/index.js @@ -18,8 +18,7 @@ eth.init(); events.on("db:connected", () => { events.on("web3:connected", () => { - const blockNum = - process.argv.length >= 3 ? parseInt(process.argv[2], 10) : 0; + const blockNum = process.argv.length >= 3 ? parseInt(process.argv[2], 10) : 0; dappConfig.getDapps().forEach(dappId => { const contracts = dappConfig.contracts(dappId); @@ -33,17 +32,24 @@ events.on("db:connected", () => { events.on("web3:event", ({ dappId, address, event, returnValues }) => { dappConfig.eventConfig(dappId, address, event).forEach(async eventConfig => { const users = await Subscribers.findVerifiedUsersByDapp(dappId); - users.forEach(user => { + users.forEach(async user => { if (addressCompare(returnValues[eventConfig.index], user.address)) { console.log("Sending email..."); - mailer.send( - dappConfig.getEmailTemplate(dappId, eventConfig.template), - dappConfig.config(dappId).from, - { - email: user.email, - ...returnValues + + let data = { + email: user.email, + ...returnValues + }; + + if (eventConfig.template.data) { + try { + data = Object.assign(data, await eventConfig.template.data(eth.web3, returnValues)); + } catch (err) { + console.err("Error using data function", err); } - ); + } + + mailer.send(dappConfig.getEmailTemplate(dappId, eventConfig.template), dappConfig.config(dappId).from, data); } }); });