我正在尝试在我的 java 应用程序中使用Ripple-lib来获取客户端的余额。
要调用 Ripple javascript API,我使用 j2v8 如下:
V8 runtime = V8.createV8Runtime();
runtime.executeVoidScript(""
+ "const RippleAPI = require('ripple-lib').RippleAPI;\n"
+ "const api = new RippleAPI({\n"
+ " server: 'wss://s.altnet.rippletest.net:51233'\n"
+ "});\n"
+ "var client = {\n"
+ " getBalance: function (address) {\n"
+ " api.connect().then(() => {\n"
+ " api.getBalances(address).then(balances => {\n"
+ " return JSON.stringify(balances, null, 2);\n"
+ " });\n"
+ " })\n"
+ " }\n"
+ "};");
V8Object client = runtime.getObject("client");
V8Array parameters = new V8Array(runtime);
parameters.push("rHY6yUsQaEigs867XUgaMp89Hhm2eJs5jQ");
String result = client.executeStringFunction("getBalance", parameters);
System.out.println(result);
parameters.release();
runtime.release();
但有例外:
Exception in thread "main" undefined:1: ReferenceError: require is not defined
const RippleAPI = require('ripple-lib').RippleAPI;
^
ReferenceError: require is not defined
at <anonymous>:1:19
com.eclipsesource.v8.V8ScriptExecutionException
有人可以帮我吗?