Skip to content
Snippets Groups Projects
Commit f8b9767b authored by Andrés Blanco's avatar Andrés Blanco
Browse files

Mejoras en parametrización de contrato

parent da2d9422
No related branches found
No related tags found
No related merge requests found
const stamperInterface = require('../../contract/build/contracts/Stamper.json')
//const stamperInterface = require('../../contract/build/contracts/Stamper.json')
class Stamper {
constructor(web3, netId) {
constructor(web3, contractAbi, contractAddress) {
this.web3 = web3
let address = stamperInterface.networks[netId].address
this.contract = new web3.eth.Contract(stamperInterface.abi, address)
this.contract = new web3.eth.Contract(contractAbi, contractAddress)
}
setSender(fromAddress) {
......@@ -32,7 +31,8 @@ class Stamper {
})
txPromise.then((receipt) => {
console.log(`${hash}: stamped (bloque: ${receipt.blockNumber})`)
console.log(`> hashes stampeados en bloque: ${receipt.blockNumber}`)
console.log(hashesToStamp)
}).catch((error) => {
console.error(error)
})
......@@ -51,6 +51,7 @@ class Stamper {
try {
let count = await this.contract.methods.getObjectCount(hash).call()
if (count == 0) {
console.log(`fallo verificación ${hash}`)
return { stamped: false, stamps: [] }
}
......@@ -62,6 +63,7 @@ class Stamper {
stamps.push({ stamper: stamp[1], block: stamp[2].toString() })
}
console.log(`exito verificación ${hash}`)
return { stamped: true, stamps: stamps }
} catch (e) {
console.error(e)
......
......@@ -16,6 +16,7 @@ const accountIsSet = process.env.GETH_ACCOUNT || false
// si no se seteó una account se usa esta const como el indice de accounts de ganache
const account = (accountIsSet) ? process.env.GETH_ACCOUNT : 1
var web3 = web3
if (typeof web3 !== 'undefined') {
console.log('Cargando web3 provider desde el entorno')
......@@ -45,6 +46,28 @@ async function setupWeb3() {
}
}
/***************************************************/
// Carga de contrato
/***************************************************/
let contractAbi;
let contractAddress;
if (process.env.CONTRACT_ABI_PATH) {
contractAbi = require(process.env.CONTRACT_ABI_PATH)
if (!process.env.CONTRACT_ADDRESS) {
console.error('Si se especifica el path de un abi, debe proveerse un address con la env CONTRACT_ADDRESS')
process.exit(1)
}
contractAddress = process.env.CONTRACT_ADDRESS
} else {
let path = '../../contract/build/contracts/Stamper.json'
console.log(`Intentando cargar ${path}`)
let data = require(path)
contractAbi = data.abi
web3.eth.net.getId().then(function(netId) {
contractAddress = data.networks[netId].address
})
}
/***************************************************/
// Setup API
/***************************************************/
......@@ -72,8 +95,7 @@ if (process.env.API_USER && process.env.API_PASS) {
// API Endpoints
/***************************************************/
app.post('/stamp', async (req, res) => {
let netId = await web3.eth.net.getId()
let ss = new Stamper(web3, netId)
let ss = new Stamper(web3, contractAbi, contractAddress)
ss.setSender(web3.eth.defaultAccount)
if (!("hashes" in req.body)) {
......@@ -109,8 +131,7 @@ app.post('/stamp', async (req, res) => {
})
app.get('/verify/:hash', async (req, res) => {
let netId = await web3.eth.net.getId()
let ss = new Stamper(web3, netId)
let ss = new Stamper(web3, contractAbi, contractAddress)
ss.setSender(web3.eth.defaultAccount)
var value = req.params.hash
......
VUE_APP_API_URL=http://localhost:3000
\ No newline at end of file
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