org.polkadot.rpc.provider.coder.RpcCoder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of polkadot-java Show documentation
Show all versions of polkadot-java Show documentation
Java Polkadot API, this is a clone of https://github.com/polkadot-java/api
The newest version!
package org.polkadot.rpc.provider.coder;
import com.alibaba.fastjson.JSON;
import org.polkadot.rpc.provider.Types.JsonRpcError;
import org.polkadot.rpc.provider.Types.JsonRpcRequest;
import org.polkadot.rpc.provider.Types.JsonRpcResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
public class RpcCoder {
private static final Logger logger = LoggerFactory.getLogger(RpcCoder.class);
private AtomicInteger id = new AtomicInteger(0);
public Object decodeResponse(JsonRpcResponse response) {
assert response != null : "Empty response object received";
assert "2.0".equals(response.getJsonrpc()) : "Invalid jsonrpc field in decoded object";
boolean isSubscription = response.getParams() != null && response.getMethod() != null;
// assert(isNumber(response.id) || (isSubscription && isNumber(response.params.subscription)), 'Invalid id field in decoded object');
this.checkError(response.getError());
assert response.getResult() != null || isSubscription : "No result found in JsonRpc response";
if (isSubscription) {
this.checkError(response.getParams().getError());
return response.getParams().getResult();
}
return response.getResult();
}
public String encodeJson(String method, List