0

我第一次在 Android 中使用 BlockChain(使用 web3j),我有现有的智能合约的价值,如 BINARY 和 ABI 详细信息,如下所示

    private final String BINARY = "0x608060405260008054600160a060020a03191633179055610357806100256000396000f3fe608060405234801561001057600080fd5b506000357c01000000000000000000000000000000000000000000000000000000009004806347bc70931161007857806347bc7093146101395780638da5cb5b1461015f578063e36f1dc214610183578063fef03da3146101af57610092f54bf6e146100c757806338a7543e14610101575b600080fd5b6100c5600480360360208110156100b557600080fd5b5035600160a060020a03166101d5565b005b6100ed600480360360208110156100dd57600080fd5b5035600160a060020a0316610223565b604080519115158252519081900360200190f35b6101276004803603602081101561011757600080fd5b5035600160a060020a0316610237565b60408051918252519081900360200190f35b6100c56004803603602081101561014f57600080fd5b5035600160a060020a0316610249565b61016761029f565b60408051600160a060020a039092168252519081900360200190f35b6100c56004803603604081101561019957600080fd5b50600160a060020a0381351690602001356102ae565b610127600480360360208110156101c557600080fd5b5035600160a060020a0316610307565b6101de33610223565b6101e757600080fd5b600160a060020a0381163014610220576000805473ffffffffffffffffffffffffffffffffffffffff1916600160a060020a0383161790555b50565b600054600160a060020a0390811691161490565b60016020526000908152604090205481565b61025233610223565b61025b57600080fd5b600160a060020a038116600081815260016020526040808220829055517faf66545c919a3be306ee446d8f42a9558b5b022620df880517bc9593ec0f2d529190a250565b600054600160a060020a031681565b6102b733610223565b6102c057600080fd5b600160a060020a038216600081815260016020526040808220849055518392917f8e09c3aa52fe5ad5aae964b6fe6fa517119d3f52a4f4031306d905054cb1e95191a35050565b600160a060020a03166000908152600160205260409020549056fea265627a7a72315820e5a946d82909f983bece2d2e68c8455dd3143ce5ece928b3c224f4e23fb12f3a64736f6c634300050b0032";
    private final String ADDRESS = "0xaA3c4b900a96520e44444669350bf5302b90EbD9";
    private final String URL = "http://10.12.13.223:8545";
    private final String ABI = "[{\"constant\":false,\"inputs\":[{\"internalType\":\"address\",\"name\":\"owner\",\"type\":\"address\"},{\"internalType\":\"bytes32\",\"name\":\"claimid\",\"type\":\"bytes32\"},{\"internalType\":\"bytes32\",\"name\":\"claimhash\",\"type\":\"bytes32\"}],\"name\":\"addClaim\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"internalType\":\"address\",\"name\":\"issuer\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"owner\",\"type\":\"address\"},{\"internalType\":\"bytes32\",\"name\":\"claimid\",\"type\":\"bytes32\"}],\"name\":\"removeClaim\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"internalType\":\"address\",\"name\":\"issuer\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"owner\",\"type\":\"address\"},{\"internalType\":\"bytes32\",\"name\":\"claimid\",\"type\":\"bytes32\"}],\"name\":\"getClaim\",\"outputs\":[{\"internalType\":\"bytes32\",\"name\":\"\",\"type\":\"bytes32\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"sender\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"owner\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"bytes32\",\"name\":\"claimid\",\"type\":\"bytes32\"},{\"indexed\":false,\"internalType\":\"bytes32\",\"name\":\"claimhash\",\"type\":\"bytes32\"}],\"name\":\"ClaimAdded\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"sender\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"owner\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"bytes32\",\"name\":\"claimid\",\"type\":\"bytes32\"}],\"name\":\"ClaimRemoved\",\"type\":\"event\"}]";

现在根据文档“如果我已经有了合同的二进制文件和 ABI,那么我不需要使用 web3j 来生成它。只需获取这些值并创建类

我已经有了这些值,但我不知道,如何使用?

就像我只需要为前创建课程一样。

class MyContract {
    private final String BINARY = "0x608060405260008054600160a060020a03191633179055610357806100256000396000f3fe608060405234801561001057600080fd5b506000357c01000000000000000000000000000000000000000000000000000000009004806347bc70931161007857806347bc7093146101395780638da5cb5b1461015f578063e36f1dc214610183578063fef03da3146101af57610092f54bf6e146100c757806338a7543e14610101575b600080fd5b6100c5600480360360208110156100b557600080fd5b5035600160a060020a03166101d5565b005b6100ed600480360360208110156100dd57600080fd5b5035600160a060020a0316610223565b604080519115158252519081900360200190f35b6101276004803603602081101561011757600080fd5b5035600160a060020a0316610237565b60408051918252519081900360200190f35b6100c56004803603602081101561014f57600080fd5b5035600160a060020a0316610249565b61016761029f565b60408051600160a060020a039092168252519081900360200190f35b6100c56004803603604081101561019957600080fd5b50600160a060020a0381351690602001356102ae565b610127600480360360208110156101c557600080fd5b5035600160a060020a0316610307565b6101de33610223565b6101e757600080fd5b600160a060020a0381163014610220576000805473ffffffffffffffffffffffffffffffffffffffff1916600160a060020a0383161790555b50565b600054600160a060020a0390811691161490565b60016020526000908152604090205481565b61025233610223565b61025b57600080fd5b600160a060020a038116600081815260016020526040808220829055517faf66545c919a3be306ee446d8f42a9558b5b022620df880517bc9593ec0f2d529190a250565b600054600160a060020a031681565b6102b733610223565b6102c057600080fd5b600160a060020a038216600081815260016020526040808220849055518392917f8e09c3aa52fe5ad5aae964b6fe6fa517119d3f52a4f4031306d905054cb1e95191a35050565b600160a060020a03166000908152600160205260409020549056fea265627a7a72315820e5a946d82909f983bece2d2e68c8455dd3143ce5ece928b3c224f4e23fb12f3a64736f6c634300050b0032";
    private final String ADDRESS = "0xaA3c4b900a96520e44444669350bf5302b90EbD9";
    private final String URL = "http://10.12.13.223:8545";
    private final String ABI = "[{\"constant\":false,\"inputs\":[{\"internalType\":\"address\",\"name\":\"owner\",\"type\":\"address\"},{\"internalType\":\"bytes32\",\"name\":\"claimid\",\"type\":\"bytes32\"},{\"internalType\":\"bytes32\",\"name\":\"claimhash\",\"type\":\"bytes32\"}],\"name\":\"addClaim\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"internalType\":\"address\",\"name\":\"issuer\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"owner\",\"type\":\"address\"},{\"internalType\":\"bytes32\",\"name\":\"claimid\",\"type\":\"bytes32\"}],\"name\":\"removeClaim\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"internalType\":\"address\",\"name\":\"issuer\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"owner\",\"type\":\"address\"},{\"internalType\":\"bytes32\",\"name\":\"claimid\",\"type\":\"bytes32\"}],\"name\":\"getClaim\",\"outputs\":[{\"internalType\":\"bytes32\",\"name\":\"\",\"type\":\"bytes32\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"sender\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"owner\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"bytes32\",\"name\":\"claimid\",\"type\":\"bytes32\"},{\"indexed\":false,\"internalType\":\"bytes32\",\"name\":\"claimhash\",\"type\":\"bytes32\"}],\"name\":\"ClaimAdded\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"sender\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"owner\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"bytes32\",\"name\":\"claimid\",\"type\":\"bytes32\"}],\"name\":\"ClaimRemoved\",\"type\":\"event\"}]";
}

现在我如何使用这个类从合同中读取或写入值?

4

1 回答 1

0

通过反复试验,我找到了解决方案。如果我有一个现有的 ABI 和 BINARY,那么它需要位于两个文件中:

例如

MyContract.abi -> In this file, I will paste ABI code
MyContract.bin -> In this file, I will paste BINARY code.

然后我将执行下面的命令来生成合约包装器。

web3j solidity generate -b /path/to/<smart-contract>.bin -a /path/to/<smart-contract>.abi -o /path/to/src/main/java -p com.your.organisation.name
于 2020-07-02T09:41:23.767 回答