web3j cannot execute 0xProject as expected
Bug_title
A clear and concise description of what the issue is. Execute the contract fillOrder method, {"jsonrpc":"2.0","id":0,"error":{"code":-32015,"data":"Reverted 0x","message":"VM execution error. "}},
Steps To Reproduce
Describe the exact steps or scenario to reproduce the issue.
Expected behavior
A clear and concise description of what you expected to happen.
Actual behavior
A description of the actual behavior occurring. Execute the contract fillOrder method, {"jsonrpc":"2.0","id":0,"error":{"code":-32015,"data":"Reverted 0x","message":"VM execution error. "}}, guess whether it is the error caused by DynamicBytes; StructuredDataEncoder also does not work as expected, and the obtained ordeHash is inconsistent with the query (node query; I don’t know if anyone has used web3j to work with 0xProject. At present, I found that many places are not as expected. Proceed. Hope can help. Thank you
Environment
Describe the environment in which the issue occurs
- Web3j version
- Java or Android version
- Operating System <web3j.version>4.6.3</web3j.version> java mac
Additional context
Add any other context about the problem here.
- Logs
- Sample code and/or code snippets
- Unit/integration tests to highlight the issue
- etherscan references
public static class Order extends DynamicStruct {
public String makerAddress;
public String takerAddress;
public String feeRecipientAddress;
public String senderAddress;
public BigInteger makerAssetAmount;
public BigInteger takerAssetAmount;
public BigInteger makerFee;
public BigInteger takerFee;
public BigInteger expirationTimeSeconds;
public BigInteger salt;
public byte[] makerAssetData;
public byte[] takerAssetData;
public Order(String makerAddress, String takerAddress, String feeRecipientAddress, String senderAddress, BigInteger makerAssetAmount
, BigInteger takerAssetAmount, BigInteger makerFee, BigInteger takerFee, BigInteger expirationTimeSeconds
, BigInteger salt, byte[] makerAssetData, byte[] takerAssetData) {
super(new Address(makerAddress), new Address(takerAddress), new Address(feeRecipientAddress), new Address(senderAddress)
, new Uint256(makerAssetAmount), new Uint256(takerAssetAmount), new Uint256(makerFee), new Uint256(takerFee), new Uint256(expirationTimeSeconds)
, new Uint256(salt), new Bytes(makerAssetData), new Bytes(takerAssetData));
this.makerAddress = makerAddress;
this.takerAddress = takerAddress;
this.feeRecipientAddress = feeRecipientAddress;
this.senderAddress = senderAddress;
this.makerAssetAmount = makerAssetAmount;
this.takerAssetAmount = takerAssetAmount;
this.makerFee = makerFee;
this.takerFee = takerFee;
this.expirationTimeSeconds = expirationTimeSeconds;
this.salt = salt;
this.makerAssetData = makerAssetData;
this.takerAssetData = takerAssetData;
}
public Order(Address makerAddress, Address takerAddress, Address feeRecipientAddress, Address senderAddress,
Uint256 makerAssetAmount, Uint256 takerAssetAmount, Uint256 makerFee, Uint256 takerFee, Uint256 expirationTimeSeconds
, Uint256 salt, Bytes makerAssetData, Bytes takerAssetData) {
super(makerAddress, takerAddress, feeRecipientAddress, senderAddress, makerAssetAmount, takerAssetAmount, makerFee,
takerFee, expirationTimeSeconds, salt, makerAssetData, takerAssetData);
this.makerAddress = makerAddress.getValue();
this.takerAddress = takerAddress.getValue();
this.feeRecipientAddress = feeRecipientAddress.getValue();
this.senderAddress = senderAddress.getValue();
this.makerAssetAmount = makerAssetAmount.getValue();
this.takerAssetAmount = takerAssetAmount.getValue();
this.makerFee = makerFee.getValue();
this.takerFee = takerFee.getValue();
this.expirationTimeSeconds = expirationTimeSeconds.getValue();
this.salt = salt.getValue();
this.makerAssetData = makerAssetData.getValue();
this.takerAssetData = takerAssetData.getValue();
}
}
Order order = new Order(makerAddress, takerAddress, feeRecipientAddress, senderAddress, makerAssetAmount, takerAssetAmount
, makerFee, takerFee, expirationTimeSeconds, salt, Numeric.hexStringToByteArray(makerAssetData), Numeric.hexStringToByteArray(takerAssetData));
EIP712Message message = constructOrder(order);
String s = JSON.toJSONString(message);
StructuredDataEncoder structured = new StructuredDataEncoder(s);
byte[] bytes = structured.hashStructuredData();
log.info("StructuredDataEncoder ===> order2 bytes:{}, orderHash:{}",bytes,Numeric.toHexString(bytes));
// ethSign 签名
final Sign.SignatureData signatureData = Sign.signPrefixedMessage(bytes, maker.getEcKeyPair());
byte[] inputSignData = com.google.common.primitives.Bytes.concat(signatureData.getV(), signatureData.getR(), signatureData.getS(), new byte[]{0x03});
// 校验签名
// isValidSignature(maker,exchangeContract,orderInfo.orderHash,inputSignData);
sendTransaction(exchangeContract, order, inputSignData);
private void approve(Credentials credentials, String contract) throws Exception {
// 发送交易用户
final CompletableFuture<EthGetTransactionCount> nonceFuture1 = web3j.ethGetTransactionCount(credentials.getAddress(), DefaultBlockParameterName.PENDING).sendAsync();
final CompletableFuture<EthGasPrice> gasPriceFuture = web3j.ethGasPrice().sendAsync();
final Function function = new Function(
"approve",
Arrays.asList(new Address("0xf1ec01d6236d3cd881a0bf0130ea25fe4234003e"), new Uint(BigInteger.valueOf(2).pow(256).subtract(BigInteger.ONE)))
, Collections.emptyList());
String data = FunctionEncoder.encode(function);
RawTransaction rawTransaction = RawTransaction.createTransaction(nonceFuture1.get().getTransactionCount(), gasPriceFuture.get().getGasPrice(), BigInteger.valueOf(210000L), contract,
BigInteger.ZERO,
data);
byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, taker);
EthSendTransaction send = web3j.ethSendRawTransaction(Numeric.toHexString(signedMessage)).send();
String transactionHash = send.getTransactionHash();
log.info("EthSendTransaction send result transactionHash:{}", transactionHash);
}
@LiYChen do you have this issue with the latest version fo web3j 4.8.4 ?