Gemeinschaft,
Ich habe mir über das folgende Problem den Kopf zerbrochen und hoffe, Sie können meinen Fehler erkennen. Ich setze den folgenden Vertrag mit Populus
on ein Testrpc
. Dies ist ein ziemlich einfacher Vertrag:
pragma solidity ^0.4.19;
contract ICO {
address public owner;
mapping(address => uint) public balances;
address[] public buyers;
uint constant public MULTIPLIER = 1000;
function ICO() public {
owner = msg.sender;
}
function getBuyersLength() public constant returns (uint) {
return buyers.length;
}
function buy() public payable {
uint coins = msg.value * MULTIPLIER;
balances[msg.sender] += coins;
buyers.push(msg.sender);
}
}
Ich rufe die buy
Funktion mit Web3js folgendermaßen auf:
let contract;
let account;
window.addEventListener('load', function () {
if (typeof web3 !== 'undefined') {
console.log('Using MetaMask connection');
w3 = new Web3(web3.currentProvider);
} else {
w3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}
contract = w3.eth.contract(contractABI).at(contractAddress);
account = w3.eth.defaultAccount;
main()
});
function main() {
let amount = w3.toWei(10, 'ether');
contract.buy({'value': amount}, function (err, res) {
if (err) {
console.error('Error occurred: ' + err);
} else {
console.log('Transaction created: ' + res);
}
})
}
Nachdem ich darauf gewartet habe, dass die Transaktion in einen Block aufgenommen wird (passiert fast sofort, seit ich testrpc
, rufe ich die getBuyersLength()
Funktion wie folgt auf:
function updateBalances() {
contract.getBuyersLength(function (err, res) {
if (err) {
console.error('Error in getBuyers: ' + err);
} else {
console.log('Count of Buyers: ' + res);
}
}
}
Das Ergebnis ist jedoch immer 0
, was bedeutet, dass das buyers.length
gleich bleibt. Kannst du einen Fehler erkennen, den ich gemacht habe?
Habe es gerade gelöst. Das Problem war, wie ich den Vertrag für bereitgestellt habe TestRPC
. Früher habe ich populus deploy ICO -c testrpc
den Vertrag bereitgestellt, aber irgendwie wurde der Vertrag nie in der Test-Blockchain gespeichert. Ich stelle jetzt den Vertrag vom Frontend aus bereit, was nicht die beste Lösung ist, aber es funktioniert vorerst. Hier ist wie:
w3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
account = w3.eth.defaultAccount;
let contractScheme = w3.eth.contract(contractABI);
let gasEstimate = w3.eth.estimateGas({data: contractBytecode});
contract = contractScheme.new({
data: contractBytecode,
from: account,
gas: gasEstimate
}, function (err, res) {
if (err) {
console.error('Error occurred in Contract Creation: ' + err)
} else {
// Callback fires twice. Once upon reception of the TransactionHash and
// once the contract receives an address.
if (!res.address) {
console.log('TransactionID for Contract Creation ' + res.transactionHash)
}
if (res.address) {
console.log('Contract Address: ' + res.address);
main()
}
}
});
Dein Vertrag scheint in Ordnung zu sein. Könnte ein Problem in Ihrer Js-Hauptdateifunktion sein w3.toWei
. Ich vermute, Sie haben w3 kein web3-Objekt zugewiesen. Also w3 ist undefiniert, Javascript wird einen Fehler auslösen. Weil keine Vergöttlichung für gefunden wurde w3.toWei
. Ihre Anweisungen werden also nicht ausgeführt, contract.buy()
werden nicht ausgeführt. Wenn diese Methode nicht ausgeführt wird, gibt es keine Transaktion im Eth-Client. Wenn Sie also anrufen, getBuyersLength()
wird nur 0 zurückgegeben.
function main() {
let contract = web3.eth.contract(CONTRACT_ABI).at(CONTRACT_ADDR);
let amount = web3.toWei(10, 'ether');
contract.buy({'value': amount}, function (err, res) {
if (err) {
console.error('Error occurred: ' + err);
} else {
console.log('Transaction created: ' + res);
}
})
}
Ersetzen Sie Ihren Code durch den oben genannten Code. Wenn Ihr Problem nicht behoben ist, aktualisieren Sie Ihre qus mit Javascript-Protokollen.
Jitendra Kumar. Balla
Ismael
buy()
ist es fehlgeschlagen.