利用网上的合约转java进行接口调用,可以查代币账户余额,接收地址的以太币也是足够手续费的,但是执行发送的时候老是报手续费不足异常。
//金额wei的转换10的18次方
BigInteger value = BigDecimal.valueOf(money).multiply(new BigDecimal("1000000000000000000")).toBigInteger();
//gasprice的获取方式web3j.ethGasPrice().sendAsync().get();
//gasLimit=4300000
TokenERC20 contract = TokenERC20.load(
contractAddress, web3j, credentials, gasPrice, Contract.GAS_LIMIT);
TransactionReceipt transactionReceipt = contract.transfer(to,value).sendAsync().get();/**
*
Auto generated code.
*
Do not modify!
*
Please use the web3j command line tools,
* or the org.web3j.codegen.SolidityFunctionWrapperGenerator in the
* codegen module to update.
*
*
Generated with web3j version 3.2.0.
*/
public class TokenERC20 extends Contract {
private static final String BINARY = "60806040526012600260006101000a81548160ff021916908360ff1602179055503480156200002d57600080fd5b50604051620013f5380380620013f5833981810160405260608110156200005357600080fd5b810190808051906020019092919080516401000000008111156200007657600080fd5b828101905060208101848111156200008d57600080fd5b8151856001820283011164010000000082111715620000ab57600080fd5b50509291906020018051640100000000811115620000c857600080fd5b82810190506020810184811115620000df57600080fd5b8151856001820283011164010000000082111715620000fd57600080fd5b5050929190505050600260009054906101000a900460ff1660ff16600a0a8302600381905550600354600460003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550816000908051906020019062000181929190620001a4565b5080600190805190602001906200019a929190620001a4565b5050505062000253565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f10620001e757805160ff191683800117855562000218565b8280016001018555821562000218579182015b8281111562000217578251825591602001919060010190620001fa565b5b5090506200022791906200022b565b5090565b6200025091905b808211156200024c57600081600090555060010162000232565b5090565b90565b61119280620002636000396000f3fe608060405234801561001057600080fd5b50600436106100b45760003560e01c806370a082311161007157806370a08231146102b057806379cc67901461030857806395d89b411461036e578063a9059cbb146103f1578063cae9ca5114610457578063dd62ed3e14610554576100b4565b806306fdde03146100b9578063095ea7b31461013c57806318160ddd146101a257806323b872dd146101c0578063313ce5671461024657806342966c681461026a575b600080fd5b6100c16105cc565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156101015780820151818401526020810190506100e6565b50505050905090810190601f16801561012e5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6101886004803603604081101561015257600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291908035906020019092919050505061066a565b604051808215151515815260200191505060405180910390f35b6101aa61075c565b6040518082815260200191505060405180910390f35b61022c600480360360608110156101d657600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050610762565b604051808215151515815260200191505060405180910390f35b61024e61088d565b604051808260ff1660ff16815260200191505060405180910390f35b6102966004803603602081101561028057600080fd5b81019080803590602001909291905050506108a0565b604051808215151515815260200191505060405180910390f35b6102f2600480360360208110156102c657600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291905050506109a2565b6040518082815260200191505060405180910390f35b6103546004803603604081101561031e57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803590602001909291905050506109ba565b604051808215151515815260200191505060405180910390f35b610376610bd0565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156103b657808201518184015260208101905061039b565b50505050905090810190601f1680156103e35780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b61043d6004803603604081101561040757600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050610c6e565b604051808215151515815260200191505060405180910390f35b61053a6004803603606081101561046d57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190803590602001906401000000008111156104b457600080fd5b8201836020820111156104c657600080fd5b803590602001918460018302840111640100000000831117156104e857600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290505050610c85565b604051808215151515815260200191505060405180910390f35b6105b66004803603604081101561056a57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610ded565b6040518082815260200191505060405180910390f35b60008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156106625780601f1061063757610100808354040283529160200191610662565b820191906000526020600020905b81548152906001019060200180831161064557829003601f168201915b505050505081565b600081600560003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925846040518082815260200191505060405180910390a36001905092915050565b60035481565b6000600560008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020548211156107ed57600080fd5b81600560008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282540392505081905550610882848484610e12565b600190509392505050565b600260009054906101000a900460ff1681565b600081600460003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205410156108ee57600080fd5b81600460003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282540392505081905550816003600082825403925050819055503373ffffffffffffffffffffffffffffffffffffffff167fcc16f5dbb4873280815c1ee09dbd06736cffcc184412cf7a71a0fdb75d397ca5836040518082815260200191505060405180910390a260019050919050565b60046020528060005260406000206000915090505481565b600081600460008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020541015610a0857600080fd5b600560008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054821115610a9157600080fd5b81600460008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555081600560008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282540392505081905550816003600082825403925050819055508273ffffffffffffffffffffffffffffffffffffffff167fcc16f5dbb4873280815c1ee09dbd06736cffcc184412cf7a71a0fdb75d397ca5836040518082815260200191505060405180910390a26001905092915050565b60018054600181600116156101000203166002900480601f016020809104026020016040519081016040528092919081815260200182805460018160011615610100020316600290048015610c665780601f10610c3b57610100808354040283529160200191610c66565b820191906000526020600020905b815481529060010190602001808311610c4957829003601f168201915b505050505081565b6000610c7b338484610e12565b6001905092915050565b600080849050610c95858561066a565b15610de4578073ffffffffffffffffffffffffffffffffffffffff16638f4ffcb1338630876040518563ffffffff1660e01b8152600401808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018481526020018373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200180602001828103825283818151815260200191508051906020019080838360005b83811015610d73578082015181840152602081019050610d58565b50505050905090810190601f168015610da05780820380516001836020036101000a031916815260200191505b5095505050505050600060405180830381600087803b158015610dc257600080fd5b505af1158015610dd6573d6000803e3d6000fd5b505050506001915050610de6565b505b9392505050565b6005602052816000526040600020602052806000526040600020600091509150505481565b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff161415610e4c57600080fd5b80600460008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020541015610e9857600080fd5b600460008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205481600460008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054011015610f2557600080fd5b6000600460008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054600460008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205401905081600460008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555081600460008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055508273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040518082815260200191505060405180910390a380600460008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054600460008773ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054011461113257fe5b5050505056fea265627a7a72305820f03a50af67b82fae8a0127a3778fe7fd02c5eb109f24b9ddf9d3159be6931f5064736f6c637827302e352e392d6e696768746c792e323031392e352e32382b636f6d6d69742e30316236623638300057";
protected TokenERC20(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
super(BINARY, contractAddress, web3j, credentials, gasPrice, gasLimit);
}
protected TokenERC20(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
super(BINARY, contractAddress, web3j, transactionManager, gasPrice, gasLimit);
}
public ListgetTransferEvents(TransactionReceipt transactionReceipt) {
final Event event = new Event("Transfer",
Arrays.>asList(new TypeReference() {}, new TypeReference() {}),
Arrays.>asList(new TypeReference() {}));
ListvalueList = extractEventParameters(event, transactionReceipt);
ArrayListresponses = new ArrayList(valueList.size());
for (EventValues eventValues : valueList) {
TransferEventResponse typedResponse = new TransferEventResponse();
typedResponse.from = (String) eventValues.getIndexedValues().get(0).getValue();
typedResponse.to = (String) eventValues.getIndexedValues().get(1).getValue();
typedResponse.value = (BigInteger) eventValues.getNonIndexedValues().get(0).getValue();
responses.add(typedResponse);
}
return responses;
}
public ObservabletransferEventObservable(DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) {
final Event event = new Event("Transfer",
Arrays.>asList(new TypeReference() {}, new TypeReference() {}),
Arrays.>asList(new TypeReference() {}));
EthFilter filter = new EthFilter(startBlock, endBlock, getContractAddress());
filter.addSingleTopic(EventEncoder.encode(event));
return web3j.ethLogObservable(filter).map(new Func1() {
@Override
public TransferEventResponse call(Log log) {
EventValues eventValues = extractEventParameters(event, log);
TransferEventResponse typedResponse = new TransferEventResponse();
typedResponse.from = (String) eventValues.getIndexedValues().get(0).getValue();
typedResponse.to = (String) eventValues.getIndexedValues().get(1).getValue();
typedResponse.value = (BigInteger) eventValues.getNonIndexedValues().get(0).getValue();
return typedResponse;
}
});
}
public ListgetApprovalEvents(TransactionReceipt transactionReceipt) {
final Event event = new Event("Approval",
Arrays.>asList(new TypeReference() {}, new TypeReference() {}),
Arrays.>asList(new TypeReference() {}));
ListvalueList = extractEventParameters(event, transactionReceipt);
ArrayListresponses = new ArrayList(valueList.size());
for (EventValues eventValues : valueList) {
ApprovalEventResponse typedResponse = new ApprovalEventResponse();
typedResponse._owner = (String) eventValues.getIndexedValues().get(0).getValue();
typedResponse._spender = (String) eventValues.getIndexedValues().get(1).getValue();
typedResponse._value = (BigInteger) eventValues.getNonIndexedValues().get(0).getValue();
responses.add(typedResponse);
}
return responses;
}
public ObservableapprovalEventObservable(DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) {
final Event event = new Event("Approval",
Arrays.>asList(new TypeReference() {}, new TypeReference() {}),
Arrays.>asList(new TypeReference() {}));
EthFilter filter = new EthFilter(startBlock, endBlock, getContractAddress());
filter.addSingleTopic(EventEncoder.encode(event));
return web3j.ethLogObservable(filter).map(new Func1() {
@Override
public ApprovalEventResponse call(Log log) {
EventValues eventValues = extractEventParameters(event, log);
ApprovalEventResponse typedResponse = new ApprovalEventResponse();
typedResponse._owner = (String) eventValues.getIndexedValues().get(0).getValue();
typedResponse._spender = (String) eventValues.getIndexedValues().get(1).getValue();
typedResponse._value = (BigInteger) eventValues.getNonIndexedValues().get(0).getValue();
return typedResponse;
}
});
}
public ListgetBurnEvents(TransactionReceipt transactionReceipt) {
final Event event = new Event("Burn",
Arrays.>asList(new TypeReference() {}),
Arrays.>asList(new TypeReference() {}));
ListvalueList = extractEventParameters(event, transactionReceipt);
ArrayListresponses = new ArrayList(valueList.size());
for (EventValues eventValues : valueList) {
BurnEventResponse typedResponse = new BurnEventResponse();
typedResponse.from = (String) eventValues.getIndexedValues().get(0).getValue();
typedResponse.value = (BigInteger) eventValues.getNonIndexedValues().get(0).getValue();
responses.add(typedResponse);
}
return responses;
}
public ObservableburnEventObservable(DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) {
final Event event = new Event("Burn",
Arrays.>asList(new TypeReference() {}),
Arrays.>asList(new TypeReference() {}));
EthFilter filter = new EthFilter(startBlock, endBlock, getContractAddress());
filter.addSingleTopic(EventEncoder.encode(event));
return web3j.ethLogObservable(filter).map(new Func1() {
@Override
public BurnEventResponse call(Log log) {
EventValues eventValues = extractEventParameters(event, log);
BurnEventResponse typedResponse = new BurnEventResponse();
typedResponse.from = (String) eventValues.getIndexedValues().get(0).getValue();
typedResponse.value = (BigInteger) eventValues.getNonIndexedValues().get(0).getValue();
return typedResponse;
}
});
}
public RemoteCallname() {
Function function = new Function("name",
Arrays.asList(),
Arrays.>asList(new TypeReference() {}));
return executeRemoteCallSingleValueReturn(function, String.class);
}
public RemoteCallapprove(String _spender, BigInteger _value) {
Function function = new Function(
"approve",
Arrays.asList(new Address(_spender),
new Uint256(_value)),
Collections.>emptyList());
return executeRemoteCallTransaction(function);
}
public RemoteCalltotalSupply() {
Function function = new Function("totalSupply",
Arrays.asList(),
Arrays.>asList(new TypeReference() {}));
return executeRemoteCallSingleValueReturn(function, BigInteger.class);
}
public RemoteCalltransferFrom(String _from, String _to, BigInteger _value) {
Function function = new Function(
"transferFrom",
Arrays.asList(new Address(_from),
new Address(_to),
new Uint256(_value)),
Collections.>emptyList());
return executeRemoteCallTransaction(function);
}
public RemoteCalldecimals() {
Function function = new Function("decimals",
Arrays.asList(),
Arrays.>asList(new TypeReference() {}));
return executeRemoteCallSingleValueReturn(function, BigInteger.class);
}
public RemoteCallburn(BigInteger _value) {
Function function = new Function(
"burn",
Arrays.asList(new Uint256(_value)),
Collections.>emptyList());
return executeRemoteCallTransaction(function);
}
public RemoteCallbalanceOf(String param0) {
Function function = new Function("balanceOf",
Arrays.asList(new Address(param0)),
Arrays.>asList(new TypeReference() {}));
return executeRemoteCallSingleValueReturn(function, BigInteger.class);
}
public RemoteCallburnFrom(String _from, BigInteger _value) {
Function function = new Function(
"burnFrom",
Arrays.asList(new Address(_from),
new Uint256(_value)),
Collections.>emptyList());
return executeRemoteCallTransaction(function);
}
public RemoteCallsymbol() {
Function function = new Function("symbol",
Arrays.asList(),
Arrays.>asList(new TypeReference() {}));
return executeRemoteCallSingleValueReturn(function, String.class);
}
public RemoteCalltransfer(String _to, BigInteger _value) {
Function function = new Function(
"transfer",
Arrays.asList(new Address(_to),
new Uint256(_value)),
Collections.>emptyList());
return executeRemoteCallTransaction(function);
}
public RemoteCallapproveAndCall(String _spender, BigInteger _value, byte[] _extraData) {
Function function = new Function(
"approveAndCall",
Arrays.asList(new Address(_spender),
new Uint256(_value),
new org.web3j.abi.datatypes.DynamicBytes(_extraData)),
Collections.>emptyList());
return executeRemoteCallTransaction(function);
}
public RemoteCallallowance(String param0, String param1) {
Function function = new Function("allowance",
Arrays.asList(new Address(param0),
new Address(param1)),
Arrays.>asList(new TypeReference() {}));
return executeRemoteCallSingleValueReturn(function, BigInteger.class);
}
public static RemoteCalldeploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit, BigInteger initialSupply, String tokenName, String tokenSymbol) {
String encodedConstructor = FunctionEncoder.encodeConstructor(Arrays.asList(new Uint256(initialSupply),
new Utf8String(tokenName),
new Utf8String(tokenSymbol)));
return deployRemoteCall(TokenERC20.class, web3j, credentials, gasPrice, gasLimit, BINARY, encodedConstructor);
}
public static RemoteCalldeploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit, BigInteger initialSupply, String tokenName, String tokenSymbol) {
String encodedConstructor = FunctionEncoder.encodeConstructor(Arrays.asList(new Uint256(initialSupply),
new Utf8String(tokenName),
new Utf8String(tokenSymbol)));
return deployRemoteCall(TokenERC20.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, encodedConstructor);
}
public static TokenERC20 load(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
return new TokenERC20(contractAddress, web3j, credentials, gasPrice, gasLimit);
}
public static TokenERC20 load(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
return new TokenERC20(contractAddress, web3j, transactionManager, gasPrice, gasLimit);
}
public static class TransferEventResponse {
public String from;
public String to;
public BigInteger value;
}
public static class ApprovalEventResponse {
public String _owner;
public String _spender;
public BigInteger _value;
}
public static class BurnEventResponse {
public String from;
public BigInteger value;
}
}
以上是使用web3j命令将合约源码转java的代码