[Ethereum] Contract error: TypeError: abi.forEach is not a function

contract-invocationweb3js

mac OS 10.14.15

node version v10.15.3
web3 version 1.0
version Parity-Ethereum/v2.5.1-beta-adabd8198-20190514/x86_64-macos/rustc1.34.1

I am trying to follow a YT tutorial to instantiate a contract object. The contract address and abi are for the OMG token. I am running a parity node.

contractAddress
'0xd26114cd6EE289AccF82350c8d8487fedB8A0C07'
abi
'[{"constant":true,"inputs":[],"name":"mintingFinished","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"unpause","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_amount","type":"uint256"}],"name":"mint","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"paused","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"finishMinting","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"pause","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_amount","type":"uint256"},{"name":"_releaseTime","type":"uint256"}],"name":"mintTimelocked","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"remaining","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"payable":false,"type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Mint","type":"event"},{"anonymous":false,"inputs":[],"name":"MintFinished","type":"event"},{"anonymous":false,"inputs":[],"name":"Pause","type":"event"},{"anonymous":false,"inputs":[],"name":"Unpause","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"}]'

const myContract2 = new web3.eth.Contract(abi,contractAddress);
TypeError: abi.forEach is not a function
at AbiMapper.map (/Users/nancyibsen/node_modules/web3-eth-contract/dist/web3-eth-contract.cjs.js:292:11)
at new AbstractContract (/Users/nancyibsen/node_modules/web3-eth-contract/dist/web3-eth-contract.cjs.js:984:38)
at ContractModuleFactory.createContract (/Users/nancyibsen/node_modules/web3-eth-contract/dist/web3-eth-contract.cjs.js:1089:14)
at new Eth._this.Contract (/Users/nancyibsen/node_modules/web3-eth/dist/web3-eth.cjs.js:403:50)

Best Answer

It's difficult to tell from the information posted, but abi may not be an array. A quick way to tell is to evaluate Array.isArray(abi). It should return true for an array.

A long shot, but assuming you have the ABI in string format (somehow), you could try replacing:

    const myContract2 = new web3.eth.Contract(abi,contractAddress)

with

    const parsedABI = JSON.parse(abi)
    const myContract2 = new web3.eth.Contract(parsedABI,contractAddress)
Related Topic