1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.marshaller = void 0;
|
4 | const utils_1 = require("@0x/utils");
|
5 | const ethereum_types_1 = require("ethereum-types");
|
6 | const ethUtil = require("ethereumjs-util");
|
7 | const _ = require("lodash");
|
8 | const utils_2 = require("./utils");
|
9 |
|
10 |
|
11 |
|
12 | exports.marshaller = {
|
13 | |
14 |
|
15 |
|
16 |
|
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 |
|
24 |
|
25 |
|
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 |
|
37 |
|
38 |
|
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 |
|
48 |
|
49 |
|
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 |
|
57 |
|
58 |
|
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 |
|
83 |
|
84 |
|
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 |
|
104 |
|
105 |
|
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 |
|
117 |
|
118 |
|
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 |
|
145 |
|
146 |
|
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 |
|
156 |
|
157 |
|
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 |
|
168 |
|
169 |
|
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 |
|
\ | No newline at end of file |