Ich möchte auf die Vertragsbilanz (dh 0x552F3AfaA0394632f4aEfa9E923fA3e2bbDAF5FE
) und seine totalSupply (Zustandsvariable) zugreifen. Ich konnte auf das balancec dieses Vertrages zugreifen, aber warum konnte ich seine Funktion nicht aufrufen, dh getTotalSupply()
ich habe folgenden Code;
async function myContractAddress() {
myContAddr = '0x552F3AfaA0394632f4aEfa9E923fA3e2bbDAF5FE';
web3.eth.getBalance(myContAddr).then(console.log);
tokens = await myContAddr.methods.getTotalSupply().call();
tokens = tokens.toString(10);
console.log("Toten supply is : ",tokens);
}
myContractAddress();
hier ist mein Fehler;
(node:6028) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'getTotalSupply' of undefined
at myContractAddress (C:\Users\jj\Desktop\temp\deploy.js:36:39)
at C:\Users\jj\Desktop\temp\deploy.js:26:1
at process._tickCallback (internal/process/next_tick.js:68:7)
(node:6028) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This
error originated either by throwing inside of an async function without a catch
block, or by rejecting a promise which was not handled with .catch(). (rejectio
id: 1)
(node:6028) [DEP0018] DeprecationWarning: Unhandled promise rejections are depr
cated. In the future, promise rejections that are not handled will terminate th
Node.js process with a non-zero exit code.
0 // actually this is my contract balance , which have shown
Was Sie falsch machen, ist in Ihrem Code myContAddr ist eine Zeichenfolge und Sie versuchen, auf Parameter dieser Variablen zuzugreifen. Um auf .methods.getTotalSupply().call() zuzugreifen, müssen Sie eine Vertragsinstanz mit dem folgenden Code erstellen:
async function myContractAddress() {
var myContAddr = '0x552F3AfaA0394632f4aEfa9E923fA3e2bbDAF5FE';
var myContractAbiDefenition = 'CONTRACT_ABI';
var myContractInstance = new web3.eth.Contract(myContractAbiDefenition, myContAddr);
tokens = await myContractInstance.methods.getTotalSupply().call();
console.log("Tokens: ",tokens);
}
myContractAddress();
Emir Ali