Skip to content

Commit

Permalink
Fixed resolving ENS addresses used as from parameters (#3961).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Oct 9, 2023
1 parent cd5f0fe commit 2616f4c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
22 changes: 18 additions & 4 deletions src.ts/contract/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from "./wrappers.js";

import type { EventFragment, FunctionFragment, InterfaceAbi, ParamType, Result } from "../abi/index.js";
import type { Addressable } from "../address/index.js";
import type { Addressable, NameResolver } from "../address/index.js";
import type { EventEmitterable, Listener } from "../utils/index.js";
import type {
BlockTag, ContractRunner, Provider, TransactionRequest, TopicFilter
Expand Down Expand Up @@ -68,6 +68,14 @@ function canSend(value: any): value is ContractRunnerSender {
return (value && typeof(value.sendTransaction) === "function");
}

function getResolver(value: any): undefined | NameResolver {
if (value != null) {
if (canResolve(value)) { return value; }
if (value.provider) { return value.provider; }
}
return undefined;
}

class PreparedTopicFilter implements DeferredTopicFilter {
#filter: Promise<TopicFilter>;
readonly fragment!: EventFragment;
Expand Down Expand Up @@ -146,9 +154,7 @@ export async function copyOverrides<O extends string = "data" | "to">(arg: any,
"cannot override data", "overrides.data", overrides.data);

// Resolve any from
if (overrides.from) {
overrides.from = await resolveAddress(overrides.from);
}
if (overrides.from) { overrides.from = overrides.from; }

return <Omit<ContractTransaction, O>>overrides;
}
Expand Down Expand Up @@ -177,6 +183,10 @@ function buildWrappedFallback(contract: BaseContract): WrappedFallback {
const tx: ContractTransaction = <any>(await copyOverrides<"data">(overrides, [ "data" ]));
tx.to = await contract.getAddress();

if (tx.from) {
tx.from = await resolveAddress(tx.from, getResolver(contract.runner));
}

const iface = contract.interface;

const noValue = (getBigInt((tx.value || BN_0), "overrides.value") === BN_0);
Expand Down Expand Up @@ -271,6 +281,10 @@ function buildWrappedMethod<A extends Array<any> = Array<any>, R = any, D extend
let overrides: Omit<ContractTransaction, "data" | "to"> = { };
if (fragment.inputs.length + 1 === args.length) {
overrides = await copyOverrides(args.pop());

if (overrides.from) {
overrides.from = await resolveAddress(overrides.from, getResolver(contract.runner));
}
}

if (fragment.inputs.length !== args.length) {
Expand Down
2 changes: 1 addition & 1 deletion src.ts/providers/abstract-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ export class AbstractProvider implements Provider {
[ "to", "from" ].forEach((key) => {
if ((<any>request)[key] == null) { return; }

const addr = resolveAddress((<any>request)[key]);
const addr = resolveAddress((<any>request)[key], this);

This comment has been minimized.

Copy link
@shazow

shazow Oct 11, 2023

@ricmoo I haven't had a chance to check this particular flow yet, but would this get the correct this since it's in the forEach callback closure? Might need to bind the this from outside the closure.

This comment has been minimized.

Copy link
@ricmoo

ricmoo Oct 11, 2023

Author Member

It uses an arrow function, so the this isn’t shadowed. Th at would only be an issue in a function() { … } declaration.

Old Babel configurations would transpile this incorrectly, but that bug in Babel is quite old. :)

This comment has been minimized.

Copy link
@shazow

shazow Oct 11, 2023

TIL!

if (isPromise(addr)) {
promises.push((async function() { (<any>request)[key] = await addr; })());
} else {
Expand Down

0 comments on commit 2616f4c

Please sign in to comment.