How to get the values from an array? #3783
Answered
by
jochem-brouwer
andreachello
asked this question in
Q&A
-
Hi, im trying to log out the first element of the array: // SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Greeter {
uint256[] public arr = [1, 2, 3];
} by doing this async function callFunction(vm: VM, contractAddress: Address, caller: Address) {
const sigHash = new Interface(['function arr(uint256)']).getSighash('arr')
// Encode the parameter
const encodedParams = defaultAbiCoder.encode(['uint256'], [0])
// Combine sigHash with encoded parameters
const data = hexToBytes(sigHash + encodedParams.slice(2))
const callResult = await vm.evm.runCall({
to: contractAddress,
caller: caller,
origin: caller,
data: hexToBytes(data),
block,
})
if (callResult.execResult.exceptionError) {
throw callResult.execResult.exceptionError
}
const results = AbiCoder.decode(['uint256'], callResult.execResult.returnValue)
return parseInt(results[0])
} but it gives me an error: EvmError { error: 'revert', errorType: 'EvmError' } |
Beta Was this translation helpful? Give feedback.
Answered by
jochem-brouwer
Nov 2, 2024
Replies: 1 comment 3 replies
-
This works for me: async function getGreeting(vm: VM, contractAddress: Address, caller: Address) {
const sigHash = new Interface(['function arr(uint256)']).getFunction('arr')!.selector
// Encode the parameter
const encodedParams = new AbiCoder().encode(['uint256'], [0])
// Combine sigHash with encoded parameters
const data = hexToBytes(sigHash + encodedParams.slice(2))
const greetResult = await vm.evm.runCall({
to: contractAddress,
caller,
origin: caller, // The tx.origin is also the caller here
data,
block,
})
if (greetResult.execResult.exceptionError) {
throw greetResult.execResult.exceptionError
}
const results = new AbiCoder().decode(['uint256'], greetResult.execResult.returnValue)
return parseInt(results[0])
} This uses an older (?) version of ethers maybe. I also noticed you are doing |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
andreachello
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This works for me: