简体   繁体   English

使用 Java Web3J 获取 ERC1155 钱包余额

[英]Getting ERC1155 Wallet Balance Using Java Web3J

Since Web3J doesn't currently support ERC1155, is there a way to get the balance for a wallet?由于 Web3J 目前不支持 ERC1155,有没有办法获得钱包的余额? My guess is to use a function for this, but I can't seem to figure out how to get it to work.我的猜测是为此使用一个函数,但我似乎无法弄清楚如何让它工作。

Function function = new Function(
            "balancedOf",
            Arrays.asList(new Address(ethAddress), new Uint256(1)),
            Arrays.asList(new org.web3j.abi.TypeReference<Bool>() {}));
String data = FunctionEncoder.encode(function);

Do I then create a transaction?然后我要创建交易吗? Or do I use ethSendRawTransaction?还是我使用 ethSendRawTransaction? balanceOf only has 2 input so I would expect to have to invoke it from a smartcontract, but I don't see a way to do it. balanceOf 只有 2 个输入,所以我希望必须从智能合约中调用它,但我看不到这样做的方法。

From reading the web3j docs, It seems that you can do the following:通过阅读 web3j 文档,您似乎可以执行以下操作:

Function function = new Function<>(
             "functionName",
             Arrays.asList(new Type(value)),  // Solidity Types in smart contract functions
             Arrays.asList(new TypeReference<Type>() {}, ...));

String encodedFunction = FunctionEncoder.encode(function)
org.web3j.protocol.core.methods.response.EthCall response = web3j.ethCall(
             Transaction.createEthCallTransaction(<from>, contractAddress, encodedFunction),
             DefaultBlockParameterName.LATEST)
             .sendAsync().get();

List<Type> someTypes = FunctionReturnDecoder.decode(
             response.getValue(), function.getOutputParameters());

The response object, from org.web3j.protocol.core.methods.response.EthCall does the JSON-RPC call "eth_call" which only retrieves data form the blockchain.来自org.web3j.protocol.core.methods.response.EthCallresponse对象执行 JSON-RPC 调用“eth_call”,它只从区块链中检索数据。

I believe this is the equivalent of doing in web3js the following:我相信这相当于在 web3js 中执行以下操作:


let contract = new web3.eth.Contract(<ABI>, <Contract Address>);
const res = await contract.functionName(<params>);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用web3j检查ERC20合同中的余额 - Checking balance in ERC20 contract using web3j 是否可以使用Java(web3j)更改以太坊钱包密码? - Is it possible to change Ethereum wallet password using Java(web3j)? 使用java web3j创建钱包,但是如何连接到私有运行的区块链? - Using java web3j to create wallet but how then connect to private running blockchain? web3j 中凭证的钱包路径是什么? - What is the wallet path for credentials in web3j? 仅使用公钥在 HD 钱包中生成以太坊地址 (bitcoinj/web3j) - Generate Ethereum addresses in HD Wallet using public key only (bitcoinj/web3j) 试图与web3j(Android)取得平衡,出现nullpointer异常 - Trying to get balance with web3j (Android), geting nullpointer exception 使用 BitcoinJ 获取钱包余额 - getting wallet balance using BitcoinJ 如何将 Metamask 连接到 Web3J (java) - How to connect Metamask to Web3J (java) 在 Web3j 中生成 Java Wrapper 时出错 - Error in generating Java Wrapper in Web3j 在 Spring Boot 应用程序中使用 web3j java 库时,我在 Web3j.build 行中收到以下异常。 任何想法来解决这个问题? - When using web3j java library in a spring boot application, I am getting the below exception in the Web3j.build line. Any idea to fix this issue?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM