docs: added cookbook entry to compute raw transaction (#1857).

This commit is contained in:
Richard Moore 2021-08-24 14:54:15 -03:00
parent 32a90b66f0
commit bdb54ac52b
No known key found for this signature in database
GPG Key ID: 665176BE8E9DC651
3 changed files with 28 additions and 1 deletions

@ -266,7 +266,9 @@ The number of blocks that have been mined (including the initial block) since th
transaction was mined. transaction was mined.
_property: transaction.raw => string<[[DataHexString]]> _property: transaction.raw => string<[[DataHexString]]>
The serialized transaction. The serialized transaction. This may be null as some backends do not
rpopulate it. If this is required, it can be computed from a **TransactionResponse**
object using [this cookbook recipe](cookbook--compute-raw-transaction).
_property: transaction.wait([ confirms = 1 ]) => Promise<[[providers-TransactionReceipt]]> _property: transaction.wait([ confirms = 1 ]) => Promise<[[providers-TransactionReceipt]]>
Resolves to the [[providers-TransactionReceipt]] once the transaction Resolves to the [[providers-TransactionReceipt]] once the transaction

@ -6,4 +6,5 @@ snippets of code that are in general useful.
_toc: _toc:
react-native react-native
transactions

@ -0,0 +1,24 @@
_section: Transactions @<cookbook--transactions>
_subsection: Compute the raw transaction @<cookbook--compute-raw-transaction>
_code: @lang<javascript>
function getRawTransaction(tx) {
function addKey(accum, key) {
if (tx[key]) { accum[key] = tx[key]; }
return accum;
}
// Extract the relevant parts of the transaction and signature
const txFields = "accessList chainId data gasPrice gasLimit maxFeePerGer maxPriorityFeePerGas nonce to type value".split(" ");
const sigFields = "v r s".split(" ");
// Seriailze the signed transaction
const raw = utils.serializeTransaction(txFields.reduce(addKey, { }), sigFields.reduce(addKey, { }));
// Double check things went well
if (utils.keccak256(raw) !== tx.hash) { throw new Error("serializing failed!"); }
return raw;
}