Skip to content

Add detailed logging to SenderProvider and AutomaticSenderProvider #6675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 45 additions & 9 deletions packages/hardhat-core/src/internal/core/providers/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,16 +263,16 @@ export class LocalAccountsProvider extends ProviderWrapperWithChainId {
gasLimit: transactionRequest.gas,
};

const accessList = txData.accessList?.map(({ address, storageKeys }) => {
const accessList = txData.accessList?.map(({ address, storageKeys }: { address: any; storageKeys: any[] | null }) => {
return {
address: addr.addChecksum(bytesToHex(address)),
storageKeys:
storageKeys !== null ? storageKeys.map((k) => bytesToHex(k)) : [],
storageKeys !== null ? storageKeys.map((k: any) => bytesToHex(k)) : [],
};
});

const authorizationList = txData.authorizationList?.map(
({ chainId: authChainId, address, nonce, yParity, r, s }) => {
({ chainId: authChainId, address, nonce, yParity, r, s }: any) => {
return {
chainId: authChainId,
address: addr.addChecksum(bytesToHex(address)),
Expand Down Expand Up @@ -392,6 +392,9 @@ abstract class SenderProvider extends ProviderWrapper {
public async request(args: RequestArguments): Promise<unknown> {
const method = args.method;
const params = this._getParams(args);
console.log( // LOGGING
`[SenderProvider.request] Method: ${method}, Params: ${JSON.stringify(params)}`
); // LOGGING

if (
method === "eth_sendTransaction" ||
Expand All @@ -400,15 +403,25 @@ abstract class SenderProvider extends ProviderWrapper {
) {
// TODO: Should we validate this type?
const tx: JsonRpcTransactionData = params[0];
console.log(`[SenderProvider.request] Matched method ${method}. Tx: ${JSON.stringify(tx)}`); // LOGGING

if (tx !== undefined && tx.from === undefined) {
console.log(`[SenderProvider.request] 'from' is undefined for method '${method}'. Attempting to get sender.`); // LOGGING
const senderAccount = await this._getSender();
console.log(`[SenderProvider.request] Obtained sender: ${senderAccount} for method '${method}'`); // LOGGING

if (senderAccount !== undefined) {
tx.from = senderAccount;
} else if (method === "eth_sendTransaction") {
console.error(`[SenderProvider.request] No sender account available for eth_sendTransaction.`); // LOGGING
throw new HardhatError(ERRORS.NETWORK.NO_REMOTE_ACCOUNT_AVAILABLE);
} else { // LOGGING
console.log(`[SenderProvider.request] No sender account found for ${method}, but not throwing as it's not eth_sendTransaction.`); // LOGGING
}
} else if (tx !== undefined) { // LOGGING
console.log(`[SenderProvider.request] 'from' is defined: ${tx.from} for method '${method}'.`); // LOGGING
} else { // LOGGING
console.log(`[SenderProvider.request] Transaction object 'tx' is undefined for method '${method}'.`); // LOGGING
}
}

Expand All @@ -422,15 +435,38 @@ export class AutomaticSenderProvider extends SenderProvider {
private _firstAccount: string | undefined;

protected async _getSender(): Promise<string | undefined> {
if (this._firstAccount === undefined) {
const accounts = (await this._wrappedProvider.request({
method: "eth_accounts",
})) as string[];
console.log("[AutomaticSenderProvider._getSender] Entered _getSender."); // LOGGING
if (this._firstAccount !== undefined) {
console.log( // LOGGING
`[AutomaticSenderProvider._getSender] Using cached address: ${this._firstAccount}`
); // LOGGING
return this._firstAccount;
}

console.log( // LOGGING
"[AutomaticSenderProvider._getSender] No cached address. Attempting to call eth_accounts."
); // LOGGING
const accounts = (await this._wrappedProvider.request({
method: "eth_accounts",
})) as string[];
console.log( // LOGGING
`[AutomaticSenderProvider._getSender] eth_accounts returned: ${JSON.stringify(
accounts
)}`
); // LOGGING

if (Array.isArray(accounts) && accounts.length > 0) {
this._firstAccount = accounts[0];
console.log( // LOGGING
`[AutomaticSenderProvider._getSender] Setting address to: ${this._firstAccount}`
); // LOGGING
return this._firstAccount;
}

return this._firstAccount;
console.warn( // LOGGING
"[AutomaticSenderProvider._getSender] eth_accounts returned no accounts or invalid response. Returning undefined."
); // LOGGING
return undefined;
}
}

Expand All @@ -442,4 +478,4 @@ export class FixedSenderProvider extends SenderProvider {
protected async _getSender(): Promise<string | undefined> {
return this._sender;
}
}
}