I'm trying to reproduce some code on python that is currently working on javascript. The main goal is to select a function to query from the contract abi and call it (just view functions).
Let's say that I want to call the balanceOf method of an ERC-20 contract. In javascript I do it like this:
const callContractFunctionWithParams = async (selection, params, block) => {
try {
console.log(`[QUERYING] Calling ${selection} method on block ${block} `);
const result = await fullNodeContract.methods[selection](...params).call(
(defaultBlock = block)
);
console.log(
`[${selection}-RESULT] Results from calling ${selection} method on block ${block}: `,
result
);
} catch (error) {
if (isArchiveQuery(error)) {
console.log("[OLD-BLOCK-QUERY] Switching to archive query");
const result = await archiveNodeContract.methods[selection](
...params
).call((defaultBlock = block));
console.log(
`[${selection}-RESULT] Results from calling ${selection} method on block ${block}: `,
result
);
} else {
console.error(error);
}
}
};
Here I switch dinamically between querying a full or archive node depending on the block and the error raised. The selection variable represents the name of the function and the params are the inputs (array-like) required from each distinct function. This work like a charm with web3.js.
On the other side I have this code on python that attempts to do just the same:
def call_contract_function_with_params(selection, params, block):
print("Params: ", params)
try:
print("[QUERYING] Calling {} method on block {}".format(selection, block))
result = full_node_contract.functions[selection](*params).call(
block_identifier=block
)
print(
"[{}-RESULT] Results from calling {} method on block {}: ".format(
selection, selection, block
),
result,
)
except Exception as e:
if "missing trie node" in str(e):
print("[OLD-BLOCK-QUERY] Switching to archive query")
result = archive_node_contract.functions[selection](*params).call(
block_identifier=block
)
print(
"[{}-RESULT] Results from calling {} method on block {}: ".format(
selection, selection, block
),
result,
)
else:
print("error: ", str(e))
Same here, selection is just the function name to call and params are the inputs expected for the function (list type). However, if I run the code on python it prints
[QUERYING] Calling balanceOf method on block 15342721
error:
Any thoughts on this? Thanks in advance for any help!