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 {
// (o defaultAccount si no se especifica)
async stamp(objects, walletAccount) {
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
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',
if (objectsToStamp.length == 0) return new Promise( (resolve) => {
console.log(`Los objects enviados ya están stampeados`)
resolve(objectsStamped)
})
console.log(`stamping ` + objectsToStamp.join(', '));
let txPromise
let gasLimit = 2000000
if (walletAccount) {
let methodPut = this.contract.methods.put(objectsToStamp)
let encodedABI = methodPut.encodeABI()
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`)
resolve(objectsStamped)
})
console.log(`stamping ` + objectsToStamp.join(', '));
let txPromise
let gasLimit = 2000000
if (walletAccount) {
let methodPut = this.contract.methods.put(objectsToStamp)
let encodedABI = methodPut.encodeABI()
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: '-',
};
for (let i=0; i < objectsToStamp.length; i++) {
// Creo un nuevo objeto con la info de la tx realizada
let new_object = {
hash: objectsToStamp[i],
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
})
}
for (let i=0; i < objectsToStamp.length; i++) {
// Creo un nuevo objeto con la info de la tx realizada
let new_object = {
hash: objectsToStamp[i],
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
return objectsStamped;
} catch (e) {
console.error(e)
throw e
}
// Retorno un array con todos los objetos stampados
return objectsStamped;
// Código anterior, comentado a la espera de confirmar el cambio
/*txPromise.then((receipt) => {
console.log(`> objects stampeados en bloque: ${receipt.blockNumber}`)
console.log(`> Hash de la Tx: ${receipt.transactionHash}`)
console.log(`> Hash/es enviado/s:`)
console.log(objectsToStamp)
}).catch((error) => {
console.error(error)
})*/
/*return new Promise((resolve, reject) => {
txPromise.on('transactionHash', (txHash) => {
resolve(objectsStamped)
})
txPromise.catch((error) => {
reject(error)
})
})*/
// Código anterior, comentado a la espera de confirmar el cambio
/*txPromise.then((receipt) => {
console.log(`> objects stampeados en bloque: ${receipt.blockNumber}`)
console.log(`> Hash de la Tx: ${receipt.transactionHash}`)
console.log(`> Hash/es enviado/s:`)
console.log(objectsToStamp)
}).catch((error) => {
console.error(error)
})*/
/*return new Promise((resolve, reject) => {
txPromise.on('transactionHash', (txHash) => {
resolve(objectsStamped)
})
txPromise.catch((error) => {
reject(error)
})
})*/
}
async wait1block() {
......
......@@ -20,6 +20,7 @@
</div>
<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>Hash de la TX: <b>{{value.tx_hash}}</b></p>
<div class="copiar">
<label class="font_small" v-html="lb_16" :for="'id_'+index"></label>
<div class="input-group">
......
......@@ -236,12 +236,17 @@ export default {
//Agrego nueva info para ser mostrada por pantalla, en cas
if(hash == response.data.txHash[k].hash){
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);
} else {
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