Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Batch requests using an instantiated Contract object #6203

Open
@spacesailor24

Description

@spacesailor24

Currently if a user wanted to perform a batch request to interact with a Contract instance, they're have to use something similar to this:

import { Contract } from 'web3';

import { ERC20TokenAbi } from './ERC20Token';

const mainnetUSDTAddress = '0xdAC17F958D2ee523a2206206994597C13D831ec7';

(async () => {
	const erc20ContractDeployed = new Contract(ERC20TokenAbi, mainnetUSDTAddress, {
		provider: 'https://yourProvider.com',
	});

	const request1 = {
		id: 1,
		method: 'eth_call',
		params: [
			{
				to: mainnetUSDTAddress,
				data: erc20ContractDeployed.methods.name().encodeABI(),
			},
			'latest',
		],
	};
	const request2 = {
		id: 2,
		method: 'eth_call',
		params: [
			{
				to: mainnetUSDTAddress,
				data: erc20ContractDeployed.methods.symbol().encodeABI(),
			},
			'latest',
		],
	};
	const request3 = {
		id: 3,
		method: 'eth_call',
		params: [
			{
				to: mainnetUSDTAddress,
				data: erc20ContractDeployed.methods.totalSupply().encodeABI(),
			},
			'latest',
		],
	};

	const batchRequester = new erc20ContractDeployed.BatchRequest();
	batchRequester.add(request1);
	batchRequester.add(request2);
	batchRequester.add(request3);

	const response = await batchRequester.execute();
	console.log(response);
})();

Ideally there would be a more straightforward way to do this using the instantiated Contract object like so:

import { Contract } from 'web3';

import { ERC20TokenAbi } from './ERC20Token';

const mainnetUSDTAddress = '0xdAC17F958D2ee523a2206206994597C13D831ec7';

(async () => {
	const erc20ContractDeployed = new Contract(ERC20TokenAbi, mainnetUSDTAddress, {
		provider: 'https://yourProvider.com',
	});

	const batchRequester = new erc20ContractDeployed.BatchRequest();
	batchRequester.add(erc20ContractDeployed.methods.name().call())
	batchRequester.add(erc20ContractDeployed.methods.symbol().call())
	batchRequester.add(erc20ContractDeployed.methods.totalSupply().call())

	const response = await batchRequester.execute();
	console.log(response);
})();

Keep in mind that we'd have to append .call() or .send() to each batch request to distinguish whether we should use eth_call or eth_sendTransaction - I suspect implementing this will not be super straightforward, but it's necessary

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions