Skip to content
Snippets Groups Projects

Markup

Merged Otto Zaiser requested to merge markup into develop
13 files
+ 3082
65
Compare changes
  • Side-by-side
  • Inline
Files
13
+ 38
10
import { sign } from "crypto";
//const stamperInterface = require('../../contract/build/contracts/Stamper.json')
class Stamper {
constructor(web3, contractAbi, contractAddress) {
this.web3 = web3
this.contractAddress = contractAddress
this.contract = new web3.eth.Contract(contractAbi, contractAddress)
}
setSender(fromAddress) {
this.from = fromAddress
}
async stamp(hashes) {
// stampea un conjunto de hashes.
// si walletAccount es undefined trata de usar la account de web3.eth.defaultAccount
async stamp(hashes, walletAccount) {
console.log(`stamping ${hashes}`)
let defaultAccount = (walletAccount) ? walletAccount.address : this.web3.eth.defaultAccount
let hashesToStamp = []
for (let i=0; i < hashes.length; i++) {
let blockNo = await this.contract.methods.getBlockNo(hashes[i], this.from).call()
let blockNo = await this.contract.methods.getBlockNo(hashes[i], defaultAccount).call()
if (blockNo == 0) hashesToStamp.push(hashes[i])
}
@@ -25,10 +27,36 @@ class Stamper {
resolve()
})
let txPromise = this.contract.methods.put(hashesToStamp).send({
from: this.from,
gasLimit: 500000
})
let txPromise
let gasLimit = 2000000
if (walletAccount) {
let methodPut = this.contract.methods.put(hashesToStamp)
let encodedABI = methodPut.encodeABI()
let tx = {
to: this.contractAddress,
// v: 47525974938 * 35 + 2,
// v: 47525974938,
chainId: '200941592',
gas: gasLimit,
// gasLimit: gasLimit,
data: encodedABI
}
// 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 = this.web3.eth.sendSignedTransaction(signedTx.rawTransaction)
} else {
txPromise = this.contract.methods.put(hashesToStamp).send({
from: defaultAccount,
gasLimit: gasLimit
})
}
txPromise.then((receipt) => {
console.log(`> hashes stampeados en bloque: ${receipt.blockNumber}`)
Loading