Skip to content

Commit

Permalink
docs: added cookbook entry to compute raw transaction (#1857).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Aug 24, 2021
1 parent 32a90b6 commit bdb54ac
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
4 changes: 3 additions & 1 deletion docs.wrm/api/providers/types.wrm
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,9 @@ The number of blocks that have been mined (including the initial block) since th
transaction was mined.

_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]]>
Resolves to the [[providers-TransactionReceipt]] once the transaction
Expand Down
1 change: 1 addition & 0 deletions docs.wrm/cookbook/index.wrm
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ snippets of code that are in general useful.
_toc:

react-native
transactions

24 changes: 24 additions & 0 deletions docs.wrm/cookbook/transactions.wrm
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit bdb54ac

Please sign in to comment.