Skip to content
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

Cannot call contract methods using Cloudflare Workers #3763

Closed
larskoole opened this issue Feb 10, 2023 · 13 comments
Closed

Cannot call contract methods using Cloudflare Workers #3763

larskoole opened this issue Feb 10, 2023 · 13 comments
Assignees
Labels
duplicate Duplicate of another issue. v6 Issues regarding v6

Comments

@larskoole
Copy link

Ethers Version

6.0.2

Search Terms

cloudflare, code generation, Code generation from strings disallowed for this context

Describe the Problem

When using CloudFlare Workers I can't call methods on a contract.
A simple await contract.balanceOf(wallet.address).call() doesn't work in my case.

The error I get is Code generation from strings disallowed for this context.

Ideally, this would get solved in the package itself (if it is even related to an internal bug).
Otherwise, if I need to use a workaround that would be totally fine too.

Code Snippet

const { JsonRpcProvider, Contract, Wallet, formatEther } = await import('ethers');

const abi = ['function balanceOf(address account) external view returns (uint)'];
const provider = new JsonRpcProvider("https://polygon-rpc.com");
const wallet = new Wallet(env.PRIVATE_KEY, provider);
const contract = new Contract("someAddress", abi, provider);

const result = await contract.balanceOf(wallet.address).call()

Contract ABI

['function balanceOf(address account) external view returns (uint)']

Errors

✘ [ERROR] Uncaught (in promise) EvalError: Code generation from strings disallowed for this context

  super();
  ^
      at WrappedMethod
  (/Users/redacted/redacted/redacted/node_modules/ethers/src.ts/contract/contract.ts:250:8)
      at getFunction
  (/Users/redacted/redacted/redacted/node_modules/ethers/src.ts/contract/contract.ts:767:28)
      at get
  (/Users/redacted/redacted/redacted/node_modules/ethers/src.ts/contract/contract.ts:705:38)

Environment

Other (please specify)

Environment (Other)

CloudFlare Workers

@larskoole larskoole added the investigate Under investigation and may be a bug. label Feb 10, 2023
@ricmoo
Copy link
Member

ricmoo commented Feb 10, 2023

That line is a call to super(). Are you using a bundler that could be mangling the code? What node version is being used on the host?

@larskoole
Copy link
Author

I've set the version to 17.9.1 but I don't have any control over how it's built.

@ricmoo
Copy link
Member

ricmoo commented Feb 10, 2023

Cloudflare Workers must be built on a separate host, and then uploaded, since the service only accepts a single-file application (unless there have been recent changes?), so there must be a bundler config (web pack or rollup) somewhere...

@larskoole
Copy link
Author

Then it's as default as it comes haha.
Is there anything I can do to test a solution?

@larskoole
Copy link
Author

The following pages goes over the bundling of a worker using the latest version of wrangler:
https://developers.cloudflare.com/workers/wrangler/bundling

Maybe there is a setting I can try/tweak.

@ricmoo
Copy link
Member

ricmoo commented Feb 10, 2023

Is there any way you can see the generated code?

@larskoole
Copy link
Author

Yes, but I'd rather not post it here publicly.
Is there a way to DM you?

@larskoole
Copy link
Author

You could also reproduce it by creating a project with Wrangler and use the following code/settings:

File: index.js

export default
{
	async fetch(request, env, ctx)
	{
		const { JsonRpcProvider, Contract, Wallet, formatEther } = await import('ethers');

		const CONTRACT = {
			address: "someAddressHere",
			abi: [
				'function balanceOf(address account) external view returns (uint)'
			]
		}

		// Provider and wallet
		const provider = new JsonRpcProvider(env.RPC_URL);
		const wallet = new Wallet(env.PRIVATE_KEY, provider);
		const contract = new Contract(CONTRACT.address, CONTRACT.abi, provider);

		const result = await contract.balanceOf(wallet.address).call();

		console.log(result);
	},
};

File: wrangler.toml

name = "whatever"
main = "src/index.js"
compatibility_date = "2023-02-09"

[vars]
RPC_URL = "https://rpc-mainnet.matic.quiknode.pro"
NODE_VERSION = "17.9.1"

And using wrangler publish --dry-run --outdir dist to output the generated code to a directory called dist.

@ricmoo
Copy link
Member

ricmoo commented Feb 10, 2023

I've got a few other things that are high priority I'm working on now, but you can send me an e-mail to me@ricmoo.com. If you can produce a small concise example with the generated code, that's always appreciated too. ;)

@larskoole
Copy link
Author

I'll send you the generated code for the example above!
Thanks for helping so far :)

@larskoole
Copy link
Author

Quick side note. I've downgraded to the latest v5 version and that one does work, so maybe that narrows the search for the bug. Let me know if you need a copy of the generated code.

@ricmoo ricmoo added on-deck This Enhancement or Bug is currently being worked on. v6 Issues regarding v6 labels Mar 7, 2023
@ricmoo
Copy link
Member

ricmoo commented Mar 9, 2023

This is the same problem as #3749, which I’ve been updating, so I’m going to close this issue to help keep all the discussion in one place.

I’m pretty sure I understand the problem now and will get v6.2.0 out asap to address this.

Thanks! :)

@ricmoo ricmoo closed this as completed Mar 9, 2023
@ricmoo ricmoo added duplicate Duplicate of another issue. and removed on-deck This Enhancement or Bug is currently being worked on. investigate Under investigation and may be a bug. labels Mar 9, 2023
@ricmoo
Copy link
Member

ricmoo commented Mar 20, 2023

This has been addressed in v6.2.0. Please try it out and let me know if there are any problems. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate Duplicate of another issue. v6 Issues regarding v6
Projects
None yet
Development

No branches or pull requests

2 participants