无法调用智能合约中的函数
cannot call the function in smart contract
我正在尝试通过 angular 应用程序调用 solidity 智能合约中的方法。但我无法调用该方法。有人可以帮帮我吗。这是我在控制台中得到的错误
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)
智能合约
contract CertificateList {
function hello() external pure returns (string memory ) {
return "Hello";
}
}
Angular 服务
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);
});
}
}
提供商
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!');
}
}
});
确保 ../assets/abi/CertificateList.json
仅包含有效的 abi。如果您直接通过执行truffle compile
或solc CertificateList.sol
获取此文件,则必须从json文件中提取abi。
var contractJson = require('../assets/abi/CertificateList.json');
this.abi = contractJson['abi'];
此外,您不能在纯函数上使用 methods.hello.send
。 send 方法只能在修改合约状态的函数上调用,即 payable 或 unspecified 函数。要调用 pure 或 view 函数,应使用 call
方法。有关详细信息,请参阅 web3.eth.Contract。
this.contract.methods.hello().call()
.then(receipt => {
console.log(receipt);
}).catch(err => {
console.error(err);
});
我正在尝试通过 angular 应用程序调用 solidity 智能合约中的方法。但我无法调用该方法。有人可以帮帮我吗。这是我在控制台中得到的错误
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)
智能合约
contract CertificateList {
function hello() external pure returns (string memory ) {
return "Hello";
}
}
Angular 服务
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);
});
}
}
提供商
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!');
}
}
});
确保 ../assets/abi/CertificateList.json
仅包含有效的 abi。如果您直接通过执行truffle compile
或solc CertificateList.sol
获取此文件,则必须从json文件中提取abi。
var contractJson = require('../assets/abi/CertificateList.json');
this.abi = contractJson['abi'];
此外,您不能在纯函数上使用 methods.hello.send
。 send 方法只能在修改合约状态的函数上调用,即 payable 或 unspecified 函数。要调用 pure 或 view 函数,应使用 call
方法。有关详细信息,请参阅 web3.eth.Contract。
this.contract.methods.hello().call()
.then(receipt => {
console.log(receipt);
}).catch(err => {
console.error(err);
});