private void initEndpoints() throws Exception { endpoints = new ConcurrentHashMap<>(); ConcurrentLinkedQueue<Exception> exceptions = new ConcurrentLinkedQueue<>(); routes.forEach((endpoint, route) -> { try { JSApiEnvironment apiEnvironment = JSApiEnvironment.execJSByName( route.contract.getDefinition().getData().getBinder(Contract.JSAPI_SCRIPT_FIELD, null), route.contract.getState().getData().getBinder(Contract.JSAPI_SCRIPT_FIELD, null), execOptions, route.scriptName, route.contract, route.jsParams == null ? new String[0] : route.jsParams ); apiEnvironment.setHandlerMethodName(route.handlerMethodName); apiEnvironment.setSlotId(route.slotId); endpoints.put(endpoint, apiEnvironment); } catch (Exception e) { exceptions.add(e); } }); if (!exceptions.isEmpty()) { throw exceptions.iterator().next(); } }
public List<String> extractJSNames() { List<String> res = new ArrayList<>(); Binder scriptBinder = getDefinition().getData().getBinder(JSAPI_SCRIPT_FIELD, null); if (scriptBinder == null) scriptBinder = getState().getData().getBinder(JSAPI_SCRIPT_FIELD, null); if (scriptBinder != null) res.addAll(JSApiHelpers.getFileNamesFromScriptBinder(scriptBinder)); return res; }
@Test public void shouldSplitAndMergeOne() throws Exception { List<Contract> listOfCoinsWithAmount = createListOfCoinsWithAmount(Arrays.asList(100, 200)); List<Wallet> wallets = Wallet.determineWallets(listOfCoinsWithAmount); //want to send 250 Wallet wallet = wallets.get(0); Contract output = wallet.buildContractWithValue("amount", new Decimal(250)); output.addSignerKeyFromFile(PRIVATE_KEY_PATH); sealCheckTrace(output, true); Contract contract = wallet.getContracts().get(0); sealCheckTrace(contract, true); assertEquals("50", contract.getState().getData().get("amount")); }
/** * Executes javascript, that previously should be saved in contract's definition * with {@link Definition#setJS(byte[], String, JSApiScriptParameters)} or {@link State#setJS(byte[], String, JSApiScriptParameters)}. * Provides instance of {@link JSApi} to this script, as 'jsApi' global var. * @param params list of strings, will be passed to javascript * @return Object, got it from 'result' global var of javascript. * @throws ScriptException if javascript throws some errors * @throws IllegalArgumentException if javascript is not defined in contract's definition */ public Object execJS(JSApiExecOptions execOptions, byte[] jsFileContent, String... params) throws Exception { JSApiEnvironment env = JSApiEnvironment.execJS( getDefinition().getData().getBinder(JSAPI_SCRIPT_FIELD, null), getState().getData().getBinder(JSAPI_SCRIPT_FIELD, null), execOptions, jsFileContent, this, params ); env.callEvent("main", true); return env.getResult(); }
/** * Executes javascript, like {@link Contract#execJS(JSApiExecOptions, byte[], String...)}, * but searches script body in contract. It should be saved previously with {@link Definition#setJS(byte[], String, JSApiScriptParameters, boolean)} * or {@link State#setJS(byte[], String, JSApiScriptParameters, boolean)} with putContentIntoContract == true. * @param scriptHash is {@link HashId} from js-file content. */ public Object execJSByScriptHash(JSApiExecOptions execOptions, HashId scriptHash, String... params) throws Exception { JSApiEnvironment env = JSApiEnvironment.execJSByScriptHash( getDefinition().getData().getBinder(JSAPI_SCRIPT_FIELD, null), getState().getData().getBinder(JSAPI_SCRIPT_FIELD, null), execOptions, scriptHash, this, params ); return env.callEvent("main", true); }
/** * Executes attached javascript if only one js is attached. Also, it should be attached with putContentIntoContract == true. */ public Object execJSByName(JSApiExecOptions execOptions, String jsFileName, String... params) throws Exception { JSApiEnvironment env = JSApiEnvironment.execJSByName( getDefinition().getData().getBinder(JSAPI_SCRIPT_FIELD, null), getState().getData().getBinder(JSAPI_SCRIPT_FIELD, null), execOptions, jsFileName, this, params ); return env.callEvent("main", true); }
public void updateThisEnvironmentByName(Contract newContract, JSApiExecOptions execOptions) throws Exception { JSApiEnvironment env = execJSByName( newContract.getDefinition().getData().getBinder(Contract.JSAPI_SCRIPT_FIELD, null), newContract.getState().getData().getBinder(Contract.JSAPI_SCRIPT_FIELD, null), execOptions, jsFileName, newContract, stringParams ); this.jsApi = env.jsApi; this.scriptEngine = env.scriptEngine; this.currentContract = env.currentContract; this.result = env.result; }
/** * Saves client's javascript in contract's state. It can be executed with {@link Contract#execJS(JSApiExecOptions, byte[], String...)} */ public void setJS(byte[] jsFileContent, String jsFileName, JSApiScriptParameters scriptParameters, boolean putContentIntoContract) { String fileNameKey = JSApiHelpers.fileName2fileKey(jsFileName); Binder scriptBinder = JSApiHelpers.createScriptBinder(jsFileContent, jsFileName, scriptParameters, putContentIntoContract); Binder scripts = getData().getBinder(JSAPI_SCRIPT_FIELD, new Binder()); scripts.set(fileNameKey, scriptBinder); getData().put(JSAPI_SCRIPT_FIELD, scripts); }
/** * Get data section of contract state * @return data section of contract state */ public Binder getStateData() { return state.getData(); }