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

Agregando try catch en API. Mostrando el hash de la TX al stampar.

parent 9d78268f
No related branches found
No related tags found
No related merge requests found
...@@ -12,117 +12,121 @@ class Stamper { ...@@ -12,117 +12,121 @@ class Stamper {
// (o defaultAccount si no se especifica) // (o defaultAccount si no se especifica)
async stamp(objects, walletAccount) { async stamp(objects, walletAccount) {
console.log(`asked to stamp ${objects}`) console.log(`asked to stamp ${objects}`)
try {
// si walletAccount es undefined trata de usar la account de web3.eth.defaultAccount
let defaultAccount = (walletAccount) ? walletAccount.address : this.web3.eth.defaultAccount
let objectsToStamp = [] // Guardo los hashes que seran enviados a la BFA
let objectsStamped = [] // Guardo los objetos que ya fueron enviados a la BFA
for (let i=0; i < objects.length; i++) {
let blockNo = await this.contract.methods.getBlockNo(objects[i], defaultAccount).call()
if (blockNo == 0){
objectsToStamp.push(objects[i]);
} else {
console.log(`already stamped: ` + objects[i] + ' blockNro: ' + blockNo);
let new_object = {
hash: objects[i],
block_number: blockNo,
status: 'already_stamped_by_this_TSA',
}
objectsStamped.push(new_object);
}
}
// si walletAccount es undefined trata de usar la account de web3.eth.defaultAccount if (objectsToStamp.length == 0) return new Promise( (resolve) => {
let defaultAccount = (walletAccount) ? walletAccount.address : this.web3.eth.defaultAccount console.log(`Los objects enviados ya están stampeados`)
let objectsToStamp = [] // Guardo los hashes que seran enviados a la BFA resolve(objectsStamped)
let objectsStamped = [] // Guardo los objetos que ya fueron enviados a la BFA })
for (let i=0; i < objects.length; i++) { console.log(`stamping ` + objectsToStamp.join(', '));
let blockNo = await this.contract.methods.getBlockNo(objects[i], defaultAccount).call()
if (blockNo == 0){ let txPromise
objectsToStamp.push(objects[i]); let gasLimit = 2000000
} else {
console.log(`already stamped: ` + objects[i] + ' blockNro: ' + blockNo); if (walletAccount) {
let new_object = { let methodPut = this.contract.methods.put(objectsToStamp)
hash: objects[i], let encodedABI = methodPut.encodeABI()
block_number: blockNo,
status: 'already_stamped_by_this_TSA', let tx = {
to: this.contractAddress,
// v: 47525974938 * 35 + 2,
// v: 47525974938,
// Parece que sin chainId funciona igual - hasta a veces mejor. Pero en la red Testnet, hay que agregar el chainID 99118822
chainId: '99118822',
gas: gasLimit,
// gasLimit: gasLimit,
data: encodedABI,
nonce: this.web3.bfa.txnonce++
}
// tx.v = Buffer.from([47525974938])
// tx.nonce = this.web3.utils.toHex(await this.web3.eth.getTransactionCount(defaultAccount))
let signedTx = await walletAccount.signTransaction(tx)
// console.log(signedTx)
// txPromise = this.web3.eth.sendSignedTransaction(signedTx)
// txPromise = this.web3.eth.sendSignedTransaction('0x' + signedTx.serialize().toString('hex'))
txPromise = await this.web3.eth.sendSignedTransaction(signedTx.rawTransaction) // Apliqué el await para que espere a que finalice la operación
let tx_result = []; // Objeto que contiene la info de la TX
if(txPromise.status == true){
tx_result = {
status: 'stamped',
block_number: txPromise.blockNumber,
hash: txPromise.transactionHash,
};
} else {
tx_result = {
status: 'error',
block_number: '-',
hash: '-',
};
} }
objectsStamped.push(new_object);
}
}
if (objectsToStamp.length == 0) return new Promise( (resolve) => {
console.log(`Los objects enviados ya están stampeados`) for (let i=0; i < objectsToStamp.length; i++) {
resolve(objectsStamped) // Creo un nuevo objeto con la info de la tx realizada
}) let new_object = {
hash: objectsToStamp[i],
console.log(`stamping ` + objectsToStamp.join(', ')); block_number: tx_result.block_number,
status: tx_result.status,
let txPromise tx_hash: tx_result.hash,
let gasLimit = 2000000 }
// Agrego el objeto al array de objetos stampados (incluye los que ya fueron stampados, si los hubiese, y los nuevos)
if (walletAccount) { objectsStamped.push(new_object)
let methodPut = this.contract.methods.put(objectsToStamp) }
let encodedABI = methodPut.encodeABI() } else {
txPromise = await this.contract.methods.put(objectsToStamp).send({
let tx = { from: defaultAccount,
to: this.contractAddress, gasLimit: gasLimit
// v: 47525974938 * 35 + 2, })
// v: 47525974938,
// Parece que sin chainId funciona igual - hasta a veces mejor. Pero en la red Testnet, hay que agregar el chainID 99118822
chainId: '99118822',
gas: gasLimit,
// gasLimit: gasLimit,
data: encodedABI,
nonce: this.web3.bfa.txnonce++
}
// tx.v = Buffer.from([47525974938])
// tx.nonce = this.web3.utils.toHex(await this.web3.eth.getTransactionCount(defaultAccount))
let signedTx = await walletAccount.signTransaction(tx)
// console.log(signedTx)
// txPromise = this.web3.eth.sendSignedTransaction(signedTx)
// txPromise = this.web3.eth.sendSignedTransaction('0x' + signedTx.serialize().toString('hex'))
txPromise = await this.web3.eth.sendSignedTransaction(signedTx.rawTransaction) // Apliqué el await para que espere a que finalice la operación
let tx_result = []; // Objeto que contiene la info de la TX
if(txPromise.status == true){
tx_result = {
status: 'stamped',
block_number: txPromise.blockNumber,
hash: txPromise.transactionHash,
};
} else {
tx_result = {
status: 'error',
block_number: '-',
hash: '-',
};
} }
// Retorno un array con todos los objetos stampados
for (let i=0; i < objectsToStamp.length; i++) { return objectsStamped;
// Creo un nuevo objeto con la info de la tx realizada } catch (e) {
let new_object = { console.error(e)
hash: objectsToStamp[i], throw e
block_number: tx_result.block_number,
status: tx_result.status,
tx_hash: tx_result.hash,
}
// Agrego el objeto al array de objetos stampados (incluye los que ya fueron stampados, si los hubiese, y los nuevos)
objectsStamped.push(new_object)
}
} else {
txPromise = await this.contract.methods.put(objectsToStamp).send({
from: defaultAccount,
gasLimit: gasLimit
})
} }
// Retorno un array con todos los objetos stampados // Código anterior, comentado a la espera de confirmar el cambio
return objectsStamped; /*txPromise.then((receipt) => {
console.log(`> objects stampeados en bloque: ${receipt.blockNumber}`)
// Código anterior, comentado a la espera de confirmar el cambio console.log(`> Hash de la Tx: ${receipt.transactionHash}`)
/*txPromise.then((receipt) => { console.log(`> Hash/es enviado/s:`)
console.log(`> objects stampeados en bloque: ${receipt.blockNumber}`) console.log(objectsToStamp)
console.log(`> Hash de la Tx: ${receipt.transactionHash}`) }).catch((error) => {
console.log(`> Hash/es enviado/s:`) console.error(error)
console.log(objectsToStamp) })*/
}).catch((error) => {
console.error(error) /*return new Promise((resolve, reject) => {
})*/ txPromise.on('transactionHash', (txHash) => {
resolve(objectsStamped)
/*return new Promise((resolve, reject) => { })
txPromise.on('transactionHash', (txHash) => { txPromise.catch((error) => {
resolve(objectsStamped) reject(error)
}) })
txPromise.catch((error) => { })*/
reject(error)
})
})*/
} }
async wait1block() { async wait1block() {
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
</div> </div>
<div v-if="value.status == 'stamped'" class="success-verify alert alert-success" role="alert"> <div v-if="value.status == 'stamped'" class="success-verify alert alert-success" role="alert">
<p><span class="glyphicon glyphicon-ok" aria-hidden="true"></span> <span v-html="lb_00"></span> <b>{{value.fileName}}</b> <span v-html="lb_01"></span></p> <p><span class="glyphicon glyphicon-ok" aria-hidden="true"></span> <span v-html="lb_00"></span> <b>{{value.fileName}}</b> <span v-html="lb_01"></span></p>
<p>Hash de la TX: <b>{{value.tx_hash}}</b></p>
<div class="copiar"> <div class="copiar">
<label class="font_small" v-html="lb_16" :for="'id_'+index"></label> <label class="font_small" v-html="lb_16" :for="'id_'+index"></label>
<div class="input-group"> <div class="input-group">
......
...@@ -236,12 +236,17 @@ export default { ...@@ -236,12 +236,17 @@ export default {
//Agrego nueva info para ser mostrada por pantalla, en cas //Agrego nueva info para ser mostrada por pantalla, en cas
if(hash == response.data.txHash[k].hash){ if(hash == response.data.txHash[k].hash){
self.uploadedFiles[i].block = response.data.txHash[k].block_number; self.uploadedFiles[i].block = response.data.txHash[k].block_number;
self.uploadedFiles[i].status = response.data.txHash[k].status; self.uploadedFiles[i].status = response.data.txHash[k].status;
if(response.data.txHash[k].status == 'stamped'){
self.uploadedFiles[i].tx_hash = response.data.txHash[k].tx_hash;
}
} }
} }
} }
console.log(self.uploadedFiles);
self.$emit('stamp', self.uploadedFiles); self.$emit('stamp', self.uploadedFiles);
} else { } else {
self.$emit('failed-stamp') self.$emit('failed-stamp')
......
  • Developer

    Hola Renzo, en la UI estoy tratando de dejar el wording como atributo de la app en el HTML para que sea fácil de cambiar sin tener que volver a compilar y que se pueda cambiar/traducir en caso de ser necesario. Me refiero a la línea 23: Hash de la TX:

  • Author Developer

    Ah dale, entonces genero un nuevo atributo con esa frase, te parece ? Respecto a VUE soy nuevo, por ahí si vez que meto mano y no corresponde, no dudes en avisar !!

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