0

I'm trying to use Ripple-lib to get client's balance, inside my java application.

To call Ripple javascript API, I use j2v8 as following:

    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();

but with exception:

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

Could anyone please help me?

Nosairat
  • 482
  • 9
  • 20

1 Answers1

1

If you are running j2v8, the engine knows only pure js, not even window variable, because window, document, and Dom manipulation api's are provided by browser not just engine. Require is not js function. It won't be recognized by v8 engine

Gopi S
  • 523
  • 1
  • 5
  • 17
  • How is it possible then to use an external package downloaded from npm? – chris Oct 31 '18 at 06:28
  • Is there a way to manually "create" the DOM objects for v8 ? – shluvme Dec 11 '22 at 17:09
  • @shleimel ASFIK, there is no way to manipulate or create DOM objects from v8. – Gopi S Dec 15 '22 at 05:16
  • One way is you have to mock all the dom functions, like create element, set attribute manually. But it is a herculean task. – Gopi S Dec 15 '22 at 05:18
  • Thanks @GopiS - solved it by manually defining a `window` object like this `V8Object window = runtime.executeObjectScript("window = {};");` . It doesn't actually hold the DOM documented attributes, but it was good enough. Thanks to ChatGPT ;) – shluvme Dec 15 '22 at 08:14