let provider_url =
"https://ropsten.infura.io/v3/d5bca90ec3084aee8abd468fdf876a11";
this.web3Provider = new Web3.providers.HttpProvider(provider_url);
window.web3_ = new Web3(this.web3Provider);
let account = "0x25bA673A96acadD7A02f4c5834Ba80C1AF6b7758";
let nonce = window.web3_.toHex(window.web3_.eth.getTransactionCount(account));
let myPrivateKey = "****";
let privateKey = new Buffer(myPrivateKey, "hex");
let functionName = "markAttendance";
let types = ["address", "uint", "uint256"];
let args = [attendeeAddress, opinion, date];
let fullName = functionName + "(" + types.join() + ")";
let signature = CryptoJS.SHA3(fullName, {outputLength: 256}).toString(CryptoJS.enc.Hex).slice(0, 8);
let dataHex = signature + coder.encodeParams(types, args);
let data = "0x" + dataHex;
// let nonce = web3.toHex();
let gasPrice = window.web3_.toHex(20000000000); // 20 Gwei
let gasLimitHex = window.web3_.toHex(4700000);
let rawTx = {
'nonce': nonce,
'gasPrice': gasPrice,
'gasLimit': gasLimitHex,
'from': account,
'to': '0xf77c958bdffee94c3f53eb763cffd646cde336d9',
'data': data,
'value': window.web3_.toHex(window.web3_.toWei("0.5", "ether")),
}
let tx = new Tx(rawTx);
tx.sign(privateKey);
tx.serialize();
console.log(tx.validate());
let serializedTx = '0x' + tx.serialize().toString('hex');
console.log(serializedTx);
window.web3_.eth.sendRawTransaction(serializedTx, function (err, txHash) {
console.log(err, txHash)
})
Contrato
contract MarkAttendance {
struct AttendeeDetails {
address attendance_giver;
address attendee;
uint attendance_opinion;
uint256 timestamp;
uint256 date_of_attendance;
}
//mapping of structure for storing the attendeeDetails
mapping(uint => AttendeeDetails) public attendeeDetails;
uint public attendeeDetailsCount;
function markAttendance(address _attendee, uint _attendance_opinion, uint256 _date) public {
attendeeDetailsCount ++;
attendeeDetails[attendeeDetailsCount] = AttendeeDetails(msg.sender, _attendee, _attendance_opinion, now, _date);
}
}
Recibo TXHASH pero la transacción se cancela cada vez. ¿Cuál podría ser el problema que estoy haciendo? ¿Alguien puede sugerirme alguna solución para esto?
Funciona bien si llamo al método desde la consola de trufas. Obtengo un recibo de transacción exitosa.
El cálculo de su selector de funciones es incorrecto. Estás usando "uint"
en lugar de "uint256"
. (El primero es un alias para el segundo, pero este último siempre debe usarse al calcular un selector de función).
Cambia esta línea:
let types = ["address", "uint", "uint256"];
a esto:
let types = ["address", "uint256", "uint256"];
EDITAR
También está adjuntando ether a la transacción, pero la función a la que llama no lo es, payable
por lo que no aceptará ether. Para solucionar esto, suelte la value: ...
línea.
payable
función que no es.
usuario19510
Shamit Verma
Mahesh Rajput
rawTx
es para:0x7145dc9549f548bef6bb2916e063cc0404d2e8d9
y su dirección de contrato real es0x7145dc9549f548bEF6BB2916e063CC0404d2E8d8
. hay un ligero cambio en el último carácter9
de8
. Corrígelo.usuario19510
Mahesh Rajput
usuario19510
sibabrat swain
sibabrat swain
sibabrat swain
Sr. Y
Mahesh Rajput
0.5 ether
para llamar a esta función? no es necesario, simplemente eliminevalue
parte de rawTx y luego intente.