Skip to content
Snippets Groups Projects
Commit f25bbcea authored by Renzo Mauro Ontivero's avatar Renzo Mauro Ontivero
Browse files

Soporte a Florar

parent 988a62d3
No related branches found
No related tags found
No related merge requests found
......@@ -35,6 +35,19 @@ module.exports = {
return res.json(balanceToEther);
});
},
// Son las blockchain con las que trabajamos desde la API REST
getPayments : async function(req, res){
var payments = [];
payments.push({name: 'ropsten', enabled: true});
payments.push({name: 'rinkeby', enabled: true});
payments.push({name: 'rsk', enabled: true});
payments.push({name: 'bfa', enabled: false});
payments.push({name: 'celo', enabled: false});
return res.json(payments);
}
};
......
......@@ -7,6 +7,7 @@
const Web3 = require('web3');
const Tx = require('ethereumjs-tx');
const base64 = require('nodejs-base64-encode');
const axios = require('axios');
const url = sails.config.custom.urlRpcRopsten;
const web3 = new Web3(url);
const accountAddress = sails.config.custom.accountAddressRopsten;
......@@ -146,5 +147,102 @@ module.exports = {
});
},
createAccount : async function (req, res){
sails.log("createAccount()");
var account_data = web3.eth.accounts.create();
return res.json(account_data);
},
getBalance : async function (req, res){
var account = req.params.account;
web3.eth.getBalance(account, async (err, bal) => {
if(err){
return res.json(err.toString());
}
var balanceToEther = web3.utils.fromWei(bal, 'ether')
sails.log("Balance Ether:", balanceToEther);
// Ahora consulto el Balance de esta criptomoneda expresado en USD
try{
var etherToUsd = await sails.helpers.ethToUsd(balanceToEther);
sails.log("EtherToUSD:", etherToUsd);
} catch (e){
throw 'No se pudo obtener la cotización del ETH';
}
// Ahora convierto a Pesos
try{
var usdToArs = await sails.helpers.usdToArs(etherToUsd);
sails.log("USDToARS:", usdToArs);
} catch (e) {
throw 'No se pudo obtener la cotización del USD';
}
sails.log("-------------------------------------------------------");
return res.json(usdToArs);
});
},
send: async function(req, res){
var _from = req.body.from;
var _to = req.body.to;
var _ether = req.body.value_ars;
var private_key = Buffer.from(
req.body.private_key.substr(2),
'hex',
);
// Tengo que convertir el dinero que viene en ARS a USD
try{
var arsToUsd = await sails.helpers.arsToUsd(_ether);
sails.log("ArsToUSD:", arsToUsd);
} catch (e){
throw 'No se pudo obtener la cotización del USD';
}
// Tengo que convertir el dinero USD a ETH
try{
var usdToEth = await sails.helpers.usdToEth(arsToUsd);
sails.log("USDToEth:", usdToEth);
} catch (e){
throw 'No se pudo obtener la cotización ETH';
}
web3.eth.getTransactionCount(_from, (err, txCount) => {
sails.log("Nonce", txCount);
// Construir la transaccion
const txObject = {
nonce: web3.utils.toHex(txCount),
to: _to,
gasLimit: web3.utils.toHex(21000),
gasPrice: web3.utils.toHex(web3.utils.toWei('1000', 'gwei')),
value: web3.utils.toHex(web3.utils.toWei(usdToEth.toString(), "ether")),
}
// Firmar la transaccion
const tx = new Tx(txObject);
tx.sign(private_key);
const serializeTransaction = tx.serialize();
const raw = '0x' + serializeTransaction.toString('hex');
// Transmitir la transacción
web3.eth.sendSignedTransaction(raw, (err, tx_hash) => {
if(err){
return res.json(err.toString());
}
return res.json({
tx_hash : tx_hash
});
});
});
},
};
/**
* CeloController
*
* @description :: Server-side actions for handling incoming requests.
* @help :: See https://sailsjs.com/docs/concepts/actions
*/
const ContractKit = require('@celo/contractkit');
const kit = ContractKit.newKit(sails.config.custom.urlRpcCelo);
const Web3 = require('web3');
var web3 = new Web3();
module.exports = {
createAccount : async function (req, res){
sails.log("createAccount()");
var account_data = web3.eth.accounts.create();
return res.json(account_data);
},
getBalance : async function (req, res){
var account = req.params.account;
let goldtoken = await kit.contracts.getGoldToken()
let balance = await goldtoken.balanceOf(account)
return res.json(balance.toString());
},
};
......@@ -7,6 +7,7 @@
const Web3 = require('web3');
const Tx = require('ethereumjs-tx');
const base64 = require('nodejs-base64-encode');
const axios = require('axios');
const url = sails.config.custom.urlRpcRinkeby;
const web3 = new Web3(url);
const accountAddress = sails.config.custom.accountAddressRinkeby;
......@@ -148,22 +149,106 @@ module.exports = {
},
createAccount : async function (req, res){
sails.log("createAccount()");
var account_data = web3.eth.accounts.create();
return res.json(account_data);
sails.log("createAccount()");
try{
var account_data = web3.eth.accounts.create();
} catch (e){
throw 'No se pudo crear la cuenta';
}
return res.json(account_data);
},
getBalance : async function (req, res){
var account = req.params.account;
var account = req.params.account;
web3.eth.getBalance(account, (err, bal) => {
if(err){
return res.json(err.toString());
web3.eth.getBalance(account,async (err, bal) => {
if(err){
return res.json(err.toString());
}
var balanceToEther = web3.utils.fromWei(bal, 'ether')
sails.log("Balance Ether:", balanceToEther);
// Ahora consulto el Balance de esta criptomoneda expresado en USD
try{
var etherToUsd = await sails.helpers.ethToUsd(balanceToEther);
sails.log("EtherToUSD:", etherToUsd);
} catch (e){
throw 'No se pudo obtener la cotización del ETH';
}
var balanceToEther = web3.utils.fromWei(bal, 'ether')
return res.json(balanceToEther);
});
}
// Ahora convierto a Pesos
try{
var usdToArs = await sails.helpers.usdToArs(etherToUsd);
sails.log("USDToARS:", usdToArs);
} catch (e) {
throw 'No se pudo obtener la cotización del USD';
}
sails.log("-------------------------------------------------------");
return res.json(usdToArs);
});
},
send: async function(req, res){
var _from = req.body.from;
var _to = req.body.to;
var _ether = req.body.value_ars;
var private_key = Buffer.from(
req.body.private_key.substr(2),
'hex',
);
// Tengo que convertir el dinero que viene en ARS a USD
try{
var arsToUsd = await sails.helpers.arsToUsd(_ether);
sails.log("ArsToUSD:", arsToUsd);
} catch (e){
throw 'No se pudo obtener la cotización del USD';
}
// Tengo que convertir el dinero USD a ETH
try{
var usdToEth = await sails.helpers.usdToEth(arsToUsd);
sails.log("USDToEth:", usdToEth);
} catch (e){
throw 'No se pudo obtener la cotización ETH';
}
web3.eth.getTransactionCount(_from, (err, txCount) => {
sails.log("Nonce", txCount);
// Construir la transaccion
const txObject = {
nonce: web3.utils.toHex(txCount),
to: _to,
gasLimit: web3.utils.toHex(21000),
gasPrice: web3.utils.toHex(web3.utils.toWei('1000', 'gwei')),
value: web3.utils.toHex(web3.utils.toWei(usdToEth.toString(), "ether")),
}
// Firmar la transaccion
const tx = new Tx(txObject);
tx.sign(private_key);
const serializeTransaction = tx.serialize();
const raw = '0x' + serializeTransaction.toString('hex');
// Transmitir la transacción
web3.eth.sendSignedTransaction(raw, (err, tx_hash) => {
if(err){
return res.json(err.toString());
}
return res.json({
tx_hash : tx_hash
});
});
});
},
};
......@@ -7,6 +7,7 @@
const Web3 = require('web3');
const Tx = require('ethereumjs-tx');
const base64 = require('nodejs-base64-encode');
const axios = require('axios');
const url = sails.config.custom.urlRpcRsk;
const web3 = new Web3(url);
const accountAddress = sails.config.custom.accountAddressRsk;
......@@ -156,14 +157,93 @@ module.exports = {
getBalance : async function (req, res){
var account = req.params.account;
web3.eth.getBalance(account, (err, bal) => {
web3.eth.getBalance(account,async (err, bal) => {
if(err){
return res.json(err.toString());
}
var balanceToEther = web3.utils.fromWei(bal, 'ether')
return res.json(balanceToEther);
sails.log("Balance Ether:", balanceToEther);
// Ahora consulto el Balance de esta criptomoneda expresado en USD
try{
var rbtcToUsd = await sails.helpers.rbtcToUsd(balanceToEther);
sails.log("RbtcToUSD:", rbtcToUsd);
} catch (e){
throw 'No se pudo obtener la cotización del RBTC';
}
// Ahora convierto a Pesos
try{
var usdToArs = await sails.helpers.usdToArs(rbtcToUsd);
sails.log("USDToARS:", usdToArs);
} catch (e){
throw 'No se pudo obtener la cotización del USD';
}
sails.log("-------------------------------------------------------");
return res.json(usdToArs);
});
}
},
send: async function(req, res){
var _from = req.body.from;
var _to = req.body.to;
var _ether = req.body.value_ars;
var private_key = Buffer.from(
req.body.private_key.substr(2),
'hex',
);
// Tengo que convertir el dinero que viene en ARS a USD
try{
var arsToUsd = await sails.helpers.arsToUsd(_ether);
sails.log("ArsToUSD:", arsToUsd);
} catch (e) {
throw 'No se pudo obtener la cotización del USD';
}
// Tengo que convertir el dinero USD a ETH
try{
var usdToRbtc = await sails.helpers.usdToRbtc(arsToUsd);
sails.log("USDToRBTC:", usdToRbtc);
} catch (e){
throw 'No se pudo obtener la cotización RBTC';
}
web3.eth.getTransactionCount(_from, (err, txCount) => {
sails.log("Nonce", txCount);
// Construir la transaccion
const txObject = {
nonce: web3.utils.toHex(txCount),
to: _to,
gasLimit: web3.utils.toHex(21000),
gasPrice: web3.utils.toHex(web3.utils.toWei('1000', 'gwei')),
value: web3.utils.toHex(web3.utils.toWei(usdToRbtc.toString(), "ether")),
}
// Firmar la transaccion
const tx = new Tx(txObject);
tx.sign(private_key);
const serializeTransaction = tx.serialize();
const raw = '0x' + serializeTransaction.toString('hex');
// Transmitir la transacción
web3.eth.sendSignedTransaction(raw, (err, tx_hash) => {
if(err){
return res.json(err.toString());
}
return res.json({
tx_hash : tx_hash
});
});
});
},
};
const axios = require('axios');
module.exports = {
friendlyName: 'Ars to usd',
description: '',
inputs: {
ars : {
type : 'number',
required : true
},
},
exits: {
success: {
description: 'All done.',
},
},
fn: async function (inputs) {
var openexchange = await axios({
method : 'GET',
url : `https://openexchangerates.org/api/latest.json?app_id=${sails.config.custom.openExchangeRatesAppId}&symbols=ARS`,
})
.then((result) => {
return result.data.rates.ARS;
})
.catch(err => {
console.log(err);
});
sails.log("Tasa de conversión USD/ARS:", openexchange);
// TODO: guardar el último valor en la BD y en caso de fallar el exchange, tomar la cotización de la BD
var arsToUsd = inputs.ars / openexchange;
return arsToUsd;
}
};
const axios = require('axios');
module.exports = {
friendlyName: 'Eth to usd',
description: '',
inputs: {
eth : {
type : 'number',
required : true
}
},
exits: {
success: {
description: 'All done.',
},
},
fn: async function (inputs) {
var exchangerate = await axios({
method : 'GET',
url : 'https://rest.coinapi.io/v1/exchangerate/ETH/USD',
headers: {
'X-CoinAPI-Key' : sails.config.custom.xCoinApiKey,
},
})
.then((result) => {
return result.data.rate;
})
.catch(err => {
console.log(err.response.data);
});
sails.log("Tasa de conversion ETH/USD:", exchangerate);
var etherToUsd = exchangerate * inputs.eth;
return etherToUsd;
}
};
const axios = require('axios');
module.exports = {
friendlyName: 'Rbtc to usd',
description: '',
inputs: {
rbtc : {
type : 'number',
required : true
}
},
exits: {
success: {
description: 'All done.',
},
},
fn: async function (inputs) {
var exchangerate = await axios({
method : 'GET',
url : 'https://rest.coinapi.io/v1/exchangerate/RBTC/USD',
headers: {
'X-CoinAPI-Key' : sails.config.custom.xCoinApiKey,
},
})
.then((result) => {
return result.data.rate;
})
.catch(err => {
console.log(err.response.data);
});
sails.log("Tasa de conversion RBTC/USD:", exchangerate);
var rbtcToUsd = exchangerate * inputs.rbtc;
return rbtcToUsd;
}
};
const axios = require('axios');
module.exports = {
friendlyName: 'Usd to ars',
description: '',
inputs: {
usd : {
type : 'number',
required : true
},
},
exits: {
success: {
description: 'All done.',
},
},
fn: async function (inputs) {
var openexchange = await axios({
method : 'GET',
url : `https://openexchangerates.org/api/latest.json?app_id=${sails.config.custom.openExchangeRatesAppId}&symbols=ARS`,
})
.then((result) => {
return result.data.rates.ARS;
})
.catch(err => {
console.log(err);
});
sails.log("Tasa de conversión USD/ARS:", openexchange);
// TODO: guardar el último valor en la BD y en caso de fallar el exchange, tomar la cotización de la BD
var usdToArs = openexchange * inputs.usd;
return usdToArs;
}
};
const axios = require('axios');
module.exports = {
friendlyName: 'Usd to eth',
description: '',
inputs: {
usd : {
type : 'number',
required : true,
}
},
exits: {
success: {
description: 'All done.',
},
},
fn: async function (inputs) {
var exchangerate = await axios({
method : 'GET',
url : 'https://rest.coinapi.io/v1/exchangerate/ETH/USD',
headers: {
'X-CoinAPI-Key' : sails.config.custom.xCoinApiKey,
},
})
.then((result) => {
return result.data.rate;
})
.catch(err => {
console.log(err.response.data);
});
sails.log("Tasa de conversion ETH/USD:", exchangerate);
var usdToEth = inputs.usd / exchangerate;
return usdToEth;
}
};
const axios = require('axios');
module.exports = {
friendlyName: 'Usd to rbtc',
description: '',
inputs: {
usd : {
type : 'number',
required : true
}
},
exits: {
success: {
description: 'All done.',
},
},
fn: async function (inputs) {
var exchangerate = await axios({
method : 'GET',
url : 'https://rest.coinapi.io/v1/exchangerate/RBTC/USD',
headers: {
'X-CoinAPI-Key' : sails.config.custom.xCoinApiKey,
},
})
.then((result) => {
return result.data.rate;
})
.catch(err => {
console.log(err.response.data);
});
sails.log("Tasa de conversion ETH/USD:", exchangerate);
var rbtcToUsd = inputs.usd / exchangerate;
return rbtcToUsd;
}
};
/**
* Celo.js
*
* @description :: A model definition represents a database table/collection.
* @docs :: https://sailsjs.com/docs/concepts/models-and-orm/models
*/
module.exports = {
attributes: {
// ╔═╗╦═╗╦╔╦╗╦╔╦╗╦╦ ╦╔═╗╔═╗
// ╠═╝╠╦╝║║║║║ ║ ║╚╗╔╝║╣ ╚═╗
// ╩ ╩╚═╩╩ ╩╩ ╩ ╩ ╚╝ ╚═╝╚═╝
// ╔═╗╔╦╗╔╗ ╔═╗╔╦╗╔═╗
// ║╣ ║║║╠╩╗║╣ ║║╚═╗
// ╚═╝╩ ╩╚═╝╚═╝═╩╝╚═╝
// ╔═╗╔═╗╔═╗╔═╗╔═╗╦╔═╗╔╦╗╦╔═╗╔╗╔╔═╗
// ╠═╣╚═╗╚═╗║ ║║ ║╠═╣ ║ ║║ ║║║║╚═╗
// ╩ ╩╚═╝╚═╝╚═╝╚═╝╩╩ ╩ ╩ ╩╚═╝╝╚╝╚═╝
},
};
......@@ -49,6 +49,13 @@ module.exports.custom = {
contractABIRsk: [{"constant":true,"inputs":[{"name":"ots","type":"string"}],"name":"getHash","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"ots","type":"string"}],"name":"getBlockNumber","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"ots","type":"string"},{"name":"file_hash","type":"string"}],"name":"verify","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"selfDestroy","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"ots","type":"string"},{"name":"file_hash","type":"string"}],"name":"stamp","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"hash","type":"string"},{"indexed":true,"name":"ots","type":"string"}],"name":"Stamped","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"}],"name":"Deploy","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"}],"name":"SelfDestroy","type":"event"}],
contractAddressRsk : '0xbdc32baf7ae3b23A87fA87D98364846AE506fdc4',
privateKeyRsk: 'D305B054E4E6965D92C79F3166D6BEFC07AD9B92CDC48B00FFA7937548974E6D',
// Datos Exchanges
xCoinApiKey: 'ED4DE42A-9F8D-4631-9ABD-DDC8E159B1F4',
openExchangeRatesAppId: '37ec0cc1dccd4564bd4f750ee6364c29',
// Datos para Cello
urlRpcCelo : 'https://alfajores-forno.celo-testnet.org',
};
......@@ -20,28 +20,53 @@ module.exports.routes = {
***************************************************************************/
'/': { view: 'pages/homepage' },
'GET /blockchain/verify/:ots/:file_hash' : 'BlockchainController.verify',
'GET /account/create' : 'AccountController.create',
'GET /account/get_balance/:account' : 'AccountController.getBalance',
//////////////////////////////////////
/////////// GET ////////////////////
//////////////////////////////////////
// Metodos de ROPSTEN
'GET /ropsten/verify/:ots/:file_hash' : 'BlockchainController.verify',
'GET /ropsten/create_account' : 'BlockchainController.createAccount',
'GET /ropsten/get_balance/:account' : 'BlockchainController.getBalance',
// Metodos de RINKEBY
'GET /rinkeby/verify/:ots/:file_hash' : 'RinkebyController.verify',
'GET /rinkeby/createAccount' : 'RinkebyController.createAccount',
'GET /rinkeby/get_balance/:account' : 'RinkebyController.getBalance',
'GET /rinkeby/create_account' : 'RinkebyController.createAccount',
'GET /rinkeby/get_balance/:account' : 'RinkebyController.getBalance',
// Metodos de BFA
'GET /bfa/verify/:ots/:file_hash' : 'BfaController.verify',
'GET /bfa/createAccount' : 'BfaController.createAccount',
'GET /bfa/create_account' : 'BfaController.createAccount',
'GET /bfa/get_balance/:account' : 'BfaController.getBalance',
// Metodos de RSK
'GET /rsk/verify/:ots/:file_hash' : 'RskController.verify',
'GET /rsk/createAccount' : 'RskController.createAccount',
'GET /rsk/get_balance/:account' : 'RskController.getBalance',
'GET /rsk/create_account' : 'RskController.createAccount',
'GET /rsk/get_balance/:account' : 'RskController.getBalance',
// Metodos de Celo
//'GET /rsk/verify/:ots/:file_hash' : 'RskController.verify',
'GET /celo/create_account' : 'CeloController.createAccount',
'GET /celo/get_balance/:account' : 'CeloController.getBalance',
// METODOS VARIOS
'GET /accounts/get_payments' : 'AccountController.getPayments',
//////////////////////////////////////
/////////// POST ////////////////////
//////////////////////////////////////
'POST /ropsten/stamp' : 'BlockchainController.stamp',
'POST /ropsten/send' : 'BlockchainController.send',
'POST /transaction/send' : 'TransactionController.send',
'POST /blockchain/stamp' : 'BlockchainController.stamp',
'POST /rinkeby/stamp' : 'RinkebyController.stamp',
'POST /bfa/stamp' : 'BfaController.stamp',
'POST /rinkeby/send' : 'RinkebyController.send',
'POST /rsk/stamp' : 'RskController.stamp',
'POST /rsk/send' : 'RskController.send',
'POST /bfa/stamp' : 'BfaController.stamp',
'POST /bfa/send' : 'BfaController.send',
/***************************************************************************
......
This diff is collapsed.
......@@ -5,9 +5,11 @@
"description": "a Sails application",
"keywords": [],
"dependencies": {
"@celo/contractkit": "^0.4.0",
"@sailshq/connect-redis": "^3.2.1",
"@sailshq/lodash": "^3.10.3",
"@sailshq/socket.io-redis": "^5.2.0",
"axios": "^0.19.2",
"ethereumjs-tx": "^1.3.7",
"grunt": "1.0.4",
"js-sha256": "^0.9.0",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment