Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot call the function in smart contract

i am try to call a method in solidity smart contract through angular app. but i unable to call the method. can someone please help me. this is the error i get in console

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


contract CertificateList {

    function hello() external 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!');
    }
  }
});
like image 355
Ragulan Avatar asked Jun 23 '26 01:06

Ragulan


1 Answers

Make sure that ../assets/abi/CertificateList.json contains a valid abi exclusively. If you acquired this file by executing truffle compile or solc CertificateList.sol directly, you have to extract the abi from the json file.

var contractJson = require('../assets/abi/CertificateList.json');
this.abi = contractJson['abi'];

Also, you can not use methods.hello.send on a pure function. The send method can be called only on functions that modify the contract's state, i.e. payable or unspecified functions. To invoke pure or view functions, the call method should be used. For more information see web3.eth.Contract.

this.contract.methods.hello().call()
  .then(receipt => {
    console.log(receipt);
  }).catch(err => {
  console.error(err);
});
like image 50
aliras Avatar answered Jun 26 '26 15:06

aliras



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!