Я развернул следующий контракт в тестовой сети Ethereum.
contract demo {
string public name = "someString";
function returnValue() constant returns (string){
return name;
}
}
Я пытаюсь получить строку контракта, и это не сработает.
var contract = web3.eth.contract(contractAddress, abi); //contractAddress+abi known
console.log(contract.returnValue);
возвращаетсяundefined
Если я использую этот код:
var asdf = web3.eth.contract(abi).at(contractAddress);
alert(asdf.returnValue);
Я получаю `function () {[собственный код]} Как я могу получить строку «имя» из моего контракта Solidity в свой javascript? Спасибо
Попробуй это:
var contract = web3.eth.contract(abiArray).at(contractAddress);
contract.returnValue.call().then(function(v) {
var strName= v.toString();
console.log("Name: "+ strName);
});
Вот как я использую трюфель:
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();
Попробуй это:
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
Uncaught TypeError: Cannot read property 'call' of undefined
Бадр Беллай
ПетросМ