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

Multiple calls to the same function #8

Open
martriay opened this issue Nov 30, 2020 · 1 comment
Open

Multiple calls to the same function #8

martriay opened this issue Nov 30, 2020 · 1 comment

Comments

@martriay
Copy link

martriay commented Nov 30, 2020

Today etherplex supports batching multiple calls to the same contract and it constructs a return object of the form result[call.caller.__name][call.fd.name] = result. The problem with this is that it writes over previous calls to the same function.

Example code to reproduce the issue:

      const values = await batch(
        provider,
        etherplexDaiContract
          .balanceOf(userAddresses[0])
          .balanceOf(userAddresses[1])
          .balanceOf(userAddresses[2])
      )

Would result in a single element array

{
    "Dai": {
        balanceOf: [ {_hex: "0x00", _ethersType: "BigNumber"} ]
        balanceOf(address): ...
    }
}
@martriay martriay changed the title Add support for multiple calls to the same function Multiple calls to the same function Dec 1, 2020
@mdatsev
Copy link

mdatsev commented Nov 2, 2022

Here is a workaround function:

const { contract, encodeData, prepareTransaction } = require('@pooltogether/etherplex');
async function batchArrayResult(provider, ...batchCalls) {
  const [, calls, data] = encodeData(...batchCalls);
  const { chainId } = await provider.getNetwork();
  const callResponse = await provider.send('eth_call', [await prepareTransaction(chainId, data), "latest"]);
  const [, returnValues] = ethers.utils.defaultAbiCoder.decode(['uint256', 'bytes[]'], callResponse);
  const results = [];
  for (let i = 0; i < returnValues.length; i++) {
    let call = calls[i]
    let decoded = call.caller.__interface.decodeFunctionResult(call.fd, returnValues[i])
    results.push(decoded);
  }
  return results;
}

using it instead of batch in your example results in:

[
  [ BigNumber { _hex: '0x00', _isBigNumber: true } ],
  [ BigNumber { _hex: '0x06acad3426894c2afc', _isBigNumber: true } ],
  [ BigNumber { _hex: '0x056bc75e2e327df451', _isBigNumber: true } ]
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants