This repository was archived by the owner on Mar 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Expand file tree
/
Copy pathtransaction_builder.ts
More file actions
247 lines (218 loc) · 7.88 KB
/
transaction_builder.ts
File metadata and controls
247 lines (218 loc) · 7.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
import {
DEFAULT_RETURN_FORMAT,
ETH_DATA_FORMAT,
FormatType,
format,
DataFormat,
isAddress,
} from 'web3-utils';
import {
EthExecutionAPI,
Address,
HexString,
ValidChains,
Hardfork,
Transaction,
TransactionWithFromLocalWalletIndex,
TransactionWithToLocalWalletIndex,
TransactionWithFromAndToLocalWalletIndex,
Common,
Web3NetAPI,
Numbers,
} from 'web3-types';
import { Web3Context } from 'web3-core';
import { privateKeyToAddress } from 'web3-eth-accounts';
import { getId } from 'web3-net';
import { isNullish, isNumber } from 'web3-validator';
import {
InvalidTransactionWithSender,
InvalidTransactionWithReceiver,
LocalWalletNotAvailableError,
TransactionDataAndInputError,
UnableToPopulateNonceError,
} from 'web3-errors';
import { NUMBER_DATA_FORMAT } from '../constants';
// eslint-disable-next-line import/no-cycle
import { getChainId, getTransactionCount } from '../rpc_method_wrappers';
import { detectTransactionType } from './detect_transaction_type';
// eslint-disable-next-line import/no-cycle
import { getTransactionGasPricing } from './get_transaction_gas_pricing';
import { transactionSchema } from '../schemas';
import { InternalTransaction } from '../types';
export const getTransactionFromOrToAttr = (
attr: 'from' | 'to',
web3Context: Web3Context<EthExecutionAPI>,
transaction?:
| Transaction
| TransactionWithFromLocalWalletIndex
| TransactionWithToLocalWalletIndex
| TransactionWithFromAndToLocalWalletIndex,
privateKey?: HexString | Buffer,
): Address | undefined => {
if (transaction !== undefined && attr in transaction && transaction[attr] !== undefined) {
if (typeof transaction[attr] === 'string' && isAddress(transaction[attr] as string)) {
return transaction[attr] as Address;
}
if (isNumber(transaction[attr] as Numbers)) {
if (web3Context.wallet) {
const account = web3Context.wallet.get(
format({ eth: 'uint' }, transaction[attr] as Numbers, NUMBER_DATA_FORMAT),
);
if (!isNullish(account)) {
return account.address;
}
throw new LocalWalletNotAvailableError();
}
throw new LocalWalletNotAvailableError();
} else {
throw attr === 'from'
? new InvalidTransactionWithSender(transaction.from)
: // eslint-disable-next-line @typescript-eslint/no-unsafe-call
new InvalidTransactionWithReceiver(transaction.to);
}
}
if (attr === 'from') {
if (!isNullish(privateKey)) return privateKeyToAddress(privateKey);
if (!isNullish(web3Context.defaultAccount)) return web3Context.defaultAccount;
}
return undefined;
};
export const getTransactionNonce = async <ReturnFormat extends DataFormat>(
web3Context: Web3Context<EthExecutionAPI>,
address?: Address,
returnFormat: ReturnFormat = DEFAULT_RETURN_FORMAT as ReturnFormat,
) => {
if (isNullish(address)) {
// TODO if (web3.eth.accounts.wallet) use address from local wallet
throw new UnableToPopulateNonceError();
}
return getTransactionCount(web3Context, address, web3Context.defaultBlock, returnFormat);
};
export const getTransactionType = (
transaction: FormatType<Transaction, typeof ETH_DATA_FORMAT>,
web3Context: Web3Context<EthExecutionAPI>,
) => {
const inferredType = detectTransactionType(transaction, web3Context);
if (!isNullish(inferredType)) return inferredType;
if (!isNullish(web3Context.defaultTransactionType))
return format({ eth: 'uint' }, web3Context.defaultTransactionType, ETH_DATA_FORMAT);
return undefined;
};
// Keep in mind that the order the properties of populateTransaction get populated matters
// as some of the properties are dependent on others
export async function defaultTransactionBuilder<ReturnType = Record<string, unknown>>(options: {
transaction: Record<string, unknown>;
web3Context: Web3Context<EthExecutionAPI & Web3NetAPI>;
privateKey?: HexString | Buffer;
}): Promise<ReturnType> {
// let populatedTransaction = { ...options.transaction } as unknown as InternalTransaction;
let populatedTransaction = format(
transactionSchema,
options.transaction,
DEFAULT_RETURN_FORMAT,
) as InternalTransaction;
if (isNullish(populatedTransaction.from)) {
populatedTransaction.from = getTransactionFromOrToAttr(
'from',
options.web3Context,
undefined,
options.privateKey,
);
}
// TODO: Debug why need to typecase getTransactionNonce
if (isNullish(populatedTransaction.nonce)) {
populatedTransaction.nonce = await getTransactionNonce(
options.web3Context,
populatedTransaction.from,
ETH_DATA_FORMAT,
);
}
if (isNullish(populatedTransaction.value)) {
populatedTransaction.value = '0x';
}
if (!isNullish(populatedTransaction.data) && !isNullish(populatedTransaction.input)) {
throw new TransactionDataAndInputError({
data: populatedTransaction.data,
input: populatedTransaction.input,
});
} else if (!isNullish(populatedTransaction.input)) {
populatedTransaction.data = populatedTransaction.input;
delete populatedTransaction.input;
}
if (isNullish(populatedTransaction.data) || populatedTransaction.data === '') {
populatedTransaction.data = '0x';
} else if (!populatedTransaction.data.startsWith('0x')) {
populatedTransaction.data = `0x${populatedTransaction.data}`;
}
if (isNullish(populatedTransaction.common)) {
if (options.web3Context.defaultCommon) {
const common = options.web3Context.defaultCommon as unknown as Common;
const chainId = common.customChain.chainId as string;
const networkId = common.customChain.networkId as string;
const name = common.customChain.name as string;
populatedTransaction.common = {
...common,
customChain: { chainId, networkId, name },
};
}
if (isNullish(populatedTransaction.chain)) {
populatedTransaction.chain = options.web3Context.defaultChain as ValidChains;
}
if (isNullish(populatedTransaction.hardfork)) {
populatedTransaction.hardfork = options.web3Context.defaultHardfork as Hardfork;
}
}
if (
isNullish(populatedTransaction.chainId) &&
isNullish(populatedTransaction.common?.customChain.chainId)
) {
populatedTransaction.chainId = await getChainId(options.web3Context, ETH_DATA_FORMAT);
}
if (isNullish(populatedTransaction.networkId)) {
populatedTransaction.networkId =
(options.web3Context.defaultNetworkId as string) ??
(await getId(options.web3Context, ETH_DATA_FORMAT));
}
if (isNullish(populatedTransaction.gasLimit) && !isNullish(populatedTransaction.gas)) {
populatedTransaction.gasLimit = populatedTransaction.gas;
}
populatedTransaction.type = getTransactionType(populatedTransaction, options.web3Context);
if (
isNullish(populatedTransaction.accessList) &&
(populatedTransaction.type === '0x1' || populatedTransaction.type === '0x2')
) {
populatedTransaction.accessList = [];
}
populatedTransaction = {
...populatedTransaction,
...(await getTransactionGasPricing(
populatedTransaction,
options.web3Context,
ETH_DATA_FORMAT,
)),
};
return populatedTransaction as ReturnType;
}
export const transactionBuilder = async <ReturnType = Record<string, unknown>>(options: {
transaction: Transaction;
web3Context: Web3Context<EthExecutionAPI>;
privateKey?: HexString | Buffer;
// eslint-disable-next-line @typescript-eslint/require-await
}) =>
(options.web3Context.transactionBuilder ?? defaultTransactionBuilder)({
...options,
transaction: options.transaction as unknown as Record<string, unknown>,
}) as unknown as ReturnType;