Holen Sie sich Smartcontract-Token-Guthaben über JSON RPC

Ü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.

Antworten (2)

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_callund 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.

Jetzt bekomme ich eine Fehlermeldung: Uncaught TypeError: MyContract.at is not a function. Mit diesen Details: var MyContract = eth.contract([{interface}]). Und: var tokens = MyContract.at("MyAddress").balanceOf(eth.accounts[0]); console.log (Token);
Ich kann nicht erkennen, was falsch ist. Das Posten weiterer Details in einer separaten Frage kann den Leuten helfen, es zu erkennen.
Verwenden Sie web.eth.contract(interface) und wahrscheinlich müssen Sie auch JSON.parse(abi) verwenden.

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;
});
  • Wo {interface}ist die ABI Ihres Vertrages 0x123....?
  • Wo 0x123....ist die Adresse Ihres Vertrages?
  • Wo 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 newVorher 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 myContractABImein {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"
    }
];
Ich habe vor 2 Tagen nach dieser Antwort gesucht) traurig, dass sie erst vor 14 Stunden erschienen ist, aber es ist wirklich eine sehr hilfreiche Antwort. Eigentlich sieht meine Lösung zu 99% genauso aus wie deine.
wie bekomme ich den ABI für {interface}-parameter? Ich habe meinen Vertrag mit Trüffel eingesetzt.
Ich versuche, dies in C# zu erreichen. Kann ich den Json dafür sehen? Ich kann es nicht herausfinden, ohne zu sehen, was ich in den POST-Körper einfügen soll.
@MelbourneDeveloper Ich habe JSON zu meinem Smart Contract hinzugefügt - hoffe, das war das, wonach Sie gefragt haben.
Ich brauche eine Dokumentation darüber, wie ich überprüfen kann, welche Token sich an einer Adresse befinden und wie hoch der Kontostand ist. Web3 wird mir nicht helfen, weil ich C# verwende.