Skip to content

Commit

Permalink
feat: export rpc client as js function sendRpcRequest(String, Object[])
Browse files Browse the repository at this point in the history
  • Loading branch information
Zurcusa committed Aug 29, 2024
1 parent 43c1900 commit 7faeed8
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/main/java/com/limechain/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@ public class Main {

public static void main(String[] args) {
log.log("Starting LimeChain node...");
exportAPI(RpcClient::sendRpcRequest, JSString.valueOf("rpc"));

RpcApp rpcApp = new RpcApp();
rpcApp.start();

HostNode client = new LightClient();

exportAPI(RpcClient::sendRpcRequest, JSString.valueOf("rpc"));
// Start the client
// NOTE: This starts the beans the client would need - mutates the global context
client.start();
log.log(Level.INFO, "\uD83D\uDE80Started light client!");
}

@JSBody(params = {"f", "apiName"}, script = "window[apiName] = f")
@JSBody(params = {"f", "apiName"}, script = "window[apiName] = f;" +
"isRpcExported = true;")
private static native void exportAPI(Function f, JSString apiName);
}
2 changes: 1 addition & 1 deletion src/main/java/com/limechain/rpc/RpcClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private static String createRpcRequestJson(String method, List<Object> params) {
return JsonUtil.stringify(request);
}

public static String sendRpcRequest(String method, String[] params) {
public static String sendRpcRequest(String method, Object[] params) {
return HttpRequest.createHttpRequest(POST, LOAD_BALANCER.getNextEndpoint(),
createRpcRequestJson(method, List.of(params)));
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/webapp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@
// stream.end();
// });
}
// Test
sendRpcRequest('system_name', []);
main();
// Test 2
sendRpcRequest('chain_getBlock', []);
</script>
</body>
</html>
10 changes: 10 additions & 0 deletions src/main/webapp/js/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,13 @@ function httpRequestSync(method, url, body) {
throw new Error('Request failed with status ' + xhr.status);
}
}

var isRpcExported = false;

function sendRpcRequest(method, params) {
if (isRpcExported === false) {
window.setTimeout(() => sendRpcRequest(method, params), 10);
} else {
console.log(rpc.sendRequest(method, params));
}
}

0 comments on commit 7faeed8

Please sign in to comment.