Über die Geth-Befehlszeile kann ich mein Smartcontract-Token-Guthaben abrufen mit:
var mytest = eth.contract([{interface}]).at(contract address);
mytest.balanceOf(eth.accounts[0])
>998
Ich möchte das Token-Guthaben auf meiner Website präsentieren. Wie kann ich das machen?
Ich habe JSON-RPC und verschiedene Methoden wie ausprobiert eth_getTransactionByHash
, kann aber nicht herausfinden, wie ich das Gleichgewicht bekomme.
Verwenden Sie web3.js , um den Kontostand auf Ihrer Website zu präsentieren.
Die Geth-Konsole, auf der Sie das Token-Guthaben erhalten konnten, ist Javascript und verwendet web3.js unter der Haube, sodass Ihr Website-Code fast identisch sein wird.
Andernfalls müssen Sie mit JSON-RPC die aufgerufene Funktion und ihre Parameter verwenden eth_call
und ABI -codieren. Es ist viel komplizierter, ein Beispiel finden Sie unter: How to call a contract method using the eth_call JSON-RPC API
Wenn serverseitiges Rendering gewünscht wird, könnte diese Designentscheidung geprüft werden, da ein Benutzer in der Lage sein sollte, eine dezentralisierte Anwendung ohne Server auszuführen: Swarm und IPFS sind Technologien, die man im Hinterkopf behalten sollte, da die Richtung, in die DApps wahrscheinlich gehen werden.
Antwort von @eth♦ und Kommentar von @Jeroen hinzugefügt
Holen Sie sich Smartcontract Token-Guthaben mit web3 1.0.0-beta.31
In 1.0.0-beta ... wird das meiste jetzt über Promises gehandhabt , daher könnten Sie Folgendes tun:
var mytest = new web3.eth.Contract({interface}, '0x123....', {
from: '0x456...', // default from address
gasPrice: '20000000000' // default gas price in wei, 20 gwei in this case
});
mytest.methods.balanceOf('0x456...').call()
.then(function(result){
//the result holds your Token Balance that you can assign to a var
var myTokenBalance = result;
return result;
});
{interface}
ist die ABI Ihres Vertrages 0x123....
?0x123....
ist die Adresse Ihres Vertrages?0x456....
ist die Adresse Ihres Ethereum-Kontos, das die Token hält (d. h. das ETH-Konto, von dem Sie sein Token-Guthaben abrufen möchten)Vergessen Sie nicht, das new
Vorher hinzuzufügenweb3.eth.Contract(....)
@Jeroen Auf diese Weise erhalten Sie den Fehler nicht
MyContract.at ist keine Funktion
mehr.
Hinweis: Ich verwende den IPC -Anbieter geth.ipc, um mit meinem Knoten zu interagieren (da ich mich auf demselben PC befinde, localhost, daher sicherer, anstatt HTTP-Anforderungen zu verwenden).
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
} else {
var net = require('net');
var web3 = new Web3('/home/yourHomeFolder/.ethereum/geth.ipc', net);
};
Bearbeiten: Als Referenz nach dem ABI JSON ( web3.eth.Contract({interface}
), das ich für meinen Smart Contract verwende, ist myContractABI
mein {interface}
:
myContractABI = [
{
"constant": false,
"inputs": [
{
"name": "newSellPrice",
"type": "uint256"
},
{
"name": "newBuyPrice",
"type": "uint256"
}
],
"name": "setPrices",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "name",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_spender",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "approve",
"outputs": [
{
"name": "success",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "totalSupply",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_from",
"type": "address"
},
{
"name": "_to",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "transferFrom",
"outputs": [
{
"name": "success",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "decimals",
"outputs": [
{
"name": "",
"type": "uint8"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_value",
"type": "uint256"
}
],
"name": "burn",
"outputs": [
{
"name": "success",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "sellPrice",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "target",
"type": "address"
},
{
"name": "mintedAmount",
"type": "uint256"
}
],
"name": "mintToken",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_from",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "burnFrom",
"outputs": [
{
"name": "success",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "buyPrice",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "owner",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "symbol",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [],
"name": "buy",
"outputs": [],
"payable": true,
"stateMutability": "payable",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_to",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "",
"type": "address"
}
],
"name": "frozenAccount",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_spender",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
},
{
"name": "_extraData",
"type": "bytes"
}
],
"name": "approveAndCall",
"outputs": [
{
"name": "success",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "",
"type": "address"
},
{
"name": "",
"type": "address"
}
],
"name": "allowance",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "amount",
"type": "uint256"
}
],
"name": "sell",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "target",
"type": "address"
},
{
"name": "freeze",
"type": "bool"
}
],
"name": "freezeAccount",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "newOwner",
"type": "address"
}
],
"name": "transferOwnership",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"name": "initialSupply",
"type": "uint256"
},
{
"name": "tokenName",
"type": "string"
},
{
"name": "tokenSymbol",
"type": "string"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"name": "target",
"type": "address"
},
{
"indexed": false,
"name": "frozen",
"type": "bool"
}
],
"name": "FrozenFunds",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "from",
"type": "address"
},
{
"indexed": true,
"name": "to",
"type": "address"
},
{
"indexed": false,
"name": "value",
"type": "uint256"
}
],
"name": "Transfer",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "from",
"type": "address"
},
{
"indexed": false,
"name": "value",
"type": "uint256"
}
],
"name": "Burn",
"type": "event"
}
];
LF00