3

I am trying to call a method in solidity smart contract through angular app, but I am getting this error:

TypeError: this.contract.methods.hello is not a function
at CertificateContractService.<anonymous> (certificate-contract.service.ts:32)
at Generator.next (<anonymous>)
at fulfilled (tslib.es6.js:70)
at ZoneDelegate.invoke (zone-evergreen.js:359)
at Object.onInvoke (core.js:39699)
at ZoneDelegate.invoke (zone-evergreen.js:358)
at Zone.run (zone-evergreen.js:124)
at zone-evergreen.js:855
at ZoneDelegate.invokeTask (zone-evergreen.js:391)
at Object.onInvokeTask (core.js:39680)

Smart contract

pragma solidity ^0.5.0;

contract CertificateList {

    function hello() public pure returns (string memory )  {
        return "Hello";
    }

}

Angular service

import Web3 from 'web3';
import {WEB3} from './WEB3';

declare let require: any;
declare let window: any;


@Injectable({
  providedIn: 'root'
})
export class CertificateContractService {
  private abi: any;
  private address = '0xb0fFD3498B219ad2A62Eb98fEDE591265b3C3B67';
  private contract;
  private accounts: string[];

  constructor(@Inject(WEB3) private web3: Web3) {
    this.init().then(res => {
    }).catch(err => {
      console.error(err);
    });
  }

  private async init() {
    this.abi = require('../assets/abi/CertificateList.json');
    // await this.web3.currentProvider.enable();
    this.accounts = await this.web3.eth.getAccounts();

    this.contract = new this.web3.eth.Contract(this.abi, this.address, {gas: 1000000, gasPrice: '10000000000000'});

    this.contract.methods.hello().send()
      .then(receipt => {
        console.log(receipt);
      }).catch(err => {
      console.error(err);
    });
  }
}

Provider

import { InjectionToken } from '@angular/core';
import Web3 from 'web3';

export const WEB3 = new InjectionToken<Web3>('web3', {
  providedIn: 'root',
  factory: () => {
    try {
      window['ethereum'].autoRefreshOnNetworkChange = false;
      const provider = ('ethereum' in window) ? window['ethereum'] : Web3['givenProvider'];
      return new Web3(provider);
    } catch (err) {
      throw new Error('Non-Ethereum browser detected. You should consider trying Mist or MetaMask!');
    }
  }
});

Can someone please help me?

  • First of all, being that function hello is constant (pure in your case), you should be using call(), not send(). Second, what is the value of this.abi? – goodvibration Dec 16 '19 at 3:38
  • for the call() also giving same error. this.abi = require('../assets/abi/CertificateList.json'); through this line i load the that contact abi json file – Ragulan28 Dec 16 '19 at 3:46
  • web3 version is 1.2.4 – Ragulan28 Dec 16 '19 at 3:51
  • Please console.log(this.abi) and then paste the result here. – goodvibration Dec 16 '19 at 3:52
  • it contain more than 290 lines. i copy the CertificateList.json file from build/contract after truffle migrate – Ragulan28 Dec 16 '19 at 6:36
2

Change this:

this.abi = require('../assets/abi/CertificateList.json')

To this:

this.abi = require('../assets/abi/CertificateList.json').abi

And this:

this.contract.methods.hello().send()

To this:

this.contract.methods.hello().call()
| improve this answer | |
  • thank you. now it working – Ragulan28 Dec 16 '19 at 7:23
  • @Ragulan28: You're welcome :) – goodvibration Dec 16 '19 at 7:23

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.