3

i'am trying to make a instance of my contract in js using web3, but when I use the function web3.eth.Contract(abi, address) the result is not the expected(I take the ABI from remix).

this is the code:

const Web3 = require('web3');
const web3 = new Web3('https://ropsten.infura.io/v3/53da5774bc2d410c90cc1e4c19d84dc4');

const account1 = '0xA4767B3a3a8D23912045E551c41c4C559572Fb85';

const contractAddress = '0xf779b15371Db4Eb0dD0eA3FD6f869cf9f5229649';
const abi = [ABI from remix];
var contract = new web3.eth.Contract(abi, contractAddress);

console.log(contract);

The output does not contain the methods defined in ABI and isn't possible call them with contract.methods.myMethods.....

This is the output:

AbstractContract {
  providersModuleFactory: ProvidersModuleFactory {},
  providerDetector: ProviderDetector {},
  providerResolver:
   ProviderResolver { providersModuleFactory: ProvidersModuleFactory {} },
  givenProvider: null,
  _currentProvider:
   HttpProvider {
     host:
      'https://ropsten.infura.io/v3/53da5774bc2d410c90cc1e4c19d84dc4',
     timeout: 0,
     headers: undefined,
     connected: true,
     providersModuleFactory: ProvidersModuleFactory {},
     agent: { httpsAgent: [Agent] } },
  _defaultAccount: undefined,
  _defaultBlock: 'latest',
  _transactionBlockTimeout: 50,
  _transactionConfirmationBlocks: 24,
  _transactionPollingTimeout: 750,
  _defaultGasPrice: undefined,
  _defaultGas: undefined,
  BatchRequest: [Function],
  contractModuleFactory:
   ContractModuleFactory {
     utils:
      { randomHex: [Function: randomHex],
        jsonInterfaceMethodToString: [Function: jsonInterfaceMethodToString],
        hexToAscii: [Function: hexToAscii],
        asciiToHex: [Function: asciiToHex],
        getUnitValue: [Function: getUnitValue],
        fromWei: [Function: fromWei],
        toWei: [Function: toWei],
        toChecksumAddress: [Function: toChecksumAddress],
        keccak256: [Function],
        sha3: [Function],
        toDecimal: [Function: hexToNumber],
        hexToNumber: [Function: hexToNumber],
        fromDecimal: [Function: numberToHex],
        numberToHex: [Function: numberToHex],
        hexToString: [Function: hexToUtf8],
        toUtf8: [Function: hexToUtf8],
        stringToHex: [Function: utf8ToHex],
        fromUtf8: [Function: utf8ToHex],
        utf8ToHex: [Function: utf8ToHex],
        toAscii: [Function: hexToAscii],
        fromAscii: [Function: asciiToHex],
        padLeft: [Function: leftPad],
        padRight: [Function: rightPad],
        getSignatureParameters: [Function: getSignatureParameters],
        isAddress: [Function: isAddress],
        isBN: [Function: isBN],
        checkAddressChecksum: [Function: checkAddressChecksum],
        toBN: [Function: toBN],
        toHex: [Function: toHex],
        hexToNumberString: [Function: hexToNumberString],
        toTwosComplement: [Function: toTwosComplement],
        isHex: [Function: isHex],
        isHexStrict: [Function: isHexStrict],
        isBloom: [Function: isBloom],
        isTopic: [Function: isTopic],
        bytesToHex: [Function: bytesToHex],
        hexToBytes: [Function: hexToBytes],
        soliditySha3: [Function: soliditySha3] },
     formatters:
      { outputBigNumberFormatter: [Function: outputBigNumberFormatter],
        isPredefinedBlockNumber: [Function: isPredefinedBlockNumber],
        inputDefaultBlockNumberFormatter: [Function: inputDefaultBlockNumberFormatter],
        inputBlockNumberFormatter: [Function: inputBlockNumberFormatter],
        txInputFormatter: [Function: txInputFormatter],
        inputCallFormatter: [Function: inputCallFormatter],
        inputTransactionFormatter: [Function: inputTransactionFormatter],
        inputSignFormatter: [Function: inputSignFormatter],
        outputTransactionFormatter: [Function: outputTransactionFormatter],
        outputTransactionReceiptFormatter: [Function: outputTransactionReceiptFormatter],
        outputBlockFormatter: [Function: outputBlockFormatter],
        inputLogFormatter: [Function: inputLogFormatter],
        outputLogFormatter: [Function: outputLogFormatter],
        inputPostFormatter: [Function: inputPostFormatter],
        outputPostFormatter: [Function: outputPostFormatter],
        inputAddressFormatter: [Function: inputAddressFormatter],
        outputSyncingFormatter: [Function: outputSyncingFormatter] },
     abiCoder: AbiCoder { utils: [Object], ethersAbiCoder: [AbiCoder] },
     accounts:
      Accounts {
        providersModuleFactory: ProvidersModuleFactory {},
        providerDetector: ProviderDetector {},
        providerResolver: [ProviderResolver],
        givenProvider: null,
        _currentProvider: [HttpProvider],
        _defaultAccount: undefined,
        _defaultBlock: 'latest',
        _transactionBlockTimeout: 50,
        _transactionConfirmationBlocks: 24,
        _transactionPollingTimeout: 750,
        _defaultGasPrice: undefined,
        _defaultGas: undefined,
        BatchRequest: [Function],
        methodFactory: [MethodFactory],
        utils: [Object],
        formatters: [Object],
        wallet: [Wallet] },
     methodModuleFactory:
      ModuleFactory {
        accounts: [Accounts],
        subscriptionsFactory: [SubscriptionsFactory],
        formatters: [Object],
        utils: [Object] } },
  abiCoder:
   AbiCoder {
     utils:
      { randomHex: [Function: randomHex],
        jsonInterfaceMethodToString: [Function: jsonInterfaceMethodToString],
        hexToAscii: [Function: hexToAscii],
        asciiToHex: [Function: asciiToHex],
        getUnitValue: [Function: getUnitValue],
        fromWei: [Function: fromWei],
        toWei: [Function: toWei],
        toChecksumAddress: [Function: toChecksumAddress],
        keccak256: [Function],
        sha3: [Function],
        toDecimal: [Function: hexToNumber],
        hexToNumber: [Function: hexToNumber],
        fromDecimal: [Function: numberToHex],
        numberToHex: [Function: numberToHex],
        hexToString: [Function: hexToUtf8],
        toUtf8: [Function: hexToUtf8],
        stringToHex: [Function: utf8ToHex],
        fromUtf8: [Function: utf8ToHex],
        utf8ToHex: [Function: utf8ToHex],
        toAscii: [Function: hexToAscii],
        fromAscii: [Function: asciiToHex],
        padLeft: [Function: leftPad],
        padRight: [Function: rightPad],
        getSignatureParameters: [Function: getSignatureParameters],
        isAddress: [Function: isAddress],
        isBN: [Function: isBN],
        checkAddressChecksum: [Function: checkAddressChecksum],
        toBN: [Function: toBN],
        toHex: [Function: toHex],
        hexToNumberString: [Function: hexToNumberString],
        toTwosComplement: [Function: toTwosComplement],
        isHex: [Function: isHex],
        isHexStrict: [Function: isHexStrict],
        isBloom: [Function: isBloom],
        isTopic: [Function: isTopic],
        bytesToHex: [Function: bytesToHex],
        hexToBytes: [Function: hexToBytes],
        soliditySha3: [Function: soliditySha3] },
     ethersAbiCoder: AbiCoder { coerceFunc: [Function] } },
  utils:
   { randomHex: [Function: randomHex],
     jsonInterfaceMethodToString: [Function: jsonInterfaceMethodToString],
     hexToAscii: [Function: hexToAscii],
     asciiToHex: [Function: asciiToHex],
     getUnitValue: [Function: getUnitValue],
     fromWei: [Function: fromWei],
     toWei: [Function: toWei],
     toChecksumAddress: [Function: toChecksumAddress],
     keccak256: { [Function: sha3] _Hash: [Object] },
     sha3: { [Function: sha3] _Hash: [Object] },
     toDecimal: [Function: hexToNumber],
     hexToNumber: [Function: hexToNumber],
     fromDecimal: [Function: numberToHex],
     numberToHex: [Function: numberToHex],
     hexToString: [Function: hexToUtf8],
     toUtf8: [Function: hexToUtf8],
     stringToHex: [Function: utf8ToHex],
     fromUtf8: [Function: utf8ToHex],
     utf8ToHex: [Function: utf8ToHex],
     toAscii: [Function: hexToAscii],
     fromAscii: [Function: asciiToHex],
     padLeft: [Function: leftPad],
     padRight: [Function: rightPad],
     getSignatureParameters: [Function: getSignatureParameters],
     isAddress: [Function: isAddress],
     isBN: [Function: isBN],
     checkAddressChecksum: [Function: checkAddressChecksum],
     toBN: [Function: toBN],
     toHex: [Function: toHex],
     hexToNumberString: [Function: hexToNumberString],
     toTwosComplement: [Function: toTwosComplement],
     isHex: [Function: isHex],
     isHexStrict: [Function: isHexStrict],
     isBloom: [Function: isBloom],
     isTopic: [Function: isTopic],
     bytesToHex: [Function: bytesToHex],
     hexToBytes: [Function: hexToBytes],
     soliditySha3: [Function: soliditySha3] },
  formatters:
   { outputBigNumberFormatter: [Function: outputBigNumberFormatter],
     isPredefinedBlockNumber: [Function: isPredefinedBlockNumber],
     inputDefaultBlockNumberFormatter: [Function: inputDefaultBlockNumberFormatter],
     inputBlockNumberFormatter: [Function: inputBlockNumberFormatter],
     txInputFormatter: [Function: txInputFormatter],
     inputCallFormatter: [Function: inputCallFormatter],
     inputTransactionFormatter: [Function: inputTransactionFormatter],
     inputSignFormatter: [Function: inputSignFormatter],
     outputTransactionFormatter: [Function: outputTransactionFormatter],
     outputTransactionReceiptFormatter: [Function: outputTransactionReceiptFormatter],
     outputBlockFormatter: [Function: outputBlockFormatter],
     inputLogFormatter: [Function: inputLogFormatter],
     outputLogFormatter: [Function: outputLogFormatter],
     inputPostFormatter: [Function: inputPostFormatter],
     outputPostFormatter: [Function: outputPostFormatter],
     inputAddressFormatter: [Function: inputAddressFormatter],
     outputSyncingFormatter: [Function: outputSyncingFormatter] },
  abiMapper:
   AbiMapper {
     utils:
      { randomHex: [Function: randomHex],
        jsonInterfaceMethodToString: [Function: jsonInterfaceMethodToString],
        hexToAscii: [Function: hexToAscii],
        asciiToHex: [Function: asciiToHex],
        getUnitValue: [Function: getUnitValue],
        fromWei: [Function: fromWei],
        toWei: [Function: toWei],
        toChecksumAddress: [Function: toChecksumAddress],
        keccak256: [Function],
        sha3: [Function],
        toDecimal: [Function: hexToNumber],
        hexToNumber: [Function: hexToNumber],
        fromDecimal: [Function: numberToHex],
        numberToHex: [Function: numberToHex],
        hexToString: [Function: hexToUtf8],
        toUtf8: [Function: hexToUtf8],
        stringToHex: [Function: utf8ToHex],
        fromUtf8: [Function: utf8ToHex],
        utf8ToHex: [Function: utf8ToHex],
        toAscii: [Function: hexToAscii],
        fromAscii: [Function: asciiToHex],
        padLeft: [Function: leftPad],
        padRight: [Function: rightPad],
        getSignatureParameters: [Function: getSignatureParameters],
        isAddress: [Function: isAddress],
        isBN: [Function: isBN],
        checkAddressChecksum: [Function: checkAddressChecksum],
        toBN: [Function: toBN],
        toHex: [Function: toHex],
        hexToNumberString: [Function: hexToNumberString],
        toTwosComplement: [Function: toTwosComplement],
        isHex: [Function: isHex],
        isHexStrict: [Function: isHexStrict],
        isBloom: [Function: isBloom],
        isTopic: [Function: isTopic],
        bytesToHex: [Function: bytesToHex],
        hexToBytes: [Function: hexToBytes],
        soliditySha3: [Function: soliditySha3] },
     abiCoder: AbiCoder { utils: [Object], ethersAbiCoder: [AbiCoder] },
     contractModuleFactory:
      ContractModuleFactory {
        utils: [Object],
        formatters: [Object],
        abiCoder: [AbiCoder],
        accounts: [Accounts],
        methodModuleFactory: [ModuleFactory] } },
  options: { address: '0xf779b15371Db4Eb0dD0eA3FD6f869cf9f5229649' },
  PromiEvent: [Function: PromiEvent],
  methodFactory:
   MethodFactory {
     accounts:
      Accounts {
        providersModuleFactory: ProvidersModuleFactory {},
        providerDetector: ProviderDetector {},
        providerResolver: [ProviderResolver],
        givenProvider: null,
        _currentProvider: [HttpProvider],
        _defaultAccount: undefined,
        _defaultBlock: 'latest',
        _transactionBlockTimeout: 50,
        _transactionConfirmationBlocks: 24,
        _transactionPollingTimeout: 750,
        _defaultGasPrice: undefined,
        _defaultGas: undefined,
        BatchRequest: [Function],
        methodFactory: [MethodFactory],
        utils: [Object],
        formatters: [Object],
        wallet: [Wallet] },
     utils:
      { randomHex: [Function: randomHex],
        jsonInterfaceMethodToString: [Function: jsonInterfaceMethodToString],
        hexToAscii: [Function: hexToAscii],
        asciiToHex: [Function: asciiToHex],
        getUnitValue: [Function: getUnitValue],
        fromWei: [Function: fromWei],
        toWei: [Function: toWei],
        toChecksumAddress: [Function: toChecksumAddress],
        keccak256: [Function],
        sha3: [Function],
        toDecimal: [Function: hexToNumber],
        hexToNumber: [Function: hexToNumber],
        fromDecimal: [Function: numberToHex],
        numberToHex: [Function: numberToHex],
        hexToString: [Function: hexToUtf8],
        toUtf8: [Function: hexToUtf8],
        stringToHex: [Function: utf8ToHex],
        fromUtf8: [Function: utf8ToHex],
        utf8ToHex: [Function: utf8ToHex],
        toAscii: [Function: hexToAscii],
        fromAscii: [Function: asciiToHex],
        padLeft: [Function: leftPad],
        padRight: [Function: rightPad],
        getSignatureParameters: [Function: getSignatureParameters],
        isAddress: [Function: isAddress],
        isBN: [Function: isBN],
        checkAddressChecksum: [Function: checkAddressChecksum],
        toBN: [Function: toBN],
        toHex: [Function: toHex],
        hexToNumberString: [Function: hexToNumberString],
        toTwosComplement: [Function: toTwosComplement],
        isHex: [Function: isHex],
        isHexStrict: [Function: isHexStrict],
        isBloom: [Function: isBloom],
        isTopic: [Function: isTopic],
        bytesToHex: [Function: bytesToHex],
        hexToBytes: [Function: hexToBytes],
        soliditySha3: [Function: soliditySha3] },
     formatters:
      { outputBigNumberFormatter: [Function: outputBigNumberFormatter],
        isPredefinedBlockNumber: [Function: isPredefinedBlockNumber],
        inputDefaultBlockNumberFormatter: [Function: inputDefaultBlockNumberFormatter],
        inputBlockNumberFormatter: [Function: inputBlockNumberFormatter],
        txInputFormatter: [Function: txInputFormatter],
        inputCallFormatter: [Function: inputCallFormatter],
        inputTransactionFormatter: [Function: inputTransactionFormatter],
        inputSignFormatter: [Function: inputSignFormatter],
        outputTransactionFormatter: [Function: outputTransactionFormatter],
        outputTransactionReceiptFormatter: [Function: outputTransactionReceiptFormatter],
        outputBlockFormatter: [Function: outputBlockFormatter],
        inputLogFormatter: [Function: inputLogFormatter],
        outputLogFormatter: [Function: outputLogFormatter],
        inputPostFormatter: [Function: inputPostFormatter],
        outputPostFormatter: [Function: outputPostFormatter],
        inputAddressFormatter: [Function: inputAddressFormatter],
        outputSyncingFormatter: [Function: outputSyncingFormatter] },
     contractModuleFactory:
      ContractModuleFactory {
        utils: [Object],
        formatters: [Object],
        abiCoder: [AbiCoder],
        accounts: [Accounts],
        methodModuleFactory: [ModuleFactory] },
     methodModuleFactory:
      ModuleFactory {
        accounts: [Accounts],
        subscriptionsFactory: [SubscriptionsFactory],
        formatters: [Object],
        utils: [Object] },
     abiCoder: AbiCoder { utils: [Object], ethersAbiCoder: [AbiCoder] } },
  abiModel: AbiModel { abi: { methods: [Object], events: {} } },
  methods:
   MethodsProxy {
     contract: [Circular],
     abiModel: AbiModel { abi: [Object] },
     methodFactory:
      MethodFactory {
        accounts: [Accounts],
        utils: [Object],
        formatters: [Object],
        contractModuleFactory: [ContractModuleFactory],
        methodModuleFactory: [ModuleFactory],
        abiCoder: [AbiCoder] },
     methodEncoder: MethodEncoder { abiCoder: [AbiCoder] },
     methodOptionsValidator: MethodOptionsValidator { utils: [Object] },
     methodOptionsMapper:
      MethodOptionsMapper { utils: [Object], formatters: [Object] },
     PromiEvent: [Function: PromiEvent] },
  events:
   EventSubscriptionsProxy {
     contract: [Circular],
     eventSubscriptionFactory:
      EventSubscriptionFactory { utils: [Object], formatters: [Object] },
     abiModel: AbiModel { abi: [Object] },
     eventOptionsMapper:
      EventOptionsMapper {
        formatters: [Object],
        eventFilterEncoder: [EventFilterEncoder] },
     eventLogDecoder: EventLogDecoder { abiCoder: [AbiCoder] },
     allEventsLogDecoder: AllEventsLogDecoder { abiCoder: [AbiCoder] },
     allEventsOptionsMapper:
      AllEventsOptionsMapper {
        formatters: [Object],
        allEventsFilterEncoder: [AllEventsFilterEncoder] },
     PromiEvent: [Function: PromiEvent] } }

I try also taking the ABI from json file, same problem.

Someone can help me? Thanks :)

  • this isn't valid Js: const abi = [ABI from remix]; -- what is remix? – ekkis Feb 28 at 18:37
  • Remix is online IDE for deploying smart contract, I wrote it so as not to rewrite the whole string that is very long. – Chaos Mar 2 at 0:01
  • Could you find a solution yet? – ferit May 20 at 10:02
  • In the end I used web3@0.20.1 throughout the project. – Chaos May 29 at 9:50
0

I had the same problem in my react application with web3js 1.0@betaXX so I switched to ethers.js.

Here's the way I did it with ethers.js. Note: I use the web3js instance elsewhere. I just use ethers.js for interactions with the contract. But you could actually completely replace web3js with ethers.js.

let web3 = this.props.web3; // or wherever you get your web3 instance from
let provider = new ethers.providers.Web3Provider(web3.givenProvider);
let deth = new ethers.Contract(
  contractAddress,
  abi,
  provider
);

Hope that helps!

  • Thanks, for the moment I'm using web3@0.20.1 that works, but I have already written a lot of code with web3js 1.0@beta46 and I would not have to rewrite all that code with another syntax and I don't know if there are all features I need. Thanks anyway for the answer! :) – Chaos Feb 28 at 13:25
0

The below link specifies how can you use the web3.eth.contract() API:

https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethcontract

  • Yes but it is for web3js 0.20.xx, at the moment I'm using that version but my question was about web3js 1.0@betaXX. – Chaos Mar 2 at 0:04
0

I solved it by downgrading to web3.1.0.0-beta.37. This is the latest release I could find which doesn't have this problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.