Rufen Sie die Vertragsfunktion mit Adresse und abi auf, um den Wert an das Frontend zurückzugeben

Ich habe den folgenden Vertrag im Ethereum-Testnetz bereitgestellt.

contract demo {
  string public name = "someString"; 

  function returnValue() constant returns (string){ 
      return name;    
  }
}

Ich versuche, die Zeichenfolge des Vertrags abzurufen, und das wird nicht funktionieren

var contract = web3.eth.contract(contractAddress, abi); //contractAddress+abi known
console.log(contract.returnValue);

kehrt zurückundefined

Wenn ich diesen Code verwende:

var asdf = web3.eth.contract(abi).at(contractAddress);
alert(asdf.returnValue);

Ich bekomme `function(){[native code]} Wie kann ich den String "name" aus meinem Soliditätsvertrag in mein Javascript bekommen? Vielen Dank

Von dort habe ich meinen Code

Antworten (1)

Versuche dies:

var contract = web3.eth.contract(abiArray).at(contractAddress);
contract.returnValue.call().then(function(v) {
    var strName= v.toString();
    console.log("Name: "+ strName);   
});

So verwende ich Trüffel:

import demo_artifacts from './../../build/contracts/demo.json';  
var Demo = contract(demo_artifacts);

window.getValue=function(){  
    Demo.deployed().then(function(contractInstance) {  
        contractInstance.returnValue.call().then(function(v) {  
            var strName= v.toString();  
            console.log("Name: "+ strName);
        });    
    });  
}    
window.getValue();  

Probier diese:

var abiDef = "(provide the abi)";   
var contractAddress = "(provide the contract address)";  
var objWeb3 = new Web3(new
Web3.providers.HttpProvider("http://localhost:8545"));   
var democontract = objWeb3.eth.contract( JSON.parse(abiDef)).at(contractAddress);  
var callData=democontract.returnValue.call();
console.log(callData); // Printed: BigNumber {s: 1, e: 3, c: Array[1]}  
console.log(callData.c[0]); // Printed: the value in my case 1000  
das ist, was ich bekommeUncaught TypeError: Cannot read property 'call' of undefined
Versuchen Sie aktualisierten Code
Ich verwende keinen Trüffel. Ich verwende die Web3-JavaScript-API, deshalb funktioniert wahrscheinlich die call()-Funktion nicht
Eine andere Version hinzugefügt, versuchen Sie das