Open hackfisher opened 5 years ago
https://github.com/tronprotocol/tron-web/blob/master/src/lib/contract/method.js#L163
const address = privateKey ? this.tronWeb.address.fromPrivateKey(privateKey) : this.tronWeb.defaultAddress.base58;
const transaction = await this.tronWeb.transactionBuilder.triggerSmartContract(
this.contract.address,
this.functionSelector,
options.feeLimit,
options.callValue,
parameters,
this.tronWeb.address.toHex(address)
);
if (!transaction.result || !transaction.result.result)
return callback('Unknown error: ' + JSON.stringify(transaction, null, 2));
// If privateKey is false, this won't be signed here. We assume sign functionality will be replaced.
const signedTransaction = await this.tronWeb.trx.sign(transaction.transaction, privateKey);
if (!signedTransaction.signature) {
if (!privateKey)
return callback('Transaction was not signed properly');
return callback('Invalid private key provided');
}
const broadcast = await this.tronWeb.trx.sendRawTransaction(signedTransaction);
unsigned contract sample
{
"result": {
"result": true
},
"txid": "89b6d57aa1502f39a427a0b0509fb93efe0de4511f122db565f40b560e05286a",
"transaction": {
"txID": "a11f18168598d912b43abdf102d098da1bf753c073f9718db30a50423d0f75bb",
"raw_data": {
"contract": [
{
"parameter": {
"value": {
"data": "be45fd62000000000000000000000000c29b9bfba00ec2a2fb0b9d881c7924b89299cbf40000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000001",
"owner_address": "41d25da92f48c771e1f62e9b62b514cdd05fef9abb",
"contract_address": "4130f6395b0759f55918cd6dc35d84837734dd032c"
},
"type_url": "type.googleapis.com/protocol.TriggerSmartContract"
},
"type": "TriggerSmartContract"
}
],
"ref_block_bytes": "1ae3",
"ref_block_hash": "9286615879a965bb",
"expiration": 1544696310000,
"fee_limit": 1000000000,
"timestamp": 1544696252318
}
}
}
Tron do not have nonce, so require to fetch the latest block's ref_block_hash and ref_block_bytes for the online network.
transaction with callValue:
{
"result": true,
"transaction": {
"txID": "90b86d51c6b9b8802453f32697b000748e298911ba92dc4bbe338768e3bd8ed1",
"raw_data": {
"contract": [
{
"parameter": {
"value": {
"data": "be45fd62000000000000000000000000ec0842c56304a4679c5417d9347ad0072b3f61cb0000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000001",
"owner_address": "41d25da92f48c771e1f62e9b62b514cdd05fef9abb",
"contract_address": "41740e99def7e22b56baf4f9d3102f11bf992e1a1d",
"call_value": 1111
},
"type_url": "type.googleapis.com/protocol.TriggerSmartContract"
},
"type": "TriggerSmartContract"
}
],
"ref_block_bytes": "ae71",
"ref_block_hash": "12c921b9e78d9aea",
"expiration": 1545473388000,
"fee_limit": 1000000000,
"timestamp": 1545473329342
},
"signature": [
"f4fd1cf078975b485f586f79cb950dc7e0899380d71683a93a52d6630457c613a44a2a866b05f3692ecb0acb2f71d66d0fe9498fe53a513b78823d1fcfed460800"
]
}
}
https://github.com/tronprotocol/tron-web/issues/104