UNPKG

11.2 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.marshaller = void 0;
4const utils_1 = require("@0x/utils");
5const ethereum_types_1 = require("ethereum-types");
6const ethUtil = require("ethereumjs-util");
7const _ = require("lodash");
8const utils_2 = require("./utils");
9/**
10 * Utils to convert ethereum structures from user-space format to RPC format. (marshall/unmarshall)
11 */
12exports.marshaller = {
13 /**
14 * Unmarshall block without transaction data
15 * @param blockWithHexValues block to unmarshall
16 * @return unmarshalled block without transaction data
17 */
18 unmarshalIntoBlockWithoutTransactionData(blockWithHexValues) {
19 const block = Object.assign(Object.assign({}, blockWithHexValues), { gasLimit: utils_2.utils.convertHexToNumber(blockWithHexValues.gasLimit), gasUsed: utils_2.utils.convertHexToNumber(blockWithHexValues.gasUsed), size: utils_2.utils.convertHexToNumber(blockWithHexValues.size), timestamp: utils_2.utils.convertHexToNumber(blockWithHexValues.timestamp), number: blockWithHexValues.number === null ? null : utils_2.utils.convertHexToNumber(blockWithHexValues.number), difficulty: utils_2.utils.convertAmountToBigNumber(blockWithHexValues.difficulty), totalDifficulty: utils_2.utils.convertAmountToBigNumber(blockWithHexValues.totalDifficulty) });
20 return block;
21 },
22 /**
23 * Unmarshall block with transaction data
24 * @param blockWithHexValues block to unmarshall
25 * @return unmarshalled block with transaction data
26 */
27 unmarshalIntoBlockWithTransactionData(blockWithHexValues) {
28 const block = Object.assign(Object.assign({}, blockWithHexValues), { gasLimit: utils_2.utils.convertHexToNumber(blockWithHexValues.gasLimit), gasUsed: utils_2.utils.convertHexToNumber(blockWithHexValues.gasUsed), size: utils_2.utils.convertHexToNumber(blockWithHexValues.size), timestamp: utils_2.utils.convertHexToNumber(blockWithHexValues.timestamp), number: blockWithHexValues.number === null ? null : utils_2.utils.convertHexToNumber(blockWithHexValues.number), difficulty: utils_2.utils.convertAmountToBigNumber(blockWithHexValues.difficulty), totalDifficulty: utils_2.utils.convertAmountToBigNumber(blockWithHexValues.totalDifficulty), transactions: [] });
29 block.transactions = _.map(blockWithHexValues.transactions, (tx) => {
30 const transaction = exports.marshaller.unmarshalTransaction(tx);
31 return transaction;
32 });
33 return block;
34 },
35 /**
36 * Unmarshall transaction
37 * @param txRpc transaction to unmarshall
38 * @return unmarshalled transaction
39 */
40 unmarshalTransaction(txRpc) {
41 const tx = Object.assign(Object.assign({}, txRpc), { blockNumber: txRpc.blockNumber !== null ? utils_2.utils.convertHexToNumber(txRpc.blockNumber) : null, transactionIndex: txRpc.transactionIndex !== null ? utils_2.utils.convertHexToNumber(txRpc.transactionIndex) : null, nonce: utils_2.utils.convertHexToNumber(txRpc.nonce), gas: utils_2.utils.convertHexToNumber(txRpc.gas), gasPrice: txRpc.gasPrice !== undefined ? utils_2.utils.convertAmountToBigNumber(txRpc.gasPrice) : undefined, maxFeePerGas: txRpc.maxFeePerGas !== undefined ? utils_2.utils.convertAmountToBigNumber(txRpc.maxFeePerGas) : undefined, maxPriorityFeePerGas: txRpc.maxPriorityFeePerGas !== undefined
42 ? utils_2.utils.convertAmountToBigNumber(txRpc.maxPriorityFeePerGas)
43 : undefined, value: utils_2.utils.convertAmountToBigNumber(txRpc.value) });
44 return tx;
45 },
46 /**
47 * Unmarshall transaction receipt
48 * @param txReceiptRpc transaction receipt to unmarshall
49 * @return unmarshalled transaction receipt
50 */
51 unmarshalTransactionReceipt(txReceiptRpc) {
52 const txReceipt = Object.assign(Object.assign({}, txReceiptRpc), { blockNumber: utils_2.utils.convertHexToNumber(txReceiptRpc.blockNumber), transactionIndex: utils_2.utils.convertHexToNumber(txReceiptRpc.transactionIndex), cumulativeGasUsed: utils_2.utils.convertHexToNumber(txReceiptRpc.cumulativeGasUsed), gasUsed: utils_2.utils.convertHexToNumber(txReceiptRpc.gasUsed), logs: _.map(txReceiptRpc.logs, exports.marshaller.unmarshalLog.bind(exports.marshaller)) });
53 return txReceipt;
54 },
55 /**
56 * Unmarshall transaction data
57 * @param txDataRpc transaction data to unmarshall
58 * @return unmarshalled transaction data
59 */
60 unmarshalTxData(txDataRpc) {
61 if (txDataRpc.from === undefined) {
62 throw new Error(`txData must include valid 'from' value.`);
63 }
64 const txData = {
65 to: txDataRpc.to,
66 from: txDataRpc.from,
67 data: txDataRpc.data,
68 value: txDataRpc.value !== undefined ? utils_2.utils.convertAmountToBigNumber(txDataRpc.value) : undefined,
69 gas: txDataRpc.gas !== undefined ? utils_2.utils.convertHexToNumber(txDataRpc.gas) : undefined,
70 gasPrice: txDataRpc.gasPrice !== undefined ? utils_2.utils.convertAmountToBigNumber(txDataRpc.gasPrice) : undefined,
71 maxFeePerGas: txDataRpc.maxFeePerGas !== undefined
72 ? utils_2.utils.convertAmountToBigNumber(txDataRpc.maxFeePerGas)
73 : undefined,
74 maxPriorityFeePerGas: txDataRpc.maxPriorityFeePerGas !== undefined
75 ? utils_2.utils.convertAmountToBigNumber(txDataRpc.maxPriorityFeePerGas)
76 : undefined,
77 nonce: txDataRpc.nonce !== undefined ? utils_2.utils.convertHexToNumber(txDataRpc.nonce) : undefined,
78 };
79 return txData;
80 },
81 /**
82 * Marshall transaction data
83 * @param txData transaction data to marshall
84 * @return marshalled transaction data
85 */
86 marshalTxData(txData) {
87 if (txData.from === undefined) {
88 throw new Error(`txData must include valid 'from' value.`);
89 }
90 const callTxDataBase = Object.assign({}, txData);
91 delete callTxDataBase.from;
92 const callTxDataBaseRPC = exports.marshaller._marshalCallTxDataBase(callTxDataBase);
93 const txDataRPC = Object.assign(Object.assign({}, callTxDataBaseRPC), { from: exports.marshaller.marshalAddress(txData.from) });
94 const prunableIfUndefined = ['gasPrice', 'maxFeePerGas', 'maxPriorityFeePerGas', 'gas', 'value', 'nonce'];
95 _.each(txDataRPC, (value, key) => {
96 if (value === undefined && _.includes(prunableIfUndefined, key)) {
97 delete txDataRPC[key];
98 }
99 });
100 return txDataRPC;
101 },
102 /**
103 * Marshall call data
104 * @param callData call data to marshall
105 * @return marshalled call data
106 */
107 marshalCallData(callData) {
108 const callTxDataBase = Object.assign({}, callData);
109 delete callTxDataBase.from;
110 delete callTxDataBase.overrides;
111 const callTxDataBaseRPC = exports.marshaller._marshalCallTxDataBase(callTxDataBase);
112 const callDataRPC = Object.assign(Object.assign({}, callTxDataBaseRPC), { from: callData.from === undefined ? undefined : exports.marshaller.marshalAddress(callData.from) });
113 return callDataRPC;
114 },
115 /**
116 * Marshall call overrides parameter for for a geth eth_call.
117 * @param overrides overrides to marshal
118 * @return marshalled overrides
119 */
120 marshalCallOverrides(overrides) {
121 const marshalled = {};
122 for (const address in overrides) {
123 if (address) {
124 const override = overrides[address];
125 const marshalledAddress = exports.marshaller.marshalAddress(address);
126 const marshalledOverride = (marshalled[marshalledAddress] = {});
127 if (override.code !== undefined) {
128 marshalledOverride.code = override.code;
129 }
130 if (override.nonce !== undefined) {
131 marshalledOverride.nonce = utils_2.utils.encodeAmountAsHexString(override.nonce);
132 }
133 if (override.balance !== undefined) {
134 marshalledOverride.balance = utils_2.utils.encodeAmountAsHexString(override.balance);
135 }
136 if (Object.keys(marshalledOverride).length === 0) {
137 delete marshalled[marshalledAddress];
138 }
139 }
140 }
141 return marshalled;
142 },
143 /**
144 * Marshall address
145 * @param address address to marshall
146 * @return marshalled address
147 */
148 marshalAddress(address) {
149 if (utils_1.addressUtils.isAddress(address)) {
150 return ethUtil.addHexPrefix(address);
151 }
152 throw new Error(`Invalid address encountered: ${address}`);
153 },
154 /**
155 * Marshall block param
156 * @param blockParam block param to marshall
157 * @return marshalled block param
158 */
159 marshalBlockParam(blockParam) {
160 if (blockParam === undefined) {
161 return ethereum_types_1.BlockParamLiteral.Latest;
162 }
163 const encodedBlockParam = _.isNumber(blockParam) ? utils_2.utils.numberToHex(blockParam) : blockParam;
164 return encodedBlockParam;
165 },
166 /**
167 * Unmarshall log
168 * @param rawLog log to unmarshall
169 * @return unmarshalled log
170 */
171 unmarshalLog(rawLog) {
172 const formattedLog = Object.assign(Object.assign({}, rawLog), { logIndex: utils_2.utils.convertHexToNumberOrNull(rawLog.logIndex), blockNumber: utils_2.utils.convertHexToNumberOrNull(rawLog.blockNumber), transactionIndex: utils_2.utils.convertHexToNumberOrNull(rawLog.transactionIndex) });
173 return formattedLog;
174 },
175 _marshalCallTxDataBase(callTxDataBase) {
176 let accessList;
177 if (callTxDataBase.accessList && Object.keys(callTxDataBase.accessList).length) {
178 accessList = Object.entries(callTxDataBase.accessList).map(([address, storageKeys]) => ({
179 address,
180 storageKeys,
181 }));
182 }
183 const callTxDataBaseRPC = {
184 data: callTxDataBase.data,
185 to: callTxDataBase.to === undefined ? undefined : exports.marshaller.marshalAddress(callTxDataBase.to),
186 gasPrice: callTxDataBase.gasPrice === undefined
187 ? undefined
188 : utils_2.utils.encodeAmountAsHexString(callTxDataBase.gasPrice),
189 maxFeePerGas: callTxDataBase.maxFeePerGas === undefined
190 ? undefined
191 : utils_2.utils.encodeAmountAsHexString(callTxDataBase.maxFeePerGas),
192 maxPriorityFeePerGas: callTxDataBase.maxPriorityFeePerGas === undefined
193 ? undefined
194 : utils_2.utils.encodeAmountAsHexString(callTxDataBase.maxPriorityFeePerGas),
195 gas: callTxDataBase.gas === undefined ? undefined : utils_2.utils.encodeAmountAsHexString(callTxDataBase.gas),
196 value: callTxDataBase.value === undefined ? undefined : utils_2.utils.encodeAmountAsHexString(callTxDataBase.value),
197 nonce: callTxDataBase.nonce === undefined ? undefined : utils_2.utils.encodeAmountAsHexString(callTxDataBase.nonce),
198 accessList,
199 };
200 return callTxDataBaseRPC;
201 },
202};
203//# sourceMappingURL=marshaller.js.map
\No newline at end of file