dogecoin-gatewayd-plugin
Version:
Three unix processes designed to automate the integration between a ripple gateway using gatewayd and a dogecoind server.
95 lines (82 loc) • 3.1 kB
JavaScript
// Generated by CoffeeScript 1.7.1
var EventEmitter, config, emitter, lastBlockHash, listNextBlock, listSinceBlock, pollForPayments, request, _;
request = require("superagent");
_ = require("underscore-node");
config = require("" + __dirname + "/../config/config.js");
EventEmitter = require("events").EventEmitter;
if (process.env.NODE_ENV !== "production") {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
}
lastBlockHash = config.get("lastBlockHash");
listSinceBlock = function(block, callback) {
return request.get(config.get('DOGECOIND_HOST') + block).auth("admin", config.get("DOGECOIND_API_KEY")).end(function(error, response) {
var sorted;
try {
sorted = _.sortBy(response.body.received.transactions, function(transaction) {
return transaction.blocktime;
});
return callback(null, sorted);
} catch (_error) {
error = _error;
return callback(error, null);
}
});
};
listNextBlock = function(lastBlock, callback) {
return listSinceBlock(lastBlock, function(error, transactions) {
var nextBlock, nextBlockHash;
if (!transactions || transactions.length === 0) {
callback(error, null);
return;
}
nextBlockHash = transactions[0].blockhash;
nextBlock = _.filter(transactions, function(transaction) {
return transaction.blockhash === nextBlockHash;
});
return callback(null, nextBlock);
});
};
pollForPayments = function(callback) {
return listNextBlock(config.get("lastBlockHash"), function(error, transactions) {
if (error) {
return callback(callback);
} else if (transactions && transactions.length > 0) {
if (transactions[0].blockhash) {
emitter.emit("block", transactions);
config.set("lastBlockHash", transactions[0].blockhash);
config.save();
}
return callback(callback);
} else {
return callback(callback);
}
});
};
emitter = new EventEmitter();
emitter.on("block", function(transactions) {
return transactions.forEach(function(transaction) {
request.get(config.get("gatewaydDomain") + "/v1/external_accounts?name=" + transaction.address).auth(config.get("adminEmail"), config.get("apiKey")).end(function(error, response) {
var externalAccount;
if (error) {
return console.log("error", error);
} else if (response.body.external_accounts && response.body.external_accounts.length > 0) {
externalAccount = response.body.external_accounts[0];
return request.post(config.get("gatewaydDomain") + "/v1/deposits").auth(config.get("adminEmail"), config.get("apiKey")).send({
external_account_id: externalAccount.id,
currency: "DOG",
amount: transaction.amount
}).end(function(error, response) {
if (error) {
return console.log("error", error);
} else {
return console.log(response.body);
}
});
} else {
return console.log("no account found");
}
});
return console.log(transaction);
});
});
pollForPayments(pollForPayments);